gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / PropertiesSupportHelper.java @ 2022
History | View | Annotate | Download (7.14 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 | 2022 | omartinez | import static org.gvsig.tools.dataTypes.DataTypes.URI; |
10 | 1694 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct; |
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 | 1933 | jjdelcerro | public class PropertiesSupportHelper implements PropertiesSupport, Persistent, Observable { |
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 | public Object getProperty(String key) { |
||
95 | return properties.get(key);
|
||
96 | } |
||
97 | |||
98 | @Override
|
||
99 | public void setProperty(String key, Object val) { |
||
100 | properties.put(key, val); |
||
101 | 2022 | omartinez | if (this.observableHelper != null) { |
102 | 1934 | jjdelcerro | this.observableHelper.notifyObservers(this, PROPERTY_CHANGE, key, val); |
103 | 1933 | jjdelcerro | } |
104 | 1694 | jjdelcerro | } |
105 | |||
106 | @Override
|
||
107 | public Map<String, Object> getProperties() { |
||
108 | return properties;
|
||
109 | } |
||
110 | |||
111 | 2022 | omartinez | public void setProperties(Map<String, Object> properties) { |
112 | 1694 | jjdelcerro | this.properties = new HashMap(); |
113 | if (properties != null) { |
||
114 | this.copyFrom(properties);
|
||
115 | } |
||
116 | 2022 | omartinez | if (this.observableHelper != null) { |
117 | 1934 | jjdelcerro | this.observableHelper.notifyObservers(this, PROPERTIES_CHANGE, null); |
118 | 1933 | jjdelcerro | } |
119 | 1694 | jjdelcerro | } |
120 | |||
121 | 2022 | omartinez | protected void copyFrom(Map<String, Object> properties) { |
122 | 1694 | jjdelcerro | for (Map.Entry<String, Object> entry : properties.entrySet()) { |
123 | String key = entry.getKey();
|
||
124 | if (key == null) { |
||
125 | continue;
|
||
126 | } |
||
127 | Object value;
|
||
128 | try {
|
||
129 | value = entry.getValue(); |
||
130 | } catch (Throwable th) { |
||
131 | 1934 | jjdelcerro | if (LOGGER.isDebugEnabled()) {
|
132 | LOGGER.warn("Can't get property " + key + ".", th); |
||
133 | 1694 | jjdelcerro | } else {
|
134 | 1934 | jjdelcerro | LOGGER.warn("Can't get property " + key + "."); |
135 | 1694 | jjdelcerro | } |
136 | value = null;
|
||
137 | } |
||
138 | this.properties.put(key, value);
|
||
139 | } |
||
140 | } |
||
141 | |||
142 | @Override
|
||
143 | public void saveToState(PersistentState state) throws PersistenceException { |
||
144 | 2022 | omartinez | HashMap<String, Object> toSave = new HashMap<String, Object>(); |
145 | for (Map.Entry<String, Object> entry : properties.entrySet()) { |
||
146 | String key = entry.getKey();
|
||
147 | Object value = entry.getValue();
|
||
148 | if (isPersistent(value)) {
|
||
149 | toSave.put(key, value); |
||
150 | } |
||
151 | } |
||
152 | state.set("properties", toSave);
|
||
153 | 1694 | jjdelcerro | } |
154 | |||
155 | 2022 | omartinez | private boolean isPersistent(Object obj) { |
156 | if (obj instanceof Boolean |
||
157 | || obj instanceof String |
||
158 | || obj instanceof File |
||
159 | || obj instanceof URL |
||
160 | || obj instanceof URI |
||
161 | || obj instanceof Short |
||
162 | || obj instanceof Integer |
||
163 | || obj instanceof Long |
||
164 | || obj instanceof Float |
||
165 | || obj instanceof Double) { |
||
166 | return true; |
||
167 | } |
||
168 | PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
||
169 | PersistenceManager.Factories factories = persistenceManager.getFactories(); |
||
170 | PersistenceFactory factory = factories.get(obj); |
||
171 | if (factory == null) { |
||
172 | return false; |
||
173 | } |
||
174 | return true; |
||
175 | } |
||
176 | |||
177 | 1694 | jjdelcerro | @Override
|
178 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
179 | this.properties = new HashMap(); |
||
180 | Map props = (Map) state.get("properties"); |
||
181 | if (props != null) { |
||
182 | copyFrom(props); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | 1933 | jjdelcerro | @Override
|
187 | public void addObserver(Observer o) { |
||
188 | 2022 | omartinez | if (this.observableHelper == null) { |
189 | 1933 | jjdelcerro | this.observableHelper = new ObservableHelper(); |
190 | } |
||
191 | this.observableHelper.addObserver(o);
|
||
192 | } |
||
193 | |||
194 | @Override
|
||
195 | public void deleteObserver(Observer o) { |
||
196 | 2022 | omartinez | if (this.observableHelper == null) { |
197 | 1933 | jjdelcerro | this.observableHelper = new ObservableHelper(); |
198 | } |
||
199 | this.observableHelper.deleteObserver(o);
|
||
200 | } |
||
201 | |||
202 | @Override
|
||
203 | public void deleteObservers() { |
||
204 | 2022 | omartinez | if (this.observableHelper == null) { |
205 | 1933 | jjdelcerro | this.observableHelper = new ObservableHelper(); |
206 | } |
||
207 | this.observableHelper.deleteObservers();
|
||
208 | } |
||
209 | |||
210 | 2022 | omartinez | public static void registerPersistence() { |
211 | 1694 | jjdelcerro | |
212 | 2022 | omartinez | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
213 | if (manager.getDefinition("PropertiesSupportHelper") == null) { |
||
214 | DynStruct definition = manager.addDefinition( |
||
215 | PropertiesSupportHelper.class, |
||
216 | "PropertiesSupportHelper",
|
||
217 | "PropertiesSupportHelper Persistence definition",
|
||
218 | null,
|
||
219 | null
|
||
220 | ); |
||
221 | definition.addDynFieldMap("properties")
|
||
222 | 1694 | jjdelcerro | .setClassOfItems(Object.class)
|
223 | .setMandatory(true);
|
||
224 | } |
||
225 | } |
||
226 | } |