Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / propertiespanel / PropertiesComponent.java @ 12121

History | View | Annotate | Download (12.8 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2007 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.gui.beans.propertiespanel;
20

    
21
import java.awt.Component;
22
import java.awt.Dimension;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.awt.Insets;
26
import java.awt.event.FocusEvent;
27
import java.awt.event.FocusListener;
28
import java.awt.event.ItemEvent;
29
import java.awt.event.ItemListener;
30
import java.awt.event.KeyEvent;
31
import java.awt.event.KeyListener;
32
import java.util.ArrayList;
33
import java.util.Enumeration;
34
import java.util.EventObject;
35
import java.util.Iterator;
36
import java.util.Properties;
37

    
38
import javax.swing.JCheckBox;
39
import javax.swing.JComboBox;
40
import javax.swing.JLabel;
41
import javax.swing.JPanel;
42
import javax.swing.JScrollPane;
43
import javax.swing.JSpinner;
44
import javax.swing.JTextField;
45
import javax.swing.event.ChangeEvent;
46
import javax.swing.event.ChangeListener;
47

    
48
import org.gvsig.gui.beans.slidertext.SliderTextContainer;
49
/**
50
 * Componente para crear un cuadro de propiedades de configuracion standard.
51
 * 
52
 * @version 19/04/2007
53
 * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
54
 */
55
public class PropertiesComponent extends JScrollPane implements FocusListener, KeyListener, ChangeListener, ItemListener, PropertiesComponentListener {
56
        private static final long serialVersionUID = 372118344763661890L;
57
        private ArrayList datalist = new ArrayList();
58
        private ArrayList actionCommandListeners = new ArrayList();
59

    
60
        private JPanel          jPanelContent = null;
61

    
62
        static final public int TYPE_DEFAULT  = 1;
63
        static final public int TYPE_SLIDER   = 2;
64
        static final public int TYPE_COMBO    = 3;
65

    
66
        /**
67
         * Constructor de la calse
68
         */
69
        public PropertiesComponent() {
70
                initialize();
71
        }
72

    
73
        /**
74
         * Constructor para poder pasarle un ArrayList de PropertyStruct
75
         * @param values
76
         */
77
        public PropertiesComponent(ArrayList values) {
78
                initialize();
79
                for (int i=0; i<values.size(); i++) {
80
                        addPropertyStruct((PropertyStruct) values.get(i));
81
                }
82
        }
83

    
84
        /**
85
         * Constructor para poder pasarle un Properties
86
         * @param values
87
         */
88
        public PropertiesComponent(Properties properties) {
89
                initialize();
90
                Enumeration elements = properties.keys();
91
                while (elements.hasMoreElements()) {
92
                        String key = (String) elements.nextElement();
93
                        addValue(key, key, properties.get(key), null);
94
                }
95
        }
96

    
97
        /**
98
         * Creaci?n de la ventana con sus componentes
99
         */
100
        private void initialize() {
101
                this.setBorder(null);
102
                jPanelContent = new JPanel();
103
                jPanelContent.setLayout(new GridBagLayout());
104
                this.setViewportView(jPanelContent);
105
        }
106

    
107
        int y = 0;
108
        /**
109
         * A?ade un PropertyStruct al componente 
110
         * @param property
111
         */
112
        public void addPropertyStruct(PropertyStruct property) {
113
                boolean without_label = false;
114

    
115
                JLabel label = new JLabel(property.getTextLabel() + ": ");
116

    
117
                Component component = new JLabel("Sin soporte para: " + property.getOldValue().getClass().toString());
118

    
119
                // Tratamiento de Strings, como un JTextField
120
                if (property.getOldValue() instanceof String) {
121
                        component = new JTextField(property.getOldValue().toString());
122
                        ((JTextField) component).setMaximumSize(new Dimension(200, 25));
123
                        ((JTextField) component).addFocusListener(this);
124
                        ((JTextField) component).addKeyListener(this);
125
                }
126

    
127
                if (property.getOldValue() instanceof JPanelProperty) {
128
                        component = (JPanelProperty) property.getOldValue();
129
                        ((JPanelProperty) component).addStateChangedListener(this);
130
                        without_label = true;
131
                }
132

    
133
                // Tratamiento de Integer
134
                if (property.getOldValue() instanceof Integer) {
135
                        boolean created = false;
136
                        if (property.getExtras() != null) {
137
                                switch (((Integer) property.getExtras()[0]).intValue()) {
138
                                        case TYPE_SLIDER:
139
                                                without_label = true;
140
                                                component = new SliderTextContainer();
141
                                                ((SliderTextContainer) component).setBorder(javax.swing.BorderFactory.createTitledBorder(property.getTextLabel()));
142
//                                                ((SliderTextContainer) component).setBorder(javax.swing.BorderFactory.createEmptyBorder());
143

    
144
                                                if (property.getExtras().length >= 2)
145
                                                        ((SliderTextContainer) component).setMinimum(((Integer) property.getExtras()[1]).intValue());
146
                                                if (property.getExtras().length >= 3)
147
                                                        ((SliderTextContainer) component).setMaximum(((Integer) property.getExtras()[2]).intValue());
148
                                                ((SliderTextContainer) component).setValue(((Integer) property.getOldValue()).intValue());
149

    
150
                                                ((SliderTextContainer) component).addChangeListener(this);
151

    
152
                                                created = true;
153
                                                break;
154
                                        case TYPE_COMBO:
155
                                                component = new JComboBox();
156
                                                ArrayList aux = (ArrayList) property.getExtras()[1];
157
                                                for (int i=0; i<aux.size(); i++)
158
                                                        ((JComboBox) component).addItem(aux.get(i).toString());
159
                                                ((JComboBox) component).setSelectedIndex(((Integer) property.getOldValue()).intValue());
160
                                                ((JComboBox) component).addItemListener(this);
161
                                                created = true;
162
                                                break;
163
                                }
164
                        }
165
                        if (!created) {
166
                                component = new JSpinner();
167
                                ((JSpinner) component).setValue(property.getOldValue());
168
                                ((JSpinner) component).addChangeListener(this);
169
                        }
170
                }
171

    
172
                // Tratamiento de Boolean
173
                if (property.getOldValue() instanceof Boolean) {
174
                        component = new JCheckBox();
175
                        ((JCheckBox) component).setSelected(((Boolean) property.getOldValue()).booleanValue());
176
                        ((JCheckBox) component).addItemListener(this);
177
                }
178

    
179
                if (without_label) {
180
                        jPanelContent.add(component, new GridBagConstraints(0, y, 2, 1, 0.0, 0.0,
181
                                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
182
                                        new Insets(0, 0, 5, 0), 0, 0));
183
                } else {
184
                        jPanelContent.add(label, new GridBagConstraints(0, y, 1, 1, 0.0, 0.0,
185
                                         GridBagConstraints.CENTER, GridBagConstraints.BOTH,
186
                                         new Insets(0, 0, 5, 5), 0, 0));
187

    
188
                        jPanelContent.add(component, new GridBagConstraints(1, y, 1, 1, 0.0, 0.0,
189
                                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
190
                                        new Insets(0, 0, 5, 0), 0, 0));
191
                }
192
                y++;
193

    
194
                label.setLabelFor(component);
195
                label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
196

    
197
                property.setJLabel(label);
198
                property.setComponent(component);
199
                datalist.add(property);
200
        }
201

    
202
        /**
203
         * A?ade una clave/valor al panel de propiedades.<br>
204
         * <br>
205
         * El componente seleccionado dependera del instanceof del valor y las
206
         * opciones extras que se pongan. Por ejemplo: para el instanceof de un String
207
         * siempre se usara un JTextField, en cambio, para un Integer, se podran usar
208
         * 3 tipos, el JSlider, JComboBox y JSpinner. Estos tipos se especifican en el
209
         * array extras, poniendolo siempre en la posicion 0. En la posici?n 1 y 2 de
210
         * un JSlider se puede especificar el m?nimo y el m?ximo del Slider.
211
         * 
212
         * @param textLabel
213
         * @param key
214
         * @param value
215
         * @param extras
216
         */
217
        public void addValue(String textLabel, String key, Object value, Object[] extras) {
218
                PropertyStruct propertyStruct = new PropertyStruct(textLabel, key, value, extras);
219
                addPropertyStruct(propertyStruct);
220
        }
221

    
222
        /**
223
         * A?ade una clave valor al panel de propiedades.
224
         * @param key
225
         * @param value
226
         */
227
        public void put(Object key, Object value) {
228
                PropertyStruct propertyStruct = new PropertyStruct((String) key, (String) key, value, null);
229
                addPropertyStruct(propertyStruct);
230
        }
231

    
232
        /**
233
         * Obtener todos los valores de la ventana, esto ser? un
234
         * <code><b>ArrayList</b></code> que contendr? elementos de tipo
235
         * <code><b>PropertyStruct</b></code>, pudiendo tener el valor antes de
236
         * ser modificado y el nuevo valor.
237
         * 
238
         * @see <code>PropertyStruct</code>
239
         * 
240
         * @return ArrayList de elementos de tipo <code>PropertyStruct</code>
241
         */
242
        public ArrayList getValues() {
243
                for (int i=0; i<datalist.size(); i++) {
244
                        PropertyStruct propertyStruct = ((PropertyStruct) datalist.get(i));
245

    
246
                        if (propertyStruct.getComponent() instanceof JTextField) {
247
                                propertyStruct.setNewValue(((JTextField) propertyStruct.getComponent()).getText());
248
                                continue;
249
                        }
250
                        if (propertyStruct.getComponent() instanceof JSpinner) {
251
                                propertyStruct.setNewValue(((JSpinner) propertyStruct.getComponent()).getValue());
252
                                continue;
253
                        }
254
                        if (propertyStruct.getComponent() instanceof SliderTextContainer) {
255
                                propertyStruct.setNewValue(new Integer((int) ((SliderTextContainer) propertyStruct.getComponent()).getValue()));
256
                                continue;
257
                        }
258
                        if (propertyStruct.getComponent() instanceof JCheckBox) {
259
                                propertyStruct.setNewValue(new Boolean(((JCheckBox) propertyStruct.getComponent()).getSelectedObjects()!=null));
260
                                continue;
261
                        }
262
                        if (propertyStruct.getComponent() instanceof JComboBox) {
263
                                propertyStruct.setNewValue(new Integer(((JComboBox) propertyStruct.getComponent()).getSelectedIndex()));
264
                                continue;
265
                        }
266
                        if (propertyStruct.getComponent() instanceof JPanelProperty) {
267
                                // No es necesario pq el mismo JPanel esta tb en el oldValue
268
                                continue;
269
                        }
270
                }
271
                return datalist;
272
        }
273

    
274
        /**
275
         * Obtener todos los valores de la ventana en formato java.util.Properties
276
         * @return
277
         */
278
        public Properties getProperties() {
279
                Properties properties = new Properties();
280
                for (int i=0; i<datalist.size(); i++) {
281
                        PropertyStruct propertyStruct = ((PropertyStruct) datalist.get(i));
282
                        String key = propertyStruct.getKey();
283

    
284
                        if (propertyStruct.getComponent() instanceof JTextField) {
285
                                properties.put(key, ((JTextField) propertyStruct.getComponent()).getText());
286
                                continue;
287
                        }
288
                        if (propertyStruct.getComponent() instanceof JSpinner) {
289
                                properties.put(key, ((JSpinner) propertyStruct.getComponent()).getValue());
290
                                continue;
291
                        }
292
                        if (propertyStruct.getComponent() instanceof SliderTextContainer) {
293
                                properties.put(key, new Integer((int) ((SliderTextContainer) propertyStruct.getComponent()).getValue()));
294
                                continue;
295
                        }
296
                        if (propertyStruct.getComponent() instanceof JCheckBox) {
297
                                properties.put(key, new Boolean(((JCheckBox) propertyStruct.getComponent()).getSelectedObjects()!=null));
298
                                continue;
299
                        }
300
                        if (propertyStruct.getComponent() instanceof JComboBox) {
301
                                properties.put(key, new Integer(((JComboBox) propertyStruct.getComponent()).getSelectedIndex()));
302
                                continue;
303
                        }
304
                        if (propertyStruct.getComponent() instanceof JPanelProperty) {
305
                                properties.put(key, (JPanelProperty) propertyStruct.getComponent());
306
                                continue;
307
                        }
308
                }
309
                return properties;
310
        }
311

    
312
        /**
313
         * A?adir el disparador de cuando se pulsa un bot?n.
314
         * @param listener
315
         */
316
        public void addStateChangedListener(PropertiesComponentListener listener) {
317
                if (!actionCommandListeners.contains(listener))
318
                        actionCommandListeners.add(listener);
319
        }
320

    
321
        /**
322
         * Borrar el disparador de eventos de los botones.
323
         * @param listener
324
         */
325
        public void removeStateChangedListener(PropertiesComponentListener listener) {
326
                actionCommandListeners.remove(listener);
327
        }
328

    
329
        private void callStateChanged() {
330
                Iterator acIterator = actionCommandListeners.iterator();
331
                while (acIterator.hasNext()) {
332
                        PropertiesComponentListener listener = (PropertiesComponentListener) acIterator.next();
333
                        listener.actionChangeProperties(new EventObject(this));
334
                }
335
        }
336

    
337
        /*
338
         * (non-Javadoc)
339
         * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
340
         */
341
        public void stateChanged(ChangeEvent e) {
342
                callStateChanged();
343
        }
344

    
345
        /*
346
         * (non-Javadoc)
347
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
348
         */
349
        public void itemStateChanged(ItemEvent e) {
350
                callStateChanged();
351
        }
352

    
353
        /*
354
         * (non-Javadoc)
355
         * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
356
         */
357
        public void focusLost(FocusEvent e) {
358
                callStateChanged();
359
        }
360

    
361
        /*
362
         * (non-Javadoc)
363
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
364
         */
365
        public void keyReleased(KeyEvent e) {
366
                if (e.getKeyCode() == 10)
367
                        callStateChanged();
368
        }
369

    
370
        /*
371
         * (non-Javadoc)
372
         * @see org.gvsig.gui.beans.propertiespanel.PropertiesComponentListener#actionChangeProperties(java.util.EventObject)
373
         */
374
        public void actionChangeProperties(EventObject e) {
375
                callStateChanged();
376
        }
377

    
378
        /*
379
         * (non-Javadoc)
380
         * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
381
         */
382
        public void keyTyped(KeyEvent e) {
383
        }
384

    
385
        /*
386
         * (non-Javadoc)
387
         * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
388
         */
389
        public void focusGained(FocusEvent e) {
390
        }
391

    
392
        /*
393
         * (non-Javadoc)
394
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
395
         */
396
        public void keyPressed(KeyEvent e) {
397
        }
398
}