1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2WebGui library.
13 *
14 * The Initial Owner of the Original Code is Power Of Two S.R.L.
15 * Portions created by Power Of Two S.R.L. are Copyright (C) Power Of Two S.R.L.
16 * All Rights Reserved.
17 *
18 * Contributor(s):
19 */
20
21 package com.pow2.webgui;
22
23 import java.util.*;
24 import org.apache.log4j.*;
25
26 import com.pow2.util.*;
27
28
29 /***
30 * Widget Factory class.
31 *
32 * @author Luca Fossato
33 * @created 12 October 2001
34 */
35 public class WidgetFactory
36 {
37 private static final String KEY_WIDGET_ENTRY = "widget.";
38 private static final String KEY_DRAWER_ENTRY = "drawer.";
39 private static final String KEY_CONTENTPROVIDER_ENTRY = "contentprovider.";
40
41 private final static Category cat = Category.getInstance(WidgetFactory.class);
42
43 private static WidgetFactory instance = null;
44 private HashMap widgetInfoMap = null;
45
46
47 /***
48 * Private Constructor for the WidgetFactory object.
49 */
50 private WidgetFactory()
51 {
52 widgetInfoMap = new HashMap();
53 }
54
55
56 /***
57 * Get the instance of WidgetFactory class.
58 *
59 * @return the instance of WidgetFactory class
60 */
61 public static synchronized WidgetFactory instance()
62 {
63 if (instance == null)
64 instance = new WidgetFactory();
65
66 return instance;
67 }
68
69
70 /***
71 * Test if the WidgetFactory hashmap contains the WidgetInfo object
72 * having the input key.
73 *
74 * @param key the widgetInfo key
75 * @return true if the widget map contains the widgetInfo object having the input key;
76 * false otherwise
77 */
78 public boolean containsWidgetInfo(String key)
79 {
80 return widgetInfoMap.containsKey(key);
81 }
82
83
84 /***
85 * Remove the widgetInfo object having the input key
86 * from the WidgetFactory hashmap.
87 *
88 * @param key the prototype key.
89 */
90 public void removeWidgetInfo(String key)
91 {
92 if (widgetInfoMap.containsKey(key))
93 {
94 widgetInfoMap.remove(key);
95 cat.debug("::removeWidgetInfo - removed the widgetInfo object having the key [" + key + "]");
96 }
97 }
98
99
100 /***
101 * Get a Widget object.
102 *
103 * @param key the key used to retrieve an instance of the registered widget
104 * from the factory hashmap
105 * @return the Widget object
106 * @exception Exception if any error occurs
107 */
108 public Widget getWidget(String key) throws Exception
109 {
110 WidgetInfo wInfo = getWidgetInfo(key);
111 Widget widget = (Widget)Class.forName(wInfo.getClassName()).newInstance();
112
113
114
115
116 widget.setWidgetInfo(wInfo);
117
118
119
120
121
122
123
124
125
126
127 return widget;
128 }
129
130
131 /***
132 * Register all the widget prototypes declared into the input properties file.
133 *
134 * @param propsFile The new prototypes value
135 * @exception Exception if any error occurs
136 */
137 public void setWidgets(String propsFile) throws Exception
138 {
139 Properties props = FileUtil.getProperties(propsFile, false);
140 registerWidgets(props);
141 registerWidgetDrawers(props);
142 registerContentProviders(props);
143 }
144
145
146 /***
147 * PRIVATE METHODS HERE
148 */
149
150
151 /***
152 * Register all the widgets declared using the KEY_WIDGET_ENTRY key.
153 *
154 * @param props the configuration properties file
155 * @exception Exception if any error occurs
156 */
157 private void registerWidgets(Properties props) throws Exception
158 {
159 Enumeration en = props.keys();
160
161 while (en.hasMoreElements())
162 {
163 Widget w = null;
164 String key = (String) en.nextElement();
165 String keyValue = props.getProperty(key);
166
167
168 if (key.startsWith(KEY_WIDGET_ENTRY))
169 {
170 WidgetInfo wInfo = new WidgetInfo();
171 String widgetName = key.substring(KEY_WIDGET_ENTRY.length(), key.length());
172 wInfo.setName (widgetName);
173 wInfo.setClassName(keyValue);
174 setWidget(widgetName, wInfo);
175 }
176 }
177 }
178
179
180 /***
181 * Register all the widgets drawers declared using the KEY_DRAWER_ENTRY key.
182 *
183 * @param props the configuration properties file
184 */
185 private void registerWidgetDrawers(Properties props)
186 {
187 Enumeration en = props.keys();
188
189 while (en.hasMoreElements())
190 {
191 String key = (String) en.nextElement();
192 String keyValue = props.getProperty(key);
193
194
195 if (key.startsWith(KEY_DRAWER_ENTRY))
196 {
197
198 String widgetName = key.substring(KEY_DRAWER_ENTRY.length(), key.lastIndexOf('.'));
199 String drawerName = key.substring(key.lastIndexOf('.')+1, key.length());
200 WidgetInfo wInfo = (WidgetInfo)widgetInfoMap.get(widgetName);
201
202 if (wInfo != null)
203 {
204 wInfo.setWidgetDrawer(drawerName, keyValue);
205 cat.info("::registerWidgetDrawers - registered the drawer, class [" + drawerName + ", " + keyValue + "] for widget [" + widgetName + "]");
206 }
207 }
208 }
209 }
210
211
212 /***
213 * Register all the widgets content providers declared using the KEY_CONTENTPROVIDER_ENTRY key.
214 *
215 * @param props the configuration properties file
216 */
217 private void registerContentProviders(Properties props)
218 {
219 Enumeration en = props.keys();
220
221 while (en.hasMoreElements())
222 {
223 String key = (String) en.nextElement();
224 String keyValue = props.getProperty(key);
225
226
227 if (key.startsWith(KEY_CONTENTPROVIDER_ENTRY))
228 {
229
230 String widgetName = key.substring(KEY_CONTENTPROVIDER_ENTRY.length(), key.lastIndexOf('.'));
231 String cpName = key.substring(key.lastIndexOf('.')+1, key.length());
232 WidgetInfo wInfo = (WidgetInfo)widgetInfoMap.get(widgetName);
233
234 if (wInfo != null)
235 {
236 wInfo.setContentProvider(cpName, keyValue);
237 cat.info("::registerContentProviders - registered the cprovider, class [" + cpName + ", " + keyValue + "] for widget [" + widgetName + "]");
238 }
239 }
240 }
241 }
242
243
244 /***
245 * Sets a new widget into the WidgetFactory map.
246 *
247 * @param key the key string used as hashmap key
248 * @param w the Widget object
249 * @param Exception if any error occurs
250 */
251 private void setWidget(String key, WidgetInfo wInfo) throws Exception
252 {
253 if (Util.isNull(key))
254 throw new Exception("key is null; cannot register this widget");
255
256 if (wInfo == null)
257 throw new Exception("widgetInfo is null; cannot register the widget havigng key [" + key + "]");
258
259 if (containsWidgetInfo(key))
260 throw new Exception("key [" + key + "] already present into the WidgetFactory map");
261
262
263 widgetInfoMap.put(key, wInfo);
264 cat.info("::setWidget - widget successfully registered using key [" + key + "]");
265 }
266
267
268 /***
269 * Get the WidgetInfo object related to the input widget key.
270 *
271 * @param key the key used to retrieve the WidgetInfo object
272 * from the factory hashmap
273 * @return the WidgetInfo object
274 * @exception Exception if any error occurs
275 */
276 private WidgetInfo getWidgetInfo(String key) throws Exception
277 {
278 WidgetInfo wInfo = null;
279
280 if ((wInfo = (WidgetInfo) widgetInfoMap.get(key)) == null)
281 throw new Exception("the key [" + key + "] doesn't match any registered widgetInfo object");
282
283 return wInfo;
284 }
285 }