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