View Javadoc
1   package fr.ifremer.dali.ui.swing.action;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.base.Joiner;
27  import com.google.common.collect.Lists;
28  import com.google.common.collect.Maps;
29  import fr.ifremer.dali.config.DaliConfiguration;
30  import fr.ifremer.dali.ui.swing.DaliUIContext;
31  import fr.ifremer.dali.ui.swing.content.DaliMainUIHandler;
32  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
33  import fr.ifremer.quadrige3.ui.swing.action.AbstractAction;
34  import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
35  import fr.ifremer.quadrige3.ui.swing.action.UpdateApplicationAction;
36  import fr.ifremer.quadrige3.ui.swing.callback.ApplicationUpdaterCallBack;
37  import fr.ifremer.quadrige3.ui.swing.callback.DatabaseUpdaterCallBack;
38  import fr.ifremer.quadrige3.ui.swing.content.db.InstallDbAction;
39  import jaxx.runtime.swing.AboutPanel;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.nuiton.updater.ApplicationInfo;
43  import org.nuiton.updater.ApplicationUpdater;
44  
45  import javax.swing.JEditorPane;
46  import javax.swing.JScrollPane;
47  import javax.swing.ScrollPaneConstants;
48  import javax.swing.event.HyperlinkEvent;
49  import java.awt.Frame;
50  import java.io.File;
51  import java.net.MalformedURLException;
52  import java.net.URL;
53  import java.util.*;
54  import java.util.stream.Collectors;
55  
56  import static org.nuiton.i18n.I18n.t;
57  
58  /**
59   * To show about panel.
60   *
61   * @since 1.2
62   */
63  public class ShowAboutAction extends AbstractMainUIAction {
64  
65      /**
66       * Logger.
67       */
68      private static final Log LOG = LogFactory.getLog(ShowAboutAction.class);
69  
70      protected AboutPanel about;
71      private boolean canUpdateApplication;
72      private boolean canUpdateData;
73  
74      /**
75       * <p>Constructor for ShowAboutAction.</p>
76       *
77       * @param handler a {@link DaliMainUIHandler} object.
78       */
79      public ShowAboutAction(DaliMainUIHandler handler) {
80          super(handler, false);
81      }
82  
83      @Override
84      public DaliUIContext getContext() {
85          return (DaliUIContext) super.getContext();
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public boolean prepareAction() throws Exception {
91          boolean doAction = super.prepareAction();
92  
93          if (doAction) {
94              // check db url is reachable
95              canUpdateApplication = getContext().checkUpdateReachable(getConfig().getUpdateApplicationUrl(), false);
96              canUpdateData = getContext().checkUpdateReachable(getConfig().getUpdateDataUrl(), false);
97  
98          }
99  
100         return doAction;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public void postSuccessAction() {
106         super.postSuccessAction();
107 
108         about.showInDialog((Frame) getUI(), true);
109 
110         // register on swing session
111         getContext().getSwingSession().add(about);
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public void doAction() throws Exception {
117 
118         about = null;
119 
120         String iconPath = "/images/dali-about.png";
121         String licensePath = "META-INF/dali-LICENSE.txt";
122         String thirdPartyPath = "META-INF/dali-THIRD-PARTY.txt";
123 
124         about = new AboutPanel();
125         about.setTitle(t("dali.about.title"));
126         about.setAboutText(t("dali.about.message", getContext().getConfiguration().getSiteUrl().toString()));
127 
128         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
129         int inceptionYear = getContext().getConfiguration().getInceptionYear();
130         String years;
131         if (currentYear != inceptionYear) {
132             years = inceptionYear + "-" + currentYear;
133         } else {
134             years = inceptionYear + "";
135         }
136 
137         about.setBottomText(t("dali.about.bottomText",
138                 getContext().getConfiguration().getOrganizationName(),
139                 years,
140                 getContext().getConfiguration().getVersion()));
141         about.setIconPath(iconPath);
142         about.setLicenseFile(licensePath);
143         about.setThirdpartyFile(thirdPartyPath);
144         about.buildTopPanel();
145 
146         // translate tab
147         addTranslateTab();
148 
149         // configuration tab
150         addConfigTab(getContext().getConfiguration());
151 
152         if (canUpdateApplication || canUpdateData) {
153 
154             // update tab
155             addUpdateTab(getContext().getConfiguration());
156 
157         }
158         about.init();
159     }
160 
161     private void addConfigTab(DaliConfiguration config) {
162         JScrollPane configPane = new JScrollPane();
163         configPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
164         JEditorPane configArea = new JEditorPane();
165         configArea.setEditable(false);
166         if (configArea.getFont() != null) {
167             configArea.setFont(configArea.getFont().deriveFont((float) 11));
168         }
169         configArea.setBorder(null);
170 
171         StringBuilder configContent = new StringBuilder();
172         Properties properties = config.getApplicationConfig().getFlatOptions();
173         Set<String> optionNames = properties.stringPropertyNames().stream()
174                 .filter(propertyName -> propertyName.startsWith("dali") || propertyName.startsWith("quadrige3"))
175                 .collect(Collectors.toCollection(TreeSet::new));
176 
177         optionNames.forEach(optionName -> configContent.append(optionName).append("=").append(properties.getProperty(optionName)).append(System.lineSeparator()));
178 
179         configArea.setText(configContent.toString());
180         configPane.getViewport().add(configArea);
181         about.getTabs().add(t("dali.about.config.title"), configPane);
182     }
183 
184     private void addTranslateTab() throws MalformedURLException {
185         JScrollPane translatePane = new JScrollPane();
186         JEditorPane translateArea = new JEditorPane();
187         translateArea.setContentType("text/html");
188         translateArea.setEditable(false);
189         if (translateArea.getFont() != null) {
190             translateArea.setFont(translateArea.getFont().deriveFont((float) 11));
191         }
192 
193         translateArea.setBorder(null);
194         File csvFile = new File(getContext().getConfiguration().getI18nDirectory(), "dali-i18n.csv");
195         String translateText = t("dali.about.translate.content", csvFile.toURI().toURL());
196         translateArea.setText(translateText);
197         translatePane.getViewport().add(translateArea);
198         translateArea.addHyperlinkListener(e -> {
199             if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
200                 URL url = e.getURL();
201                 if (LOG.isInfoEnabled()) {
202                     LOG.info("edit url: " + url);
203                 }
204                 ApplicationUIUtil.openLink(url);
205             }
206         });
207 
208         about.getTabs().add(t("dali.about.translate.title"), translatePane);
209     }
210 
211     /**
212      * <p>addUpdate.</p>
213      *
214      * @param source a {@link java.util.Map} object.
215      * @param target a {@link java.util.Map} object.
216      * @param type a {@link java.lang.String} object.
217      */
218     private void addUpdate(Map<String, ApplicationInfo> source,
219                            Map<String, ApplicationInfo> target,
220                            String type) {
221         ApplicationInfo info = source.get(type.toLowerCase());
222         target.put(type, info);
223     }
224 
225     /**
226      * <p>addUpdateTab.</p>
227      *
228      * @param config a {@link DaliConfiguration} object.
229      */
230     private void addUpdateTab(DaliConfiguration config) {
231         File current = config.getBaseDirectory();
232         String urlApplication = config.getUpdateApplicationUrl();
233         String urlData = config.getUpdateDataUrl();
234 
235         ApplicationUpdater up = new ApplicationUpdater();
236 
237         // create final update map
238         final Map<String, ApplicationInfo> versions = Maps.newLinkedHashMap();
239 
240         if (canUpdateApplication) {
241 
242             // get application updates
243             Map<String, ApplicationInfo> applicationVersions = up.getVersions(urlApplication, current);
244 
245             addUpdate(applicationVersions, versions, ApplicationUpdaterCallBack.UpdateType.JRE.name());
246             addUpdate(applicationVersions, versions, ApplicationUpdaterCallBack.UpdateType.APPLICATION.name());
247             addUpdate(applicationVersions, versions, ApplicationUpdaterCallBack.UpdateType.I18N.name());
248             addUpdate(applicationVersions, versions, ApplicationUpdaterCallBack.UpdateType.HELP.name());
249         }
250 
251         if (canUpdateData) {
252 
253             // get db updates
254             Map<String, ApplicationInfo> dbVersions = up.getVersions(urlData, config.getDataDirectory());
255             addUpdate(dbVersions, versions, DatabaseUpdaterCallBack.DB_UPDATE_NAME);
256         }
257         JScrollPane updatePane = new JScrollPane();
258         JEditorPane updateArea = new JEditorPane();
259         updateArea.setContentType("text/html");
260         updateArea.setEditable(false);
261         if (updateArea.getFont() != null) {
262             updateArea.setFont(updateArea.getFont().deriveFont((float) 11));
263         }
264         updateArea.setBorder(null);
265 
266         List<String> params = Lists.newArrayList();
267         for (Map.Entry<String, ApplicationInfo> entry : versions.entrySet()) {
268             String appName = entry.getKey();
269             ApplicationInfo info = entry.getValue();
270             String oldVersion = info != null ? info.oldVersion : t("dali.about.update.app.undefined");
271             String newVersion = info != null ? info.newVersion : null;
272             String i18nKey = "quadrige3.update." + appName.toLowerCase(); // keep quadrige3. i18n base
273             String appLabel = t(i18nKey);
274 
275             if (LOG.isInfoEnabled()) {
276                 LOG.info(String.format(
277                         "Module %s, version courante %s, nouvelle version %s",
278                         appLabel, oldVersion, newVersion));
279             }
280             if (newVersion == null) {
281 
282                 // no update
283                 params.add(t("dali.about.update.app.noup.detail", appLabel, oldVersion));
284             } else {
285                 // update exists
286                 params.add(t("dali.about.update.app.up.detail", appLabel, oldVersion, newVersion, appName));
287             }
288         }
289 
290         String updateText = t("dali.about.update.content", urlApplication, urlData, Joiner.on("\n").join(params));
291         updateArea.setText(updateText);
292         updatePane.getViewport().add(updateArea);
293         updateArea.addHyperlinkListener(e -> {
294             if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
295                 URL url = e.getURL();
296                 if (url != null) {
297                     ApplicationUIUtil.openLink(url);
298                 } else {
299                     String appType = e.getDescription();
300 
301                     if (LOG.isInfoEnabled()) {
302                         LOG.info("Open url: " + appType);
303                     }
304                     AbstractAction<?, ?, ?> action;
305 
306                     if (DatabaseUpdaterCallBack.DB_UPDATE_NAME.equals(appType)) {
307                         if (getContext().isDbExist()) {
308                             // Update DB referential
309                             action = getContext().getActionFactory().createLogicAction(getHandler(), ImportReferentialSynchroAction.class);
310                         } else {
311                             // Install db
312                             action = getContext().getActionFactory().createLogicAction(getHandler(), InstallDbAction.class);
313                             action.setActionDescription(t("dali.action.installDb.tip"));
314                         }
315                     } else {
316 
317                         ApplicationUpdaterCallBack.UpdateType updateType
318                                 = ApplicationUpdaterCallBack.UpdateType.valueOf(appType.toUpperCase());
319 
320                         UpdateApplicationAction logicAction = getContext().getActionFactory().createLogicAction(getHandler(), UpdateApplicationAction.class);
321                         logicAction.setTypes(updateType);
322                         logicAction.setActionDescription(t("dali.action.updateSpecificApplication.tip", updateType.getLabel()));
323                         action = logicAction;
324                     }
325 
326                     // close this dialog
327                     getActionEngine().runAction(about.getClose());
328 
329                     // do update
330                     getActionEngine().runAction(action);
331                 }
332 
333             }
334         });
335         about.getTabs().add(t("dali.about.update.title"), updatePane);
336     }
337 
338 }