Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / util / Configuration.java @ 17491

History | View | Annotate | Download (8.16 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
                if (singleton.hashMap.get(key) == null)
76
                        singleton.hashMap.put(key, defaultValue);
77
                try {
78
                        return Boolean.valueOf(getXMLEntity().getStringProperty(key));
79
                } catch (Exception e) {
80
                }
81
                getXMLEntity().putProperty(key, defaultValue.booleanValue());
82
                return defaultValue;
83
        }
84

    
85
        /**
86
         * Devuelve un valor Double para el key especificado
87
         * @param key
88
         * @param defaultValue
89
         * @return
90
         */
91
        static public Double getValue(String key, Double defaultValue) {
92
                if (singleton.hashMap.get(key) == null)
93
                        singleton.hashMap.put(key, defaultValue);
94
                try {
95
                        return Double.valueOf(getXMLEntity().getStringProperty(key));
96
                } catch (Exception e) {
97
                }
98
                getXMLEntity().putProperty(key, defaultValue.doubleValue());
99
                return defaultValue;
100
        }
101

    
102
        /**
103
         * Devuelve un valor Float para el key especificado
104
         * @param key
105
         * @param defaultValue
106
         * @return
107
         */
108
        static public Float getValue(String key, Float defaultValue) {
109
                if (singleton.hashMap.get(key) == null)
110
                        singleton.hashMap.put(key, defaultValue);
111
                try {
112
                        return Float.valueOf(getXMLEntity().getStringProperty(key));
113
                } catch (Exception e) {
114
                }
115
                getXMLEntity().putProperty(key, defaultValue.floatValue());
116
                return defaultValue;
117
        }
118

    
119
        /**
120
         * Devuelve un valor Integer para el key especificado
121
         * @param key
122
         * @param defaultValue
123
         * @return
124
         */
125
        static public Integer getValue(String key, Integer defaultValue) {
126
                if (singleton.hashMap.get(key) == null)
127
                        singleton.hashMap.put(key, defaultValue);
128
                try {
129
                        return Integer.valueOf(getXMLEntity().getStringProperty(key));
130
                } catch (Exception e) {
131
                }
132
                getXMLEntity().putProperty(key, defaultValue.intValue());
133
                return defaultValue;
134
        }
135

    
136
        /**
137
         * Devuelve un valor Long para el key especificado
138
         * @param key
139
         * @param defaultValue
140
         * @return
141
         */
142
        static public Long getValue(String key, Long defaultValue) {
143
                if (singleton.hashMap.get(key) == null)
144
                        singleton.hashMap.put(key, defaultValue);
145
                try {
146
                        return Long.valueOf(getXMLEntity().getStringProperty(key));
147
                } catch (Exception e) {
148
                }
149
                getXMLEntity().putProperty(key, defaultValue.longValue());
150
                return defaultValue;
151
        }
152

    
153
        /**
154
         * Devuelve un valor String para el key especificado
155
         * @param key
156
         * @param defaultValue
157
         * @return
158
         */
159
        static public String getValue(String key, String defaultValue) {
160
                if (singleton.hashMap.get(key) == null)
161
                        singleton.hashMap.put(key, defaultValue);
162
                try {
163
                        return getXMLEntity().getStringProperty(key);
164
                } catch (Exception e) {
165
                }
166
                getXMLEntity().putProperty(key, defaultValue);
167
                return defaultValue;
168
        }
169

    
170
        /**
171
         * Devuelve el valor por defecto de un key
172
         * @param key
173
         * @return
174
         */
175
        static public Object getDefaultValue(String key) {
176
                return singleton.hashMap.get(key);
177
        }
178

    
179
        /**
180
         * Guarda en la configuracion el Objeto pasado por parametro asociado a dicho
181
         * key
182
         * @param key
183
         * @param value
184
         */
185
        private void putProperty(String key, Object value) {
186
                if (Integer.class.isInstance(value)) {
187
                        getXMLEntity().putProperty(key, ((Integer) value).intValue());
188
                        return;
189
                }
190
                if (Double.class.isInstance(value)) {
191
                        getXMLEntity().putProperty(key, ((Double) value).doubleValue());
192
                        return;
193
                }
194
                if (Float.class.isInstance(value)) {
195
                        getXMLEntity().putProperty(key, ((Float) value).floatValue());
196
                        return;
197
                }
198
                if (Boolean.class.isInstance(value)) {
199
                        getXMLEntity().putProperty(key, ((Boolean) value).booleanValue());
200
                        return;
201
                }
202
                if (Long.class.isInstance(value)) {
203
                        getXMLEntity().putProperty(key, ((Long) value).longValue());
204
                        return;
205
                }
206
                if (String.class.isInstance(value)) {
207
                        getXMLEntity().putProperty(key, (String) value);
208
                        return;
209
                }
210
                getXMLEntity().putProperty(key, value);
211
        }
212

    
213
        /**
214
         * Establece un valor en la configuracion
215
         * @param name
216
         * @param value
217
         */
218
        static public void setValue(String key, Object value) {
219
                if (value == null) {
220
                        getXMLEntity().remove(key);
221
                        singleton.callConfigurationChanged(key, value);
222
                        return;
223
                }
224

    
225
                String oldValue = getValue(key, value.toString());
226

    
227
                singleton.putProperty(key, value);
228

    
229
                if (!oldValue.equals(value.toString()))
230
                        singleton.callConfigurationChanged(key, value);
231
        }
232

    
233
        /**
234
         * A?adir un listener a la lista de eventos
235
         * @param listener
236
         */
237
        static public void addValueChangedListener(ConfigurationListener listener) {
238
                if (!singleton.actionCommandListeners.contains(listener))
239
                        singleton.actionCommandListeners.add(listener);
240
        }
241

    
242
        /**
243
         * Borrar un listener de la lista de eventos
244
         * @param listener
245
         */
246
        static public void removeValueChangedListener(ConfigurationListener listener) {
247
                singleton.actionCommandListeners.remove(listener);
248
        }
249

    
250
        /**
251
         * Invocar a los eventos asociados al componente
252
         */
253
        private void callConfigurationChanged(String key, Object value) {
254
                Iterator iterator = actionCommandListeners.iterator();
255
                while (iterator.hasNext()) {
256
                        ConfigurationListener listener = (ConfigurationListener) iterator.next();
257
                        listener.actionConfigurationChanged(new ConfigurationEvent(this, key, value));
258
                }
259
        }
260

    
261
        /**
262
         * Devuelve una instancia unica al XMLEntity de Configuration
263
         * @return
264
         */
265
        static private XMLEntity getXMLEntity() {
266
                return singleton.xml;
267
        }
268
}