1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.application; |
16 | |
17 | import java.awt.Window; |
18 | import java.io.FileInputStream; |
19 | import java.io.IOException; |
20 | import java.util.Properties; |
21 | |
22 | import javax.swing.JComponent; |
23 | import javax.swing.JDialog; |
24 | import javax.swing.JFrame; |
25 | |
26 | import org.apache.commons.logging.Log; |
27 | import org.apache.commons.logging.LogFactory; |
28 | import org.apache.hivemind.ApplicationRuntimeException; |
29 | import org.apache.hivemind.Registry; |
30 | import org.apache.hivemind.impl.RegistryBuilder; |
31 | import org.apache.log4j.LogManager; |
32 | import org.apache.log4j.xml.DOMConfigurator; |
33 | |
34 | import org.jdesktop.application.Application; |
35 | import org.jdesktop.application.ResourceConverter; |
36 | import org.jdesktop.application.SingleFrameApplication; |
37 | |
38 | import net.sourceforge.hivegui.component.ActiveWindowHolder; |
39 | import net.sourceforge.hivegui.dialog.DialogPanel; |
40 | import net.sourceforge.hivegui.dialog.GenericDialog; |
41 | import net.sourceforge.hivegui.message.MessageFactory; |
42 | import net.sourceforge.hivegui.message.UserChoice; |
43 | |
44 | public class HiveGuiApplicationMain extends SingleFrameApplication |
45 | { |
46 | // Name of arguments that must be used to drive the way the application |
47 | // will start |
48 | static public final String ARG_CONFIG_DIR = "-configdir"; |
49 | static private final String LOG_FILE_NAME = "log4j.xml"; |
50 | static private final String PROPS_FILE_NAME = "application.properties"; |
51 | static private final String DEFAULT_CONFIG_DIR = "."; |
52 | |
53 | static private Log _logger; |
54 | |
55 | static public void main(String[] args) |
56 | { |
57 | Application.launch(HiveGuiApplicationMain.class, args); |
58 | } |
59 | |
60 | protected HiveGuiApplicationMain() |
61 | { |
62 | } |
63 | |
64 | @Override protected void initialize(String[] args) |
65 | { |
66 | // Register additional resource converters |
67 | registerConverters(); |
68 | |
69 | // Check arguments to find out how to configure the application |
70 | checkArguments(args); |
71 | |
72 | // Configure logging first |
73 | initLogging(); |
74 | |
75 | // Initialize message box manager |
76 | _messages = new MessageFactory(getContext()); |
77 | |
78 | // Get external properties file into System properties |
79 | initProperties(); |
80 | |
81 | // Initialize HiveMind Registry and get some important services |
82 | initRegistry(); |
83 | |
84 | // Initialize |
85 | _contextHolder.setContext(getContext()); |
86 | _initializer.initialize(this, args); |
87 | } |
88 | |
89 | @Override protected void startup() |
90 | { |
91 | ActiveWindowHolder.init(); |
92 | _initializer.startup(); |
93 | } |
94 | |
95 | @Override protected void ready() |
96 | { |
97 | _initializer.ready(); |
98 | } |
99 | |
100 | @Override protected void shutdown() |
101 | { |
102 | super.shutdown(); |
103 | _initializer.shutdown(); |
104 | shutdownRegistry(); |
105 | _logger.info("============================= exit() ============================="); |
106 | shutdownLogging(); |
107 | } |
108 | |
109 | protected void shutdownRegistry() |
110 | { |
111 | if (_registry != null) |
112 | { |
113 | _registry.shutdown(); |
114 | _registry = null; |
115 | } |
116 | } |
117 | |
118 | protected void shutdownLogging() |
119 | { |
120 | LogManager.shutdown(); |
121 | } |
122 | |
123 | public boolean showDialog(DialogPanel panel) |
124 | { |
125 | // Find right parent first |
126 | Window active = ActiveWindowHolder.instance().getActiveWindow(); |
127 | GenericDialog dialog; |
128 | if (active instanceof JDialog) |
129 | { |
130 | dialog = new GenericDialog((JDialog) active, panel); |
131 | } |
132 | else if (active instanceof JFrame) |
133 | { |
134 | dialog = new GenericDialog((JFrame) active, panel); |
135 | } |
136 | else |
137 | { |
138 | // No currently active parent |
139 | dialog = new GenericDialog((JFrame) null, panel); |
140 | } |
141 | dialog.setContext(getContext()); |
142 | show(dialog); |
143 | return !dialog.wasCancelled(); |
144 | } |
145 | |
146 | // Just extend visibility of SingleFrameApplication show() method |
147 | @Override public void show(JComponent component) |
148 | { |
149 | super.show(component); |
150 | } |
151 | |
152 | public UserChoice showMessage(String id, Object... args) |
153 | { |
154 | return _messages.message(id, args); |
155 | } |
156 | |
157 | protected void registerConverters() |
158 | { |
159 | ResourceConverter.register(new ImageConverter()); |
160 | ResourceConverter.register(new CursorInfoConverter()); |
161 | ResourceConverter.register(new CursorConverter()); |
162 | ResourceConverter.register(new EnumConverter()); |
163 | } |
164 | |
165 | protected void checkArguments(String[] args) |
166 | { |
167 | _configDir = getDefaultConfigDir(); |
168 | int i = 0; |
169 | while (i < args.length) |
170 | { |
171 | if (ARG_CONFIG_DIR.equals(args[i])) |
172 | { |
173 | i++; |
174 | if (i < args.length) |
175 | { |
176 | _configDir = args[i]; |
177 | } |
178 | } |
179 | i++; |
180 | } |
181 | |
182 | // Remove any trailing slash (it will be added on files path construction) |
183 | if (_configDir.endsWith("/")) |
184 | { |
185 | if (_configDir.length() == 1) |
186 | { |
187 | _configDir = ""; |
188 | } |
189 | else |
190 | { |
191 | _configDir = _configDir.substring(0, _configDir.length() - 2); |
192 | } |
193 | } |
194 | } |
195 | |
196 | protected String getDefaultConfigDir() |
197 | { |
198 | return DEFAULT_CONFIG_DIR; |
199 | } |
200 | |
201 | protected String getConfigDir() |
202 | { |
203 | return _configDir; |
204 | } |
205 | |
206 | protected void initLogging() |
207 | { |
208 | LogManager.resetConfiguration(); |
209 | DOMConfigurator.configure(_configDir + "/" + getLogFileName()); |
210 | _logger = LogFactory.getLog(HiveGuiApplicationMain.class); |
211 | _logger.debug("========== main =========="); |
212 | |
213 | // Redirect JUL (Swing AppFW) to commons-logging |
214 | JavaLoggingToCommonLoggingRedirector.activate(); |
215 | } |
216 | |
217 | protected String getLogFileName() |
218 | { |
219 | return LOG_FILE_NAME; |
220 | } |
221 | |
222 | protected void initProperties() |
223 | { |
224 | try |
225 | { |
226 | Properties properties = System.getProperties(); |
227 | properties.load(new FileInputStream(_configDir + "/" + getPropsFileName())); |
228 | } |
229 | catch (IOException e) |
230 | { |
231 | _logger.error("initProperties()", e); |
232 | } |
233 | } |
234 | |
235 | protected String getPropsFileName() |
236 | { |
237 | return PROPS_FILE_NAME; |
238 | } |
239 | |
240 | final protected void initRegistry() |
241 | { |
242 | try |
243 | { |
244 | _registry = createRegistry(); |
245 | _contextHolder = (ApplicationContextHolder) _registry.getService( |
246 | "hivegui.ApplicationContextHolder", ApplicationContextHolder.class); |
247 | _initializer = (ApplicationInitializer) _registry.getService( |
248 | "hivegui.ApplicationInitializer", ApplicationInitializer.class); |
249 | } |
250 | catch (ApplicationRuntimeException e) |
251 | { |
252 | _logger.fatal("initRegistry()", e); |
253 | throw e; |
254 | } |
255 | } |
256 | |
257 | protected Registry createRegistry() |
258 | { |
259 | return RegistryBuilder.constructDefaultRegistry(); |
260 | } |
261 | |
262 | public Registry getRegistry() |
263 | { |
264 | return _registry; |
265 | } |
266 | |
267 | public ApplicationInitializer getInitializer() |
268 | { |
269 | return _initializer; |
270 | } |
271 | |
272 | private String _configDir; |
273 | private Registry _registry; |
274 | private MessageFactory _messages; |
275 | private ApplicationInitializer _initializer; |
276 | private ApplicationContextHolder _contextHolder; |
277 | } |