gvsig-raster / org.gvsig.raster.tilecache / trunk / org.gvsig.raster.tilecache / org.gvsig.raster.tilecache.app / src / main / java / org / gvsig / raster / tilecache / app / Configuration.java @ 2477
History | View | Annotate | Download (10.4 KB)
1 | 2477 | nbrodin | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
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 2
|
||
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 | */
|
||
22 | package org.gvsig.raster.tilecache.app; |
||
23 | |||
24 | import java.util.ArrayList; |
||
25 | import java.util.HashMap; |
||
26 | import java.util.Iterator; |
||
27 | |||
28 | import org.gvsig.andami.PluginServices; |
||
29 | import org.gvsig.raster.fmap.layers.IConfiguration; |
||
30 | import org.gvsig.utils.XMLEntity; |
||
31 | |||
32 | /**
|
||
33 | * La clase <code>Configuration</code> sirve para poder leer y escribir valores en el entorno
|
||
34 | * de raster a nivel de configuraci?n. Para leer o escribir hay que usar los
|
||
35 | * metodos getValue y setValue, estos metodos lanzan eventos en el caso de
|
||
36 | * cambiar el valor que habia establecido. Forma de uso:<p>
|
||
37 | *
|
||
38 | * En la lectura es recomendable pasar un valor por defecto en el get, para que
|
||
39 | * si no existe o si existe pero no corresponde el tipo de datos devolvera el
|
||
40 | * valor por defecto<p>
|
||
41 | *
|
||
42 | * <code>Boolean valor = Configuration.getValue("valorBooleano", Boolean.valueOf(true));</code><p>
|
||
43 | *
|
||
44 | * <code>Configuration.setValue("valorBooleano", Boolean.valueOf(false));</code><p>
|
||
45 | *
|
||
46 | * Solo se pueden usar los siguientes tipos de datos:<br>
|
||
47 | * - <b>Boolean</b>, <b>Double</b>, <b>Float</b>, <b>Integer</b>, <b>Long</b>
|
||
48 | * y <b>String</b>.<p>
|
||
49 | *
|
||
50 | * Otra funcionalidad que tiene, es que puedes agregar un manejador de eventos
|
||
51 | * para controlar los cambios de las variables y actuar en consecuencia si cambia
|
||
52 | * la que deseas.
|
||
53 | *
|
||
54 | * @version 07/12/2007
|
||
55 | * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
||
56 | */
|
||
57 | public class Configuration implements IConfiguration { |
||
58 | static private Configuration singleton = new Configuration(); |
||
59 | private ArrayList<ConfigurationListener> |
||
60 | actionCommandListeners = new ArrayList<ConfigurationListener>(); |
||
61 | private XMLEntity xml = null; |
||
62 | private HashMap<String, Object> |
||
63 | hashMap = new HashMap<String, Object>(); |
||
64 | |||
65 | /**
|
||
66 | * Constructor privado. Nos aseguramos de que nadie pueda crear una instancia
|
||
67 | * desde fuera, la configuraci?n es ?nica para todos.
|
||
68 | */
|
||
69 | private Configuration() { |
||
70 | try {
|
||
71 | PluginServices ps = PluginServices.getPluginServices("org.gvsig.raster.tilecache.app");
|
||
72 | xml = ps.getPersistentXML(); |
||
73 | } catch (NullPointerException e) { |
||
74 | //No est? inicializado Configuration
|
||
75 | xml = new XMLEntity();
|
||
76 | } |
||
77 | } |
||
78 | |||
79 | /**
|
||
80 | * Devuelve un valor Boolean para el key especificado
|
||
81 | * @param key
|
||
82 | * @param defaultValue
|
||
83 | * @return
|
||
84 | */
|
||
85 | static public Boolean getValue(String key, Boolean defaultValue) { |
||
86 | singleton.saveDefaultValue(key, defaultValue); |
||
87 | try {
|
||
88 | return Boolean.valueOf(getXMLEntity().getStringProperty(key)); |
||
89 | } catch (Exception e) { |
||
90 | } |
||
91 | try {
|
||
92 | getXMLEntity().putProperty(key, defaultValue.booleanValue()); |
||
93 | } catch(NullPointerException e) { |
||
94 | //No est? inicializada la configuraci?n. Devuelve el default
|
||
95 | } |
||
96 | return defaultValue;
|
||
97 | } |
||
98 | |||
99 | /**
|
||
100 | * Devuelve un valor Double para el key especificado
|
||
101 | * @param key
|
||
102 | * @param defaultValue
|
||
103 | * @return
|
||
104 | */
|
||
105 | static public Double getValue(String key, Double defaultValue) { |
||
106 | singleton.saveDefaultValue(key, defaultValue); |
||
107 | try {
|
||
108 | return Double.valueOf(getXMLEntity().getStringProperty(key)); |
||
109 | } catch (Exception e) { |
||
110 | } |
||
111 | getXMLEntity().putProperty(key, defaultValue.doubleValue()); |
||
112 | return defaultValue;
|
||
113 | } |
||
114 | |||
115 | /**
|
||
116 | * Devuelve un valor Float para el key especificado
|
||
117 | * @param key
|
||
118 | * @param defaultValue
|
||
119 | * @return
|
||
120 | */
|
||
121 | static public Float getValue(String key, Float defaultValue) { |
||
122 | singleton.saveDefaultValue(key, defaultValue); |
||
123 | try {
|
||
124 | return Float.valueOf(getXMLEntity().getStringProperty(key)); |
||
125 | } catch (Exception e) { |
||
126 | } |
||
127 | getXMLEntity().putProperty(key, defaultValue.floatValue()); |
||
128 | return defaultValue;
|
||
129 | } |
||
130 | |||
131 | /**
|
||
132 | * Devuelve un valor Integer para el key especificado
|
||
133 | * @param key
|
||
134 | * @param defaultValue
|
||
135 | * @return
|
||
136 | */
|
||
137 | static public Integer getValue(String key, Integer defaultValue) { |
||
138 | singleton.saveDefaultValue(key, defaultValue); |
||
139 | try {
|
||
140 | return Integer.valueOf(getXMLEntity().getStringProperty(key)); |
||
141 | } catch (Exception e) { |
||
142 | } |
||
143 | getXMLEntity().putProperty(key, defaultValue.intValue()); |
||
144 | return defaultValue;
|
||
145 | } |
||
146 | |||
147 | /**
|
||
148 | * Devuelve un valor Long para el key especificado
|
||
149 | * @param key
|
||
150 | * @param defaultValue
|
||
151 | * @return
|
||
152 | */
|
||
153 | static public Long getValue(String key, Long defaultValue) { |
||
154 | singleton.saveDefaultValue(key, defaultValue); |
||
155 | try {
|
||
156 | return Long.valueOf(getXMLEntity().getStringProperty(key)); |
||
157 | } catch (Exception e) { |
||
158 | } |
||
159 | getXMLEntity().putProperty(key, defaultValue.longValue()); |
||
160 | return defaultValue;
|
||
161 | } |
||
162 | |||
163 | /**
|
||
164 | * Devuelve un valor String para el key especificado
|
||
165 | * @param key
|
||
166 | * @param defaultValue
|
||
167 | * @return
|
||
168 | */
|
||
169 | static public String getValue(String key, String defaultValue) { |
||
170 | singleton.saveDefaultValue(key, defaultValue); |
||
171 | try {
|
||
172 | return getXMLEntity().getStringProperty(key);
|
||
173 | } catch (Exception e) { |
||
174 | } |
||
175 | getXMLEntity().putProperty(key, defaultValue); |
||
176 | return defaultValue;
|
||
177 | } |
||
178 | |||
179 | /**
|
||
180 | * Guarda el valor por defecto en caso de que no exista
|
||
181 | * @param key
|
||
182 | * @param defaultValue
|
||
183 | */
|
||
184 | private void saveDefaultValue(String key, Object defaultValue) { |
||
185 | if (hashMap.get(key) == null) |
||
186 | hashMap.put(key, defaultValue); |
||
187 | } |
||
188 | |||
189 | /**
|
||
190 | * Devuelve el valor por defecto de un key
|
||
191 | * @param key
|
||
192 | * @return
|
||
193 | */
|
||
194 | static public Object getDefaultValue(String key) { |
||
195 | return singleton.hashMap.get(key);
|
||
196 | } |
||
197 | |||
198 | /**
|
||
199 | * Guarda en la configuracion el Objeto pasado por parametro asociado a dicho
|
||
200 | * key
|
||
201 | * @param key
|
||
202 | * @param value
|
||
203 | */
|
||
204 | private void putProperty(String key, Object value) { |
||
205 | if (Integer.class.isInstance(value)) { |
||
206 | getXMLEntity().putProperty(key, ((Integer) value).intValue());
|
||
207 | return;
|
||
208 | } |
||
209 | if (Double.class.isInstance(value)) { |
||
210 | getXMLEntity().putProperty(key, ((Double) value).doubleValue());
|
||
211 | return;
|
||
212 | } |
||
213 | if (Float.class.isInstance(value)) { |
||
214 | getXMLEntity().putProperty(key, ((Float) value).floatValue());
|
||
215 | return;
|
||
216 | } |
||
217 | if (Boolean.class.isInstance(value)) { |
||
218 | getXMLEntity().putProperty(key, ((Boolean) value).booleanValue());
|
||
219 | return;
|
||
220 | } |
||
221 | if (Long.class.isInstance(value)) { |
||
222 | getXMLEntity().putProperty(key, ((Long) value).longValue());
|
||
223 | return;
|
||
224 | } |
||
225 | if (String.class.isInstance(value)) { |
||
226 | getXMLEntity().putProperty(key, (String) value);
|
||
227 | return;
|
||
228 | } |
||
229 | getXMLEntity().putProperty(key, value); |
||
230 | } |
||
231 | |||
232 | /**
|
||
233 | * Establece un valor en la configuracion
|
||
234 | * @param uri
|
||
235 | * @param value
|
||
236 | */
|
||
237 | static public void setValue(String key, Object value) { |
||
238 | if (value == null) { |
||
239 | getXMLEntity().remove(key); |
||
240 | singleton.callConfigurationChanged(key, value); |
||
241 | return;
|
||
242 | } |
||
243 | |||
244 | String oldValue = getValue(key, value.toString());
|
||
245 | |||
246 | singleton.putProperty(key, value); |
||
247 | |||
248 | if (!oldValue.equals(value.toString()))
|
||
249 | singleton.callConfigurationChanged(key, value); |
||
250 | } |
||
251 | |||
252 | /**
|
||
253 | * A?adir un listener a la lista de eventos
|
||
254 | * @param listener
|
||
255 | */
|
||
256 | static public void addValueChangedListener(ConfigurationListener listener) { |
||
257 | if (!singleton.actionCommandListeners.contains(listener))
|
||
258 | singleton.actionCommandListeners.add(listener); |
||
259 | } |
||
260 | |||
261 | /**
|
||
262 | * Borrar un listener de la lista de eventos
|
||
263 | * @param listener
|
||
264 | */
|
||
265 | static public void removeValueChangedListener(ConfigurationListener listener) { |
||
266 | singleton.actionCommandListeners.remove(listener); |
||
267 | } |
||
268 | |||
269 | /**
|
||
270 | * Invocar a los eventos asociados al componente
|
||
271 | */
|
||
272 | private void callConfigurationChanged(String key, Object value) { |
||
273 | Iterator<ConfigurationListener> iterator = actionCommandListeners.iterator();
|
||
274 | while (iterator.hasNext()) {
|
||
275 | ConfigurationListener listener = iterator.next(); |
||
276 | listener.actionConfigurationChanged(new ConfigurationEvent(this, key, value)); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | /**
|
||
281 | * Devuelve una instancia unica al XMLEntity de Configuration
|
||
282 | * @return
|
||
283 | */
|
||
284 | static private XMLEntity getXMLEntity() { |
||
285 | return singleton.xml;
|
||
286 | } |
||
287 | |||
288 | /**
|
||
289 | * Devuelve una instancia al unico objeto de configuraci?n que puede existir.
|
||
290 | * @return
|
||
291 | */
|
||
292 | static public Configuration getSingleton() { |
||
293 | return singleton;
|
||
294 | } |
||
295 | |||
296 | /*
|
||
297 | * (non-Javadoc)
|
||
298 | * @see org.gvsig.fmap.raster.conf.IConfiguration#getValueBoolean(java.lang.String, java.lang.Boolean)
|
||
299 | */
|
||
300 | public Boolean getValueBoolean(String name, Boolean defaultValue) { |
||
301 | return Configuration.getValue(name, defaultValue); |
||
302 | } |
||
303 | |||
304 | /*
|
||
305 | * (non-Javadoc)
|
||
306 | * @see org.gvsig.fmap.raster.conf.IConfiguration#getValueString(java.lang.String, java.lang.String)
|
||
307 | */
|
||
308 | public String getValueString(String name, String defaultValue) { |
||
309 | return Configuration.getValue(name, defaultValue); |
||
310 | } |
||
311 | |||
312 | /*
|
||
313 | * (non-Javadoc)
|
||
314 | * @see org.gvsig.fmap.raster.conf.IConfiguration#getValueDouble(java.lang.String, java.lang.Double)
|
||
315 | */
|
||
316 | public Double getValueDouble(String name, Double defaultValue) { |
||
317 | return Configuration.getValue(name, defaultValue); |
||
318 | } |
||
319 | |||
320 | /*
|
||
321 | * (non-Javadoc)
|
||
322 | * @see org.gvsig.fmap.raster.conf.IConfiguration#getValueFloat(java.lang.String, java.lang.Float)
|
||
323 | */
|
||
324 | public Float getValueFloat(String name, Float defaultValue) { |
||
325 | return Configuration.getValue(name, defaultValue); |
||
326 | } |
||
327 | |||
328 | /*
|
||
329 | * (non-Javadoc)
|
||
330 | * @see org.gvsig.fmap.raster.conf.IConfiguration#getValueInteger(java.lang.String, java.lang.Integer)
|
||
331 | */
|
||
332 | public Integer getValueInteger(String name, Integer defaultValue) { |
||
333 | return Configuration.getValue(name, defaultValue); |
||
334 | } |
||
335 | |||
336 | /*
|
||
337 | * (non-Javadoc)
|
||
338 | * @see org.gvsig.fmap.raster.conf.IConfiguration#getValueLong(java.lang.String, java.lang.Long)
|
||
339 | */
|
||
340 | public Long getValueLong(String name, Long defaultValue) { |
||
341 | return Configuration.getValue(name, defaultValue); |
||
342 | } |
||
343 | } |