View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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 org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.jdesktop.swingx.util.JVM;
29  
30  import java.lang.reflect.Field;
31  
32  /**
33   * @author peck7 on 29/06/2017.
34   */
35  public abstract class Application {
36  
37      private static final Log LOG = LogFactory.getLog(Application.class);
38  
39      public static final int NORMAL_EXIT_CODE = 0;
40      public static final int UPDATE_EXIT_CODE = 88;
41  
42      public Application() {
43  
44          // override default exception management
45          Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
46  
47          // manage swingx JVM class here because jdk version > 1.7 is not managed in SwingLabs components (see Mantis #31061)
48          JVM jvm = JVM.current();
49          Class jvmClass = jvm.getClass();
50          try {
51              Field jdkVersionField = jvmClass.getDeclaredField("jdkVersion");
52              jdkVersionField.setAccessible(true);
53              // simulate the jdk version to JDK 1.7
54              jdkVersionField.setInt(jvm, JVM.JDK1_7);
55          } catch (NoSuchFieldException | IllegalAccessException e) {
56              LOG.warn("Unable to simulate JVM 1.7 in SwingLabs components. Flickering will happen !");
57          }
58  
59      }
60  
61      /**
62       * Main method to start the application
63       *
64       * @param args
65       */
66      protected final void start(String... args) {
67          if (init(args)) {
68              show();
69          }
70      }
71  
72      /**
73       * Initialize the application
74       *
75       * @param args application arguments
76       * @return true if initialization allow the application to show its UI
77       */
78      protected abstract boolean init(String... args);
79  
80      /**
81       * Shows the initialized UI
82       */
83      protected abstract void show();
84  
85      /**
86       * Exit the application with an exit code
87       * @param exitCode
88       */
89      public static void exit(int exitCode) {
90          System.exit(exitCode);
91      }
92  }