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

History | View | Annotate | Download (3.6 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

    
31
public class ListElement<T> {
32

    
33
    private final String label;
34
    private final T value;
35

    
36
    public ListElement(String label, T value) {
37
        this.label = label;
38
        this.value = value;
39
    }
40

    
41
    @Override
42
    public String toString() {
43
        return this.label;
44
    }
45

    
46
    public T getValue() {
47
        return this.value;
48
    }
49

    
50
    public static void setSelected(JList list, Object anObject) {
51
        setSelectedValue(list, anObject);
52
    }
53
    
54
    public static void setSelected(JComboBox combo, Object anObject) {
55
        setSelectedItem(combo, anObject);
56
    }
57
    
58
    public static Object getSelected(JList list) {
59
        return getSelectedValue(list);
60
    }
61
    
62
    public static Object getSelected(JComboBox combo) {
63
        return getSelectedItem(combo);
64
    }
65
    
66
    public static void setSelectedValue(JList list, Object anObject) {
67
        ListModel dataModel = list.getModel();
68
        for (int i = 0; i < dataModel.getSize(); i++) {
69
            Object element = dataModel.getElementAt(i);
70
            if( element instanceof ListElement ) {
71
                element = ((ListElement)element).getValue();
72
            }
73
            if( anObject.equals(element) ) {
74
                list.setSelectedIndex(i);
75
                return;
76
            }
77
        }
78
    }
79
    
80
    public static Object getSelectedValue(JList list) {
81
        Object element = list.getSelectedValue();
82
        if( element == null ) {
83
            return null;
84
        }
85
        if( element instanceof ListElement ) {
86
            return ((ListElement)element).getValue();
87
        }
88
        return element;
89
    }
90

    
91

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