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