root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / propertiespanel / PropertiesComponent.java @ 13136
History | View | Annotate | Download (13.1 KB)
1 | 11561 | bsanchez | /* 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 | 11707 | bsanchez | 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 | 11561 | bsanchez | import java.util.ArrayList; |
33 | 11576 | bsanchez | import java.util.Enumeration; |
34 | 11707 | bsanchez | import java.util.EventObject; |
35 | import java.util.Iterator; |
||
36 | 11576 | bsanchez | import java.util.Properties; |
37 | 11561 | bsanchez | |
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 | 11707 | bsanchez | import javax.swing.event.ChangeEvent; |
46 | import javax.swing.event.ChangeListener; |
||
47 | |||
48 | import org.gvsig.gui.beans.slidertext.SliderTextContainer; |
||
49 | 12180 | bsanchez | import org.gvsig.gui.beans.slidertext.listeners.SliderEvent; |
50 | import org.gvsig.gui.beans.slidertext.listeners.SliderListener; |
||
51 | 11561 | bsanchez | /**
|
52 | * Componente para crear un cuadro de propiedades de configuracion standard.
|
||
53 | 12368 | bsanchez | *
|
54 | 11561 | bsanchez | * @version 19/04/2007
|
55 | 12368 | bsanchez | * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
56 | 11561 | bsanchez | */
|
57 | 12180 | bsanchez | public class PropertiesComponent extends JScrollPane implements FocusListener, KeyListener, ChangeListener, ItemListener, PropertiesComponentListener, SliderListener { |
58 | 11561 | bsanchez | private static final long serialVersionUID = 372118344763661890L; |
59 | 12121 | bsanchez | private ArrayList datalist = new ArrayList(); |
60 | private ArrayList actionCommandListeners = new ArrayList(); |
||
61 | 12012 | bsanchez | |
62 | 12107 | bsanchez | private JPanel jPanelContent = null; |
63 | 12012 | bsanchez | |
64 | 12107 | bsanchez | static final public int TYPE_DEFAULT = 1; |
65 | static final public int TYPE_SLIDER = 2; |
||
66 | static final public int TYPE_COMBO = 3; |
||
67 | 11561 | bsanchez | |
68 | /**
|
||
69 | * Constructor de la calse
|
||
70 | */
|
||
71 | public PropertiesComponent() {
|
||
72 | initialize(); |
||
73 | } |
||
74 | 12012 | bsanchez | |
75 | 11561 | bsanchez | /**
|
76 | 11563 | bsanchez | * Constructor para poder pasarle un ArrayList de PropertyStruct
|
77 | * @param values
|
||
78 | */
|
||
79 | public PropertiesComponent(ArrayList values) { |
||
80 | initialize(); |
||
81 | for (int i=0; i<values.size(); i++) { |
||
82 | addPropertyStruct((PropertyStruct) values.get(i)); |
||
83 | } |
||
84 | } |
||
85 | 12012 | bsanchez | |
86 | 11563 | bsanchez | /**
|
87 | 11576 | bsanchez | * Constructor para poder pasarle un Properties
|
88 | * @param values
|
||
89 | 11561 | bsanchez | */
|
90 | 11576 | bsanchez | public PropertiesComponent(Properties properties) { |
91 | initialize(); |
||
92 | Enumeration elements = properties.keys();
|
||
93 | while (elements.hasMoreElements()) {
|
||
94 | String key = (String) elements.nextElement(); |
||
95 | addValue(key, key, properties.get(key), null);
|
||
96 | } |
||
97 | } |
||
98 | 12012 | bsanchez | |
99 | 11576 | bsanchez | /**
|
100 | * Creaci?n de la ventana con sus componentes
|
||
101 | */
|
||
102 | 11561 | bsanchez | private void initialize() { |
103 | this.setBorder(null); |
||
104 | jPanelContent = new JPanel(); |
||
105 | jPanelContent.setLayout(new GridBagLayout()); |
||
106 | this.setViewportView(jPanelContent);
|
||
107 | } |
||
108 | |||
109 | 11565 | bsanchez | int y = 0; |
110 | 11561 | bsanchez | /**
|
111 | 12368 | bsanchez | * A?ade un PropertyStruct al componente
|
112 | 11565 | bsanchez | * @param property
|
113 | 11561 | bsanchez | */
|
114 | 11563 | bsanchez | public void addPropertyStruct(PropertyStruct property) { |
115 | 12012 | bsanchez | boolean without_label = false; |
116 | |||
117 | 11565 | bsanchez | JLabel label = new JLabel(property.getTextLabel() + ": "); |
118 | 12012 | bsanchez | |
119 | 11576 | bsanchez | Component component = new JLabel("Sin soporte para: " + property.getOldValue().getClass().toString()); |
120 | 12012 | bsanchez | |
121 | 11561 | bsanchez | // Tratamiento de Strings, como un JTextField
|
122 | 11565 | bsanchez | if (property.getOldValue() instanceof String) { |
123 | component = new JTextField(property.getOldValue().toString()); |
||
124 | 11561 | bsanchez | ((JTextField) component).setMaximumSize(new Dimension(200, 25)); |
125 | 11707 | bsanchez | ((JTextField) component).addFocusListener(this); |
126 | ((JTextField) component).addKeyListener(this); |
||
127 | 11561 | bsanchez | } |
128 | 12012 | bsanchez | |
129 | if (property.getOldValue() instanceof JPanelProperty) { |
||
130 | component = (JPanelProperty) property.getOldValue(); |
||
131 | ((JPanelProperty) component).addStateChangedListener(this);
|
||
132 | without_label = true;
|
||
133 | } |
||
134 | |||
135 | 11561 | bsanchez | // Tratamiento de Integer
|
136 | 11565 | bsanchez | if (property.getOldValue() instanceof Integer) { |
137 | 11561 | bsanchez | boolean created = false; |
138 | 11565 | bsanchez | if (property.getExtras() != null) { |
139 | switch (((Integer) property.getExtras()[0]).intValue()) { |
||
140 | 11561 | bsanchez | case TYPE_SLIDER:
|
141 | 12012 | bsanchez | without_label = true;
|
142 | 11707 | bsanchez | component = new SliderTextContainer();
|
143 | ((SliderTextContainer) component).setBorder(javax.swing.BorderFactory.createTitledBorder(property.getTextLabel())); |
||
144 | // ((SliderTextContainer) component).setBorder(javax.swing.BorderFactory.createEmptyBorder());
|
||
145 | |||
146 | 11565 | bsanchez | if (property.getExtras().length >= 2) |
147 | 11707 | bsanchez | ((SliderTextContainer) component).setMinimum(((Integer) property.getExtras()[1]).intValue()); |
148 | 11565 | bsanchez | if (property.getExtras().length >= 3) |
149 | 11707 | bsanchez | ((SliderTextContainer) component).setMaximum(((Integer) property.getExtras()[2]).intValue()); |
150 | ((SliderTextContainer) component).setValue(((Integer) property.getOldValue()).intValue());
|
||
151 | 12012 | bsanchez | |
152 | 12180 | bsanchez | ((SliderTextContainer) component).addValueChangedListener(this);
|
153 | 12012 | bsanchez | |
154 | 11561 | bsanchez | created = true;
|
155 | break;
|
||
156 | case TYPE_COMBO:
|
||
157 | component = new JComboBox(); |
||
158 | 11565 | bsanchez | ArrayList aux = (ArrayList) property.getExtras()[1]; |
159 | 12012 | bsanchez | for (int i=0; i<aux.size(); i++) |
160 | 11561 | bsanchez | ((JComboBox) component).addItem(aux.get(i).toString());
|
161 | 11565 | bsanchez | ((JComboBox) component).setSelectedIndex(((Integer) property.getOldValue()).intValue()); |
162 | 11707 | bsanchez | ((JComboBox) component).addItemListener(this); |
163 | 11561 | bsanchez | created = true;
|
164 | break;
|
||
165 | } |
||
166 | } |
||
167 | if (!created) {
|
||
168 | component = new JSpinner(); |
||
169 | 11565 | bsanchez | ((JSpinner) component).setValue(property.getOldValue());
|
170 | 11707 | bsanchez | ((JSpinner) component).addChangeListener(this); |
171 | 11561 | bsanchez | } |
172 | } |
||
173 | 12012 | bsanchez | |
174 | 11561 | bsanchez | // Tratamiento de Boolean
|
175 | 11565 | bsanchez | if (property.getOldValue() instanceof Boolean) { |
176 | 11561 | bsanchez | component = new JCheckBox(); |
177 | 11565 | bsanchez | ((JCheckBox) component).setSelected(((Boolean) property.getOldValue()).booleanValue()); |
178 | 11707 | bsanchez | ((JCheckBox) component).addItemListener(this); |
179 | 11561 | bsanchez | } |
180 | 12012 | bsanchez | |
181 | if (without_label) {
|
||
182 | 11707 | bsanchez | jPanelContent.add(component, new GridBagConstraints(0, y, 2, 1, 0.0, 0.0, |
183 | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
||
184 | new Insets(0, 0, 5, 0), 0, 0)); |
||
185 | } else {
|
||
186 | jPanelContent.add(label, new GridBagConstraints(0, y, 1, 1, 0.0, 0.0, |
||
187 | 12012 | bsanchez | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
188 | new Insets(0, 0, 5, 5), 0, 0)); |
||
189 | |||
190 | 11707 | bsanchez | jPanelContent.add(component, new GridBagConstraints(1, y, 1, 1, 0.0, 0.0, |
191 | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
||
192 | new Insets(0, 0, 5, 0), 0, 0)); |
||
193 | } |
||
194 | 11561 | bsanchez | y++; |
195 | 12012 | bsanchez | |
196 | 11561 | bsanchez | label.setLabelFor(component); |
197 | label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); |
||
198 | 12012 | bsanchez | |
199 | 11565 | bsanchez | property.setJLabel(label); |
200 | property.setComponent(component); |
||
201 | 11563 | bsanchez | datalist.add(property); |
202 | } |
||
203 | |||
204 | 11565 | bsanchez | /**
|
205 | * A?ade una clave/valor al panel de propiedades.<br>
|
||
206 | * <br>
|
||
207 | * El componente seleccionado dependera del instanceof del valor y las
|
||
208 | * opciones extras que se pongan. Por ejemplo: para el instanceof de un String
|
||
209 | * siempre se usara un JTextField, en cambio, para un Integer, se podran usar
|
||
210 | * 3 tipos, el JSlider, JComboBox y JSpinner. Estos tipos se especifican en el
|
||
211 | * array extras, poniendolo siempre en la posicion 0. En la posici?n 1 y 2 de
|
||
212 | * un JSlider se puede especificar el m?nimo y el m?ximo del Slider.
|
||
213 | 12368 | bsanchez | *
|
214 | 11565 | bsanchez | * @param textLabel
|
215 | * @param key
|
||
216 | * @param value
|
||
217 | * @param extras
|
||
218 | */
|
||
219 | 11563 | bsanchez | public void addValue(String textLabel, String key, Object value, Object[] extras) { |
220 | 11565 | bsanchez | PropertyStruct propertyStruct = new PropertyStruct(textLabel, key, value, extras);
|
221 | 11563 | bsanchez | addPropertyStruct(propertyStruct); |
222 | 11561 | bsanchez | } |
223 | |||
224 | /**
|
||
225 | 11576 | bsanchez | * A?ade una clave valor al panel de propiedades.
|
226 | * @param key
|
||
227 | * @param value
|
||
228 | */
|
||
229 | public void put(Object key, Object value) { |
||
230 | PropertyStruct propertyStruct = new PropertyStruct((String) key, (String) key, value, null); |
||
231 | addPropertyStruct(propertyStruct); |
||
232 | } |
||
233 | |||
234 | /**
|
||
235 | 11561 | bsanchez | * Obtener todos los valores de la ventana, esto ser? un
|
236 | * <code><b>ArrayList</b></code> que contendr? elementos de tipo
|
||
237 | * <code><b>PropertyStruct</b></code>, pudiendo tener el valor antes de
|
||
238 | * ser modificado y el nuevo valor.
|
||
239 | 12368 | bsanchez | *
|
240 | 11561 | bsanchez | * @see <code>PropertyStruct</code>
|
241 | 12368 | bsanchez | *
|
242 | 11561 | bsanchez | * @return ArrayList de elementos de tipo <code>PropertyStruct</code>
|
243 | */
|
||
244 | public ArrayList getValues() { |
||
245 | for (int i=0; i<datalist.size(); i++) { |
||
246 | PropertyStruct propertyStruct = ((PropertyStruct) datalist.get(i)); |
||
247 | |||
248 | 11565 | bsanchez | if (propertyStruct.getComponent() instanceof JTextField) { |
249 | propertyStruct.setNewValue(((JTextField) propertyStruct.getComponent()).getText());
|
||
250 | 11561 | bsanchez | continue;
|
251 | } |
||
252 | 11565 | bsanchez | if (propertyStruct.getComponent() instanceof JSpinner) { |
253 | propertyStruct.setNewValue(((JSpinner) propertyStruct.getComponent()).getValue());
|
||
254 | 11561 | bsanchez | continue;
|
255 | } |
||
256 | 11707 | bsanchez | if (propertyStruct.getComponent() instanceof SliderTextContainer) { |
257 | propertyStruct.setNewValue(new Integer((int) ((SliderTextContainer) propertyStruct.getComponent()).getValue())); |
||
258 | 11561 | bsanchez | continue;
|
259 | } |
||
260 | 11565 | bsanchez | if (propertyStruct.getComponent() instanceof JCheckBox) { |
261 | propertyStruct.setNewValue(new Boolean(((JCheckBox) propertyStruct.getComponent()).getSelectedObjects()!=null)); |
||
262 | 11561 | bsanchez | continue;
|
263 | } |
||
264 | 11565 | bsanchez | if (propertyStruct.getComponent() instanceof JComboBox) { |
265 | propertyStruct.setNewValue(new Integer(((JComboBox) propertyStruct.getComponent()).getSelectedIndex())); |
||
266 | 11561 | bsanchez | continue;
|
267 | } |
||
268 | 12012 | bsanchez | if (propertyStruct.getComponent() instanceof JPanelProperty) { |
269 | // No es necesario pq el mismo JPanel esta tb en el oldValue
|
||
270 | continue;
|
||
271 | } |
||
272 | 11561 | bsanchez | } |
273 | return datalist;
|
||
274 | } |
||
275 | 12012 | bsanchez | |
276 | 11576 | bsanchez | /**
|
277 | * Obtener todos los valores de la ventana en formato java.util.Properties
|
||
278 | * @return
|
||
279 | */
|
||
280 | public Properties getProperties() { |
||
281 | Properties properties = new Properties(); |
||
282 | for (int i=0; i<datalist.size(); i++) { |
||
283 | PropertyStruct propertyStruct = ((PropertyStruct) datalist.get(i)); |
||
284 | String key = propertyStruct.getKey();
|
||
285 | |||
286 | if (propertyStruct.getComponent() instanceof JTextField) { |
||
287 | properties.put(key, ((JTextField) propertyStruct.getComponent()).getText());
|
||
288 | continue;
|
||
289 | } |
||
290 | if (propertyStruct.getComponent() instanceof JSpinner) { |
||
291 | properties.put(key, ((JSpinner) propertyStruct.getComponent()).getValue());
|
||
292 | continue;
|
||
293 | } |
||
294 | 11707 | bsanchez | if (propertyStruct.getComponent() instanceof SliderTextContainer) { |
295 | properties.put(key, new Integer((int) ((SliderTextContainer) propertyStruct.getComponent()).getValue())); |
||
296 | 11576 | bsanchez | continue;
|
297 | } |
||
298 | if (propertyStruct.getComponent() instanceof JCheckBox) { |
||
299 | properties.put(key, new Boolean(((JCheckBox) propertyStruct.getComponent()).getSelectedObjects()!=null)); |
||
300 | continue;
|
||
301 | } |
||
302 | if (propertyStruct.getComponent() instanceof JComboBox) { |
||
303 | properties.put(key, new Integer(((JComboBox) propertyStruct.getComponent()).getSelectedIndex())); |
||
304 | continue;
|
||
305 | } |
||
306 | 12012 | bsanchez | if (propertyStruct.getComponent() instanceof JPanelProperty) { |
307 | properties.put(key, (JPanelProperty) propertyStruct.getComponent()); |
||
308 | continue;
|
||
309 | } |
||
310 | 11576 | bsanchez | } |
311 | return properties;
|
||
312 | 11707 | bsanchez | } |
313 | |||
314 | /**
|
||
315 | * A?adir el disparador de cuando se pulsa un bot?n.
|
||
316 | * @param listener
|
||
317 | */
|
||
318 | public void addStateChangedListener(PropertiesComponentListener listener) { |
||
319 | if (!actionCommandListeners.contains(listener))
|
||
320 | actionCommandListeners.add(listener); |
||
321 | } |
||
322 | |||
323 | /**
|
||
324 | * Borrar el disparador de eventos de los botones.
|
||
325 | * @param listener
|
||
326 | */
|
||
327 | public void removeStateChangedListener(PropertiesComponentListener listener) { |
||
328 | 12012 | bsanchez | actionCommandListeners.remove(listener); |
329 | 11707 | bsanchez | } |
330 | 12012 | bsanchez | |
331 | 11707 | bsanchez | private void callStateChanged() { |
332 | Iterator acIterator = actionCommandListeners.iterator();
|
||
333 | while (acIterator.hasNext()) {
|
||
334 | PropertiesComponentListener listener = (PropertiesComponentListener) acIterator.next(); |
||
335 | listener.actionChangeProperties(new EventObject(this)); |
||
336 | } |
||
337 | } |
||
338 | 12012 | bsanchez | |
339 | /*
|
||
340 | * (non-Javadoc)
|
||
341 | * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
|
||
342 | */
|
||
343 | 11707 | bsanchez | public void stateChanged(ChangeEvent e) { |
344 | callStateChanged(); |
||
345 | } |
||
346 | |||
347 | 12012 | bsanchez | /*
|
348 | * (non-Javadoc)
|
||
349 | * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
|
||
350 | */
|
||
351 | 11707 | bsanchez | public void itemStateChanged(ItemEvent e) { |
352 | callStateChanged(); |
||
353 | } |
||
354 | |||
355 | 12012 | bsanchez | /*
|
356 | * (non-Javadoc)
|
||
357 | * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
|
||
358 | */
|
||
359 | 11707 | bsanchez | public void focusLost(FocusEvent e) { |
360 | callStateChanged(); |
||
361 | } |
||
362 | |||
363 | 12012 | bsanchez | /*
|
364 | * (non-Javadoc)
|
||
365 | * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
|
||
366 | */
|
||
367 | 11707 | bsanchez | public void keyReleased(KeyEvent e) { |
368 | if (e.getKeyCode() == 10) |
||
369 | callStateChanged(); |
||
370 | } |
||
371 | |||
372 | 12012 | bsanchez | /*
|
373 | * (non-Javadoc)
|
||
374 | * @see org.gvsig.gui.beans.propertiespanel.PropertiesComponentListener#actionChangeProperties(java.util.EventObject)
|
||
375 | */
|
||
376 | public void actionChangeProperties(EventObject e) { |
||
377 | callStateChanged(); |
||
378 | } |
||
379 | |||
380 | /*
|
||
381 | * (non-Javadoc)
|
||
382 | 12180 | bsanchez | * @see org.gvsig.gui.beans.slidertext.listeners.SliderListener#actionValueChanged(org.gvsig.gui.beans.slidertext.listeners.SliderEvent)
|
383 | 12012 | bsanchez | */
|
384 | 12180 | bsanchez | public void actionValueChanged(SliderEvent e) { |
385 | callStateChanged(); |
||
386 | 12012 | bsanchez | } |
387 | |||
388 | /*
|
||
389 | * (non-Javadoc)
|
||
390 | 12180 | bsanchez | * @see org.gvsig.gui.beans.slidertext.listeners.SliderListener#actionValueDragged(org.gvsig.gui.beans.slidertext.listeners.SliderEvent)
|
391 | 12012 | bsanchez | */
|
392 | 12180 | bsanchez | public void actionValueDragged(SliderEvent e) { |
393 | 12239 | nacho | //callStateChanged();
|
394 | 12012 | bsanchez | } |
395 | |||
396 | 12180 | bsanchez | public void keyTyped(KeyEvent e) {} |
397 | public void focusGained(FocusEvent e) {} |
||
398 | public void keyPressed(KeyEvent e) {} |
||
399 | 11561 | bsanchez | } |