Revision 2022 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/PropertiesSupportHelper.java
PropertiesSupportHelper.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.util; |
2 | 2 |
|
3 |
import java.io.File; |
|
4 |
import java.net.URI; |
|
5 |
import java.net.URL; |
|
3 | 6 |
import java.util.HashMap; |
4 | 7 |
import java.util.Map; |
5 | 8 |
import org.gvsig.tools.ToolsLocator; |
9 |
import static org.gvsig.tools.dataTypes.DataTypes.URI; |
|
6 | 10 |
import org.gvsig.tools.dynobject.DynStruct; |
7 | 11 |
import org.gvsig.tools.observer.Observable; |
8 | 12 |
import org.gvsig.tools.observer.ObservableHelper; |
9 | 13 |
import org.gvsig.tools.observer.Observer; |
14 |
import org.gvsig.tools.persistence.PersistenceFactory; |
|
10 | 15 |
import org.gvsig.tools.persistence.PersistenceManager; |
11 | 16 |
import org.gvsig.tools.persistence.Persistent; |
12 | 17 |
import org.gvsig.tools.persistence.PersistentState; |
... | ... | |
15 | 20 |
import org.slf4j.LoggerFactory; |
16 | 21 |
|
17 | 22 |
/** |
18 |
* This class is intended to serve as an aid to the implementation |
|
19 |
* of PropertiesSupport.
|
|
20 |
*
|
|
21 |
* When we have a class that implements the PropertiesSupport interface, |
|
22 |
* we will use PropertiesSupportHelper to implement it. For this, we will
|
|
23 |
* declare a property "propertiesHelper", and we will implement the interface
|
|
24 |
* methods delegating in it.
|
|
25 |
*
|
|
23 |
* 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 |
* |
|
26 | 31 |
* <code> |
27 | 32 |
* private PropertiesSupportHelper propertiesHelper = new PropertiesSupportHelper(); |
28 |
*
|
|
33 |
* |
|
29 | 34 |
* ... |
30 |
*
|
|
35 |
* |
|
31 | 36 |
* @Override |
32 | 37 |
* public Object getProperty(String key) { |
33 | 38 |
* return this.propertiesHelper.getProperty(key); |
34 | 39 |
* } |
35 |
*
|
|
40 |
* |
|
36 | 41 |
* @Override |
37 | 42 |
* public void setProperty(String key, Object obj) { |
38 | 43 |
* this.propertiesHelper.setProperty(key, obj); |
39 | 44 |
* } |
40 |
*
|
|
45 |
* |
|
41 | 46 |
* @Override |
42 |
* public Map<String,Object> getProperties() { |
|
43 |
* return this.propertiesHelper.getProperties(); |
|
44 |
* } |
|
45 |
* </code> |
|
46 |
* If the class is persistent, we will declare "propertiesHelper" as: |
|
47 |
* |
|
47 |
* public Map<String,Object> getProperties() { return |
|
48 |
* this.propertiesHelper.getProperties(); } |
|
49 |
* </code> If the class is persistent, we will declare "propertiesHelper" as: |
|
50 |
* |
|
48 | 51 |
* <code> |
49 | 52 |
* definition.addDynFieldObject("propertiesHelper") |
50 | 53 |
* .setClassOfValue(PropertiesSupportHelper.class) |
51 | 54 |
* .setMandatory(false); |
52 | 55 |
* </code> |
53 |
*
|
|
56 |
* |
|
54 | 57 |
* We will add the following code to the "loadFromState" and "saveToState": |
55 |
*
|
|
58 |
* |
|
56 | 59 |
* <code> |
57 | 60 |
* // in loadFromState |
58 | 61 |
* this.propertiesHelper = (PropertiesSupportHelper) state.get("propertiesHelper"); |
59 |
*
|
|
62 |
* |
|
60 | 63 |
* // in saveToState |
61 | 64 |
* state.set("propertiesHelper",propertiesHelper); |
62 | 65 |
* </code> |
63 |
*
|
|
66 |
* |
|
64 | 67 |
* And we will record the persistence invoking: |
65 |
*
|
|
68 |
* |
|
66 | 69 |
* <code> |
67 | 70 |
* new PropertiesSupportHelper.RegisterPersistence().call() |
68 | 71 |
* </code> |
69 |
*
|
|
72 |
* |
|
70 | 73 |
* Or using <code>Caller</code>: |
71 |
*
|
|
74 |
* |
|
72 | 75 |
* <code> |
73 | 76 |
* caller.add(new new PropertiesSupportHelper.RegisterPersistence()); |
74 | 77 |
* ... |
75 | 78 |
* caller.call() |
76 | 79 |
* </code> |
77 |
*
|
|
80 |
* |
|
78 | 81 |
* @author gvSIG Team |
79 | 82 |
*/ |
80 |
|
|
81 | 83 |
public class PropertiesSupportHelper implements PropertiesSupport, Persistent, Observable { |
82 | 84 |
|
83 | 85 |
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesSupportHelper.class); |
84 | 86 |
|
85 | 87 |
public static final String PROPERTY_CHANGE = "PropertyChange"; |
86 | 88 |
public static final String PROPERTIES_CHANGE = "PropertiesChange"; |
87 |
|
|
89 |
|
|
88 | 90 |
private Map<String, Object> properties = new HashMap<>(); |
89 | 91 |
private ObservableHelper observableHelper; |
90 | 92 |
|
... | ... | |
96 | 98 |
@Override |
97 | 99 |
public void setProperty(String key, Object val) { |
98 | 100 |
properties.put(key, val); |
99 |
if( this.observableHelper!=null ) {
|
|
101 |
if (this.observableHelper != null) {
|
|
100 | 102 |
this.observableHelper.notifyObservers(this, PROPERTY_CHANGE, key, val); |
101 | 103 |
} |
102 | 104 |
} |
... | ... | |
106 | 108 |
return properties; |
107 | 109 |
} |
108 | 110 |
|
109 |
public void setProperties(Map<String,Object> properties) { |
|
111 |
public void setProperties(Map<String, Object> properties) {
|
|
110 | 112 |
this.properties = new HashMap(); |
111 | 113 |
if (properties != null) { |
112 | 114 |
this.copyFrom(properties); |
113 | 115 |
} |
114 |
if( this.observableHelper!=null ) {
|
|
116 |
if (this.observableHelper != null) {
|
|
115 | 117 |
this.observableHelper.notifyObservers(this, PROPERTIES_CHANGE, null); |
116 | 118 |
} |
117 | 119 |
} |
118 | 120 |
|
119 |
private void copyFrom(Map<String, Object> properties) {
|
|
121 |
protected void copyFrom(Map<String, Object> properties) {
|
|
120 | 122 |
for (Map.Entry<String, Object> entry : properties.entrySet()) { |
121 | 123 |
String key = entry.getKey(); |
122 | 124 |
if (key == null) { |
... | ... | |
139 | 141 |
|
140 | 142 |
@Override |
141 | 143 |
public void saveToState(PersistentState state) throws PersistenceException { |
142 |
state.set("properties", properties); |
|
144 |
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); |
|
143 | 153 |
} |
144 | 154 |
|
155 |
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 |
|
|
145 | 177 |
@Override |
146 | 178 |
public void loadFromState(PersistentState state) throws PersistenceException { |
147 | 179 |
this.properties = new HashMap(); |
... | ... | |
153 | 185 |
|
154 | 186 |
@Override |
155 | 187 |
public void addObserver(Observer o) { |
156 |
if( this.observableHelper==null ) {
|
|
188 |
if (this.observableHelper == null) {
|
|
157 | 189 |
this.observableHelper = new ObservableHelper(); |
158 | 190 |
} |
159 | 191 |
this.observableHelper.addObserver(o); |
... | ... | |
161 | 193 |
|
162 | 194 |
@Override |
163 | 195 |
public void deleteObserver(Observer o) { |
164 |
if( this.observableHelper==null ) {
|
|
196 |
if (this.observableHelper == null) {
|
|
165 | 197 |
this.observableHelper = new ObservableHelper(); |
166 | 198 |
} |
167 | 199 |
this.observableHelper.deleteObserver(o); |
... | ... | |
169 | 201 |
|
170 | 202 |
@Override |
171 | 203 |
public void deleteObservers() { |
172 |
if( this.observableHelper==null ) {
|
|
204 |
if (this.observableHelper == null) {
|
|
173 | 205 |
this.observableHelper = new ObservableHelper(); |
174 | 206 |
} |
175 | 207 |
this.observableHelper.deleteObservers(); |
176 | 208 |
} |
177 | 209 |
|
178 |
public static class RegisterPersistence implements Callable {
|
|
210 |
public static void registerPersistence() {
|
|
179 | 211 |
|
180 |
@Override |
|
181 |
public Object call() { |
|
182 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
|
183 |
if( manager.getDefinition("PropertiesSupportHelper")==null ) { |
|
184 |
DynStruct definition = manager.addDefinition( |
|
185 |
PropertiesSupportHelper.class, |
|
186 |
"PropertiesSupportHelper", |
|
187 |
"PropertiesSupportHelper Persistence definition", |
|
188 |
null, |
|
189 |
null |
|
190 |
); |
|
191 |
definition.addDynFieldMap("properties") |
|
212 |
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") |
|
192 | 222 |
.setClassOfItems(Object.class) |
193 | 223 |
.setMandatory(true); |
194 |
} |
|
195 |
return Boolean.TRUE; |
|
196 | 224 |
} |
197 | 225 |
} |
198 |
|
|
199 | 226 |
} |
Also available in: Unified diff