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 / DropDownButton.java @ 2537
History | View | Annotate | Download (8.2 KB)
1 |
package org.gvsig.tools.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Cursor; |
5 |
import java.awt.Point; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.awt.event.ItemEvent; |
9 |
import java.awt.event.ItemListener; |
10 |
import java.awt.event.MouseAdapter; |
11 |
import java.awt.event.MouseEvent; |
12 |
import java.util.HashSet; |
13 |
import java.util.List; |
14 |
import java.util.Objects; |
15 |
import java.util.Set; |
16 |
import javax.swing.AbstractButton; |
17 |
import javax.swing.ComboBoxModel; |
18 |
import javax.swing.ImageIcon; |
19 |
import javax.swing.JComponent; |
20 |
import javax.swing.JMenuItem; |
21 |
import javax.swing.JPopupMenu; |
22 |
import javax.swing.UIManager; |
23 |
import org.apache.commons.lang3.StringUtils; |
24 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
25 |
import org.gvsig.tools.swing.api.DropDown; |
26 |
import org.gvsig.tools.util.LabeledValue; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author jjdelcerro
|
31 |
*/
|
32 |
@SuppressWarnings("UseSpecificCatch") |
33 |
public class DropDownButton implements DropDown { |
34 |
|
35 |
private final AbstractButton component; |
36 |
private final Set<ItemListener> itemListeners; |
37 |
private ComboBoxModel model; |
38 |
private int selectedIndex; |
39 |
private JPopupMenu popup; |
40 |
private List<ImageIcon>icons; |
41 |
private boolean readonly = false; |
42 |
private Color disabledForeground = null; |
43 |
private Color enabledForeground = null; |
44 |
private boolean visibleDropdownArrow = true; |
45 |
private final Color warningBackgroundColor; |
46 |
private final Color backgroundColor; |
47 |
private final ActionListenerSupport dropdownListsnersHelper; |
48 |
|
49 |
public DropDownButton(final AbstractButton button, Color warningBackgroundColor) { |
50 |
this.component = button;
|
51 |
this.itemListeners = new HashSet<>(); |
52 |
this.dropdownListsnersHelper = new DefaultActionListenerSupport(); |
53 |
this.component.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
54 |
this.component.addMouseListener(new MouseAdapter() { |
55 |
@Override
|
56 |
public void mouseClicked(MouseEvent e) { |
57 |
doShowPopup(); |
58 |
} |
59 |
}); |
60 |
this.icons=null; |
61 |
this.warningBackgroundColor = warningBackgroundColor;
|
62 |
this.backgroundColor = button.getBackground();
|
63 |
String s = this.component.getText(); |
64 |
if( !s.endsWith(" \u25BE")) { |
65 |
this.component.setText(s+" \u25BE"); |
66 |
} |
67 |
this.component.addActionListener((ActionEvent e) -> { |
68 |
doShowPopup(); |
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("Button.disabledText"); |
86 |
enabledForeground = UIManager.getColor("Button.foreground"); |
87 |
} |
88 |
|
89 |
this.readonly = readOnly;
|
90 |
if( readOnly ) {
|
91 |
component.setForeground(disabledForeground); |
92 |
} else {
|
93 |
component.setForeground(enabledForeground); |
94 |
} |
95 |
} |
96 |
|
97 |
@Override
|
98 |
public void setVisibleDropdownArrow(boolean visible) { |
99 |
this.visibleDropdownArrow = visible;
|
100 |
String s = this.component.getText(); |
101 |
if( this.visibleDropdownArrow ) { |
102 |
if( !s.endsWith(" \u25BE")) { |
103 |
this.component.setText(s+" \u25BE"); |
104 |
} |
105 |
} else {
|
106 |
if( s.endsWith(" \u25BE")) { |
107 |
this.component.setText(s.substring(0, s.length()-1)); |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public boolean isVisibleDropdownArrow() { |
114 |
return this.visibleDropdownArrow; |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public void setEnabled(boolean enabled) { |
119 |
this.component.setEnabled(enabled);
|
120 |
} |
121 |
|
122 |
private void doShowPopup() { |
123 |
ActionEvent ev = new ActionEvent(this, 0, null); |
124 |
dropdownListsnersHelper.fireActionEvent(ev); |
125 |
if (model == null) { |
126 |
return;
|
127 |
} |
128 |
this.popup = new JPopupMenu(); |
129 |
for (int i = 0; i < model.getSize(); i++) { |
130 |
this.popup.add(this.createItem(i)); |
131 |
} |
132 |
Point p = component.getLocationOnScreen();
|
133 |
popup.show(component, 0, component.getHeight());
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public void setIcons(List<ImageIcon> icons) { |
138 |
this.icons= icons;
|
139 |
} |
140 |
|
141 |
@Override
|
142 |
public List<ImageIcon> getIcons() { |
143 |
return this.icons; |
144 |
} |
145 |
|
146 |
private ImageIcon getIcon(int index) { |
147 |
if( this.icons == null || index<0 || index>this.icons.size() ) { |
148 |
return null; |
149 |
} |
150 |
return this.icons.get(index); |
151 |
} |
152 |
|
153 |
private JMenuItem createItem(final int index) { |
154 |
JMenuItem item = new JMenuItem(Objects.toString(model.getElementAt(index), "")); |
155 |
item.setEnabled(!readonly); |
156 |
item.setIcon(this.getIcon(index));
|
157 |
item.addActionListener(new ActionListener() { |
158 |
@Override
|
159 |
public void actionPerformed(ActionEvent e) { |
160 |
if( readonly ) {
|
161 |
return;
|
162 |
} |
163 |
setSelectedIndex(index); |
164 |
ItemEvent ie = new ItemEvent(DropDownButton.this, 1, index, ItemEvent.SELECTED); |
165 |
for (ItemListener itemListener : itemListeners) { |
166 |
if (itemListener != null) { |
167 |
itemListener.itemStateChanged(ie); |
168 |
} |
169 |
} |
170 |
} |
171 |
}); |
172 |
return item;
|
173 |
} |
174 |
|
175 |
@Override
|
176 |
public void setModel(ComboBoxModel model) { |
177 |
this.model = model;
|
178 |
} |
179 |
|
180 |
@Override
|
181 |
public ComboBoxModel getModel() { |
182 |
return this.model; |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void setValue(Object value) { |
187 |
ComboBoxModel model = this.getModel(); |
188 |
if (model == null) { |
189 |
throw new IllegalStateException("Can't set value when model is null"); |
190 |
} |
191 |
for (int i = 0; i < model.getSize(); i++) { |
192 |
Object item = model.getElementAt(i);
|
193 |
if (item instanceof LabeledValue) { |
194 |
item = ((LabeledValue) item).getValue(); |
195 |
} |
196 |
|
197 |
if (Objects.equals(item, value)) {
|
198 |
this.setSelectedIndex(i);
|
199 |
return;
|
200 |
} |
201 |
} |
202 |
|
203 |
this.setText(Objects.toString(value), this.warningBackgroundColor); |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public void setSelectedIndex(int i) { |
208 |
if( model == null ) { |
209 |
return;
|
210 |
} |
211 |
String value=null; |
212 |
if( i<0 ) { |
213 |
this.component.setToolTipText(null); |
214 |
} else {
|
215 |
value = Objects.toString(this.model.getElementAt(i), ""); |
216 |
this.component.setToolTipText(value);
|
217 |
} |
218 |
if( StringUtils.isBlank(value) ) {
|
219 |
value = " ";
|
220 |
} |
221 |
this.setText(value);
|
222 |
this.selectedIndex = i;
|
223 |
} |
224 |
|
225 |
private void setText(String s, Color backgroundColor) { |
226 |
if( this.visibleDropdownArrow ) { |
227 |
if( !s.endsWith(" \u25BE")) { |
228 |
this.component.setText(s+" \u25BE"); |
229 |
} |
230 |
} else {
|
231 |
if( s.endsWith(" \u25BE")) { |
232 |
this.component.setText(s.substring(0, s.length()-1)); |
233 |
} |
234 |
} |
235 |
this.component.setBackground(backgroundColor);
|
236 |
} |
237 |
|
238 |
protected void setText(String s) { |
239 |
this.setText(s, backgroundColor);
|
240 |
} |
241 |
|
242 |
@Override
|
243 |
public Object getSelectedItem() { |
244 |
if( model == null ) { |
245 |
return null; |
246 |
} |
247 |
return this.model.getElementAt(selectedIndex); |
248 |
} |
249 |
|
250 |
@Override
|
251 |
public int getSelectedIndex() { |
252 |
return this.selectedIndex; |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public Object[] getSelectedObjects() { |
257 |
if( model == null ) { |
258 |
return null; |
259 |
} |
260 |
return new Object[]{this.getSelectedItem()}; |
261 |
} |
262 |
|
263 |
@Override
|
264 |
public void addItemListener(ItemListener itemListener) { |
265 |
this.itemListeners.add(itemListener);
|
266 |
} |
267 |
|
268 |
@Override
|
269 |
public void removeItemListener(ItemListener l) { |
270 |
this.itemListeners.remove(l);
|
271 |
} |
272 |
|
273 |
@Override
|
274 |
public void addDropdownListener(ActionListener listener) { |
275 |
this.dropdownListsnersHelper.addActionListener(listener);
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public void removeDropdownListener(ActionListener listener) { |
280 |
this.dropdownListsnersHelper.removeActionListener(listener);
|
281 |
} |
282 |
} |