1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.gui.swing;
18
19 import java.awt.EventQueue;
20
21 import javax.swing.Action;
22 import javax.swing.JComponent;
23 import javax.swing.JDialog;
24 import javax.swing.JFrame;
25 import javax.swing.UIManager;
26 import javax.swing.UnsupportedLookAndFeelException;
27
28 import org.bushe.swing.event.EventServiceExistsException;
29 import org.bushe.swing.event.EventServiceLocator;
30 import org.jdesktop.application.Application;
31 import org.jdesktop.application.SingleFrameApplication;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.context.ConfigurableApplicationContext;
37
38 import ch.elca.el4j.core.context.ModuleApplicationContext;
39 import ch.elca.el4j.core.context.ModuleApplicationContextConfiguration;
40 import ch.elca.el4j.services.gui.swing.config.DefaultConfig;
41 import ch.elca.el4j.services.gui.swing.cookswing.TagLibraryFactory;
42 import ch.elca.el4j.services.gui.swing.cookswing.action.ActionsContextAware;
43 import ch.elca.el4j.services.gui.swing.eventbus.ExceptionThrowingEventService;
44 import ch.elca.el4j.services.gui.swing.exceptions.CookXmlExceptionHandler;
45 import ch.elca.el4j.services.gui.swing.exceptions.Exceptions;
46 import ch.elca.el4j.services.gui.swing.frames.ApplicationFrame;
47 import ch.elca.el4j.services.gui.swing.wrapper.JFrameWrapperFactory;
48 import ch.elca.el4j.util.config.GenericConfig;
49 import cookxml.cookswing.CookSwing;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public abstract class GUIApplication extends SingleFrameApplication implements ActionsContextAware {
74
75
76
77
78 private static final Logger s_logger = LoggerFactory.getLogger(
79 GUIApplication.class);
80
81
82
83
84 protected ApplicationContext springContext;
85
86
87
88
89 protected ActionsContext actionsContext = ActionsContext.create(this);
90
91
92
93
94
95 protected GenericConfig config;
96
97
98
99
100
101 public ApplicationContext getSpringContext() {
102 return springContext;
103 }
104
105
106
107
108
109
110 public void setSpringContext(ApplicationContext springContext) {
111 if (springContext instanceof ModuleApplicationContext) {
112 ModuleApplicationContext context = (ModuleApplicationContext) springContext;
113 context.getBeanFactory().registerSingleton("GUIApplication", this);
114
115 }
116 this.springContext = springContext;
117 }
118
119
120
121
122 public GenericConfig getConfig() {
123 return config;
124 }
125
126
127
128
129 public void setConfig(GenericConfig config) {
130 this.config = config;
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public static synchronized <T extends GUIApplication> void launch(
149 final Class<T> applicationClass, final String[] args,
150 final ModuleApplicationContextConfiguration contextConfig) {
151
152
153 Thread.setDefaultUncaughtExceptionHandler(Exceptions.getInstance());
154
155 System.setProperty("sun.awt.exception.handler", Exceptions.class.getName());
156
157 try {
158 EventServiceLocator.setEventService(EventServiceLocator.SERVICE_NAME_EVENT_BUS,
159 new ExceptionThrowingEventService());
160 } catch (EventServiceExistsException e) {
161 s_logger.warn("Unable to register EventService.", e);
162 }
163
164
165 CookSwing.setDefaultExceptionHandler(CookXmlExceptionHandler.getInstance());
166 CookSwing.setDefaultAccessible(true);
167 CookSwing.setSwingTagLibrary(TagLibraryFactory.getTagLibrary());
168
169 Runnable doCreateAndShowGUI = new Runnable() {
170 public void run() {
171 try {
172 GUIApplication application = Application.create(applicationClass);
173
174 Application.setInstance(application);
175
176
177 application.setSpringContext(new ModuleApplicationContext(contextConfig));
178
179
180 application.setConfig(new DefaultConfig());
181
182 setupLookAndFeel(application);
183
184 application.initialize(args);
185 application.startup();
186 application.waitForReady();
187 } catch (Exception e) {
188 String msg = String.format("Application %s failed to launch", applicationClass);
189 s_logger.error(msg, e);
190 throw (new Error(msg, e));
191 }
192 }
193 };
194 EventQueue.invokeLater(doCreateAndShowGUI);
195 }
196
197
198
199
200 public static GUIApplication getInstance() {
201 return (GUIApplication) Application.getInstance();
202 }
203
204
205
206
207
208 @SuppressWarnings("unchecked")
209 public void show(String beanName) throws NoSuchBeanDefinitionException {
210 if (!springContext.containsBean(beanName)) {
211 throw new NoSuchBeanDefinitionException(beanName);
212 }
213 Class beanClass = springContext.getType(beanName);
214 if (JComponent.class.isAssignableFrom(beanClass)) {
215 show((JComponent) springContext.getBean(beanName));
216 } else if (JDialog.class.isAssignableFrom(beanClass)) {
217 show((JDialog) springContext.getBean(beanName));
218 }
219 }
220
221
222
223
224
225 public void show(JComponent component) {
226 show(JFrameWrapperFactory.wrap(component));
227 }
228
229
230
231
232
233 public void show(ApplicationFrame frame) {
234 frame.show();
235 }
236
237
238
239
240
241 public void showMain(JFrame frame) {
242 setMainFrame(frame);
243 showMain();
244 }
245
246
247
248
249
250 public void showMain(JComponent component) {
251 super.show(component);
252 }
253
254
255
256
257 public void showMain() {
258 super.show(getMainFrame());
259 }
260
261
262 public ActionsContext getActionsContext() {
263 return actionsContext;
264 }
265
266
267
268
269
270
271
272 public Action getAction(Object object, String actionName) {
273 org.jdesktop.application.ApplicationContext ac
274 = Application.getInstance().getContext();
275
276 return ac.getActionMap(Object.class, object).get(actionName);
277 }
278
279
280
281
282
283
284 public String getString(String id) {
285 org.jdesktop.application.ApplicationContext ac
286 = Application.getInstance().getContext();
287
288 return ac.getResourceMap().getString(id);
289 }
290
291
292 @Override
293 protected void shutdown() {
294 try {
295 EventServiceLocator.setEventService(EventServiceLocator.SERVICE_NAME_EVENT_BUS, null);
296 } catch (EventServiceExistsException e) {
297 s_logger.warn("Unable to unregister EventService.", e);
298 }
299
300 if (springContext != null) {
301 if (springContext instanceof ConfigurableApplicationContext) {
302 ((ConfigurableApplicationContext) springContext).close();
303 }
304 }
305 super.shutdown();
306 }
307
308
309
310
311
312 private static void setupLookAndFeel(GUIApplication application) {
313
314 final String lnfKey = "Application.preferredLookAndFeel";
315 String lnfs = application.getContext().getResourceMap().getString(lnfKey);
316 if (lnfs != null) {
317 for (String lnf : lnfs.split(",")) {
318 try {
319 lnf = lnf.trim();
320 if (lnf.equalsIgnoreCase("system")) {
321 String name = UIManager.getSystemLookAndFeelClassName();
322 UIManager.setLookAndFeel(name);
323 } else {
324 UIManager.setLookAndFeel(lnf);
325 }
326
327 break;
328 } catch (ClassNotFoundException e) {
329 s_logger.info("Look and feel '" + lnf + "' is not available.");
330 continue;
331 } catch (InstantiationException e) {
332 s_logger.info("Look and feel '" + lnf + "' is not available.");
333 continue;
334 } catch (IllegalAccessException e) {
335 s_logger.info("Look and feel '" + lnf + "' is not available.");
336 continue;
337 } catch (UnsupportedLookAndFeelException e) {
338 s_logger.info("Look and feel '" + lnf + "' is not available.");
339 continue;
340
341 }
342 }
343 }
344 }
345
346 }