View Javadoc
1   package fr.ifremer.tutti.ui.swing;
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 fr.ifremer.tutti.TuttiConfiguration;
26  import fr.ifremer.tutti.ui.swing.content.MainUI;
27  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
28  import fr.ifremer.tutti.ui.swing.content.actions.StartAction;
29  import fr.ifremer.tutti.ui.swing.update.actions.UpdateApplicationAction;
30  import fr.ifremer.tutti.ui.swing.update.actions.UpdateReportAction;
31  import fr.ifremer.tutti.ui.swing.util.TuttiExceptionHandler;
32  import jaxx.runtime.SwingUtil;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import javax.swing.SwingUtilities;
37  import javax.swing.UIManager;
38  import javax.swing.plaf.BorderUIResource;
39  import java.awt.Color;
40  import java.awt.Dimension;
41  import java.util.Arrays;
42  
43  /**
44   * To start Tutti application.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 0.1
48   */
49  public class RunTutti {
50  
51      /** Logger. */
52      private static final Log log = LogFactory.getLog(RunTutti.class);
53  
54      public static final int RESTART_EXIT_CODE = 88;
55  
56      public static final int STOP_EXIT_CODE = 0;
57  
58      public static void main(String... args) {
59  
60          if (log.isInfoEnabled()) {
61              log.info("Starting Tutti with arguments: " + Arrays.toString(args));
62          }
63  
64          // Create configuration
65          TuttiConfiguration config = new TuttiConfiguration("tutti.config", args);
66  
67          // Create application context
68          final TuttiUIContext context = TuttiUIContext.newContext(config);
69  
70          // override default exception management (after config init)
71          Thread.setDefaultUncaughtExceptionHandler(new TuttiExceptionHandler(context.getErrorHelper()));
72          // See http://forge.codelutin.com/issues/2055
73          //System.setProperty("sun.awt.exception.handler", TuttiExceptionHandler.class.getName());
74  
75          // prepare context (mainly init configs, i18n)
76          context.init();
77  
78          // Prepare ui look&feel and load ui properties
79          try {
80              SwingUtil.initNimbusLoookAndFeel();
81              UIManager.getLookAndFeelDefaults().put("ScrollBar.minimumThumbSize", new Dimension(30, 30));
82          } catch (Exception e) {
83              // could not find nimbus look-and-feel
84              if (log.isWarnEnabled()) {
85                  log.warn("Failed to init nimbus look and feel", e);
86              }
87          }
88  
89          boolean reload = false;
90  
91          if (config.isFullLaunchMode()) {
92  
93              if (log.isInfoEnabled()) {
94                  log.info("Full launch mode, try to update.");
95              }
96  
97              // check application url is reachable
98              // no popup see https://forge.codelutin.com/issues/8328
99              boolean canUpdateApplication = context.checkUpdateApplicationReachable(false);
100 
101             if (canUpdateApplication) {
102                 // try to update jre - i18n - application - help and exit if so
103                 UpdateApplicationAction logicAction = context.getActionFactory().createLogicAction(new MainUIHandler() {
104 
105                     @Override
106                     public TuttiUIContext getContext() {
107                         return context;
108                     }
109                 }, UpdateApplicationAction.class);
110                 context.getActionEngine().runActionAndWait(logicAction);
111 
112                 reload = logicAction.isReload();
113             }
114 
115             // check data url is reachable
116             // no popup see https://forge.codelutin.com/issues/8328
117             boolean canUpdateData = context.checkUpdateDataReachable(false);
118 
119             if (canUpdateData) {
120                 // try to update report and exit if so
121                 UpdateReportAction logicAction = context.getActionFactory().createLogicAction(new MainUIHandler() {
122 
123                     @Override
124                     public TuttiUIContext getContext() {
125                         return context;
126                     }
127                 }, UpdateReportAction.class);
128                 context.getActionEngine().runActionAndWait(logicAction);
129 
130                 reload |= logicAction.isReload();
131             }
132         }
133 
134         if (!reload) {
135             if (log.isInfoEnabled()) {
136                 log.info("Will start Tutti...");
137             }
138             startTutti(context, true);
139         }
140     }
141 
142     public static void startTutti(TuttiUIContext context, boolean openContext) {
143 
144         if (openContext) {
145             context.open();
146         }
147 
148         UIManager.put("Table.alternateRowColor", context.getConfig().getColorAlternateRow());
149         UIManager.put("Table[Disabled+Selected].textBackground", context.getConfig().getColorSelectedRow());
150         UIManager.put("Table[Enabled+Selected].textBackground", context.getConfig().getColorSelectedRow());
151         UIManager.put("Table.focusCellHighlightBorder", new BorderUIResource.LineBorderUIResource(Color.BLACK));
152 
153         final MainUI mainUI = new MainUI(context);
154         context.addMessageNotifier(mainUI.getHandler());
155 
156         SwingUtilities.invokeLater(() -> mainUI.setVisible(true));
157 
158         // launch start action (use the tutti-start-action file)
159         StartAction uiAction = context.getActionFactory().createLogicAction(
160                 mainUI.getHandler(), StartAction.class);
161         context.getActionEngine().runAction(uiAction);
162     }
163 
164     public static void closeTutti(MainUIHandler handler, Integer exitCode) {
165 
166         TuttiUIContext context = handler.getContext();
167 
168         // close ui
169         handler.onCloseUI();
170 
171         //close context
172         context.saveSwingSession();
173         context.close();
174 
175         if (exitCode != null) {
176             System.exit(exitCode);
177         }
178     }
179 }