KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > builtin > Configurator


1 /*
2  * Copyright 2005 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.buchuki.ensmer.builtin;
18
19 import com.buchuki.ensmer.Area;
20 import com.buchuki.ensmer.AreaManager;
21 import com.buchuki.ensmer.EnsmerManager;
22 import com.buchuki.ensmer.object.Backend;
23 import com.buchuki.ensmer.object.Frontend;
24 import java.io.Serializable JavaDoc;
25 import java.util.*;
26 import javax.swing.event.ChangeEvent JavaDoc;
27
28 /**
29  * Backend Class to manage configuration parameters on a per-object basis.
30  *
31  * @author Dusty Phillips [dusty@buchuki.com]
32  */

33 public class Configurator extends Backend {
34     
35     /**
36      * Creates a new instance of Configurator
37      *
38      * @param id the id of the object the configurator is for. This could
39      * break everything, there has to be a default constructor for all backends,
40      * only maybe not in this special case.
41      */

42     public Configurator(Long JavaDoc id) {
43         properties = new Properties();
44         this.id = id;
45     }
46     
47     /**
48      * Construct a Configurator object based on serialized properties data.
49      *
50      * @param data the previously prevalyed properties
51      */

52     public Configurator(Serializable JavaDoc data) {
53         Object JavaDoc[] arr = (Object JavaDoc[]) data;
54         properties = (Properties) arr[0];
55         id = (Long JavaDoc) arr[1];
56     }
57     
58     /**
59      * Retrieve the Serializable data for the configurator to store it.
60      *
61      * @return the Serializable data
62      */

63     @Override JavaDoc
64     public Serializable JavaDoc getSerializable() {
65         return new Object JavaDoc[] {properties, id};
66     }
67     
68     /**
69      * Set a specific key/value pair
70      *
71      * @param key the name of the property
72      * @param value the value of that property
73      */

74     public void setProperty(String JavaDoc key, String JavaDoc value) {
75         properties.setProperty(key, value);
76         //probably should be elsewhere, but putting it in a changelistener just bjorks everything
77
AreaManager areaman = EnsmerManager.instance().getAreaManager();
78         Long JavaDoc areaID = areaman.getAreaIDForObject(id);
79         if (areaID != null) {
80             Area area = areaman.getArea(areaID);
81             Frontend front = area.getFrontend(id);
82             if (front != null) {
83                 front.stateChanged(new ChangeEvent JavaDoc(EnsmerManager.instance().getBackhoe().findBackend(id)));
84             }
85         }
86         fireChangeEvent();
87     }
88     
89     /**
90      * Get a value for a specific property
91      *
92      * @param key the name of the property to get a value for
93      * @return the value for that key or null if it does not exist
94      */

95     public String JavaDoc getProperty(String JavaDoc key) {
96         return properties.getProperty(key);
97     }
98     
99     /**
100      * List of property names in the properties
101      */

102     public List<String JavaDoc> getPropertyNames() {
103         Enumeration en = properties.propertyNames();
104         List<String JavaDoc> retVal = new ArrayList<String JavaDoc>();
105         while (en.hasMoreElements()) {
106             retVal.add((String JavaDoc) en.nextElement());
107         }
108         return retVal;
109     }
110     
111     /**
112      * Clear all Properties
113      */

114     public void clearProperties() {
115         properties.clear();
116         fireChangeEvent();
117     }
118     
119     /**
120      * The properties file associated with the object
121      */

122     private Properties properties;
123     
124     /**
125      * The identifier of the object associated with this configurator
126      */

127     private Long JavaDoc id;
128     
129 }
130
Popular Tags