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

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

    
45
import javax.swing.BorderFactory;
46
import javax.swing.JButton;
47
import javax.swing.JComponent;
48
import javax.swing.JList;
49
import javax.swing.JPanel;
50
import javax.swing.JScrollPane;
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.service.ServiceException;
58
import org.gvsig.tools.swing.api.ToolsSwingLocator;
59
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
60
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
61
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
62
import org.gvsig.tools.swing.spi.DelegatedJFieldComponent;
63

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

    
71
    private static final int MINIMUM_LIST_SIZE = 5;
72

    
73
    private static final Object UNKNOWN = "unknown";
74

    
75
    private JList list;
76

    
77
    private JButton sbutton;
78

    
79
    private JButton jbutton;
80

    
81
    // private DefaultListModel model;
82

    
83
    private JPanel panel;
84

    
85
    private JScrollPane listScrollPane;
86

    
87
    private NValueField items;
88

    
89
    private JPanel upperSubPanel;
90

    
91
    // private DynField defaultField;
92

    
93
    /**
94
     * @param serviceManager
95
     * @param dynObject
96
     * @param dynField
97
     */
98
    public JNDynFieldComponent(ValueField parent, DynField dynField) {
99
        super(parent, dynField);
100
        this.addValueChangedListener(this);
101
    }
102

    
103
    private void addElement(Object element) {
104
        Object[] values =
105
            this.getDynField().getElementsType().getAvailableValues();
106
        if (values != null)
107
            items.addElement(element, values[(Integer) element]);
108
        else
109
            if (this.getDynField().getElementsType().getType() == DataTypes.BOOLEAN)
110
                if (element == null)
111
                    items.addElement(element, UNKNOWN);
112
                else
113
                    items.addElement(element, element);
114
            else
115
                items.addElement(element, element);
116
        this.list.setSelectedIndex(items.getSize() - 1);
117
    }
118

    
119
    private void setValue(Object element, int index) {
120
        Object[] values =
121
            this.getDynField().getElementsType().getAvailableValues();
122
        if (element instanceof Integer) {
123
            int ind = (Integer) element;
124
            if ((values != null) && (ind > -1))
125
                items.setValue(index, element, values[(Integer) element]);
126
            else
127
                items.setValue(index, element, element);
128
        } else
129
            if (this.getDynField().getElementsType().getType() == DataTypes.BOOLEAN)
130
                if (element == null)
131
                    items.setValue(index, element, UNKNOWN);
132
                else
133
                    items.setValue(index, element, element);
134
            else
135
                items.setValue(index, element, element);
136
        this.list.setSelectedIndex(index);
137
    }
138

    
139
    // private void addElement(int index, Object item, boolean
140
    // addItemToInnerList) {
141
    // addItem(index,item);
142
    // // if (addItemToInnerList)
143
    // // this.items.add(index,item);
144
    //        
145
    // if (index>=0)
146
    // this.list.setSelectedIndex(index);
147
    // }
148

    
149
    // /**
150
    // * @param parent
151
    // * @param dynField
152
    // * @return
153
    // */
154
    // private static JDynFieldComponent createComponent(ValueField parent,
155
    // DynField dynField) {
156
    // ToolsSwingLocator.getDynObjectSwingManager()
157
    // .createJDynFieldComponent(this, dynField.getElementsType());
158
    //
159
    // }
160

    
161
    // /**
162
    // * @param object
163
    // * @param object2
164
    // */
165
    // private void addItem(Integer index, Object value) {
166
    // if (value==null) return;
167
    // if (index!=null){
168
    // this.items.add(index, value);
169
    // }else
170
    // this.items.add(value);
171
    // // this.items.setValue(value);
172
    // }
173

    
174
    /**
175
     * 
176
     */
177
    protected void afterUI() {
178
        //
179
        // Object valueItems = getFieldValue();
180
        // if (valueItems instanceof List) {
181
        // populateItems((List) valueItems);
182
        // }
183
        refresh();
184
    }
185

    
186
    private JButton createButton(String text) {
187
        return ToolsSwingLocator.getUsabilitySwingManager().createJToolButton(
188
            text.toString());
189
    }
