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

History | View | Annotate | Download (15.3 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.awt.event.MouseAdapter;
42
import java.awt.event.MouseEvent;
43
import java.util.ArrayList;
44
import java.util.Iterator;
45
import java.util.List;
46

    
47
import javax.swing.BorderFactory;
48
import javax.swing.DefaultListModel;
49
import javax.swing.JButton;
50
import javax.swing.JComponent;
51
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
import org.gvsig.tools.dynobject.DynField;
59
import org.gvsig.tools.swing.api.ToolsSwingLocator;
60
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
61
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
 * 
67
 */
68
public class JNDynFieldComponent extends AbstractNValueFieldComponent implements
69
    JDynFieldComponent, ValueChangedListener, ListSelectionListener {
70

    
71
    private JList list;
72

    
73
    private JButton removeButton;
74

    
75
    private JButton addButton;
76

    
77
    private JPanel subPanel;
78

    
79
    /**
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

    
93
    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

    
101
        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

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

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

    
125
    /*
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

    
136
    /*
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

    
146
        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

    
155
    /*
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

    
174
    /**
175
     *  
176
     */
177
    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

    
185
            int lastSelectedIndex;
186

    
187
            public void mouseClicked(MouseEvent e) {
188

    
189
                int index = list.locationToIndex(e.getPoint());
190

    
191
                if (index != -1 && index == lastSelectedIndex) {
192
                    list.clearSelection();
193
                    getComponent().asJComponent().requestFocus();
194
                }
195

    
196
                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

    
247
        // listScrollPane = new JScrollPane(list);
248
        // listScrollPane.setViewportView(this.list);
249
        // listScrollPane.setLocale(this.list.getLocale());
250

    
251
        // int span = 3;
252

    
253
        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

    
265
        c = new GridBagConstraints();
266
        c.insets = new Insets(2, 2, 2, 2);
267

    
268
        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

    
276
        // 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

    
287
        // 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

    
298
    }
299

    
300
    /*
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

    
310
    @Override
311
    protected void initData() {
312

    
313
    }
314

    
315
    public void initJButtons() {
316
        removeButton = createButton("-");
317
        removeButton.setActionCommand("-");
318
        removeButton.addActionListener(new ActionListener() {
319

    
320
            public void actionPerformed(ActionEvent e) {
321
                removeElement(list.getSelectedIndex());
322
                refresh();
323

    
324
                fireValueChangedEvent();
325
            }
326
        });
327

    
328
        addButton = createButton("+");
329
        addButton.setActionCommand("+");
330
        addButton.addActionListener(new ActionListener() {
331

    
332
            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

    
344
    @Override
345
    protected void initUI() {
346
        initJButtons();
347
        this.initComponents();
348
    }
349

    
350
    private void initComponents() {
351
        initJList(this);
352
        initBoxLayout();
353
    }
354

    
355
    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

    
364
    protected void setSelectedIndex(int index) {
365
        if (index > -2) {
366
            this.list.setSelectedIndex(index);
367
        }
368
    }
369

    
370
    /*
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

    
383
    public void refresh() {
384
        this.setEnabled(!isEmpty());
385
        // this.fireValueChangedEvent();
386
    }
387

    
388
    protected void removeElement(int ind) {
389

    
390
        super.removeSelectedElement(this.list, this.removeButton);
391

    
392
        if (getSize() < 0) {
393
            this.getComponent().setValue(null);
394
        }
395
        refresh();
396
    }
397

    
398
    public void requestFocus() {
399
        this.getComponent().requestFocus();
400
    }
401

    
402
    public void saveStatus() {
403
        setFieldValue(getModel());
404
    }
405

    
406
    public void setEnabled(boolean isEnabled) {
407

    
408
        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

    
423
        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

    
444
        // this.list.updateUI();
445
        // getComponent().asJComponent().updateUI();
446

    
447
    }
448

    
449
    private boolean isEmpty() {
450
        // return (getModel().isEmpty() || (list.getSelectedIndex() < 0));
451
        return getModel().isEmpty();
452
    }
453

    
454
    @Override
455
    protected void setJDynFieldComponentListeners() {
456

    
457
    }
458

    
459
    @Override
460
    protected void setNonNullValue(Object value) {
461
        if (this.list == null) {
462
            initComponents();
463
        }
464

    
465
        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

    
479
    @Override
480
    protected void setNullValue() {
481
        setNonNullValue(null);
482
    }
483

    
484
    /*
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

    
494
    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
}