root / trunk / extensions / extTopology / src / org / gvsig / gui / JComboBoxWithDisabledItems.java @ 21273
History | View | Annotate | Download (6.13 KB)
1 |
/*
|
---|---|
2 |
* Created on 10-abr-2006
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id:
|
47 |
* $Log:
|
48 |
*/
|
49 |
package org.gvsig.gui; |
50 |
|
51 |
import java.awt.Component; |
52 |
import java.awt.event.ActionEvent; |
53 |
import java.awt.event.ActionListener; |
54 |
import java.util.Vector; |
55 |
|
56 |
import javax.swing.ComboBoxModel; |
57 |
import javax.swing.JComboBox; |
58 |
import javax.swing.JLabel; |
59 |
import javax.swing.JList; |
60 |
import javax.swing.ListCellRenderer; |
61 |
import javax.swing.UIManager; |
62 |
import javax.swing.border.EmptyBorder; |
63 |
|
64 |
/**
|
65 |
* Implementation of JComboBox that allows us to enable/disable
|
66 |
* individual items of the JComboBox.
|
67 |
*
|
68 |
* Based in the work of Nobuo Tamemasa, in
|
69 |
* http://www.codeguru.com/java/articles/165.shtml
|
70 |
*
|
71 |
*
|
72 |
* @author Alvaro Zabala
|
73 |
*
|
74 |
*/
|
75 |
public class JComboBoxWithDisabledItems extends JComboBox { |
76 |
|
77 |
private static final long serialVersionUID = -1375989245835223722L; |
78 |
|
79 |
public JComboBoxWithDisabledItems(){
|
80 |
super();
|
81 |
// initialize();
|
82 |
} |
83 |
|
84 |
public JComboBoxWithDisabledItems(ComboBoxModel model){ |
85 |
super(model);
|
86 |
initialize(); |
87 |
} |
88 |
|
89 |
public JComboBoxWithDisabledItems(Object[] items){ |
90 |
super(items);
|
91 |
initialize(); |
92 |
} |
93 |
|
94 |
public JComboBoxWithDisabledItems(Vector<?> items){ |
95 |
super(items);
|
96 |
initialize(); |
97 |
} |
98 |
|
99 |
|
100 |
public void initialize(){ |
101 |
setRenderer(new ComboRenderer());
|
102 |
addActionListener(new ComboListener(this)); |
103 |
} |
104 |
|
105 |
public void addItem(Object item){ |
106 |
if(item instanceof ComboItem){ |
107 |
super.addItem(item);
|
108 |
}else{
|
109 |
ComboItem newItem = new ComboItem(item);
|
110 |
newItem.setEnabled(true);
|
111 |
super.addItem(newItem);
|
112 |
} |
113 |
} |
114 |
|
115 |
public Object getItemAt(int index){ |
116 |
ComboItem item = (ComboItem) super.getItemAt(index);
|
117 |
return item.obj;
|
118 |
} |
119 |
|
120 |
public Object getSelectedItem(){ |
121 |
ComboItem item = (ComboItem) super.getSelectedItem();
|
122 |
return item.obj;
|
123 |
} |
124 |
|
125 |
public void setItemEnable(int index, boolean enabled){ |
126 |
ComboItem item = (ComboItem) super.getItemAt(index);
|
127 |
item.isEnable = enabled; |
128 |
} |
129 |
|
130 |
public boolean isItemEnabled(int index){ |
131 |
if(index >= 0){ |
132 |
ComboItem item = (ComboItem) super.getItemAt(index);
|
133 |
return item.isEnable;
|
134 |
}else
|
135 |
return false; |
136 |
} |
137 |
/**
|
138 |
* Combo box renderer.
|
139 |
*
|
140 |
* @author Alvaro Zabala
|
141 |
*
|
142 |
*/
|
143 |
class ComboRenderer extends JLabel implements ListCellRenderer { |
144 |
private static final long serialVersionUID = 4216706963467674095L; |
145 |
|
146 |
public ComboRenderer() {
|
147 |
setOpaque(true);
|
148 |
setBorder(new EmptyBorder(1, 1, 1, 1)); |
149 |
} |
150 |
|
151 |
public Component getListCellRendererComponent( JList list, |
152 |
Object value, int index, boolean isSelected, boolean cellHasFocus) { |
153 |
if (isSelected) {
|
154 |
setBackground(list.getSelectionBackground()); |
155 |
setForeground(list.getSelectionForeground()); |
156 |
} else {
|
157 |
setBackground(list.getBackground()); |
158 |
setForeground(list.getForeground()); |
159 |
} |
160 |
// if (! ((ComboItem)value).isEnabled()) {
|
161 |
if(! isItemEnabled(index)){
|
162 |
setBackground(list.getBackground()); |
163 |
setForeground(UIManager.getColor("Label.disabledForeground")); |
164 |
}else{
|
165 |
setBackground(list.getBackground()); |
166 |
setForeground(list.getForeground()); |
167 |
} |
168 |
setFont(list.getFont()); |
169 |
setText((value == null) ? "" : value.toString()); |
170 |
return this; |
171 |
} |
172 |
|
173 |
} |
174 |
|
175 |
|
176 |
/**
|
177 |
* Listens for combo box selection events.
|
178 |
*
|
179 |
* It the new selected items are not enabled, the combo box
|
180 |
* will remain unchanged.
|
181 |
*
|
182 |
* @author Alvaro Zabala
|
183 |
*
|
184 |
*/
|
185 |
class ComboListener implements ActionListener { |
186 |
JComboBox combo;
|
187 |
Object currentItem;
|
188 |
|
189 |
ComboListener(JComboBox combo) {
|
190 |
this.combo = combo;
|
191 |
combo.setSelectedIndex(0);
|
192 |
currentItem = combo.getSelectedItem(); |
193 |
} |
194 |
|
195 |
public void actionPerformed(ActionEvent e) { |
196 |
Object tempItem = combo.getSelectedItem();
|
197 |
int selectedIndex = combo.getSelectedIndex();
|
198 |
//If the selected item in the combo box is enable,
|
199 |
//the combo will change. If not, the selected item
|
200 |
//will be the previous selected item
|
201 |
// if (! ((ComboItem)tempItem).isEnabled()) {
|
202 |
if(isItemEnabled(selectedIndex)){
|
203 |
combo.setSelectedItem(currentItem); |
204 |
} else {
|
205 |
currentItem = tempItem; |
206 |
} |
207 |
} |
208 |
}//ComboListener
|
209 |
|
210 |
/**
|
211 |
* Combo box item that allows to enabling / disabling combo
|
212 |
* box entries.
|
213 |
*
|
214 |
* @author Alvaro Zabala
|
215 |
*
|
216 |
*/
|
217 |
class ComboItem { |
218 |
Object obj;
|
219 |
boolean isEnable;
|
220 |
|
221 |
ComboItem(Object obj,boolean isEnable) { |
222 |
this.obj = obj;
|
223 |
this.isEnable = isEnable;
|
224 |
} |
225 |
|
226 |
ComboItem(Object obj) {
|
227 |
this(obj, true); |
228 |
} |
229 |
|
230 |
public boolean isEnabled() { |
231 |
return isEnable;
|
232 |
} |
233 |
|
234 |
public void setEnabled(boolean isEnable) { |
235 |
this.isEnable = isEnable;
|
236 |
} |
237 |
|
238 |
public String toString() { |
239 |
return obj.toString();
|
240 |
} |
241 |
} |
242 |
} |