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 / DropDownLabel.java @ 2010
History | View | Annotate | Download (6.96 KB)
1 |
package org.gvsig.tools.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
5 |
import java.awt.Cursor; |
6 |
import java.awt.Point; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.awt.event.ItemEvent; |
10 |
import java.awt.event.ItemListener; |
11 |
import java.awt.event.MouseAdapter; |
12 |
import java.awt.event.MouseEvent; |
13 |
import java.lang.reflect.Method; |
14 |
import java.util.HashSet; |
15 |
import java.util.List; |
16 |
import java.util.Objects; |
17 |
import java.util.Set; |
18 |
import javax.swing.AbstractButton; |
19 |
import javax.swing.ComboBoxModel; |
20 |
import javax.swing.ImageIcon; |
21 |
import javax.swing.JComponent; |
22 |
import javax.swing.JLabel; |
23 |
import javax.swing.JMenuItem; |
24 |
import javax.swing.JPopupMenu; |
25 |
import javax.swing.UIManager; |
26 |
import org.apache.commons.lang3.StringUtils; |
27 |
import org.gvsig.tools.swing.api.DropDown; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
@SuppressWarnings("UseSpecificCatch") |
34 |
public class DropDownLabel implements DropDown { |
35 |
|
36 |
private final JComponent component; |
37 |
private final Set<ItemListener> itemListeners; |
38 |
private ComboBoxModel model; |
39 |
private int selectedIndex; |
40 |
private JPopupMenu popup; |
41 |
private List<ImageIcon>icons; |
42 |
private boolean readonly = false; |
43 |
private final JLabel arrow; |
44 |
private Color disabledForeground = null; |
45 |
private Color enabledForeground = null; |
46 |
|
47 |
|
48 |
public DropDownLabel(final JComponent component) { |
49 |
this.component = component;
|
50 |
this.itemListeners = new HashSet<>(); |
51 |
this.component.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
52 |
this.component.addMouseListener(new MouseAdapter() { |
53 |
@Override
|
54 |
public void mouseClicked(MouseEvent e) { |
55 |
doShowPopup(); |
56 |
} |
57 |
}); |
58 |
this.icons=null; |
59 |
this.component.setLayout(new BorderLayout(2,0)); |
60 |
// http://www.unicode.org/charts/PDF/U25A0.pdf
|
61 |
this.arrow = new JLabel("\u25BE"); |
62 |
this.component.add(arrow, BorderLayout.EAST); |
63 |
this.arrow.setVisible(false); |
64 |
this.arrow.addMouseListener(new MouseAdapter() { |
65 |
@Override
|
66 |
public void mouseClicked(MouseEvent e) { |
67 |
doShowPopup(); |
68 |
} |
69 |
}); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public JComponent asJComponent() { |
74 |
return this.component; |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public boolean isReadOnly() { |
79 |
return this.readonly; |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public void setReadOnly(boolean readOnly) { |
84 |
if( disabledForeground == null ) { |
85 |
disabledForeground = UIManager.getColor("Label.disabledForeground"); |
86 |
enabledForeground = UIManager.getColor("Label.foreground"); |
87 |
if( this.component instanceof AbstractButton ) { |
88 |
disabledForeground = UIManager.getColor("Button.disabledText"); |
89 |
enabledForeground = UIManager.getColor("Button.foreground"); |
90 |
} |
91 |
} |
92 |
|
93 |
this.readonly = readOnly;
|
94 |
if( readOnly ) {
|
95 |
component.setForeground(disabledForeground); |
96 |
} else {
|
97 |
component.setForeground(enabledForeground); |
98 |
} |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void setVisibleDropdownArrow(boolean visible) { |
103 |
this.arrow.setVisible(visible);
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public boolean isVisibleDropdownArrow() { |
108 |
return this.arrow.isVisible(); |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public void setEnabled(boolean enabled) { |
113 |
this.component.setEnabled(enabled);
|
114 |
this.arrow.setEnabled(enabled);
|
115 |
} |
116 |
|
117 |
private void doShowPopup() { |
118 |
if (model == null) { |
119 |
return;
|
120 |
} |
121 |
this.popup = new JPopupMenu(); |
122 |
for (int i = 0; i < model.getSize(); i++) { |
123 |
this.popup.add(this.createItem(i)); |
124 |
} |
125 |
Point p = component.getLocationOnScreen();
|
126 |
popup.show(component, 0, component.getHeight());
|
127 |
} |
128 |
|
129 |
@Override
|
130 |
public void setIcons(List<ImageIcon> icons) { |
131 |
this.icons= icons;
|
132 |
} |
133 |
|
134 |
@Override
|
135 |
public List<ImageIcon> getIcons() { |
136 |
return this.icons; |
137 |
} |
138 |
|
139 |
private ImageIcon getIcon(int index) { |
140 |
if( this.icons == null || index<0 || index>this.icons.size() ) { |
141 |
return null; |
142 |
} |
143 |
return this.icons.get(index); |
144 |
} |
145 |
|
146 |
private JMenuItem createItem(final int index) { |
147 |
JMenuItem item = new JMenuItem(Objects.toString(model.getElementAt(index), "")); |
148 |
item.setEnabled(!readonly); |
149 |
item.setIcon(this.getIcon(index));
|
150 |
item.addActionListener(new ActionListener() { |
151 |
@Override
|
152 |
public void actionPerformed(ActionEvent e) { |
153 |
if( readonly ) {
|
154 |
return;
|
155 |
} |
156 |
setSelectedIndex(index); |
157 |
ItemEvent ie = new ItemEvent(DropDownLabel.this, 1, index, ItemEvent.SELECTED); |
158 |
for (ItemListener itemListener : itemListeners) { |
159 |
if (itemListener != null) { |
160 |
itemListener.itemStateChanged(ie); |
161 |
} |
162 |
} |
163 |
} |
164 |
}); |
165 |
return item;
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public void setModel(ComboBoxModel model) { |
170 |
this.model = model;
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public ComboBoxModel getModel() { |
175 |
return this.model; |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public void setSelectedIndex(int i) { |
180 |
if( model == null ) { |
181 |
return;
|
182 |
} |
183 |
String value=null; |
184 |
if( i<0 ) { |
185 |
this.component.setToolTipText(null); |
186 |
} else {
|
187 |
value = Objects.toString(this.model.getElementAt(i), ""); |
188 |
this.component.setToolTipText(value);
|
189 |
} |
190 |
if( StringUtils.isBlank(value) ) {
|
191 |
value = " ";
|
192 |
} |
193 |
this.setText(value);
|
194 |
this.selectedIndex = i;
|
195 |
} |
196 |
|
197 |
private void setText(String s) { |
198 |
if( this.component instanceof JLabel ) { |
199 |
((JLabel)this.component).setText(s); |
200 |
} else if( this.component instanceof AbstractButton ) { |
201 |
((AbstractButton)this.component).setText(s); |
202 |
} else {
|
203 |
try {
|
204 |
Class<? extends JComponent> theClass = this.component.getClass(); |
205 |
Method method = theClass.getMethod("setText", String.class); |
206 |
method.invoke(this.component, s);
|
207 |
} catch(Throwable th) { |
208 |
// Ignore
|
209 |
} |
210 |
} |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public Object getSelectedItem() { |
215 |
if( model == null ) { |
216 |
return null; |
217 |
} |
218 |
return this.model.getElementAt(selectedIndex); |
219 |
} |
220 |
|
221 |
@Override
|
222 |
public int getSelectedIndex() { |
223 |
return this.selectedIndex; |
224 |
} |
225 |
|
226 |
@Override
|
227 |
public Object[] getSelectedObjects() { |
228 |
if( model == null ) { |
229 |
return null; |
230 |
} |
231 |
return new Object[]{this.getSelectedItem()}; |
232 |
} |
233 |
|
234 |
@Override
|
235 |
public void addItemListener(ItemListener itemListener) { |
236 |
this.itemListeners.add(itemListener);
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
public void removeItemListener(ItemListener l) { |
241 |
this.itemListeners.remove(l);
|
242 |
} |
243 |
|
244 |
} |