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 @ 647
History | View | Annotate | Download (15.3 KB)
1 | 168 | cmartin | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | 270 | cmartin | *
|
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 | 168 | cmartin | /*
|
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.GridBagConstraints; |
||
37 | import java.awt.GridBagLayout; |
||
38 | import java.awt.Insets; |
||
39 | import java.awt.event.ActionEvent; |
||
40 | import java.awt.event.ActionListener; |
||
41 | 647 | cmartin | import java.awt.event.MouseAdapter; |
42 | import java.awt.event.MouseEvent; |
||
43 | 585 | cmartin | import java.util.ArrayList; |
44 | 494 | cmartin | import java.util.Iterator; |
45 | 168 | cmartin | import java.util.List; |
46 | |||
47 | import javax.swing.BorderFactory; |
||
48 | 536 | cmartin | import javax.swing.DefaultListModel; |
49 | 168 | cmartin | import javax.swing.JButton; |
50 | 230 | cordinyana | import javax.swing.JComponent; |
51 | 168 | cmartin | import javax.swing.JList; |
52 | import javax.swing.JPanel; |
||
53 | import javax.swing.ListSelectionModel; |
||
54 | import javax.swing.event.ListSelectionEvent; |
||
55 | import javax.swing.event.ListSelectionListener; |
||
56 | |||
57 | import org.gvsig.tools.dataTypes.DataTypes; |
||
58 | 536 | cmartin | import org.gvsig.tools.dynobject.DynField; |
59 | 168 | cmartin | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
60 | 202 | cmartin | import org.gvsig.tools.swing.api.dynobject.ValueChangedListener; |
61 | 168 | cmartin | import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent; |
62 | import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField; |
||
63 | |||
64 | /**
|
||
65 | * @author <a href="mailto:reinhold@uji.es">cmartin</a>
|
||
66 | 270 | cmartin | *
|
67 | 168 | cmartin | */
|
68 | 494 | cmartin | public class JNDynFieldComponent extends AbstractNValueFieldComponent implements |
69 | 647 | cmartin | JDynFieldComponent, ValueChangedListener, ListSelectionListener {
|
70 | 168 | cmartin | |
71 | 647 | cmartin | private JList list; |
72 | 168 | cmartin | |
73 | 647 | cmartin | private JButton removeButton; |
74 | 168 | cmartin | |
75 | 647 | cmartin | private JButton addButton; |
76 | 168 | cmartin | |
77 | 647 | cmartin | private JPanel subPanel; |
78 | 168 | cmartin | |
79 | 647 | cmartin | /**
|
80 | * @param writable
|
||
81 | * @param definition
|
||
82 | * @param serviceManager
|
||
83 | * @param dynObject
|
||
84 | * @param dynField
|
||
85 | */
|
||
86 | public JNDynFieldComponent(DynField definition, ValueField valueField,
|
||
87 | boolean writable) {
|
||
88 | super(definition, valueField, writable);
|
||
89 | this.setEnabled(valueField.getDynField().isReadOnly());
|
||
90 | this.getComponent().addValueChangedListener(this); |
||
91 | } |
||
92 | 168 | cmartin | |
93 | 647 | cmartin | private int findIndex(Object objectValue) { |
94 | Object[] values = |
||
95 | this.getDynField().getElementsType().getAvailableValues();
|
||
96 | int index = -1; |
||
97 | if (values == null) { |
||
98 | return -1; |
||
99 | } |
||
100 | 494 | cmartin | |
101 | 647 | cmartin | for (Object value : values) { |
102 | index++; |
||
103 | if (value != null) { |
||
104 | if (value.equals(objectValue)) {
|
||
105 | return index;
|
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | return -1; |
||
110 | } |
||
111 | 494 | cmartin | |
112 | 647 | cmartin | /**
|
113 | 168 | cmartin | *
|
114 | */
|
||
115 | 647 | cmartin | @Override
|
116 | protected void afterUI() { |
||
117 | refresh(); |
||
118 | } |
||
119 | 168 | cmartin | |
120 | 647 | cmartin | private JButton createButton(String text) { |
121 | return ToolsSwingLocator.getUsabilitySwingManager().createJToolButton(
|
||
122 | text.toString()); |
||
123 | } |
||
124 | 298 | cmartin | |
125 | 647 | cmartin | /*
|
126 | * (non-Javadoc)
|
||
127 | *
|
||
128 | * @see
|
||
129 | * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
|
||
130 | */
|
||
131 | @Override
|
||
132 | public Object getDefaultValue() { |
||
133 | return this.getDynField().getDefaultValue(); |
||
134 | } |
||
135 | 168 | cmartin | |
136 | 647 | cmartin | /*
|
137 | * (non-Javadoc)
|
||
138 | *
|
||
139 | * @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#getValue()
|
||
140 | */
|
||
141 | public Object getValue() { |
||
142 | if (getSize() <= 0) { |
||
143 | return null; |
||
144 | } |
||
145 | 585 | cmartin | |
146 | 647 | cmartin | Object valueItem;
|
147 | List listItems = new ArrayList(); |
||
148 | for (int i = 0; i < this.list.getModel().getSize(); i++) { |
||
149 | valueItem = this.list.getModel().getElementAt(i);
|
||
150 | listItems.add(valueItem); |
||
151 | } |
||
152 | return listItems;
|
||
153 | } |
||
154 | 168 | cmartin | |
155 | 647 | cmartin | /*
|
156 | * (non-Javadoc)
|
||
157 | *
|
||
158 | * @see
|
||
159 | *
|
||
160 | * org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
|
||
161 | * (org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
|
||
162 | */
|
||
163 | public void handleValueChanged(JDynFieldComponent component) { |
||
164 | if (getComponent().isValid() && getComponent().getValue() != null) { |
||
165 | int index = this.list.getSelectedIndex(); |
||
166 | if (index >= 0) { |
||
167 | setListValue(getComponent().getValue(), index); |
||
168 | } |
||
169 | } else {
|
||
170 | this.list.setSelectedIndex(-1); |
||
171 | } |
||
172 | } |
||
173 | 298 | cmartin | |
174 | 647 | cmartin | /**
|
175 | 168 | cmartin | *
|
176 | */
|
||
177 | 647 | cmartin | private void initBoxLayout() { |
178 | subPanel = new JPanel(new GridBagLayout()); |
||
179 | subPanel.setBorder(BorderFactory.createTitledBorder("")); |
||
180 | list.setVisibleRowCount(4);
|
||
181 | list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
|
||
182 | list.setBorder(BorderFactory.createTitledBorder("")); |
||
183 | list.addMouseListener(new MouseAdapter() { |
||
184 | 168 | cmartin | |
185 | 647 | cmartin | int lastSelectedIndex;
|
186 | 270 | cmartin | |
187 | 647 | cmartin | public void mouseClicked(MouseEvent e) { |
188 | 536 | cmartin | |
189 | 647 | cmartin | int index = list.locationToIndex(e.getPoint());
|
190 | 270 | cmartin | |
191 | 647 | cmartin | if (index != -1 && index == lastSelectedIndex) { |
192 | list.clearSelection(); |
||
193 | getComponent().asJComponent().requestFocus(); |
||
194 | } |
||
195 | 536 | cmartin | |
196 | 647 | cmartin | lastSelectedIndex = list.getSelectedIndex(); |
197 | } |
||
198 | }); |
||
199 | // list.addMouseListener(new MouseListener() {
|
||
200 | //
|
||
201 | // private int previousIndex = -1;
|
||
202 | //
|
||
203 | // public void mouseClicked(MouseEvent arg0) {
|
||
204 | // if (list.getSelectedIndex() != previousIndex) {
|
||
205 | // previousIndex = list.getSelectedIndex();
|
||
206 | // } else {
|
||
207 | // setSelectedIndex(-2);
|
||
208 | // }
|
||
209 | // list.updateUI();
|
||
210 | // }
|
||
211 | //
|
||
212 | // public void mouseEntered(MouseEvent arg0) {
|
||
213 | // // TODO Auto-generated method stub
|
||
214 | //
|
||
215 | // }
|
||
216 | //
|
||
217 | // public void mouseExited(MouseEvent arg0) {
|
||
218 | // // TODO Auto-generated method stub
|
||
219 | //
|
||
220 | // }
|
||
221 | //
|
||
222 | // public void mousePressed(MouseEvent arg0) {
|
||
223 | // // TODO Auto-generated method stub
|
||
224 | //
|
||
225 | // }
|
||
226 | //
|
||
227 | // public void mouseReleased(MouseEvent arg0) {
|
||
228 | // // TODO Auto-generated method stub
|
||
229 | //
|
||
230 | // }
|
||
231 | //
|
||
232 | // });
|
||
233 | // list.addListSelectionListener(new ListSelectionListener() {
|
||
234 | //
|
||
235 | // private int previousIndex = -1;
|
||
236 | //
|
||
237 | // public void valueChanged(ListSelectionEvent event) {
|
||
238 | // if (list.getSelectedIndex() != previousIndex) {
|
||
239 | // previousIndex = list.getSelectedIndex();
|
||
240 | // } else {
|
||
241 | // list.setSelectedIndex(-1);
|
||
242 | // }
|
||
243 | // }
|
||
244 | //
|
||
245 | // });
|
||
246 | 270 | cmartin | |
247 | 647 | cmartin | // listScrollPane = new JScrollPane(list);
|
248 | // listScrollPane.setViewportView(this.list);
|
||
249 | // listScrollPane.setLocale(this.list.getLocale());
|
||
250 | 270 | cmartin | |
251 | 647 | cmartin | // int span = 3;
|
252 | 168 | cmartin | |
253 | 647 | cmartin | GridBagConstraints c = new GridBagConstraints(); |
254 | c.insets = new Insets(2, 2, 2, 2); |
||
255 | // c.gridx = 0;
|
||
256 | // c.gridy = 0;
|
||
257 | // c.gridwidth = span;
|
||
258 | // c.fill = GridBagConstraints.BOTH;
|
||
259 | c.weightx = 1.0;
|
||
260 | c.fill = GridBagConstraints.HORIZONTAL;
|
||
261 | c.anchor = GridBagConstraints.NORTHWEST;
|
||
262 | JComponent comp = getComponent().asJComponent();
|
||
263 | subPanel.add(comp, c); |
||
264 | 298 | cmartin | |
265 | 647 | cmartin | c = new GridBagConstraints(); |
266 | c.insets = new Insets(2, 2, 2, 2); |
||
267 | 168 | cmartin | |
268 | 647 | cmartin | c.weightx = 0.0;
|
269 | // c.gridx = span;
|
||
270 | // c.gridy = 0;
|
||
271 | // c.ipady = 0;
|
||
272 | c.fill = GridBagConstraints.HORIZONTAL;
|
||
273 | c.anchor = GridBagConstraints.NORTHEAST;
|
||
274 | subPanel.add(addButton, c); |
||
275 | 168 | cmartin | |
276 | 647 | cmartin | // list of items
|
277 | c = new GridBagConstraints(); |
||
278 | c.insets = new Insets(4, 2, 2, 2); |
||
279 | c.gridx = 0;
|
||
280 | c.gridy = 1;
|
||
281 | c.weightx = 1.0;
|
||
282 | // c.gridwidth = span;
|
||
283 | c.fill = GridBagConstraints.BOTH;
|
||
284 | c.anchor = GridBagConstraints.NORTHWEST;
|
||
285 | subPanel.add(this.list, c);
|
||
286 | 270 | cmartin | |
287 | 647 | cmartin | // remove Button
|
288 | c = new GridBagConstraints(); |
||
289 | c.insets = new Insets(4, 2, 2, 2); |
||
290 | // c.gridx = span;
|
||
291 | c.gridy = 1;
|
||
292 | c.ipady = 0;
|
||
293 | c.weightx = 0.0;
|
||
294 | c.fill = GridBagConstraints.NONE;
|
||
295 | c.anchor = GridBagConstraints.NORTHWEST;
|
||
296 | subPanel.add(removeButton, c); |
||
297 | 585 | cmartin | |
298 | 647 | cmartin | } |
299 | 270 | cmartin | |
300 | 647 | cmartin | /*
|
301 | * (non-Javadoc)
|
||
302 | *
|
||
303 | * @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
|
||
304 | */
|
||
305 | public JComponent asJComponent() { |
||
306 | // return this.listScrollPane;
|
||
307 | return this.subPanel; |
||
308 | } |
||
309 | 494 | cmartin | |
310 | 647 | cmartin | @Override
|
311 | protected void initData() { |
||
312 | 494 | cmartin | |
313 | 647 | cmartin | } |
314 | 168 | cmartin | |
315 | 647 | cmartin | public void initJButtons() { |
316 | removeButton = createButton("-");
|
||
317 | removeButton.setActionCommand("-");
|
||
318 | removeButton.addActionListener(new ActionListener() { |
||
319 | 270 | cmartin | |
320 | 647 | cmartin | public void actionPerformed(ActionEvent e) { |
321 | removeElement(list.getSelectedIndex()); |
||
322 | refresh(); |
||
323 | 270 | cmartin | |
324 | 647 | cmartin | fireValueChangedEvent(); |
325 | } |
||
326 | }); |
||
327 | 168 | cmartin | |
328 | 647 | cmartin | addButton = createButton("+");
|
329 | addButton.setActionCommand("+");
|
||
330 | addButton.addActionListener(new ActionListener() { |
||
331 | 270 | cmartin | |
332 | 647 | cmartin | public void actionPerformed(ActionEvent e) { |
333 | // add at the end of the list
|
||
334 | if (getComponent().isValid()
|
||
335 | && getComponent().getValue() != null) {
|
||
336 | addElement(list, getComponent()); |
||
337 | refresh(); |
||
338 | fireValueChangedEvent(); |
||
339 | } |
||
340 | } |
||
341 | }); |
||
342 | } |
||
343 | 168 | cmartin | |
344 | 647 | cmartin | @Override
|
345 | protected void initUI() { |
||
346 | initJButtons(); |
||
347 | this.initComponents();
|
||
348 | } |
||
349 | 168 | cmartin | |
350 | 647 | cmartin | private void initComponents() { |
351 | initJList(this);
|
||
352 | initBoxLayout(); |
||
353 | } |
||
354 | 168 | cmartin | |
355 | 647 | cmartin | public void initJList(ListSelectionListener listener) { |
356 | this.list = new JList(getModel()); |
||
357 | this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
||
358 | // this.list.addListSelectionListener(listener);
|
||
359 | // if (getSize() > 0) {
|
||
360 | // this.list.setSelectedIndex(0);
|
||
361 | // }
|
||
362 | } |
||
363 | 168 | cmartin | |
364 | 647 | cmartin | protected void setSelectedIndex(int index) { |
365 | if (index > -2) { |
||
366 | this.list.setSelectedIndex(index);
|
||
367 | } |
||
368 | } |
||
369 | 494 | cmartin | |
370 | 647 | cmartin | /*
|
371 | * (non-Javadoc)
|
||
372 | *
|
||
373 | * @see
|
||
374 | * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isValid()
|
||
375 | */
|
||
376 | public boolean isValid() { |
||
377 | if (getSize() > 0) { |
||
378 | return getComponent().isValid();
|
||
379 | } |
||
380 | return !this.isMandatory(); |
||
381 | } |
||
382 | 561 | cmartin | |
383 | 647 | cmartin | public void refresh() { |
384 | this.setEnabled(!isEmpty());
|
||
385 | // this.fireValueChangedEvent();
|
||
386 | } |
||
387 | 561 | cmartin | |
388 | 647 | cmartin | protected void removeElement(int ind) { |
389 | 270 | cmartin | |
390 | 647 | cmartin | super.removeSelectedElement(this.list, this.removeButton); |
391 | 168 | cmartin | |
392 | 647 | cmartin | if (getSize() < 0) { |
393 | this.getComponent().setValue(null); |
||
394 | } |
||
395 | refresh(); |
||
396 | } |
||
397 | 494 | cmartin | |
398 | 647 | cmartin | public void requestFocus() { |
399 | this.getComponent().requestFocus();
|
||
400 | } |
||
401 | 298 | cmartin | |
402 | 647 | cmartin | public void saveStatus() { |
403 | setFieldValue(getModel()); |
||
404 | } |
||
405 | 298 | cmartin | |
406 | 647 | cmartin | public void setEnabled(boolean isEnabled) { |
407 | 168 | cmartin | |
408 | 647 | cmartin | this.addButton.setEnabled(true); |
409 | if ((getModel().isEmpty()) || (list.getSelectedIndex() < 0)) { |
||
410 | if (this.getComponent().getDynField().getType() != DataTypes.DYNOBJECT) { |
||
411 | this.getComponent().requestFocus();
|
||
412 | } |
||
413 | this.removeButton.setEnabled(false); |
||
414 | this.addButton.setEnabled(true); |
||
415 | } else {
|
||
416 | if (this.list.getSelectedIndex() < 0) { |
||
417 | this.removeButton.setEnabled(false); |
||
418 | } else {
|
||
419 | this.removeButton.setEnabled(true); |
||
420 | } |
||
421 | } |
||
422 | 298 | cmartin | |
423 | 647 | cmartin | if (isReadOnly()) {
|
424 | this.addButton.setEnabled(false); |
||
425 | this.getComponent().setEnabled(false); |
||
426 | this.removeButton.setEnabled(false); |
||
427 | } |
||
428 | // // this.list.updateUI();
|
||
429 | // // getComponent().asJComponent().updateUI();
|
||
430 | //
|
||
431 | // // this.removeButton.setEnabled(isEnabled);
|
||
432 | // if (isReadOnly()) {
|
||
433 | // if (this.getValue() == null) {
|
||
434 | // this.addButton.setEnabled(true);
|
||
435 | // this.getComponent().setEnabled(true);
|
||
436 | // this.removeButton.setEnabled(false);
|
||
437 | // } else {
|
||
438 | // this.addButton.setEnabled(isEmpty());
|
||
439 | // this.getComponent().setEnabled(true);
|
||
440 | // this.removeButton.setEnabled(true);
|
||
441 | // }
|
||
442 | // }
|
||
443 | 494 | cmartin | |
444 | 647 | cmartin | // this.list.updateUI();
|
445 | // getComponent().asJComponent().updateUI();
|
||
446 | 536 | cmartin | |
447 | 647 | cmartin | } |
448 | 494 | cmartin | |
449 | 647 | cmartin | private boolean isEmpty() { |
450 | // return (getModel().isEmpty() || (list.getSelectedIndex() < 0));
|
||
451 | return getModel().isEmpty();
|
||
452 | } |
||
453 | 536 | cmartin | |
454 | 647 | cmartin | @Override
|
455 | protected void setJDynFieldComponentListeners() { |
||
456 | 561 | cmartin | |
457 | 647 | cmartin | } |
458 | 561 | cmartin | |
459 | 647 | cmartin | @Override
|
460 | protected void setNonNullValue(Object value) { |
||
461 | if (this.list == null) { |
||
462 | initComponents(); |
||
463 | } |
||
464 | 561 | cmartin | |
465 | 647 | cmartin | if (value instanceof List) { |
466 | // this.getModel().setValue(value);
|
||
467 | Iterator<Object> itemsIter = ((List) value).iterator(); |
||
468 | if (itemsIter.hasNext()) {
|
||
469 | this.getModel().clear();
|
||
470 | } |
||
471 | while (itemsIter.hasNext()) {
|
||
472 | this.getModel().addElement(itemsIter.next());
|
||
473 | } |
||
474 | } else {
|
||
475 | this.list.setSelectedValue(value, true); |
||
476 | } |
||
477 | } |
||
478 | 561 | cmartin | |
479 | 647 | cmartin | @Override
|
480 | protected void setNullValue() { |
||
481 | setNonNullValue(null);
|
||
482 | } |
||
483 | 561 | cmartin | |
484 | 647 | cmartin | /*
|
485 | * (non-Javadoc)
|
||
486 | *
|
||
487 | * @see org.gvsig.tools.swing.spi.AbstractJDynField#setReadOnly()
|
||
488 | */
|
||
489 | @Override
|
||
490 | protected void setReadOnly() { |
||
491 | this.setEnabled(false); |
||
492 | } |
||
493 | 561 | cmartin | |
494 | 647 | cmartin | private void setListValue(Object element, int index) { |
495 | super.setValue(element, index);
|
||
496 | refresh(); |
||
497 | // this.list.setSelectedIndex(index);
|
||
498 | } |
||
499 | |||
500 | public void valueChanged(ListSelectionEvent e) { |
||
501 | |||
502 | int index = list.getSelectedIndex();
|
||
503 | if (index >= 0) { |
||
504 | this.getComponent().setValue(
|
||
505 | getModel().getElementAt(list.getSelectedIndex())); |
||
506 | } else {
|
||
507 | int size = getSize();
|
||
508 | if (size > 0) { |
||
509 | index = getModel().indexOf(getModel().firstElement()); |
||
510 | if (index != this.list.getSelectedIndex()) { |
||
511 | this.list.setSelectedIndex(index);
|
||
512 | } |
||
513 | } |
||
514 | } |
||
515 | refresh(); |
||
516 | } |
||
517 | |||
518 | public String getValidationMessage() { |
||
519 | return null; |
||
520 | } |
||
521 | |||
522 | @Override
|
||
523 | protected DefaultListModel getModel() { |
||
524 | if (this.list == null) { |
||
525 | return super.getModel(); |
||
526 | } |
||
527 | return (DefaultListModel) this.list.getModel(); |
||
528 | } |
||
529 | |||
530 | 168 | cmartin | } |