gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / dynobject / dynfield / JNDynFieldComponent.java @ 306
History | View | Annotate | Download (13.8 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
/*
|
23 |
* AUTHORS (In addition to CIT):
|
24 |
* 2010 Institute of New Imaging Technologies (INIT):
|
25 |
* http://www.init.uji.es
|
26 |
* Geographic Information research group:
|
27 |
* http://www.geoinfo.uji.es
|
28 |
* Universitat Jaume I, Spain
|
29 |
*/
|
30 |
|
31 |
/**
|
32 |
*
|
33 |
*/
|
34 |
package org.gvsig.tools.swing.impl.dynobject.dynfield; |
35 |
|
36 |
import java.awt.BorderLayout; |
37 |
import java.awt.GridBagConstraints; |
38 |
import java.awt.GridBagLayout; |
39 |
import java.awt.Insets; |
40 |
import java.awt.event.ActionEvent; |
41 |
import java.awt.event.ActionListener; |
42 |
import java.util.List; |
43 |
|
44 |
import javax.swing.BorderFactory; |
45 |
import javax.swing.JButton; |
46 |
import javax.swing.JComponent; |
47 |
import javax.swing.JList; |
48 |
import javax.swing.JPanel; |
49 |
import javax.swing.JScrollPane; |
50 |
import javax.swing.ListSelectionModel; |
51 |
import javax.swing.event.ListSelectionEvent; |
52 |
import javax.swing.event.ListSelectionListener; |
53 |
|
54 |
import org.gvsig.tools.dataTypes.DataTypes; |
55 |
import org.gvsig.tools.service.ServiceException; |
56 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
57 |
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener; |
58 |
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent; |
59 |
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField; |
60 |
|
61 |
/**
|
62 |
* @author <a href="mailto:reinhold@uji.es">cmartin</a>
|
63 |
*
|
64 |
*/
|
65 |
public class JNDynFieldComponent extends DelegatedJFieldComponent implements |
66 |
JDynFieldComponent, ListSelectionListener, ValueChangedListener {
|
67 |
|
68 |
private static final Object UNKNOWN = "unknown"; |
69 |
|
70 |
private JList list; |
71 |
|
72 |
private JButton sbutton; |
73 |
|
74 |
private JButton jbutton; |
75 |
|
76 |
private JPanel panel; |
77 |
|
78 |
private JScrollPane listScrollPane; |
79 |
|
80 |
private JPanel upperSubPanel; |
81 |
|
82 |
/**
|
83 |
* @param serviceManager
|
84 |
* @param dynObject
|
85 |
* @param dynField
|
86 |
*/
|
87 |
public JNDynFieldComponent(ValueField valueField) {
|
88 |
super(valueField, valueField.getDynField());
|
89 |
this.component.addValueChangedListener(this); |
90 |
} |
91 |
|
92 |
private void addElement(Object element) { |
93 |
NValueField items = getItems(); |
94 |
Object[] values = |
95 |
this.getDynField().getElementsType().getAvailableValues();
|
96 |
if (values != null) { |
97 |
items.addElement(element, values[(Integer) element]);
|
98 |
} else {
|
99 |
int type = this.getDynField().getElementsType().getType(); |
100 |
if (type == DataTypes.BOOLEAN) {
|
101 |
if (element == null) { |
102 |
items.addElement(element, UNKNOWN); |
103 |
} else {
|
104 |
items.addElement(element, element); |
105 |
} |
106 |
} else {
|
107 |
items.addElement(element, element); |
108 |
} |
109 |
} |
110 |
this.list.setSelectedIndex(items.getSize() - 1); |
111 |
} |
112 |
|
113 |
/**
|
114 |
*
|
115 |
*/
|
116 |
@Override
|
117 |
protected void afterUI() { |
118 |
//
|
119 |
// Object valueItems = getFieldValue();
|
120 |
// if (valueItems instanceof List) {
|
121 |
// populateItems((List) valueItems);
|
122 |
// }
|
123 |
refresh(); |
124 |
} |
125 |
|
126 |
/*
|
127 |
* (non-Javadoc)
|
128 |
*
|
129 |
* @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
|
130 |
*/
|
131 |
public JComponent asJComponent() { |
132 |
return this.panel; |
133 |
} |
134 |
|
135 |
private JButton createButton(String text) { |
136 |
return ToolsSwingLocator.getUsabilitySwingManager().createJToolButton(
|
137 |
text.toString()); |
138 |
} |
139 |
|
140 |
/*
|
141 |
* (non-Javadoc)
|
142 |
*
|
143 |
* @see
|
144 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
|
145 |
*/
|
146 |
@Override
|
147 |
public Object getDefaultValue() { |
148 |
return this.getDynField().getDefaultValue(); |
149 |
} |
150 |
|
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
*
|
154 |
* @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#getValue()
|
155 |
*/
|
156 |
@Override
|
157 |
public Object getValue() { |
158 |
if (this.list == null) { |
159 |
return null; |
160 |
} |
161 |
if (this.getDynField().getType() == DataTypes.STRING) { |
162 |
return this.list.getSelectedValue(); |
163 |
} |
164 |
return this.list.getSelectedIndex(); |
165 |
} |
166 |
|
167 |
/*
|
168 |
* (non-Javadoc)
|
169 |
*
|
170 |
* @see
|
171 |
*
|
172 |
* org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
|
173 |
* (org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
|
174 |
*/
|
175 |
public void handleValueChanged(JDynFieldComponent field) { |
176 |
Object value = field.getValue();
|
177 |
if (value == null) { |
178 |
this.list.setSelectedIndex(-1); |
179 |
} |
180 |
int index = this.list.getSelectedIndex(); |
181 |
if (index > -1) { |
182 |
setValue(value, index); |
183 |
} |
184 |
this.fireValueChangedEvent();
|
185 |
} |
186 |
|
187 |
/**
|
188 |
*
|
189 |
*/
|
190 |
private void initBoxLayout() { |
191 |
panel = new JPanel(new BorderLayout(2, 2)); |
192 |
panel.setBorder(BorderFactory.createTitledBorder("")); |
193 |
|
194 |
JPanel subPanel = new JPanel(new GridBagLayout()); |
195 |
// subPanel.setBorder(BorderFactory.createTitledBorder(""));
|
196 |
list.setVisibleRowCount(4);
|
197 |
// list.setLayout(new GridBagLayout());
|
198 |
GridBagConstraints c = new GridBagConstraints(); |
199 |
// c.insets = new Insets(2, 2, 2, 2);
|
200 |
// c.anchor = GridBagConstraints.PAGE_START;
|
201 |
// c.ipadx = 0;
|
202 |
// c.fill = GridBagConstraints.HORIZONTAL;
|
203 |
|
204 |
listScrollPane = new JScrollPane(list); |
205 |
listScrollPane.setViewportView(this.list);
|
206 |
listScrollPane.setLocale(this.list.getLocale());
|
207 |
|
208 |
// Adding elements
|
209 |
|
210 |
c.insets = new Insets(2, 2, 2, 2); |
211 |
c.anchor = GridBagConstraints.LINE_START;
|
212 |
c.ipadx = 0;
|
213 |
c.weightx = 0.75;
|
214 |
c.fill = GridBagConstraints.HORIZONTAL;
|
215 |
|
216 |
c.gridx = 0;
|
217 |
c.gridy = 0;
|
218 |
subPanel.add(component.asJComponent(), c); |
219 |
|
220 |
// c.insets = new Insets(2, 2, 2, 2);
|
221 |
c.gridx = 1;
|
222 |
c.gridy = 0;
|
223 |
c.weightx = 0.0; // 0 |
224 |
c.fill = GridBagConstraints.NONE;
|
225 |
c.anchor = GridBagConstraints.LINE_END;
|
226 |
subPanel.add(jbutton, c); |
227 |
|
228 |
panel.add(subPanel, BorderLayout.NORTH);
|
229 |
|
230 |
this.upperSubPanel = panel;
|
231 |
|
232 |
subPanel = new JPanel(new GridBagLayout()); |
233 |
|
234 |
c.insets = new Insets(2, 2, 2, 2); |
235 |
c.anchor = GridBagConstraints.LINE_START;
|
236 |
c.ipadx = 0;
|
237 |
c.weightx = 0.75;
|
238 |
c.ipady = 40;
|
239 |
c.fill = GridBagConstraints.HORIZONTAL;
|
240 |
|
241 |
c.gridx = 0;
|
242 |
c.gridy = 0;
|
243 |
subPanel.add(this.listScrollPane, c);
|
244 |
|
245 |
c.insets = new Insets(4, 2, 2, 2); |
246 |
c.gridx = 1;
|
247 |
c.gridy = 0;
|
248 |
c.ipady = 0;
|
249 |
c.weightx = 0.0; // 0 |
250 |
c.fill = GridBagConstraints.RELATIVE;
|
251 |
c.anchor = GridBagConstraints.PAGE_START;
|
252 |
subPanel.add(sbutton, c); |
253 |
|
254 |
panel.add(subPanel); |
255 |
} |
256 |
|
257 |
@Override
|
258 |
protected void initData() { |
259 |
|
260 |
} |
261 |
|
262 |
public void initJButtons() { |
263 |
sbutton = createButton("-");
|
264 |
sbutton.setActionCommand("-");
|
265 |
sbutton.addActionListener(new ActionListener() { |
266 |
|
267 |
public void actionPerformed(ActionEvent e) { |
268 |
removeElement(list.getSelectedIndex()); |
269 |
refresh(); |
270 |
} |
271 |
}); |
272 |
|
273 |
jbutton = createButton("+");
|
274 |
jbutton.setActionCommand("+");
|
275 |
jbutton.addActionListener(new ActionListener() { |
276 |
|
277 |
public void actionPerformed(ActionEvent e) { |
278 |
// add at the end of the list
|
279 |
if (component.isValid()) {
|
280 |
addElement(component.getValue()); |
281 |
refresh(); |
282 |
} |
283 |
} |
284 |
}); |
285 |
} |
286 |
|
287 |
/**
|
288 |
*
|
289 |
*/
|
290 |
private void initJList() { |
291 |
this.list = new JList(this.getItems()); |
292 |
this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
293 |
this.list.addListSelectionListener(this); |
294 |
if (getItems().getSize() > 0) { |
295 |
this.list.setSelectedIndex(0); |
296 |
} |
297 |
} |
298 |
|
299 |
/**
|
300 |
* @throws ServiceException
|
301 |
*
|
302 |
*/
|
303 |
@Override
|
304 |
protected void initUI() { |
305 |
initJList(); |
306 |
initJButtons(); |
307 |
initBoxLayout(); |
308 |
refresh(); |
309 |
} |
310 |
|
311 |
/*
|
312 |
* (non-Javadoc)
|
313 |
*
|
314 |
* @see
|
315 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isValid()
|
316 |
*/
|
317 |
public boolean isValid() { |
318 |
if (this.isMandatory()) { |
319 |
if ((this.getItems().getSize() > 0) && (component.isValid())) { |
320 |
return true; |
321 |
} else {
|
322 |
return false; |
323 |
} |
324 |
} |
325 |
return component.isValid();
|
326 |
} |
327 |
|
328 |
public void refresh() { |
329 |
NValueField items = getItems(); |
330 |
if ((items.isEmpty()) || (list.getSelectedIndex() < 0)) { |
331 |
this.sbutton.setEnabled(false); |
332 |
this.component.requestFocus();
|
333 |
} else {
|
334 |
this.sbutton.setEnabled(true); |
335 |
} |
336 |
this.fireValueChangedEvent();
|
337 |
} |
338 |
|
339 |
private void removeElement(int ind) { |
340 |
|
341 |
if ((ind >= 0) && (ind < this.getItems().getSize())) { |
342 |
removeItem(ind); |
343 |
} |
344 |
|
345 |
if (getItems().getSize() < 0) { |
346 |
this.sbutton.setEnabled(false); |
347 |
this.component.setValue(null); |
348 |
} else {
|
349 |
if (ind > 0) { |
350 |
this.list.setSelectedIndex(ind - 1); |
351 |
} |
352 |
} |
353 |
} |
354 |
|
355 |
/**
|
356 |
* @param ind
|
357 |
*/
|
358 |
private void removeItem(int ind) { |
359 |
// model.removeElement(model.get(ind));
|
360 |
this.getItems().removeElement(ind);
|
361 |
} |
362 |
|
363 |
/*
|
364 |
* (non-Javadoc)
|
365 |
*
|
366 |
* @see
|
367 |
*
|
368 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#requestFocus
|
369 |
* ()
|
370 |
*/
|
371 |
public void requestFocus() { |
372 |
this.component.requestFocus();
|
373 |
} |
374 |
|
375 |
/**
|
376 |
*
|
377 |
*/
|
378 |
|
379 |
/*
|
380 |
* (non-Javadoc)
|
381 |
*
|
382 |
* @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#saveStatus()
|
383 |
*/
|
384 |
public void saveStatus() { |
385 |
setFieldValue(getItems()); |
386 |
} |
387 |
|
388 |
/*
|
389 |
* (non-Javadoc)
|
390 |
*
|
391 |
* @see
|
392 |
*
|
393 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setEnabled
|
394 |
* (boolean)
|
395 |
*/
|
396 |
public void setEnabled(boolean isEnabled) { |
397 |
if ((isEnabled) && (this.getDynField().isReadOnly())) { |
398 |
return;
|
399 |
} |
400 |
this.sbutton.setVisible(isEnabled);
|
401 |
this.upperSubPanel.setEnabled(isEnabled);
|
402 |
} |
403 |
|
404 |
@Override
|
405 |
protected void setJDynFieldComponentListeners() { |
406 |
|
407 |
} |
408 |
|
409 |
@Override
|
410 |
protected void setNonNullValue(Object value) { |
411 |
if (value instanceof List) { |
412 |
this.getItems().setValue(value);
|
413 |
} else {
|
414 |
this.list.setSelectedValue(value, true); |
415 |
} |
416 |
} |
417 |
|
418 |
@Override
|
419 |
protected void setNullValue() { |
420 |
setNonNullValue(null);
|
421 |
} |
422 |
|
423 |
/*
|
424 |
* (non-Javadoc)
|
425 |
*
|
426 |
* @see org.gvsig.tools.swing.spi.AbstractJDynField#setReadOnly()
|
427 |
*/
|
428 |
@Override
|
429 |
protected void setReadOnly() { |
430 |
this.setEnabled(false); |
431 |
} |
432 |
|
433 |
private void setValue(Object element, int index) { |
434 |
Object[] values = |
435 |
this.getDynField().getElementsType().getAvailableValues();
|
436 |
NValueField items = getItems(); |
437 |
if (element instanceof Integer) { |
438 |
int ind = (Integer) element; |
439 |
if ((values != null) && (ind > -1)) { |
440 |
items.setValue(index, element, values[(Integer) element]);
|
441 |
} else {
|
442 |
items.setValue(index, element, element); |
443 |
} |
444 |
} else
|
445 |
if (this.getDynField().getElementsType().getType() == DataTypes.BOOLEAN) { |
446 |
if (element == null) { |
447 |
items.setValue(index, element, UNKNOWN); |
448 |
} else {
|
449 |
items.setValue(index, element, element); |
450 |
} |
451 |
} else {
|
452 |
items.setValue(index, element, element); |
453 |
} |
454 |
this.list.setSelectedIndex(index);
|
455 |
} |
456 |
|
457 |
/*
|
458 |
* (non-Javadoc)
|
459 |
*
|
460 |
* @see
|
461 |
* javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event
|
462 |
* .ListSelectionEvent)
|
463 |
*/
|
464 |
public void valueChanged(ListSelectionEvent e) { |
465 |
NValueField items = getItems(); |
466 |
int index = list.getSelectedIndex();
|
467 |
int size = items.getSize();
|
468 |
if (index >= 0) { |
469 |
this.component
|
470 |
.setValue(items.getElementAt(list.getSelectedIndex())); |
471 |
// this.list.setVisibleRowCount(model.getSize());
|
472 |
this.sbutton.setVisible(true); |
473 |
// this.listScrollPane.setVisible(true);
|
474 |
} else {
|
475 |
if (size < 1) { |
476 |
// this.sbutton.setVisible(false);
|
477 |
// this.listScrollPane.setVisible(false);
|
478 |
} else {
|
479 |
index = items.indexOf(items.firstElement()); |
480 |
if (index != this.list.getSelectedIndex()) { |
481 |
this.list.setSelectedIndex(index);
|
482 |
} |
483 |
} |
484 |
} |
485 |
refresh(); |
486 |
this.fireValueChangedEvent();
|
487 |
} |
488 |
} |