|
1 |
| package org.jconfig; |
|
2 |
| |
|
3 |
| import java.util.Properties; |
|
4 |
| import java.util.StringTokenizer; |
|
5 |
| import java.util.Vector; |
|
6 |
| |
|
7 |
| import javax.swing.event.EventListenerList; |
|
8 |
| |
|
9 |
| import org.jconfig.event.CategoryChangedEvent; |
|
10 |
| import org.jconfig.event.CategoryListener; |
|
11 |
| import org.jconfig.event.ConfigurationChangedEventImpl; |
|
12 |
| import org.jconfig.event.PropertyChangedEvent; |
|
13 |
| import org.jconfig.event.PropertyListener; |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| public class DefaultCategory implements Category { |
|
35 |
| |
|
36 |
| private static final VariableManager vm = VariableManager.getInstance(); |
|
37 |
| private String extendsCategory; |
|
38 |
| private String name = null; |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| private Properties properties = new Properties(); |
|
44 |
| |
|
45 |
| private EventListenerList listenerList = new EventListenerList(); |
|
46 |
| |
|
47 |
| private String configurationName; |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
329
| public DefaultCategory(String categoryName) {
|
|
54 |
329
| name = categoryName;
|
|
55 |
| } |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
655
| public Category setProperty(String propertyName, String propertyValue) {
|
|
71 |
655
| String temp = getProperty(propertyName);
|
|
72 |
655
| if( propertyValue == null ) {
|
|
73 |
4
| properties.remove(propertyName);
|
|
74 |
4
| firePropertyChangedEvent(
|
|
75 |
| new ConfigurationChangedEventImpl( |
|
76 |
| PropertyChangedEvent.PROPERTY_REMOVED, this, |
|
77 |
| propertyName, temp, propertyValue)); |
|
78 |
4
| return this;
|
|
79 |
| } |
|
80 |
651
| properties.setProperty(propertyName, propertyValue);
|
|
81 |
651
| if(temp == null) {
|
|
82 |
641
| firePropertyChangedEvent(
|
|
83 |
| new ConfigurationChangedEventImpl( |
|
84 |
| PropertyChangedEvent.PROPERTY_ADDED, this, |
|
85 |
| propertyName, temp, propertyValue)); |
|
86 |
| } |
|
87 |
| else { |
|
88 |
10
| if (!temp.equals(propertyValue))
|
|
89 |
7
| firePropertyChangedEvent(
|
|
90 |
| new ConfigurationChangedEventImpl( |
|
91 |
| PropertyChangedEvent.PROPERTY_CHANGED, this, |
|
92 |
| propertyName, temp, propertyValue)); |
|
93 |
| } |
|
94 |
651
| return this;
|
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
825
| public String getProperty(String propertyName) {
|
|
108 |
825
| String value = properties.getProperty(propertyName);
|
|
109 |
825
| if (value != null) {
|
|
110 |
132
| return vm.replaceVariables(value,getConfigurationName());
|
|
111 |
| } else { |
|
112 |
693
| return value;
|
|
113 |
| } |
|
114 |
| } |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
3
| public String getProperty(String propertyName, String defaultValue) {
|
|
129 |
3
| String temp = properties.getProperty(propertyName, vm.replaceVariables(defaultValue,getConfigurationName()));
|
|
130 |
3
| if(temp != null) {
|
|
131 |
3
| return vm.replaceVariables(temp,getConfigurationName());
|
|
132 |
| } |
|
133 |
0
| return properties.getProperty(propertyName, vm.replaceVariables(defaultValue,getConfigurationName()));
|
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
307
| public String getCategoryName() {
|
|
143 |
307
| return name;
|
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
264
| public void addCategoryListener(CategoryListener listener) {
|
|
154 |
264
| listenerList.add(CategoryListener.class, listener);
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
0
| public void removeCategoryListener(CategoryListener listener) {
|
|
165 |
0
| listenerList.remove(CategoryListener.class, listener);
|
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
917
| public void fireCategoryChangedEvent(CategoryChangedEvent event) {
|
|
176 |
| |
|
177 |
917
| Object[] listeners = listenerList.getListenerList();
|
|
178 |
| |
|
179 |
| |
|
180 |
917
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
181 |
603
| if (listeners[i] == CategoryListener.class) {
|
|
182 |
| |
|
183 |
600
| ((CategoryListener) listeners[i + 1]).categoryChanged(event);
|
|
184 |
| } |
|
185 |
| } |
|
186 |
| |
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
37
| public Properties getProperties() {
|
|
196 |
37
| return properties;
|
|
197 |
| } |
|
198 |
| |
|
199 |
0
| public String[] getArray(String key) {
|
|
200 |
0
| return getArray(key,null);
|
|
201 |
| } |
|
202 |
| |
|
203 |
0
| public String[] getArray(String key, String[] defaultValue) {
|
|
204 |
0
| String value = properties.getProperty(key);
|
|
205 |
0
| if ( value == null ) {
|
|
206 |
0
| return defaultValue;
|
|
207 |
| } |
|
208 |
0
| Vector all = new Vector();
|
|
209 |
0
| StringTokenizer sto = new StringTokenizer(value,",");
|
|
210 |
0
| while ( sto.hasMoreElements() ) {
|
|
211 |
0
| String val = (String)sto.nextElement();
|
|
212 |
0
| val = vm.replaceVariables(val,configurationName);
|
|
213 |
0
| all.add(val);
|
|
214 |
| } |
|
215 |
0
| return (String[])all.toArray(new String[0]);
|
|
216 |
| } |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
5
| public boolean getBooleanProperty(String name, boolean defaultValue) {
|
|
228 |
5
| String value = properties.getProperty(name);
|
|
229 |
5
| if ( value == null ) {
|
|
230 |
1
| return defaultValue;
|
|
231 |
| } else { |
|
232 |
4
| try {
|
|
233 |
4
| return Boolean.valueOf(vm.replaceVariables(value,getConfigurationName())).booleanValue();
|
|
234 |
| } catch (Exception e) { |
|
235 |
0
| return defaultValue;
|
|
236 |
| } |
|
237 |
| } |
|
238 |
| } |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
4
| public void setBooleanProperty(String name, boolean value) {
|
|
248 |
4
| Boolean bool = new Boolean(value);
|
|
249 |
4
| setProperty(name, bool.toString());
|
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
8
| public char getCharProperty(String name, char defaultValue) {
|
|
262 |
8
| String value = properties.getProperty(name);
|
|
263 |
8
| if (value == null) {
|
|
264 |
0
| return defaultValue;
|
|
265 |
| } else { |
|
266 |
8
| String subValue = vm.replaceVariables(value,getConfigurationName());
|
|
267 |
8
| if (subValue.length() == 1) {
|
|
268 |
8
| return subValue.charAt(0);
|
|
269 |
| } else { |
|
270 |
0
| return defaultValue;
|
|
271 |
| } |
|
272 |
| } |
|
273 |
| } |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
3
| public void setCharProperty(String key, char value) {
|
|
283 |
3
| Character temp = new Character(value);
|
|
284 |
3
| setProperty(key, temp.toString());
|
|
285 |
| } |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
3
| public double getDoubleProperty(String name, double defaultValue) {
|
|
297 |
3
| String value = properties.getProperty(name);
|
|
298 |
3
| if (value == null) {
|
|
299 |
0
| return defaultValue;
|
|
300 |
| } else { |
|
301 |
3
| try {
|
|
302 |
3
| return Double.parseDouble(vm.replaceVariables(value,getConfigurationName()));
|
|
303 |
| } catch (Exception e) { |
|
304 |
0
| return defaultValue;
|
|
305 |
| } |
|
306 |
| } |
|
307 |
| |
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
4
| public int getIntProperty(String name, int defaultValue) {
|
|
323 |
4
| String value = properties.getProperty(name);
|
|
324 |
4
| if (value == null) {
|
|
325 |
0
| return defaultValue;
|
|
326 |
| } else { |
|
327 |
4
| try {
|
|
328 |
4
| return Integer.parseInt(vm.replaceVariables(value,getConfigurationName()));
|
|
329 |
| } catch (NumberFormatException nfe) { |
|
330 |
0
| return defaultValue;
|
|
331 |
| } |
|
332 |
| } |
|
333 |
| |
|
334 |
| } |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
3
| public void setIntProperty(String key, int value) {
|
|
344 |
3
| Integer temp = new Integer(value);
|
|
345 |
3
| setProperty(key, temp.toString());
|
|
346 |
| } |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
3
| public long getLongProperty(String name, long defaultValue) {
|
|
361 |
3
| String value = (String)properties.get(name);
|
|
362 |
3
| value = getProperty(name);
|
|
363 |
3
| if (value == null) {
|
|
364 |
0
| return defaultValue;
|
|
365 |
| } else { |
|
366 |
3
| try {
|
|
367 |
3
| return Long.parseLong(vm.replaceVariables(value,getConfigurationName()));
|
|
368 |
| } catch (Exception e) { |
|
369 |
0
| return defaultValue;
|
|
370 |
| } |
|
371 |
| } |
|
372 |
| } |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
3
| public void setLongProperty(String key, long value) {
|
|
382 |
3
| Long temp = new Long(value);
|
|
383 |
3
| setProperty(key, temp.toString());
|
|
384 |
| } |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
160
| public String getConfigurationName() {
|
|
394 |
160
| return configurationName;
|
|
395 |
| } |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
301
| public void setConfigurationName(String configurationName) {
|
|
405 |
301
| this.configurationName = configurationName;
|
|
406 |
| } |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
3
| public void setDoubleProperty(String key, double value) {
|
|
416 |
3
| Double temp = new Double(value);
|
|
417 |
3
| setProperty(key, temp.toString());
|
|
418 |
| } |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
1
| public void addPropertyListener( PropertyListener listener ) {
|
|
428 |
1
| listenerList.add( PropertyListener.class, listener );
|
|
429 |
| } |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
0
| public void removePropertyListener( PropertyListener listener ) {
|
|
439 |
0
| listenerList.remove( PropertyListener.class, listener );
|
|
440 |
| } |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
652
| public void firePropertyChangedEvent(PropertyChangedEvent event) {
|
|
449 |
| |
|
450 |
652
| Object[] listeners = listenerList.getListenerList();
|
|
451 |
| |
|
452 |
652
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
453 |
338
| if (listeners[i] == PropertyListener.class) {
|
|
454 |
| |
|
455 |
3
| ((PropertyListener) listeners[i + 1]).propertyChanged(event);
|
|
456 |
| } |
|
457 |
| } |
|
458 |
652
| fireCategoryChangedEvent((CategoryChangedEvent)event);
|
|
459 |
| } |
|
460 |
| |
|
461 |
3
| public void setExtendsCategory(String extendsCategory) {
|
|
462 |
3
| this.extendsCategory = extendsCategory;
|
|
463 |
| } |
|
464 |
| |
|
465 |
4
| public String getExtendsCategory() {
|
|
466 |
4
| return extendsCategory;
|
|
467 |
| } |
|
468 |
| |
|
469 |
| } |