190

    
191
    /*
192
     * (non-Javadoc)
193
     * 
194
     * @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
195
     */
196
    public JComponent asJComponent() {
197
        return this.panel;
198
    }
199

    
200
    /*
201
     * (non-Javadoc)
202
     * 
203
     * @see
204
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getDefaultValue()
205
     */
206
    public Object getDefaultValue() {
207
        return this.getDynField().getDefaultValue();
208
    }
209

    
210
    /*
211
     * (non-Javadoc)
212
     * 
213
     * @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#getFieldValue()
214
     */
215
    @Override
216
    public Object getFieldValue() {
217
        Object value = getParentValueField().getValue();
218
        if (value == null)
219
            value = getParentValueField().getDefaultValue();
220
        return value;
221
        // Object value = this..getValue();
222
        // if (value==null)
223
        // value = parentValues.getDefaultValue();
224
        // return value;
225
    }
226

    
227
    /*
228
     * (non-Javadoc)
229
     * 
230
     * @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#getValue()
231
     */
232
    @Override
233
    public Object getValue() {
234
        if (this.list == null)
235
            return null;
236
        if (this.getDynField().getType() == DataTypes.STRING)
237
            return this.list.getSelectedValue();
238
        return this.list.getSelectedIndex();
239
    }
240

    
241
    /**
242
     *  
243
     */
244
    private void initBoxLayout() {
245
        panel = new JPanel(new BorderLayout(2, 2));
246
        panel.setBorder(BorderFactory.createTitledBorder(""));
247

    
248
        JPanel subPanel = new JPanel(new GridBagLayout());
249
        // subPanel.setBorder(BorderFactory.createTitledBorder(""));
250

    
251
        list.setVisibleRowCount(4);
252
        // list.setLayout(new GridBagLayout());
253
        GridBagConstraints c = new GridBagConstraints();
254
        // c.insets = new Insets(2, 2, 2, 2);
255
        // c.anchor = GridBagConstraints.PAGE_START;
256
        // c.ipadx = 0;
257
        // c.fill = GridBagConstraints.HORIZONTAL;
258

    
259
        listScrollPane = new JScrollPane(list);
260
        listScrollPane.setViewportView(this.list);
261

    
262
        // Adding elements
263

    
264
        c.insets = new Insets(2, 2, 2, 2);
265
        c.anchor = GridBagConstraints.LINE_START;
266
        c.ipadx = 0;
267
        c.weightx = 0.75;
268
        c.fill = GridBagConstraints.HORIZONTAL;
269

    
270
        c.gridx = 0;
271
        c.gridy = 0;
272
        subPanel.add((Component) component.asJComponent(), c);
273

    
274
        // c.insets = new Insets(2, 2, 2, 2);
275
        c.gridx = 1;
276
        c.gridy = 0;
277
        c.weightx = 0.0; // 0
278
        c.fill = GridBagConstraints.NONE;
279
        c.anchor = GridBagConstraints.LINE_END;
280
        subPanel.add(jbutton, c);
281

    
282
        panel.add(subPanel, BorderLayout.NORTH);
283

    
284
        this.upperSubPanel = panel;
285

    
286
        subPanel = new JPanel(new GridBagLayout());
287

    
288
        c.insets = new Insets(2, 2, 2, 2);
289
        c.anchor = GridBagConstraints.LINE_START;
290
        c.ipadx = 0;
291
        c.weightx = 0.75;
292
        c.ipady = 40;
293
        c.fill = GridBagConstraints.HORIZONTAL;
294

    
295
        c.gridx = 0;
296
        c.gridy = 0;
297
        subPanel.add(this.listScrollPane, c);
298

    
299
        c.insets = new Insets(4, 2, 2, 2);
300
        c.gridx = 1;
301
        c.gridy = 0;
302
        c.ipady = 0;
303
        c.weightx = 0.0; // 0
304
        c.fill = GridBagConstraints.RELATIVE;
305
        c.anchor = GridBagConstraints.PAGE_START;
306
        subPanel.add(sbutton, c);
307

    
308
        panel.add(subPanel);
309
        // c.fill = GridBagConstraints.HORIZONTAL;
310
        // // c.anchor = GridBagConstraints.LAST_LINE_END;
311
        // // c.gridx = 7; // Cell start at (2,0)
312
        // // c.gridy = 5;
313
        // // c.gridheight = 0; // span of 2
314
        // // c.gridwidth = 0; // no span
315
        // //// c.gridheight = 0; // no span
316
        // //// c.weightx = 0.5; //
317
        // panel.add(sbutton,c);
318

    
319
        // panel.add((Component) component.getComponent(),
320
        // BorderLayout.PAGE_START);
321

    
322
        // c.fill = GridBagConstraints.HORIZONTAL;
323
        // c.ipady = 2; //reset to default
324
        // c.weighty = 1.0; //request any extra vertical space
325
        // c.anchor = GridBagConstraints.PAGE_END; //bottom of space
326
        // c.insets = new Insets(10,0,0,0); //top padding
327
        // c.gridx = 1; //aligned with button 2
328
        // c.gridwidth = 2; //2 columns wide
329
        // c.gridy = 2; //third row
330
        // pane.add(button, c);
331
        //        
332
        // JPanel pane = createGridBagLayout((Component)
333
        // component.getComponent(),jbutton, 1,1,null);
334
    }
