Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / propertiespanel / PropertiesComponent.java @ 40561

History | View | Annotate | Download (16.5 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
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
25
*
26
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
27
*
28
* This program is free software; you can redistribute it and/or
29
* modify it under the terms of the GNU General Public License
30
* as published by the Free Software Foundation; either version 2
31
* of the License, or (at your option) any later version.
32
*
33
* This program is distributed in the hope that it will be useful,
34
* but WITHOUT ANY WARRANTY; without even the implied warranty of
35
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
* GNU General Public License for more details.
37
*
38
* You should have received a copy of the GNU General Public License
39
* along with this program; if not, write to the Free Software
40
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
41
*/
42
package org.gvsig.gui.beans.propertiespanel;
43

    
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.GridBagConstraints;
47
import java.awt.GridBagLayout;
48
import java.awt.Insets;
49
import java.awt.event.FocusEvent;
50
import java.awt.event.FocusListener;
51
import java.awt.event.ItemEvent;
52
import java.awt.event.ItemListener;
53
import java.awt.event.KeyEvent;
54
import java.awt.event.KeyListener;
55
import java.util.ArrayList;
56
import java.util.Enumeration;
57
import java.util.EventObject;
58
import java.util.Iterator;
59
import java.util.Properties;
60

    
61
import javax.swing.JCheckBox;
62
import javax.swing.JComboBox;
63
import javax.swing.JLabel;
64
import javax.swing.JPanel;
65
import javax.swing.JScrollPane;
66
import javax.swing.JSpinner;
67
import javax.swing.JTextField;
68
import javax.swing.event.ChangeEvent;
69
import javax.swing.event.ChangeListener;
70

    
71
import org.gvsig.gui.beans.slidertext.SliderTextContainer;
72
import org.gvsig.gui.beans.slidertext.listeners.SliderEvent;
73
import org.gvsig.gui.beans.slidertext.listeners.SliderListener;
74
/**
75
 * Componente para crear un cuadro de propiedades de configuracion standard.
76
 *
77
 * @version 19/04/2007
78
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
79
 */
80
public class PropertiesComponent extends JScrollPane implements FocusListener, KeyListener, ChangeListener, ItemListener, PropertiesComponentListener, SliderListener {
81
        private static final long serialVersionUID = 372118344763661890L;
82
        private ArrayList       datalist               = new ArrayList();
83
        private ArrayList       actionCommandListeners = new ArrayList();
84

    
85
        private JPanel          jPanelContent          = null;
86

    
87
        static final public int TYPE_DEFAULT           = 1;
88
        static final public int TYPE_SLIDER            = 2;
89
        static final public int TYPE_COMBO             = 3;
90

    
91
        /**
92
         * Constructor de la calse
93
         */
94
        public PropertiesComponent() {
95
                initialize();
96
        }
97

    
98
        /**
99
         * Constructor para poder pasarle un ArrayList de PropertyStruct
100
         * @param values
101
         */
102
        public PropertiesComponent(ArrayList values) {
103
                initialize();
104
                for (int i=0; i<values.size(); i++) {
105
                        addPropertyStruct((PropertyStruct) values.get(i));
106
                }
107
        }
108

    
109
        /**
110
         * Constructor para poder pasarle un Properties
111
         * @param values
112
         */
113
        public PropertiesComponent(Properties properties) {
114
                initialize();
115
                Enumeration elements = properties.keys();
116
                while (elements.hasMoreElements()) {
117
                        String key = (String) elements.nextElement();
118
                        addValue(key, key, properties.get(key), null);
119
                }
120
        }
121

    
122
        /**
123
         * Creaci?n de la ventana con sus componentes
124
         */
125
        private void initialize() {
126
                this.setBorder(null);
127
                jPanelContent = new JPanel();
128
                jPanelContent.setLayout(new GridBagLayout());
129
                
130
                JPanel jPanelPrincipal = new JPanel();
131
                jPanelPrincipal.setLayout(new GridBagLayout());
132

    
133
                JPanel jPanelEmpty = new JPanel();
134
                jPanelEmpty.setPreferredSize(new Dimension(0, 0));
135
                
136
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
137
                gridBagConstraints.fill = GridBagConstraints.BOTH;
138
                jPanelPrincipal.add(jPanelContent, gridBagConstraints);
139

    
140
                gridBagConstraints = new java.awt.GridBagConstraints();
141
                gridBagConstraints.gridx = 0;
142
                gridBagConstraints.gridy = 1;
143
                gridBagConstraints.weightx = 1.0;
144
                gridBagConstraints.weighty = 1.0;
145
                jPanelPrincipal.add(jPanelEmpty, gridBagConstraints);
146
                
147
                this.setViewportView(jPanelPrincipal);
148
        }
149

    
150
        int y = 0;
151
        /**
152
         * A?ade un PropertyStruct al componente
153
         * @param property
154
         */
155
        public void addPropertyStruct(PropertyStruct property) {
156
                boolean without_label = false;
157

    
158
                JLabel label = new JLabel(property.getTextLabel() + ": ");
159

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

    
162
                if (property.getOldValue() instanceof Component) {
163
                        component = (Component) property.getOldValue();
164
                        without_label = true;
165
                }
166

    
167
                // Tratamiento de Strings, como un JTextField
168
                if (property.getOldValue() instanceof String) {
169
                        component = new JTextField(property.getOldValue().toString());
170
                        ((JTextField) component).setMaximumSize(new Dimension(200, 25));
171
                        ((JTextField) component).addFocusListener(this);
172
                        ((JTextField) component).addKeyListener(this);
173
                }
174

    
175
                if (property.getOldValue() instanceof JPanelProperty) {
176
                        component = (JPanelProperty) property.getOldValue();
177
                        ((JPanelProperty) component).addStateChangedListener(this);
178
                        without_label = true;
179
                }
180

    
181
                // Tratamiento de Integer
182
                if (property.getOldValue() instanceof Integer || property.getOldValue() instanceof Double) {
183
                        boolean created = false;
184
                        if (property.getExtras() != null) {
185
                                switch (((Integer) property.getExtras()[0]).intValue()) {
186
                                        case TYPE_SLIDER:
187
                                                without_label = true;
188
                                                component = new SliderTextContainer();
189
                                                ((SliderTextContainer) component).setBorder(javax.swing.BorderFactory.createTitledBorder(property.getTextLabel()));
190

    
191
                                                if (property.getExtras().length >= 2)
192
                                                        ((SliderTextContainer) component).setMinimum(((Integer) property.getExtras()[1]).intValue());
193
                                                if (property.getExtras().length >= 3)
194
                                                        ((SliderTextContainer) component).setMaximum(((Integer) property.getExtras()[2]).intValue());
195
                                                if (property.getOldValue() instanceof Integer) {
196
                                                        ((SliderTextContainer) component).setDecimal(false);
197
                                                        ((SliderTextContainer) component).setValue(((Integer) property.getOldValue()).intValue());
198
                                                }
199
                                                if (property.getOldValue() instanceof Double) {
200
                                                        ((SliderTextContainer) component).setDecimal(true);
201
                                                        ((SliderTextContainer) component).setValue(((Double) property.getOldValue()).doubleValue());
202
                                                }
203

    
204
                                                ((SliderTextContainer) component).addValueChangedListener(this);
205

    
206
                                                created = true;
207
                                                break;
208
                                        case TYPE_COMBO:
209
                                                if(property.getOldValue() instanceof Integer) {
210
                                                        component = new JComboBox();
211
                                                        ArrayList aux = (ArrayList) property.getExtras()[1];
212
                                                        for (int i=0; i<aux.size(); i++)
213
                                                                ((JComboBox) component).addItem(aux.get(i).toString());
214
                                                        if(property.getOldValue() instanceof Integer)
215
                                                                ((JComboBox) component).setSelectedIndex(((Integer) property.getOldValue()).intValue());
216
                                                        ((JComboBox) component).addItemListener(this);
217
                                                        created = true;
218
                                                }
219
                                                break;
220
                                }
221
                        }
222
                        if (!created) {
223
                                component = new JSpinner();
224
                                ((JSpinner) component).setValue(property.getOldValue());
225
                                ((JSpinner) component).addChangeListener(this);
226
                        }
227
                }
228

    
229
                // Tratamiento de Boolean
230
                if (property.getOldValue() instanceof Boolean) {
231
                        component = new JCheckBox();
232
                        ((JCheckBox) component).setSelected(((Boolean) property.getOldValue()).booleanValue());
233
                        ((JCheckBox) component).addItemListener(this);
234
                }
235

    
236
                GridBagConstraints gridBagConstraints;
237
                if (without_label) {
238
                        gridBagConstraints = new GridBagConstraints();
239
                        gridBagConstraints.gridwidth = 2;
240
                        gridBagConstraints.gridx = 0;
241
                        gridBagConstraints.gridy = y;
242
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
243
                        gridBagConstraints.weightx = 1.0;
244
                        gridBagConstraints.insets = new Insets(2, 5, 2, 5);
245
                        jPanelContent.add(component, gridBagConstraints);
246
                } else {
247
                        gridBagConstraints = new GridBagConstraints();
248
                        gridBagConstraints.gridx = 0;
249
                        gridBagConstraints.gridy = y;
250
                        gridBagConstraints.anchor = GridBagConstraints.EAST;
251
                        gridBagConstraints.insets = new Insets(2, 5, 2, 2);
252
                        jPanelContent.add(label, gridBagConstraints);
253

    
254
                        gridBagConstraints = new GridBagConstraints();
255
                        gridBagConstraints.gridx = 1;
256
                        gridBagConstraints.gridy = y;
257
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
258
                        gridBagConstraints.weightx = 1.0;
259
                        gridBagConstraints.insets = new Insets(2, 2, 2, 5);
260
                        jPanelContent.add(component, gridBagConstraints);
261
                }
262
                y++;
263

    
264
                label.setLabelFor(component);
265

    
266
                property.setJLabel(label);
267
                property.setComponent(component);
268
                datalist.add(property);
269
        }
270

    
271
        /**
272
         * A?ade una clave/valor al panel de propiedades.<br>
273
         * <br>
274
         * El componente seleccionado dependera del instanceof del valor y las
275
         * opciones extras que se pongan. Por ejemplo: para el instanceof de un String
276
         * siempre se usara un JTextField, en cambio, para un Integer, se podran usar
277
         * 3 tipos, el JSlider, JComboBox y JSpinner. Estos tipos se especifican en el
278
         * array extras, poniendolo siempre en la posicion 0. En la posici?n 1 y 2 de
279
         * un JSlider se puede especificar el m?nimo y el m?ximo del Slider.
280
         *
281
         * @param textLabel
282
         * @param key
283
         * @param value
284
         * @param extras
285
         */
286
        public void addValue(String textLabel, String key, Object value, Object[] extras) {
287
                PropertyStruct propertyStruct = new PropertyStruct(textLabel, key, value, extras);
288
                addPropertyStruct(propertyStruct);
289
        }
290

    
291
        /**
292
         * A?ade una clave valor al panel de propiedades.
293
         * @param key
294
         * @param value
295
         */
296
        public void put(Object key, Object value) {
297
                PropertyStruct propertyStruct = new PropertyStruct((String) key, (String) key, value, null);
298
                addPropertyStruct(propertyStruct);
299
        }
300

    
301
        /**
302
         * Obtener todos los valores de la ventana, esto ser? un
303
         * <code><b>ArrayList</b></code> que contendr? elementos de tipo
304
         * <code><b>PropertyStruct</b></code>, pudiendo tener el valor antes de
305
         * ser modificado y el nuevo valor.
306
         *
307
         * @see <code>PropertyStruct</code>
308
         *
309
         * @return ArrayList de elementos de tipo <code>PropertyStruct</code>
310
         */
311
        public ArrayList getValues() {
312
                for (int i = 0; i < datalist.size(); i++) {
313
                        PropertyStruct propertyStruct = ((PropertyStruct) datalist.get(i));
314

    
315
                        if (propertyStruct.getComponent() instanceof JTextField) {
316
                                propertyStruct.setNewValue(((JTextField) propertyStruct.getComponent()).getText());
317
                                continue;
318
                        }
319
                        if (propertyStruct.getComponent() instanceof JSpinner) {
320
                                propertyStruct.setNewValue(((JSpinner) propertyStruct.getComponent()).getValue());
321
                                continue;
322
                        }
323
                        if (propertyStruct.getComponent() instanceof SliderTextContainer) {
324
                                if (propertyStruct.getOldValue() instanceof Double)
325
                                        propertyStruct.setNewValue(new Double((double) ((SliderTextContainer) propertyStruct.getComponent()).getValue()));
326
                                else
327
                                        propertyStruct.setNewValue(new Integer((int) ((SliderTextContainer) propertyStruct.getComponent()).getValue()));
328
                                continue;
329
                        }
330
                        if (propertyStruct.getComponent() instanceof JCheckBox) {
331
                                propertyStruct.setNewValue(new Boolean(((JCheckBox) propertyStruct.getComponent()).getSelectedObjects()!=null));
332
                                continue;
333
                        }
334
                        if (propertyStruct.getComponent() instanceof JComboBox) {
335
                                propertyStruct.setNewValue(new Integer(((JComboBox) propertyStruct.getComponent()).getSelectedIndex()));
336
                                continue;
337
                        }
338
                        if (propertyStruct.getComponent() instanceof JPanelProperty) {
339
                                // No es necesario pq el mismo JPanel esta tb en el oldValue
340
                                continue;
341
                        }
342
                }
343
                return datalist;
344
        }
345

    
346
        /**
347
         * Devuelve el componente del interfaz que trata esa variable, hay que tener
348
         * cuidado, puede devolver null o un componente distinto al esperado si se
349
         * mod?fica esta clase.
350
         * @param name
351
         * @return
352
         */
353
        public Component getComponentUI(String name) {
354
                for (int i = 0; i < datalist.size(); i++) {
355
                        PropertyStruct propertyStruct = ((PropertyStruct) datalist.get(i));
356
                        String key = propertyStruct.getKey();
357
                        if (key.equals(name))
358
                                return propertyStruct.getComponent();
359
                }
360
                return null;
361
        }
362
        
363
        /**
364
         * Obtener todos los valores de la ventana en formato java.util.Properties
365
         * @return
366
         */
367
        public Properties getProperties() {
368
                Properties properties = new Properties();
369
                for (int i = 0; i < datalist.size(); i++) {
370
                        PropertyStruct propertyStruct = ((PropertyStruct) datalist.get(i));
371
                        String key = propertyStruct.getKey();
372

    
373
                        if (propertyStruct.getComponent() instanceof JTextField) {
374
                                properties.put(key, ((JTextField) propertyStruct.getComponent()).getText());
375
                                continue;
376
                        }
377
                        if (propertyStruct.getComponent() instanceof JSpinner) {
378
                                properties.put(key, ((JSpinner) propertyStruct.getComponent()).getValue());
379
                                continue;
380
                        }
381
                        if (propertyStruct.getComponent() instanceof SliderTextContainer) {
382
                                if (propertyStruct.getOldValue() instanceof Double)
383
                                        properties.put(key, new Double((double) ((SliderTextContainer) propertyStruct.getComponent()).getValue()));
384
                                else
385
                                        properties.put(key, new Integer((int) ((SliderTextContainer) propertyStruct.getComponent()).getValue()));
386
                                continue;
387
                        }
388
                        if (propertyStruct.getComponent() instanceof JCheckBox) {
389
                                properties.put(key, new Boolean(((JCheckBox) propertyStruct.getComponent()).getSelectedObjects() != null));
390
                                continue;
391
                        }
392
                        if (propertyStruct.getComponent() instanceof JComboBox) {
393
                                properties.put(key, new Integer(((JComboBox) propertyStruct.getComponent()).getSelectedIndex()));
394
                                continue;
395
                        }
396
                        if (propertyStruct.getComponent() instanceof JPanelProperty) {
397
                                properties.put(key, (JPanelProperty) propertyStruct.getComponent());
398
                                continue;
399
                        }
400
                }
401
                return properties;
402
        }
403

    
404
        /**
405
         * A?adir el disparador de cuando se pulsa un bot?n.
406
         * @param listener
407
         */
408
        public void addStateChangedListener(PropertiesComponentListener listener) {
409
                if (!actionCommandListeners.contains(listener))
410
                        actionCommandListeners.add(listener);
411
        }
412

    
413
        /**
414
         * Borrar el disparador de eventos de los botones.
415
         * @param listener
416
         */
417
        public void removeStateChangedListener(PropertiesComponentListener listener) {
418
                actionCommandListeners.remove(listener);
419
        }
420

    
421
        private void callStateChanged() {
422
                Iterator acIterator = actionCommandListeners.iterator();
423
                while (acIterator.hasNext()) {
424
                        PropertiesComponentListener listener = (PropertiesComponentListener) acIterator.next();
425
                        listener.actionChangeProperties(new EventObject(this));
426
                }
427
        }
428

    
429
        /*
430
         * (non-Javadoc)
431
         * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
432
         */
433
        public void stateChanged(ChangeEvent e) {
434
                callStateChanged();
435
        }
436

    
437
        /*
438
         * (non-Javadoc)
439
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
440
         */
441
        public void itemStateChanged(ItemEvent e) {
442
                callStateChanged();
443
        }
444

    
445
        /*
446
         * (non-Javadoc)
447
         * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
448
         */
449
        public void focusLost(FocusEvent e) {
450
                callStateChanged();
451
        }
452

    
453
        /*
454
         * (non-Javadoc)
455
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
456
         */
457
        public void keyReleased(KeyEvent e) {
458
                if (e.getKeyCode() == 10)
459
                        callStateChanged();
460
        }
461

    
462
        /*
463
         * (non-Javadoc)
464
         * @see org.gvsig.gui.beans.propertiespanel.PropertiesComponentListener#actionChangeProperties(java.util.EventObject)
465
         */
466
        public void actionChangeProperties(EventObject e) {
467
                callStateChanged();
468
        }
469

    
470
        /*
471
         * (non-Javadoc)
472
         * @see org.gvsig.gui.beans.slidertext.listeners.SliderListener#actionValueChanged(org.gvsig.gui.beans.slidertext.listeners.SliderEvent)
473
         */
474
        public void actionValueChanged(SliderEvent e) {
475
                callStateChanged();
476
        }
477

    
478
        /*
479
         * (non-Javadoc)
480
         * @see org.gvsig.gui.beans.slidertext.listeners.SliderListener#actionValueDragged(org.gvsig.gui.beans.slidertext.listeners.SliderEvent)
481
         */
482
        public void actionValueDragged(SliderEvent e) {
483
                //callStateChanged();
484
        }
485

    
486
        public void keyTyped(KeyEvent e) {}
487
        public void focusGained(FocusEvent e) {}
488
        public void keyPressed(KeyEvent e) {}
489
}