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