View Javadoc
1   package fr.ifremer.quadrige3.ui.swing;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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 fr.ifremer.quadrige3.core.config.QuadrigeCoreConfiguration;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.jdesktop.swingx.util.JVM;
30  
31  import java.lang.reflect.Field;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * @author peck7 on 29/06/2017.
37   */
38  public abstract class Application {
39  
40      private static final Log LOG = LogFactory.getLog(Application.class);
41  
42      public static final int NORMAL_EXIT_CODE = 0;
43      public static final int UPDATE_EXIT_CODE = 88;
44  
45      private static Application instance;
46  
47      public Application() {
48  
49          // this instance
50          instance = this;
51  
52          // override default exception management
53          Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
54  
55          // manage swingx JVM class here because jdk version > 1.7 is not managed in SwingLabs components (see Mantis #31061)
56          JVM jvm = JVM.current();
57          Class jvmClass = jvm.getClass();
58          try {
59              Field jdkVersionField = jvmClass.getDeclaredField("jdkVersion");
60              jdkVersionField.setAccessible(true);
61              // simulate the jdk version to JDK 1.7
62              jdkVersionField.setInt(jvm, JVM.JDK1_7);
63          } catch (NoSuchFieldException | IllegalAccessException e) {
64              LOG.warn("Unable to simulate JVM 1.7 in SwingLabs components. Flickering will happen !");
65          }
66  
67      }
68  
69      public static Application getInstance() {
70          return instance;
71      }
72  
73      /**
74       * Main method to start the application
75       *
76       * @param args application arguments
77       */
78      protected final void start(String... args) {
79  
80          if (init(args)) {
81  
82              // Execute Updates
83              if (getConfig().isFullLaunchMode()) {
84  
85                  if (LOG.isInfoEnabled()) {
86                      LOG.info("Full launch mode, try to update.");
87                  }
88  
89                  // Check update availability
90                  boolean updateAvailable = getContext().checkUpdateReachable(getConfig().getUpdateApplicationUrl(), false);
91                  updateAvailable &= getContext().checkUpdateReachable(getConfig().getUpdateDataUrl(), false);
92  
93                  if (updateAvailable) {
94  
95                      if (getContext().doUpdates()) {
96                          exit(UPDATE_EXIT_CODE);
97                      }
98  
99                  } else {
100 
101                     getContext().getErrorHelper().showErrorDialog(t("quadrige3.error.update"));
102                 }
103             }
104 
105             // Run other actions
106             try {
107                 getConfig().getApplicationConfig().doAllAction();
108             } catch (Exception e) {
109                 LOG.error(e.getLocalizedMessage(), e);
110                 // Stop, if error at start up
111                 exit(NORMAL_EXIT_CODE);
112             }
113 
114             if (getConfig().isSilentLaunchMode()) {
115                 if (LOG.isInfoEnabled()) {
116                     LOG.info("Stopping silently");
117                 }
118                 exit(NORMAL_EXIT_CODE);
119             }
120 
121             // Shows the UI
122             if (LOG.isInfoEnabled()) {
123                 LOG.info(String.format("Will start %s %s", getConfig().getApplicationName(), getConfig().getVersion()));
124             }
125 
126             show();
127         }
128     }
129 
130     /**
131      * Initialize the application
132      *
133      * @param args application arguments
134      * @return true if initialization allow the application to show its UI
135      */
136     protected abstract boolean init(String... args);
137 
138     /**
139      * Shows the initialized UI
140      */
141     protected abstract void show();
142 
143     /**
144      * Restart the application interface
145      */
146     public abstract void restartUI();
147 
148     protected abstract QuadrigeCoreConfiguration getConfig();
149 
150     protected abstract ApplicationUIContext getContext();
151 
152     /**
153      * Exit the application with an exit code
154      *
155      * @param exitCode exit code
156      */
157     public static void exit(int exitCode) {
158         System.exit(exitCode);
159     }
160 }