svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / util / Configuration.java @ 17665
History | View | Annotate | Download (8.18 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.fmap.raster.util; |
20 |
|
21 |
import java.util.ArrayList; |
22 |
import java.util.HashMap; |
23 |
import java.util.Iterator; |
24 |
|
25 |
|
26 |
import com.iver.andami.PluginServices; |
27 |
import com.iver.utiles.XMLEntity; |
28 |
/**
|
29 |
* La clase <code>Configuration</code> sirve para poder leer y escribir valores en el entorno
|
30 |
* de raster a nivel de configuraci?n. Para leer o escribir hay que usar los
|
31 |
* metodos getValue y setValue, estos metodos lanzan eventos en el caso de
|
32 |
* cambiar el valor que habia establecido. Forma de uso:<p>
|
33 |
*
|
34 |
* En la lectura es recomendable pasar un valor por defecto en el get, para que
|
35 |
* si no existe o si existe pero no corresponde el tipo de datos devolvera el
|
36 |
* valor por defecto<p>
|
37 |
*
|
38 |
* <code>Boolean valor = Configuration.getValue("valorBooleano", Boolean.valueOf(true));</code><p>
|
39 |
*
|
40 |
* <code>Configuration.setValue("valorBooleano", Boolean.valueOf(false));</code><p>
|
41 |
*
|
42 |
* Solo se pueden usar los siguientes tipos de datos:<br>
|
43 |
* - <b>Boolean</b>, <b>Double</b>, <b>Float</b>, <b>Integer</b>, <b>Long</b>
|
44 |
* y <b>String</b>.<p>
|
45 |
*
|
46 |
* Otra funcionalidad que tiene, es que puedes agregar un manejador de eventos
|
47 |
* para controlar los cambios de las variables y actuar en consecuencia si cambia
|
48 |
* la que deseas.
|
49 |
*
|
50 |
* @version 07/12/2007
|
51 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
52 |
*/
|
53 |
public class Configuration { |
54 |
static private Configuration singleton = new Configuration(); |
55 |
private ArrayList actionCommandListeners = new ArrayList(); |
56 |
private XMLEntity xml = null; |
57 |
private HashMap hashMap = new HashMap(); |
58 |
|
59 |
/**
|
60 |
* Constructor privado. Nos aseguramos de que nadie pueda crear una instancia
|
61 |
* desde fuera, la configuraci?n es ?nica para todos.
|
62 |
*/
|
63 |
private Configuration() { |
64 |
PluginServices ps = PluginServices.getPluginServices("org.gvsig.rastertools");
|
65 |
xml = ps.getPersistentXML(); |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Devuelve un valor Boolean para el key especificado
|
70 |
* @param key
|
71 |
* @param defaultValue
|
72 |
* @return
|
73 |
*/
|
74 |
static public Boolean getValue(String key, Boolean defaultValue) { |
75 |
singleton.saveDefaultValue(key, defaultValue); |
76 |
try {
|
77 |
return Boolean.valueOf(getXMLEntity().getStringProperty(key)); |
78 |
} catch (Exception e) { |
79 |
} |
80 |
getXMLEntity().putProperty(key, defaultValue.booleanValue()); |
81 |
return defaultValue;
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* Devuelve un valor Double para el key especificado
|
86 |
* @param key
|
87 |
* @param defaultValue
|
88 |
* @return
|
89 |
*/
|
90 |
static public Double getValue(String key, Double defaultValue) { |
91 |
singleton.saveDefaultValue(key, defaultValue); |
92 |
try {
|
93 |
return Double.valueOf(getXMLEntity().getStringProperty(key)); |
94 |
} catch (Exception e) { |
95 |
} |
96 |
getXMLEntity().putProperty(key, defaultValue.doubleValue()); |
97 |
return defaultValue;
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* Devuelve un valor Float para el key especificado
|
102 |
* @param key
|
103 |
* @param defaultValue
|
104 |
* @return
|
105 |
*/
|
106 |
static public Float getValue(String key, Float defaultValue) { |
107 |
singleton.saveDefaultValue(key, defaultValue); |
108 |
try {
|
109 |
return Float.valueOf(getXMLEntity().getStringProperty(key)); |
110 |
} catch (Exception e) { |
111 |
} |
112 |
getXMLEntity().putProperty(key, defaultValue.floatValue()); |
113 |
return defaultValue;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Devuelve un valor Integer para el key especificado
|
118 |
* @param key
|
119 |
* @param defaultValue
|
120 |
* @return
|
121 |
*/
|
122 |
static public Integer getValue(String key, Integer defaultValue) { |
123 |
singleton.saveDefaultValue(key, defaultValue); |
124 |
try {
|
125 |
return Integer.valueOf(getXMLEntity().getStringProperty(key)); |
126 |
} catch (Exception e) { |
127 |
} |
128 |
getXMLEntity().putProperty(key, defaultValue.intValue()); |
129 |
return defaultValue;
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* Devuelve un valor Long para el key especificado
|
134 |
* @param key
|
135 |
* @param defaultValue
|
136 |
* @return
|
137 |
*/
|
138 |
static public Long getValue(String key, Long defaultValue) { |
139 |
singleton.saveDefaultValue(key, defaultValue); |
140 |
try {
|
141 |
return Long.valueOf(getXMLEntity().getStringProperty(key)); |
142 |
} catch (Exception e) { |
143 |
} |
144 |
getXMLEntity().putProperty(key, defaultValue.longValue()); |
145 |
return defaultValue;
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* Devuelve un valor String para el key especificado
|
150 |
* @param key
|
151 |
* @param defaultValue
|
152 |
* @return
|
153 |
*/
|
154 |
static public String getValue(String key, String defaultValue) { |
155 |
singleton.saveDefaultValue(key, defaultValue); |
156 |
try {
|
157 |
return getXMLEntity().getStringProperty(key);
|
158 |
} catch (Exception e) { |
159 |
} |
160 |
getXMLEntity().putProperty(key, defaultValue); |
161 |
return defaultValue;
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* Guarda el valor por defecto en caso de que no exista
|
166 |
* @param key
|
167 |
* @param defaultValue
|
168 |
*/
|
169 |
private void saveDefaultValue(String key, Object defaultValue) { |
170 |
if (hashMap.get(key) == null) |
171 |
hashMap.put(key, defaultValue); |
172 |
} |
173 |
|
174 |
/**
|
175 |
* Devuelve el valor por defecto de un key
|
176 |
* @param key
|
177 |
* @return
|
178 |
*/
|
179 |
static public Object getDefaultValue(String key) { |
180 |
return singleton.hashMap.get(key);
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Guarda en la configuracion el Objeto pasado por parametro asociado a dicho
|
185 |
* key
|
186 |
* @param key
|
187 |
* @param value
|
188 |
*/
|
189 |
private void putProperty(String key, Object value) { |
190 |
if (Integer.class.isInstance(value)) { |
191 |
getXMLEntity().putProperty(key, ((Integer) value).intValue());
|
192 |
return;
|
193 |
} |
194 |
if (Double.class.isInstance(value)) { |
195 |
getXMLEntity().putProperty(key, ((Double) value).doubleValue());
|
196 |
return;
|
197 |
} |
198 |
if (Float.class.isInstance(value)) { |
199 |
getXMLEntity().putProperty(key, ((Float) value).floatValue());
|
200 |
return;
|
201 |
} |
202 |
if (Boolean.class.isInstance(value)) { |
203 |
getXMLEntity().putProperty(key, ((Boolean) value).booleanValue());
|
204 |
return;
|
205 |
} |
206 |
if (Long.class.isInstance(value)) { |
207 |
getXMLEntity().putProperty(key, ((Long) value).longValue());
|
208 |
return;
|
209 |
} |
210 |
if (String.class.isInstance(value)) { |
211 |
getXMLEntity().putProperty(key, (String) value);
|
212 |
return;
|
213 |
} |
214 |
getXMLEntity().putProperty(key, value); |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Establece un valor en la configuracion
|
219 |
* @param name
|
220 |
* @param value
|
221 |
*/
|
222 |
static public void setValue(String key, Object value) { |
223 |
if (value == null) { |
224 |
getXMLEntity().remove(key); |
225 |
singleton.callConfigurationChanged(key, value); |
226 |
return;
|
227 |
} |
228 |
|
229 |
String oldValue = getValue(key, value.toString());
|
230 |
|
231 |
singleton.putProperty(key, value); |
232 |
|
233 |
if (!oldValue.equals(value.toString()))
|
234 |
singleton.callConfigurationChanged(key, value); |
235 |
} |
236 |
|
237 |
/**
|
238 |
* A?adir un listener a la lista de eventos
|
239 |
* @param listener
|
240 |
*/
|
241 |
static public void addValueChangedListener(ConfigurationListener listener) { |
242 |
if (!singleton.actionCommandListeners.contains(listener))
|
243 |
singleton.actionCommandListeners.add(listener); |
244 |
} |
245 |
|
246 |
/**
|
247 |
* Borrar un listener de la lista de eventos
|
248 |
* @param listener
|
249 |
*/
|
250 |
static public void removeValueChangedListener(ConfigurationListener listener) { |
251 |
singleton.actionCommandListeners.remove(listener); |
252 |
} |
253 |
|
254 |
/**
|
255 |
* Invocar a los eventos asociados al componente
|
256 |
*/
|
257 |
private void callConfigurationChanged(String key, Object value) { |
258 |
Iterator iterator = actionCommandListeners.iterator();
|
259 |
while (iterator.hasNext()) {
|
260 |
ConfigurationListener listener = (ConfigurationListener) iterator.next(); |
261 |
listener.actionConfigurationChanged(new ConfigurationEvent(this, key, value)); |
262 |
} |
263 |
} |
264 |
|
265 |
/**
|
266 |
* Devuelve una instancia unica al XMLEntity de Configuration
|
267 |
* @return
|
268 |
*/
|
269 |
static private XMLEntity getXMLEntity() { |
270 |
return singleton.xml;
|
271 |
} |
272 |
} |