335

    
336
    protected void initData() {
337
        // Creates a list that allows adds and removes
338
        this.items = getValueField();
339
    }
340

    
341
    public void initJButtons() {
342
        sbutton = createButton("-");
343
        sbutton.setActionCommand("-");
344
        sbutton.addActionListener(new ActionListener() {
345

    
346
            public void actionPerformed(ActionEvent e) {
347
                removeElement(list.getSelectedIndex());
348
                refresh();
349
            }
350
        });
351

    
352
        jbutton = createButton("+");
353
        jbutton.setActionCommand("+");
354
        jbutton.addActionListener(new ActionListener() {
355

    
356
            public void actionPerformed(ActionEvent e) {
357
                // add at the end of the list
358
                if (component.isValid()) {
359
                    addElement(component.getValue());
360
                    refresh();
361
                }
362
            }
363
        });
364
    }
365

    
366
    /**
367
     * 
368
     */
369
    private void initJList() {
370
        this.list = new JList(this.items);
371
        this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
372
        this.list.addListSelectionListener(this);
373
        if (this.items.getSize() > 0)
374
            this.list.setSelectedIndex(0);
375
    }
376

    
377
    /**
378
     * @throws ServiceException
379
     * 
380
     */
381
    protected void initUI() {
382
        initJList();
383
        initJButtons();
384
        initBoxLayout();
385
        // initListPanel();
386
        refresh();
387
    }
388

    
389
    /*
390
     * (non-Javadoc)
391
     * 
392
     * @see
393
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isValid()
394
     */
395
    public boolean isValid() {
396
        return this.component.isValid();
397
    }
398

    
399
    /**
400
     * 
401
     */
402
    private void populateItems(List items) {
403
        if (items == null)
404
            return;
405
        else
406
            this.items.setValue(items);
407
        // if (!items.isEmpty())
408
        // this.list.setSelectedIndex(0);
409
    }
410

    
411
    public void refresh() {
412
        if ((items.isEmpty()) || (list.getSelectedIndex() < 0)) {
413
            this.sbutton.setEnabled(false);
414
            this.component.requestFocus();
415
        } else
416
            this.sbutton.setEnabled(true);
417
    }
418

    
419
    private void removeElement(int ind) {
420

    
421
        if ((ind >= 0) && (ind < this.items.getSize()))
422
            removeItem(ind);
423

    
424
        if (items.getSize() < 0)
425
            sbutton.setEnabled(false);
426
        else {
427
            if (ind > 0)
428
                this.list.setSelectedIndex(ind - 1);
429
        }
430
        resize();
431
    }
432

    
433
    /**
434
     * @param ind
435
     */
436
    private void removeItem(int ind) {
437
        // model.removeElement(model.get(ind));
438
        this.items.removeElement(ind);
439
    }
440

    
441
    /*
442
     * (non-Javadoc)
443
     * 
444
     * @see
445
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#requestFocus
446
     * ()
447
     */
448
    public void requestFocus() {
449
        this.component.requestFocus();
450
    }
451

    
452
    /**
453
     * 
454
     */
