1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.gui.swing.wrapper;
18
19 import java.lang.ref.WeakReference;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import javax.swing.JComponent;
24
25 import org.jdesktop.application.Application;
26 import org.jdesktop.application.ApplicationContext;
27 import org.jdesktop.application.ResourceMap;
28
29 import ch.elca.el4j.services.gui.swing.frames.ApplicationFrame;
30
31 /**
32 * This abstract class helps wrapping components into a container like
33 * {@link JInternalFrame} or {@link JFrame}. As the module Swing supports
34 * both MDI and docking, this has been introduced to use a single component
35 * in both approaches. Depending on the base class of the application
36 * ({@link MDIApplication} or {@link DockingApplication}), forms provided
37 * as {@link JComponent}s are automatically wrapped into the right "container"
38 * (see {@link GUIApplication#show(JComponent)}.
39 *
40 * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/ch/elca/el4j/services/gui/swing/wrapper/AbstractWrapperFactory.java $
41 *
42 * @param <T> the type of wrapper
43 *
44 * @author Stefan Wismer (SWI)
45 */
46 public abstract class AbstractWrapperFactory<T extends ApplicationFrame> {
47 /**
48 * A mapping between wrapped component and wrapper.
49 */
50 protected static Map<JComponent, WeakReference<ApplicationFrame>> s_componentToWrapper
51 = new HashMap<JComponent, WeakReference<ApplicationFrame>>();
52
53 /**
54 * Wraps a GUI component.
55 *
56 * @param component the component to wrap
57 * @return the wrapper
58 */
59 @SuppressWarnings("unchecked")
60 protected T wrapComponent(JComponent component) {
61 // check if component has already a wrapper
62 if (s_componentToWrapper.get(component) != null) {
63 if (s_componentToWrapper.get(component).get() != null) {
64 return (T) s_componentToWrapper.get(component).get();
65 }
66 }
67 ApplicationContext appContext = Application.getInstance().getContext();
68 ResourceMap map = appContext.getResourceMap(component.getClass());
69
70 String name = map.getString("name");
71 if (name == null) {
72 name = component.getClass().getName();
73 }
74
75 String title = map.getString("title");
76 if (title == null) {
77 title = name;
78 }
79
80 T wrapper = createApplicationFrame(name, title, component);
81
82
83 // inject values from properties file
84 component.setName(name);
85 map.injectComponents(component);
86
87 s_componentToWrapper.put(component, new WeakReference<ApplicationFrame>(wrapper));
88
89 return wrapper;
90 }
91
92
93 /**
94 * @return the concrete wrapper
95 */
96 protected abstract T createApplicationFrame(String name, String title, JComponent component);
97
98 /**
99 * @param component a wrapped component
100 * @return the corresponding wrapper
101 */
102 public static ApplicationFrame getFrame(JComponent component) {
103 if (s_componentToWrapper.get(component) == null) {
104 return null;
105 } else {
106 return s_componentToWrapper.get(component).get();
107 }
108 }
109
110 /**
111 * Remove the wrapper from a component.
112 * @param component the component to remove the wrapper
113 */
114 public static void removeWrapper(JComponent component) {
115 s_componentToWrapper.remove(component);
116 }
117 }