Statistics
| Revision:

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 @ 585

History | View | Annotate | Download (10.9 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.GridBagConstraints;
37
import java.awt.GridBagLayout;
38
import java.awt.Insets;
39
import java.awt.event.ActionEvent;
40
import java.awt.event.ActionListener;
41
import java.util.ArrayList;
42
import java.util.Iterator;
43
import java.util.List;
44

    
45
import javax.swing.BorderFactory;
46
import javax.swing.DefaultListModel;
47
import javax.swing.JButton;
48
import javax.swing.JComponent;
49
import javax.swing.JList;
50
import javax.swing.JPanel;
51
import javax.swing.ListSelectionModel;
52
import javax.swing.event.ListSelectionEvent;
53
import javax.swing.event.ListSelectionListener;
54

    
55
import org.gvsig.tools.dataTypes.DataTypes;
56
import org.gvsig.tools.dynobject.DynField;
57
import org.gvsig.tools.swing.api.ToolsSwingLocator;
58
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
59
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
60
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
61

    
62
/**
63
 * @author <a href="mailto:reinhold@uji.es">cmartin</a>
64
 * 
65
 */
66
public class JNDynFieldComponent extends AbstractNValueFieldComponent implements
67
                JDynFieldComponent, ValueChangedListener, ListSelectionListener {
68

    
69
        private JList list;
70

    
71
        private JButton removeButton;
72

    
73
        private JButton addButton;
74

    
75
        private JPanel subPanel;
76

    
77
        /**
78
         * @param writable
79
         * @param definition
80
         * @param serviceManager
81
         * @param dynObject
82
         * @param dynField
83
         */
84
        public JNDynFieldComponent(DynField definition, ValueField valueField,
85
                        boolean writable) {
86
                super(definition, valueField, writable);
87
                this.setEnabled(valueField.getDynField().isReadOnly());
88
                this.getComponent().addValueChangedListener(this);
89
        }
90

    
91
        private int findIndex(Object objectValue) {
92
                Object[] values = this.getDynField().getElementsType()
93
                                .getAvailableValues();
94
                int index = -1;
95
                if (values == null) {
96
                        return -1;
97
                }
98

    
99
                for (Object value : values) {
100
                        index++;
101
                        if (value != null) {
102
                                if (value.equals(objectValue)) {
103
                                        return index;
104
                                }
105
                        }
106
                }
107
                return -1;
108
        }
109

    
110
        /**
111
     * 
112
     */
113
        @Override
114
        protected void afterUI() {
115
                refresh();
116
        }
117

    
118
        private JButton createButton(String text) {
119
                return ToolsSwingLocator.getUsabilitySwingManager().createJToolButton(
120
                                text.toString());
121
        }
122

    
123
        /*
124
         * (non-Javadoc)
125
         * 
126
         * @see
127
         * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
128
         */
129
        @Override
130
        public Object getDefaultValue() {
131
                return this.getDynField().getDefaultValue();
132
        }
133

    
134
        /*
135
         * (non-Javadoc)
136
         * 
137
         * @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#getValue()
138
         */
139
        public Object getValue() {
140
                if (getSize() <= 0) {
141
                        return null;
142
                }
143

    
144
                Object valueItem;
145
                List listItems = new ArrayList();
146
                for (int i = 0; i < this.list.getModel().getSize(); i++) {
147
                        valueItem = this.list.getModel().getElementAt(i);
148
                        listItems.add(valueItem);
149
                }
150
                return listItems;
151
        }
152

    
153
        /*
154
         * (non-Javadoc)
155
         * 
156
         * @see
157
         * 
158
         * org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
159
         * (org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
160
         */
161
        public void handleValueChanged(JDynFieldComponent component) {
162
                if (getComponent().isValid() && getComponent().getValue() != null) {
163
                        int index = this.list.getSelectedIndex();
164
                        if (index >= 0) {
165
                                setListValue(getComponent().getValue(), index);
166
                        }
167
                } else {
168
                        this.list.setSelectedIndex(-1);
169
                }
170
        }
171

    
172
        /**
173
     *  
174
     */
175
        private void initBoxLayout() {
176
                subPanel = new JPanel(new GridBagLayout());
177
                subPanel.setBorder(BorderFactory.createTitledBorder(""));
178
                list.setVisibleRowCount(4);
179
        
180
                list.setBorder(BorderFactory.createTitledBorder(""));
181
                // listScrollPane = new JScrollPane(list);
182
                // listScrollPane.setViewportView(this.list);
183
                // listScrollPane.setLocale(this.list.getLocale());
184

    
185
                // int span = 3;
186

    
187
                GridBagConstraints c = new GridBagConstraints();
188
                c.insets = new Insets(2, 2, 2, 2);
189
                // c.gridx = 0;
190
                // c.gridy = 0;
191
                // c.gridwidth = span;
192
                // c.fill = GridBagConstraints.BOTH;
193
                c.weightx = 1.0;
194
                c.fill = GridBagConstraints.HORIZONTAL;
195
                c.anchor = GridBagConstraints.NORTHWEST;
196
                JComponent comp = getComponent().asJComponent();
197
                subPanel.add(comp, c);
198

    
199
                c = new GridBagConstraints();
200
                c.insets = new Insets(2, 2, 2, 2);
201

    
202
                c.weightx = 0.0;
203
                // c.gridx = span;
204
                // c.gridy = 0;
205
                // c.ipady = 0;
206
                c.fill = GridBagConstraints.HORIZONTAL;
207
                c.anchor = GridBagConstraints.NORTHEAST;
208
                subPanel.add(addButton, c);
209

    
210
                // list of items
211
                c = new GridBagConstraints();
212
                c.insets = new Insets(4, 2, 2, 2);
213
                c.gridx = 0;
214
                c.gridy = 1;
215
                c.weightx = 1.0;
216
                // c.gridwidth = span;
217
                c.fill = GridBagConstraints.BOTH;
218
                c.anchor = GridBagConstraints.NORTHWEST;
219
                subPanel.add(this.list, c);
220

    
221
                // remove Button
222
                c = new GridBagConstraints();
223
                c.insets = new Insets(4, 2, 2, 2);
224
                // c.gridx = span;
225
                c.gridy = 1;
226
                c.ipady = 0;
227
                c.weightx = 0.0;
228
                c.fill = GridBagConstraints.NONE;
229
                c.anchor = GridBagConstraints.NORTHWEST;
230
                subPanel.add(removeButton, c);
231

    
232
        }
233

    
234
        /*
235
         * (non-Javadoc)
236
         * 
237
         * @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
238
         */
239
        public JComponent asJComponent() {
240
                // return this.listScrollPane;
241
                return this.subPanel;
242
        }
243

    
244
        @Override
245
        protected void initData() {
246

    
247
        }
248

    
249
        public void initJButtons() {
250
                removeButton = createButton("-");
251
                removeButton.setActionCommand("-");
252
                removeButton.addActionListener(new ActionListener() {
253

    
254
                        public void actionPerformed(ActionEvent e) {
255
                                removeElement(list.getSelectedIndex());
256
                                refresh();
257

    
258
                                fireValueChangedEvent();
259
                        }
260
                });
261

    
262
                addButton = createButton("+");
263
                addButton.setActionCommand("+");
264
                addButton.addActionListener(new ActionListener() {
265

    
266
                        public void actionPerformed(ActionEvent e) {
267
                                // add at the end of the list
268
                                if (getComponent().isValid()
269
                                                && getComponent().getValue() != null) {
270
                                        addElement(getComponent());
271
                                }
272
                                refresh();
273
                                fireValueChangedEvent();
274
                        }
275
                });
276
        }
277

    
278
        @Override
279
        protected void initUI() {
280
                initJButtons();
281
                this.initComponents();
282
        }
283

    
284
        private void initComponents() {
285
                initJList(this);
286
                initBoxLayout();
287
        }
288

    
289
        public void initJList(ListSelectionListener listener) {
290
                this.list = new JList(getModel());
291
                this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
292
                this.list.addListSelectionListener(listener);
293
                if (getSize() > 0) {
294
                        this.list.setSelectedIndex(0);
295
                }
296
        }
297

    
298
        protected void setSelectedIndex(int index) {
299
                if (index > -2) {
300
                        this.list.setSelectedIndex(index);
301
                }
302
        }
303

    
304
        /*
305
         * (non-Javadoc)
306
         * 
307
         * @see
308
         * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isValid()
309
         */
310
        public boolean isValid() {
311
                if (getSize() > 0) {
312
                        return getComponent().isValid();
313
                }
314
                return !isReadOnly();
315
        }
316

    
317
        public void refresh() {
318
                this.setEnabled(!isEmpty());
319
                // this.fireValueChangedEvent();
320
        }
321

    
322
        protected void removeElement(int ind) {
323

    
324
                super.removeElement(ind);
325

    
326
                if (getSize() < 0) {
327
                        this.getComponent().setValue(null);
328
                }
329
                refresh();
330
        }
331

    
332
        public void requestFocus() {
333
                this.getComponent().requestFocus();
334
        }
335

    
336
        public void saveStatus() {
337
                setFieldValue(getModel());
338
        }
339

    
340
        public void setEnabled(boolean isEnabled) {
341

    
342
                this.removeButton.setEnabled(isEnabled);
343
                if ((getModel().isEmpty()) || (list.getSelectedIndex() < 0)) {
344
                        if (this.getComponent().getDynField().getType() != DataTypes.DYNOBJECT) {
345
                                this.getComponent().requestFocus();
346
                        }
347
                }
348

    
349
                this.list.updateUI();
350
                getComponent().asJComponent().updateUI();
351

    
352
                this.removeButton.setEnabled(isEnabled);
353
                if (isReadOnly()) {
354
                        if (this.getValue() == null) {
355
                                this.addButton.setEnabled(true);
356
                                this.getComponent().setEnabled(true);
357
                                this.removeButton.setEnabled(false);
358
                        } else {
359
                                this.addButton.setEnabled(isEmpty());
360
                                this.getComponent().setEnabled(!isEmpty());
361
                                this.removeButton.setEnabled(true);
362
                        }
363
                }
364

    
365
                this.list.updateUI();
366
                getComponent().asJComponent().updateUI();
367

    
368
        }
369

    
370
        private boolean isEmpty() {
371
                return (getModel().isEmpty() || (list.getSelectedIndex() < 0));
372
        }
373

    
374
        @Override
375
        protected void setJDynFieldComponentListeners() {
376

    
377
        }
378

    
379
        @Override
380
        protected void setNonNullValue(Object value) {
381
                if (this.list == null) {
382
                        initComponents();
383
                }
384

    
385
                if (value instanceof List) {
386
                        // this.getModel().setValue(value);
387
                        Iterator<Object> itemsIter = ((List) value).iterator();
388
                        if (itemsIter.hasNext()) {
389
                                this.getModel().clear();
390
                        }
391
                        while (itemsIter.hasNext()) {
392
                                this.getModel().addElement(itemsIter.next());
393
                        }
394
                } else {
395
                        this.list.setSelectedValue(value, true);
396
                }
397
        }
398

    
399
        @Override
400
        protected void setNullValue() {
401
                setNonNullValue(null);
402
        }
403

    
404
        /*
405
         * (non-Javadoc)
406
         * 
407
         * @see org.gvsig.tools.swing.spi.AbstractJDynField#setReadOnly()
408
         */
409
        @Override
410
        protected void setReadOnly() {
411
                this.setEnabled(false);
412
        }
413

    
414
        private void setListValue(Object element, int index) {
415
                super.setValue(element, index);
416
                refresh();
417
                // this.list.setSelectedIndex(index);
418
        }
419

    
420
        public void valueChanged(ListSelectionEvent e) {
421

    
422
                int index = list.getSelectedIndex();
423
                if (index >= 0) {
424
                        this.getComponent().setValue(
425
                                        getModel().getElementAt(list.getSelectedIndex()));
426
                }else{
427
                        int size = getSize();
428
                        if (size > 0) {
429
                                index = getModel().indexOf(getModel().firstElement());
430
                                if (index != this.list.getSelectedIndex()) {
431
                                        this.list.setSelectedIndex(index);
432
                                }
433
                        }
434
                }
435
                refresh();
436
        }
437

    
438
        public String getValidationMessage() {
439
                return null;
440
        }
441

    
442
        @Override
443
        protected DefaultListModel getModel() {
444
                if (this.list == null) {
445
                        return super.getModel();
446
                }
447
                return (DefaultListModel) this.list.getModel();
448
        }
449

    
450
}