455
    private void resize() {
456

    
457
        // int list_height = this.list.getSize().height;
458
        // int scrollpane_width = this.listScrollPane.getSize().width;
459
        // int scrollpane_height = this.listScrollPane.getSize().height;
460
        // int min_height = this.listScrollPane.getMinimumSize().height;
461
        //
462
        // if (scrollpane_height!=list_height)
463
        // if (list_height < min_height)
464
        // this.listScrollPane.setSize(scrollpane_width,min_height);
465
        // else
466
        // this.listScrollPane.setSize(scrollpane_width,list_height);
467
        // this.listScrollPane.revalidate();
468
        // this.panel.revalidate();
469
    }
470

    
471
    /*
472
     * (non-Javadoc)
473
     * 
474
     * @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#saveStatus()
475
     */
476
    public void saveStatus() {
477
        // for (int i=0;i<this.model.size();i++){
478
        // this.items.addItem(i,this.model.get(i));
479
        // }
480
        setFieldValue(this.items);
481
    }
482

    
483
    /*
484
     * (non-Javadoc)
485
     * 
486
     * @see
487
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setEnabled
488
     * (boolean)
489
     */
490
    public void setEnabled(boolean isEnabled) {
491
        if ((isEnabled) && (this.getDynField().isReadOnly()))
492
            return;
493
        this.sbutton.setVisible(isEnabled);
494
        this.upperSubPanel.setEnabled(isEnabled);
495
    }
496

    
497
    /*
498
     * (non-Javadoc)
499
     * 
500
     * @see
501
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setFieldValue
502
     * (java.lang.Object)
503
     */
504
    public void setFieldValue(Object value) {
505
        this.getParentValueField().setValue(value);
506
    }
507

    
508
    /*
509
     * (non-Javadoc)
510
     * 
511
     * @see org.gvsig.tools.swing.spi.AbstractJDynField#setReadOnly()
512
     */
513
    @Override
514
    protected void setReadOnly() {
515
        this.setEnabled(false);
516
    }
517

    
518
    /*
519
     * (non-Javadoc)
520
     * 
521
     * @see
522
     * org.gvsig.tools.swing.spi.DelegatedJFieldComponent#setValue(java.lang
523
     * .Object)
524
     */
525
    @Override
526
    public void setValue(Object value) {
527
        if (value instanceof List) {
528
            this.items.setValue((List) value);
529
        } else {
530
            this.list.setSelectedValue(value, true);
531
        }
532
        this.fireValueChangedEvent();
533
    }
534

    
535
    /*
536
     * (non-Javadoc)
537
     * 
538
     * @see
539
     * javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event
540
     * .ListSelectionEvent)
541
     */
542
    public void valueChanged(ListSelectionEvent e) {
543
        if (list.getSelectedIndex() >= 0) {
544
            this.component.setValue(this.items.getElementAt(list
545
                .getSelectedIndex()));
546
            // this.list.setVisibleRowCount(model.getSize());
547
            this.sbutton.setVisible(true);
548
            // this.listScrollPane.setVisible(true);
549
        } else {
550
            if (this.items.getSize() < 1) {
551
                // this.sbutton.setVisible(false);
552
                // this.listScrollPane.setVisible(false);
553
            } else {
554
                int index = this.items.indexOf(this.items.firstElement());
555
                if (index != this.list.getSelectedIndex())
556
                    this.list.setSelectedIndex(index);
557
            }
558
        }
559
        refresh();
560
        this.fireValueChangedEvent();
561
    }
562

    
563
    /*
564
     * (non-Javadoc)
565
     * 
566
     * @see
567
     * org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
568
     * (org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
569
     */
570
    public void handleValueChanged(JDynFieldComponent field) {
571
        Object value = field.getValue();
572
        int index = this.list.getSelectedIndex();
573
        if (index > -1) {
574
            setValue(value, index);
575
        }
576
    }
577

    
578
    // /* (non-Javadoc)
579
    // * @see org.gvsig.tools.swing.spi.DelegatedJFieldComponent#getDynField()
580
    // */
581
    // @Override
582
    // public DynField getDynField() {
583
    // return this.defaultField;
584
    // }
585
}