Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / propertiespanel / PropertiesPanel.java @ 11346

History | View | Annotate | Download (7.32 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.BorderLayout;
22
import java.awt.Component;
23
import java.awt.Dimension;
24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
26
import java.awt.Insets;
27
import java.util.ArrayList;
28

    
29
import javax.swing.JCheckBox;
30
import javax.swing.JComboBox;
31
import javax.swing.JLabel;
32
import javax.swing.JPanel;
33
import javax.swing.JScrollPane;
34
import javax.swing.JSlider;
35
import javax.swing.JSpinner;
36
import javax.swing.JTextField;
37

    
38
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
39
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
40
/**
41
 * Panel para crear un cuadro de propiedades de configuracion standard.
42
 *
43
 * @version 19/04/2007
44
 * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
45
 *
46
 */
47
public class PropertiesPanel extends DefaultButtonsPanel {
48
        private static final long serialVersionUID = 372118344763661890L;
49
        JScrollPane jScrollPane = null;
50
        JPanel jPanelContent = null;
51
        ArrayList datalist = new ArrayList();
52
        
53
        static final public int TYPE_DEFAULT = 1;
54
        static final public int TYPE_SLIDER = 2;
55
        static final public int TYPE_COMBO = 3;
56

    
57
        /**
58
         * Constructor de la calse
59
         */
60
        public PropertiesPanel() {
61
                super(ButtonsPanel.BUTTONS_ACCEPTCANCELAPPLY);
62
                initialize();
63
        }
64
        
65
        /**
66
         * Creaci?n de la ventana con sus componentes 
67
         */
68
        private void initialize() {
69
                this.setLayout(new BorderLayout(0, 0));
70
                jScrollPane = new JScrollPane();
71
                jScrollPane.setBorder(null);
72
                jPanelContent = new JPanel();
73
                jPanelContent.setLayout(new GridBagLayout());
74
                jScrollPane.setViewportView(jPanelContent);
75
                this.add(jScrollPane, BorderLayout.CENTER);
76
        }
77

    
78
        /**
79
         * A?ade una clave/valor al panel de propiedades.<br>
80
         * <br>
81
         * El componente seleccionado dependera del instanceof del valor y las
82
         * opciones extras que se pongan. Por ejemplo: para el instanceof de un String
83
         * siempre se usara un JTextField, en cambio, para un Integer, se podran usar
84
         * 3 tipos, el JSlider, JComboBox y JSpinner. Estos tipos se especifican en el
85
         * array extras, poniendolo siempre en la posicion 0. En la posici?n 1 y 2 de
86
         * un JSlider se puede especificar el m?nimo y el m?ximo del Slider.
87
         * 
88
         * @param textLabel
89
         * @param key
90
         * @param value
91
         * @param extras
92
         */
93
        int y = 0;
94

    
95
        public void addValue(String textLabel, String key, Object value, Object[] extras) {
96
                JLabel label = new JLabel(textLabel + ": ");
97
                
98
                jPanelContent.add(label, new GridBagConstraints(0, y, 1, 1, 0.0, 0.0,
99
        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
100
        new Insets(0, 0, 5, 5), 0, 0));
101
                
102
                Component component = null;
103
                
104
                // Tratamiento de Strings, como un JTextField
105
                if (value instanceof String) {
106
                        component = new JTextField(value.toString());
107
                        ((JTextField) component).setMaximumSize(new Dimension(200, 25));
108
                }
109
                
110
                // Tratamiento de Integer
111
                if (value instanceof Integer) {
112
                        boolean created = false;
113
                        if (extras != null) {
114
                                switch (((Integer) extras[0]).intValue()) {
115
                                        case TYPE_SLIDER:
116
                                                component = new JSlider();
117
                                                if (extras.length >= 2)
118
                                                        ((JSlider) component).setMinimum(((Integer) extras[1]).intValue());
119
                                                if (extras.length >= 3)
120
                                                        ((JSlider) component).setMaximum(((Integer) extras[2]).intValue());
121
                                                ((JSlider) component).setValue(((Integer) value).intValue());
122
                                                created = true;
123
                                                break;
124
                                        case TYPE_COMBO:
125
                                                component = new JComboBox();
126
                                                ArrayList aux = (ArrayList) extras[1];
127
                                                for (int i=0; i<aux.size(); i++) 
128
                                                        ((JComboBox) component).addItem(aux.get(i).toString());
129
                                                ((JComboBox) component).setSelectedIndex(((Integer) value).intValue());
130
                                                created = true;
131
                                                break;
132
                                }
133
                        }
134
                        if (!created) {
135
                                component = new JSpinner();
136
                                ((JSpinner) component).setValue(value);
137
                        }
138
                }
139
                
140
                // Tratamiento de Boolean
141
                if (value instanceof Boolean) {
142
                        component = new JCheckBox();
143
                        ((JCheckBox) component).setSelected(((Boolean) value).booleanValue());
144
                }
145
                
146
                jPanelContent.add(component, new GridBagConstraints(1, y, 1, 1, 0.0, 0.0,
147
        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
148
        new Insets(0, 0, 5, 0), 0, 0));
149
                y++;
150
                
151
                label.setLabelFor(component);
152
                label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
153
                
154
                PropertyStruct propertyStruct = new PropertyStruct();
155
                propertyStruct.key = key;
156
                propertyStruct.oldValue = value;
157
                propertyStruct.extras = extras;
158
                propertyStruct.jLabel = label;
159
                propertyStruct.component = component;
160

    
161
                datalist.add(propertyStruct);
162
        }
163

    
164
        /**
165
         * Obtener todos los valores de la ventana, esto ser? un
166
         * <code><b>ArrayList</b></code> que contendr? elementos de tipo
167
         * <code><b>PropertyStruct</b></code>, pudiendo tener el valor antes de
168
         * ser modificado y el nuevo valor.
169
         * 
170
         * @see <code>PropertyStruct</code>
171
         * 
172
         * @return ArrayList de elementos de tipo <code>PropertyStruct</code>
173
         */
174
        public ArrayList getValues() {
175
                for (int i=0; i<datalist.size(); i++) {
176
                        PropertyStruct propertyStruct = ((PropertyStruct) datalist.get(i));
177

    
178
                        if (propertyStruct.component instanceof JTextField) {
179
                                propertyStruct.newValue = ((JTextField) propertyStruct.component).getText();
180
                                continue;
181
                        }
182
                        if (propertyStruct.component instanceof JSpinner) {
183
                                propertyStruct.newValue = ((JSpinner) propertyStruct.component).getValue();
184
                                continue;
185
                        }
186
                        if (propertyStruct.component instanceof JSlider) {
187
                                propertyStruct.newValue = new Integer(((JSlider) propertyStruct.component).getValue());
188
                                continue;
189
                        }
190
                        if (propertyStruct.component instanceof JCheckBox) {
191
                                propertyStruct.newValue = new Boolean(((JCheckBox) propertyStruct.component).getSelectedObjects()!=null);
192
                                continue;
193
                        }
194
                        if (propertyStruct.component instanceof JComboBox) {
195
                                propertyStruct.newValue = new Integer(((JComboBox) propertyStruct.component).getSelectedIndex());
196
                                continue;
197
                        }
198
                }
199
                return datalist;
200
        }
201
        
202
        /**
203
         * Clase con todos los datos posibles que puede tener una clave/valor
204
         * especificada.
205
         * 
206
         * @version 23/04/2007
207
         * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
208
         */
209
        public class PropertyStruct {
210
                /**
211
                 * Clave del elemento 
212
                 */
213
                public String key;
214

    
215
                /**
216
                 * Valor inicial del elemento
217
                 */
218
                public Object oldValue;
219

    
220
                /**
221
                 * Valor despu?s de haberlo modificado en la ventana
222
                 */
223
                public Object newValue;
224
                
225
                /**
226
                 * Lista de opciones extras que determina como sera un
227
                 * <code>Component</code>
228
                 */
229
                public Object[] extras;
230
                
231
                /**
232
                 * <code>JLabel</code> que representa al componente 
233
                 */
234
                public JLabel jLabel;
235
                
236
                /**
237
                 * Componente en si donde se modificar? el valor
238
                 */
239
                public Component component;
240
        }
241

    
242
}