Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.api / src / main / java / org / gvsig / tools / swing / api / ListElement.java @ 2126

History | View | Annotate | Download (3.62 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2015 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.tools.swing.api;
24

    
25
import java.util.Objects;
26
import javax.swing.ComboBoxModel;
27
import javax.swing.JComboBox;
28
import javax.swing.JList;
29
import javax.swing.ListModel;
30
import org.gvsig.tools.util.LabeledValueImpl;
31

    
32
public class ListElement<T> extends LabeledValueImpl<T>{
33

    
34
    public ListElement(String label, T value) {
35
        super(label, value);
36
    }
37

    
38
    public static void setSelected(JList list, Object anObject) {
39
        setSelectedValue(list, anObject);
40
    }
41
    
42
    public static void setSelected(JComboBox combo, Object anObject) {
43
        setSelectedItem(combo, anObject);
44
    }
45
    
46
    public static Object getSelected(JList list) {
47
        return getSelectedValue(list);
48
    }
49
    
50
    public static Object getSelected(JComboBox combo) {
51
        return getSelectedItem(combo);
52
    }
53
    
54
    public static void setSelectedValue(JList list, Object anObject) {
55
        if( anObject == null ) {
56
            list.setSelectedIndex(-1);
57
            return;
58
        }
59
        ListModel dataModel = list.getModel();
60
        for (int i = 0; i < dataModel.getSize(); i++) {
61
            Object element = dataModel.getElementAt(i);
62
            if( element instanceof ListElement ) {
63
                element = ((ListElement)element).getValue();
64
            }
65
            if( anObject.equals(element) ) {
66
                list.setSelectedIndex(i);
67
                return;
68
            }
69
        }
70
        list.setSelectedIndex(-1);
71
    }
72
    
73
    public static Object getSelectedValue(JList list) {
74
        Object element = list.getSelectedValue();
75
        if( element == null ) {
76
            return null;
77
        }
78
        if( element instanceof ListElement ) {
79
            return ((ListElement)element).getValue();
80
        }
81
        return element;
82
    }
83

    
84

    
85
    public static void setSelectedItem(JComboBox combo, Object anObject) {
86
        if( anObject == null ) {
87
            combo.setSelectedIndex(-1);
88
            return;
89
        }
90
        ComboBoxModel dataModel = combo.getModel();
91
        for (int i = 0; i < dataModel.getSize(); i++) {
92
            Object element = dataModel.getElementAt(i);
93
            if( element instanceof ListElement ) {
94
                element = ((ListElement)element).getValue();
95
            }
96
            if( Objects.equals(anObject,element) ) {
97
                combo.setSelectedIndex(i);
98
                return;
99
            }
100
        }
101
        combo.setSelectedIndex(-1);
102
    }
103
    
104
    public static Object getSelectedItem(JComboBox combo) {
105
        Object element = combo.getSelectedItem();
106
        if( element == null ) {
107
            return null;
108
        }
109
        if( element instanceof ListElement ) {
110
            return ((ListElement)element).getValue();
111
        }
112
        return element;
113
    }
114
}