svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / ExtendedPropertiesHelper.java @ 43215
History | View | Annotate | Download (4.67 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.mapcontext.layers; |
25 |
|
26 |
import java.util.HashMap; |
27 |
import java.util.Map; |
28 |
import java.util.Map.Entry; |
29 |
import java.util.Set; |
30 |
|
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.dynobject.DynStruct; |
36 |
import org.gvsig.tools.persistence.PersistenceManager; |
37 |
import org.gvsig.tools.persistence.Persistent; |
38 |
import org.gvsig.tools.persistence.PersistentState; |
39 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
40 |
import org.gvsig.tools.util.Callable; |
41 |
|
42 |
public class ExtendedPropertiesHelper implements ExtendedPropertiesSupport, Persistent { |
43 |
|
44 |
private static final Logger logger = LoggerFactory.getLogger(ExtendedPropertiesHelper.class); |
45 |
|
46 |
/*
|
47 |
Add this to class that implements ExtendedPropertiesSupport
|
48 |
|
49 |
private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
|
50 |
|
51 |
This methods
|
52 |
|
53 |
public Object getProperty(Object key) {
|
54 |
return this.propertiesHelper.getProperty(key);
|
55 |
}
|
56 |
|
57 |
public void setProperty(Object key, Object obj) {
|
58 |
this.propertiesHelper.setProperty(key, obj);
|
59 |
}
|
60 |
|
61 |
public Map getExtendedProperties() {
|
62 |
return this.propertiesHelper.getExtendedProperties();
|
63 |
}
|
64 |
|
65 |
To the persistence defnition
|
66 |
|
67 |
definition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
|
68 |
.setMandatory(false);
|
69 |
|
70 |
To the loadFromState
|
71 |
|
72 |
this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
|
73 |
|
74 |
And to the saveToState
|
75 |
|
76 |
state.set("propertiesHelper",propertiesHelper);
|
77 |
|
78 |
*/
|
79 |
private Map properties = new HashMap(); |
80 |
|
81 |
@Override
|
82 |
public Object getProperty(Object key) { |
83 |
return properties.get(key);
|
84 |
} |
85 |
|
86 |
@Override
|
87 |
public void setProperty(Object key, Object val) { |
88 |
properties.put(key, val); |
89 |
} |
90 |
|
91 |
@Override
|
92 |
public Map getExtendedProperties() { |
93 |
return properties;
|
94 |
} |
95 |
|
96 |
public void setExtendedProperties(Map properties) { |
97 |
this.properties = new HashMap(); |
98 |
if( properties!=null ) { |
99 |
// this.properties.putAll(properties);
|
100 |
this.copyFrom(properties);
|
101 |
} |
102 |
} |
103 |
|
104 |
private void copyFrom(Map properties) { |
105 |
for(Entry entry : (Set<Entry>)properties.entrySet() ) { |
106 |
Object key = entry.getKey();
|
107 |
if( key == null ) { |
108 |
continue;
|
109 |
} |
110 |
Object value;
|
111 |
try {
|
112 |
value = entry.getValue(); |
113 |
} catch(Throwable th) { |
114 |
if( logger.isDebugEnabled() ) {
|
115 |
logger.warn("Can't get property "+key.toString()+".",th); |
116 |
} else {
|
117 |
logger.warn("Can't get property "+key.toString()+"."); |
118 |
} |
119 |
value = null;
|
120 |
} |
121 |
this.properties.put(key, value);
|
122 |
} |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void saveToState(PersistentState state) throws PersistenceException { |
127 |
state.set("properties", properties);
|
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void loadFromState(PersistentState state) throws PersistenceException { |
132 |
this.properties = new HashMap(); |
133 |
Map props = (Map) state.get("properties"); |
134 |
if( props != null) { |
135 |
copyFrom(props); |
136 |
} |
137 |
} |
138 |
|
139 |
public static class RegisterPersistence implements Callable { |
140 |
|
141 |
@Override
|
142 |
public Object call() { |
143 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
144 |
DynStruct definition = manager.addDefinition( |
145 |
ExtendedPropertiesHelper.class, |
146 |
"ExtendedPropertiesHelper",
|
147 |
"ExtendedPropertiesHelper Persistence definition",
|
148 |
null,
|
149 |
null
|
150 |
); |
151 |
definition.addDynFieldMap("properties").setClassOfItems(Object.class) |
152 |
.setMandatory(true);
|
153 |
|
154 |
return Boolean.TRUE; |
155 |
} |
156 |
} |
157 |
|
158 |
} |