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