1 package fr.ifremer.quadrige3.ui.swing;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
50 instance = this;
51
52
53 Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
54
55
56 JVM jvm = JVM.current();
57 Class jvmClass = jvm.getClass();
58 try {
59 Field jdkVersionField = jvmClass.getDeclaredField("jdkVersion");
60 jdkVersionField.setAccessible(true);
61
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
75
76
77
78 protected final void start(String... args) {
79
80 if (init(args)) {
81
82
83 if (getConfig().isFullLaunchMode()) {
84
85 if (LOG.isInfoEnabled()) {
86 LOG.info("Full launch mode, try to update.");
87 }
88
89
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
106 try {
107 getConfig().getApplicationConfig().doAllAction();
108 } catch (Exception e) {
109 LOG.error(e.getLocalizedMessage(), e);
110
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
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
132
133
134
135
136 protected abstract boolean init(String... args);
137
138
139
140
141 protected abstract void show();
142
143
144
145
146 public abstract void restartUI();
147
148 protected abstract QuadrigeCoreConfiguration getConfig();
149
150 protected abstract ApplicationUIContext getContext();
151
152
153
154
155
156
157 public static void exit(int exitCode) {
158 System.exit(exitCode);
159 }
160 }