gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / PropertiesSupportHelper.java @ 2651
History | View | Annotate | Download (7.6 KB)
1 | 1694 | jjdelcerro | package org.gvsig.tools.util; |
---|---|---|---|
2 | |||
3 | 2022 | omartinez | import java.io.File; |
4 | import java.net.URI; |
||
5 | import java.net.URL; |
||
6 | 1694 | jjdelcerro | import java.util.HashMap; |
7 | import java.util.Map; |
||
8 | import org.gvsig.tools.ToolsLocator; |
||
9 | import org.gvsig.tools.dynobject.DynStruct; |
||
10 | 2651 | omartinez | import org.gvsig.tools.lang.CloneableUtils; |
11 | 1933 | jjdelcerro | import org.gvsig.tools.observer.Observable; |
12 | import org.gvsig.tools.observer.ObservableHelper; |
||
13 | import org.gvsig.tools.observer.Observer; |
||
14 | 2022 | omartinez | import org.gvsig.tools.persistence.PersistenceFactory; |
15 | 1694 | jjdelcerro | import org.gvsig.tools.persistence.PersistenceManager; |
16 | import org.gvsig.tools.persistence.Persistent; |
||
17 | import org.gvsig.tools.persistence.PersistentState; |
||
18 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
19 | import org.slf4j.Logger; |
||
20 | import org.slf4j.LoggerFactory; |
||
21 | |||
22 | /**
|
||
23 | 2022 | omartinez | * This class is intended to serve as an aid to the implementation of
|
24 | * PropertiesSupport.
|
||
25 | *
|
||
26 | * When we have a class that implements the PropertiesSupport interface, we will
|
||
27 | * use PropertiesSupportHelper to implement it. For this, we will declare a
|
||
28 | * property "propertiesHelper", and we will implement the interface methods
|
||
29 | * delegating in it.
|
||
30 | *
|
||
31 | 1694 | jjdelcerro | * <code>
|
32 | * private PropertiesSupportHelper propertiesHelper = new PropertiesSupportHelper();
|
||
33 | 2022 | omartinez | *
|
34 | 1694 | jjdelcerro | * ...
|
35 | 2022 | omartinez | *
|
36 | 1694 | jjdelcerro | * @Override
|
37 | * public Object getProperty(String key) {
|
||
38 | * return this.propertiesHelper.getProperty(key);
|
||
39 | * }
|
||
40 | 2022 | omartinez | *
|
41 | 1694 | jjdelcerro | * @Override
|
42 | * public void setProperty(String key, Object obj) {
|
||
43 | * this.propertiesHelper.setProperty(key, obj);
|
||
44 | * }
|
||
45 | 2022 | omartinez | *
|
46 | 1694 | jjdelcerro | * @Override
|
47 | 2022 | omartinez | * public Map<String,Object> getProperties() { return
|
48 | * this.propertiesHelper.getProperties(); }
|
||
49 | * </code> If the class is persistent, we will declare "propertiesHelper" as:
|
||
50 | *
|
||
51 | 1694 | jjdelcerro | * <code>
|
52 | * definition.addDynFieldObject("propertiesHelper")
|
||
53 | * .setClassOfValue(PropertiesSupportHelper.class)
|
||
54 | * .setMandatory(false);
|
||
55 | * </code>
|
||
56 | 2022 | omartinez | *
|
57 | 1694 | jjdelcerro | * We will add the following code to the "loadFromState" and "saveToState":
|
58 | 2022 | omartinez | *
|
59 | 1694 | jjdelcerro | * <code>
|
60 | * // in loadFromState
|
||
61 | * this.propertiesHelper = (PropertiesSupportHelper) state.get("propertiesHelper");
|
||
62 | 2022 | omartinez | *
|
63 | 1694 | jjdelcerro | * // in saveToState
|
64 | * state.set("propertiesHelper",propertiesHelper);
|
||
65 | * </code>
|
||
66 | 2022 | omartinez | *
|
67 | 1694 | jjdelcerro | * And we will record the persistence invoking:
|
68 | 2022 | omartinez | *
|
69 | 1694 | jjdelcerro | * <code>
|
70 | * new PropertiesSupportHelper.RegisterPersistence().call()
|
||
71 | * </code>
|
||
72 | 2022 | omartinez | *
|
73 | 1694 | jjdelcerro | * Or using <code>Caller</code>:
|
74 | 2022 | omartinez | *
|
75 | 1694 | jjdelcerro | * <code>
|
76 | * caller.add(new new PropertiesSupportHelper.RegisterPersistence());
|
||
77 | * ...
|
||
78 | * caller.call()
|
||
79 | * </code>
|
||
80 | 2022 | omartinez | *
|
81 | 1694 | jjdelcerro | * @author gvSIG Team
|
82 | */
|
||
83 | 2651 | omartinez | public class PropertiesSupportHelper implements PropertiesSupport, Persistent, Observable, org.gvsig.tools.lang.Cloneable { |
84 | 1694 | jjdelcerro | |
85 | 1934 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesSupportHelper.class); |
86 | 1694 | jjdelcerro | |
87 | 1934 | jjdelcerro | public static final String PROPERTY_CHANGE = "PropertyChange"; |
88 | public static final String PROPERTIES_CHANGE = "PropertiesChange"; |
||
89 | 2022 | omartinez | |
90 | 1694 | jjdelcerro | private Map<String, Object> properties = new HashMap<>(); |
91 | 1933 | jjdelcerro | private ObservableHelper observableHelper;
|
92 | 1694 | jjdelcerro | |
93 | @Override
|
||
94 | 2651 | omartinez | public PropertiesSupportHelper clone() throws CloneNotSupportedException { |
95 | PropertiesSupportHelper other = (PropertiesSupportHelper) super.clone();
|
||
96 | |||
97 | other.properties = (Map<String, Object>) new HashMap<>(this.properties); |
||
98 | if (observableHelper!=null) { |
||
99 | other.observableHelper = this.observableHelper.clone();
|
||
100 | } |
||
101 | |||
102 | return other;
|
||
103 | } |
||
104 | |||
105 | |||
106 | @Override
|
||
107 | 1694 | jjdelcerro | public Object getProperty(String key) { |
108 | 2619 | jjdelcerro | return properties.get(key.toUpperCase());
|
109 | 1694 | jjdelcerro | } |
110 | |||
111 | @Override
|
||
112 | public void setProperty(String key, Object val) { |
||
113 | 2619 | jjdelcerro | properties.put(key.toUpperCase(), val); |
114 | 2022 | omartinez | if (this.observableHelper != null) { |
115 | 1934 | jjdelcerro | this.observableHelper.notifyObservers(this, PROPERTY_CHANGE, key, val); |
116 | 1933 | jjdelcerro | } |
117 | 1694 | jjdelcerro | } |
118 | |||
119 | @Override
|
||
120 | public Map<String, Object> getProperties() { |
||
121 | return properties;
|
||
122 | } |
||
123 | |||
124 | 2022 | omartinez | public void setProperties(Map<String, Object> properties) { |
125 | 1694 | jjdelcerro | this.properties = new HashMap(); |
126 | if (properties != null) { |
||
127 | this.copyFrom(properties);
|
||
128 | } |
||
129 | 2022 | omartinez | if (this.observableHelper != null) { |
130 | 1934 | jjdelcerro | this.observableHelper.notifyObservers(this, PROPERTIES_CHANGE, null); |
131 | 1933 | jjdelcerro | } |
132 | 1694 | jjdelcerro | } |
133 | |||
134 | 2022 | omartinez | protected void copyFrom(Map<String, Object> properties) { |
135 | 1694 | jjdelcerro | for (Map.Entry<String, Object> entry : properties.entrySet()) { |
136 | String key = entry.getKey();
|
||
137 | if (key == null) { |
||
138 | continue;
|
||
139 | } |
||
140 | Object value;
|
||
141 | try {
|
||
142 | value = entry.getValue(); |
||
143 | } catch (Throwable th) { |
||
144 | 1934 | jjdelcerro | if (LOGGER.isDebugEnabled()) {
|
145 | LOGGER.warn("Can't get property " + key + ".", th); |
||
146 | 1694 | jjdelcerro | } else {
|
147 | 1934 | jjdelcerro | LOGGER.warn("Can't get property " + key + "."); |
148 | 1694 | jjdelcerro | } |
149 | value = null;
|
||
150 | } |
||
151 | this.properties.put(key, value);
|
||
152 | } |
||
153 | } |
||
154 | |||
155 | @Override
|
||
156 | public void saveToState(PersistentState state) throws PersistenceException { |
||
157 | 2022 | omartinez | HashMap<String, Object> toSave = new HashMap<String, Object>(); |
158 | for (Map.Entry<String, Object> entry : properties.entrySet()) { |
||
159 | String key = entry.getKey();
|
||
160 | Object value = entry.getValue();
|
||
161 | if (isPersistent(value)) {
|
||
162 | toSave.put(key, value); |
||
163 | } |
||
164 | } |
||
165 | state.set("properties", toSave);
|
||
166 | 1694 | jjdelcerro | } |
167 | |||
168 | 2022 | omartinez | private boolean isPersistent(Object obj) { |
169 | if (obj instanceof Boolean |
||
170 | || obj instanceof String |
||
171 | || obj instanceof File |
||
172 | || obj instanceof URL |
||
173 | || obj instanceof URI |
||
174 | || obj instanceof Short |
||
175 | || obj instanceof Integer |
||
176 | || obj instanceof Long |
||
177 | || obj instanceof Float |
||
178 | || obj instanceof Double) { |
||
179 | return true; |
||
180 | } |
||
181 | PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
||
182 | PersistenceManager.Factories factories = persistenceManager.getFactories(); |
||
183 | PersistenceFactory factory = factories.get(obj); |
||
184 | if (factory == null) { |
||
185 | return false; |
||
186 | } |
||
187 | return true; |
||
188 | } |
||
189 | |||
190 | 1694 | jjdelcerro | @Override
|
191 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
192 | this.properties = new HashMap(); |
||
193 | Map props = (Map) state.get("properties"); |
||
194 | if (props != null) { |
||
195 | copyFrom(props); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | 1933 | jjdelcerro | @Override
|
200 | public void addObserver(Observer o) { |
||
201 | 2022 | omartinez | if (this.observableHelper == null) { |
202 | 1933 | jjdelcerro | this.observableHelper = new ObservableHelper(); |
203 | } |
||
204 | this.observableHelper.addObserver(o);
|
||
205 | } |
||
206 | |||
207 | @Override
|
||
208 | public void deleteObserver(Observer o) { |
||
209 | 2022 | omartinez | if (this.observableHelper == null) { |
210 | 1933 | jjdelcerro | this.observableHelper = new ObservableHelper(); |
211 | } |
||
212 | this.observableHelper.deleteObserver(o);
|
||
213 | } |
||
214 | |||
215 | @Override
|
||
216 | public void deleteObservers() { |
||
217 | 2022 | omartinez | if (this.observableHelper == null) { |
218 | 1933 | jjdelcerro | this.observableHelper = new ObservableHelper(); |
219 | } |
||
220 | this.observableHelper.deleteObservers();
|
||
221 | } |
||
222 | |||
223 | 2022 | omartinez | public static void registerPersistence() { |
224 | 1694 | jjdelcerro | |
225 | 2022 | omartinez | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
226 | if (manager.getDefinition("PropertiesSupportHelper") == null) { |
||
227 | DynStruct definition = manager.addDefinition( |
||
228 | PropertiesSupportHelper.class, |
||
229 | "PropertiesSupportHelper",
|
||
230 | "PropertiesSupportHelper Persistence definition",
|
||
231 | null,
|
||
232 | null
|
||
233 | ); |
||
234 | definition.addDynFieldMap("properties")
|
||
235 | 1694 | jjdelcerro | .setClassOfItems(Object.class)
|
236 | .setMandatory(true);
|
||
237 | } |
||
238 | } |
||
239 | } |