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 @ 1825
History | View | Annotate | Download (3.7 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 |
if( anObject == null ) { |
68 |
list.setSelectedIndex(-1);
|
69 |
return;
|
70 |
} |
71 |
ListModel dataModel = list.getModel();
|
72 |
for (int i = 0; i < dataModel.getSize(); i++) { |
73 |
Object element = dataModel.getElementAt(i);
|
74 |
if( element instanceof ListElement ) { |
75 |
element = ((ListElement)element).getValue(); |
76 |
} |
77 |
if( anObject.equals(element) ) {
|
78 |
list.setSelectedIndex(i); |
79 |
return;
|
80 |
} |
81 |
} |
82 |
} |
83 |
|
84 |
public static Object getSelectedValue(JList list) { |
85 |
Object element = list.getSelectedValue();
|
86 |
if( element == null ) { |
87 |
return null; |
88 |
} |
89 |
if( element instanceof ListElement ) { |
90 |
return ((ListElement)element).getValue();
|
91 |
} |
92 |
return element;
|
93 |
} |
94 |
|
95 |
|
96 |
public static void setSelectedItem(JComboBox combo, Object anObject) { |
97 |
if( anObject == null ) { |
98 |
combo.setSelectedIndex(-1);
|
99 |
return;
|
100 |
} |
101 |
ComboBoxModel dataModel = combo.getModel();
|
102 |
for (int i = 0; i < dataModel.getSize(); i++) { |
103 |
Object element = dataModel.getElementAt(i);
|
104 |
if( element instanceof ListElement ) { |
105 |
element = ((ListElement)element).getValue(); |
106 |
} |
107 |
if( Objects.equals(anObject,element) ) {
|
108 |
combo.setSelectedIndex(i); |
109 |
return;
|
110 |
} |
111 |
} |
112 |
} |
113 |
|
114 |
public static Object getSelectedItem(JComboBox combo) { |
115 |
Object element = combo.getSelectedItem();
|
116 |
if( element == null ) { |
117 |
return null; |
118 |
} |
119 |
if( element instanceof ListElement ) { |
120 |
return ((ListElement)element).getValue();
|
121 |
} |
122 |
return element;
|
123 |
} |
124 |
} |