Revision 2010 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
DropDownLabel.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.swing.impl; |
2 | 2 |
|
3 | 3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
|
4 | 5 |
import java.awt.Cursor; |
5 | 6 |
import java.awt.Point; |
6 | 7 |
import java.awt.event.ActionEvent; |
... | ... | |
9 | 10 |
import java.awt.event.ItemListener; |
10 | 11 |
import java.awt.event.MouseAdapter; |
11 | 12 |
import java.awt.event.MouseEvent; |
13 |
import java.lang.reflect.Method; |
|
12 | 14 |
import java.util.HashSet; |
13 | 15 |
import java.util.List; |
14 | 16 |
import java.util.Objects; |
15 | 17 |
import java.util.Set; |
18 |
import javax.swing.AbstractButton; |
|
16 | 19 |
import javax.swing.ComboBoxModel; |
17 | 20 |
import javax.swing.ImageIcon; |
18 | 21 |
import javax.swing.JComponent; |
... | ... | |
27 | 30 |
* |
28 | 31 |
* @author jjdelcerro |
29 | 32 |
*/ |
33 |
@SuppressWarnings("UseSpecificCatch") |
|
30 | 34 |
public class DropDownLabel implements DropDown { |
31 | 35 |
|
32 |
private final JLabel label;
|
|
36 |
private final JComponent component;
|
|
33 | 37 |
private final Set<ItemListener> itemListeners; |
34 | 38 |
private ComboBoxModel model; |
35 | 39 |
private int selectedIndex; |
36 | 40 |
private JPopupMenu popup; |
37 | 41 |
private List<ImageIcon>icons; |
38 | 42 |
private boolean readonly = false; |
39 |
private final JLabel arrow; |
|
40 |
|
|
41 |
public DropDownLabel(final JLabel label) { |
|
42 |
this.label = label; |
|
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; |
|
43 | 50 |
this.itemListeners = new HashSet<>(); |
44 |
this.label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
|
45 |
this.label.addMouseListener(new MouseAdapter() {
|
|
51 |
this.component.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
|
52 |
this.component.addMouseListener(new MouseAdapter() {
|
|
46 | 53 |
@Override |
47 | 54 |
public void mouseClicked(MouseEvent e) { |
48 | 55 |
doShowPopup(); |
49 | 56 |
} |
50 | 57 |
}); |
51 | 58 |
this.icons=null; |
52 |
this.label.setLayout(new BorderLayout(2,0));
|
|
59 |
this.component.setLayout(new BorderLayout(2,0));
|
|
53 | 60 |
// http://www.unicode.org/charts/PDF/U25A0.pdf |
54 | 61 |
this.arrow = new JLabel("\u25BE"); |
55 |
this.label.add(arrow, BorderLayout.EAST);
|
|
62 |
this.component.add(arrow, BorderLayout.EAST);
|
|
56 | 63 |
this.arrow.setVisible(false); |
57 | 64 |
this.arrow.addMouseListener(new MouseAdapter() { |
58 | 65 |
@Override |
... | ... | |
64 | 71 |
|
65 | 72 |
@Override |
66 | 73 |
public JComponent asJComponent() { |
67 |
return this.label;
|
|
74 |
return this.component;
|
|
68 | 75 |
} |
69 | 76 |
|
70 | 77 |
@Override |
... | ... | |
74 | 81 |
|
75 | 82 |
@Override |
76 | 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 |
|
|
77 | 93 |
this.readonly = readOnly; |
78 | 94 |
if( readOnly ) { |
79 |
label.setForeground(UIManager.getColor("Label.disabledForeground"));
|
|
95 |
component.setForeground(disabledForeground);
|
|
80 | 96 |
} else { |
81 |
label.setForeground(UIManager.getColor("Label.foreground"));
|
|
97 |
component.setForeground(enabledForeground);
|
|
82 | 98 |
} |
83 | 99 |
} |
84 | 100 |
|
... | ... | |
94 | 110 |
|
95 | 111 |
@Override |
96 | 112 |
public void setEnabled(boolean enabled) { |
97 |
this.label.setEnabled(enabled);
|
|
113 |
this.component.setEnabled(enabled);
|
|
98 | 114 |
this.arrow.setEnabled(enabled); |
99 | 115 |
} |
100 | 116 |
|
... | ... | |
106 | 122 |
for (int i = 0; i < model.getSize(); i++) { |
107 | 123 |
this.popup.add(this.createItem(i)); |
108 | 124 |
} |
109 |
Point p = label.getLocationOnScreen();
|
|
110 |
popup.show(label, 0, label.getHeight());
|
|
125 |
Point p = component.getLocationOnScreen();
|
|
126 |
popup.show(component, 0, component.getHeight());
|
|
111 | 127 |
} |
112 | 128 |
|
113 | 129 |
@Override |
... | ... | |
166 | 182 |
} |
167 | 183 |
String value=null; |
168 | 184 |
if( i<0 ) { |
169 |
this.label.setToolTipText(null);
|
|
185 |
this.component.setToolTipText(null);
|
|
170 | 186 |
} else { |
171 | 187 |
value = Objects.toString(this.model.getElementAt(i), ""); |
172 |
this.label.setToolTipText(value);
|
|
188 |
this.component.setToolTipText(value);
|
|
173 | 189 |
} |
174 | 190 |
if( StringUtils.isBlank(value) ) { |
175 | 191 |
value = " "; |
176 | 192 |
} |
177 |
this.label.setText(value);
|
|
193 |
this.setText(value); |
|
178 | 194 |
this.selectedIndex = i; |
179 | 195 |
} |
180 | 196 |
|
181 |
@Override |
|
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 |
|
182 | 214 |
public Object getSelectedItem() { |
183 | 215 |
if( model == null ) { |
184 | 216 |
return null; |
Also available in: Unified diff