Revision 2010
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.api/src/main/java/org/gvsig/tools/swing/api/ToolsSwingManager.java | ||
---|---|---|
185 | 185 |
public DropDown createDropDown(JComboBox combo); |
186 | 186 |
|
187 | 187 |
public DropDown createDropDown(JLabel label); |
188 |
|
|
189 |
public DropDown createDropDown(AbstractButton button); |
|
190 |
|
|
191 |
public DropDown createDropDown(JComponent component); |
|
188 | 192 |
|
189 | 193 |
/** |
190 | 194 |
* Contructor for creating a CompoundIcon where the icons are |
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 | ||
---|---|---|
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 |
} |
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 | ||
---|---|---|
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; |
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/DefaultToolsSwingManager.java | ||
---|---|---|
527 | 527 |
} |
528 | 528 |
|
529 | 529 |
@Override |
530 |
public DropDown createDropDown(AbstractButton button) { |
|
531 |
DropDown c = new DropDownButton(button); |
|
532 |
return c; |
|
533 |
} |
|
534 |
|
|
535 |
@Override |
|
536 |
public DropDown createDropDown(JComponent component) { |
|
537 |
DropDown c = new DropDownLabel(component); |
|
538 |
return c; |
|
539 |
} |
|
540 |
|
|
541 |
@Override |
|
530 | 542 |
public CompoundIcon createCompoundIcon(Icon... icons) { |
531 | 543 |
DefaultCompoundIcon i = new DefaultCompoundIcon(icons); |
532 | 544 |
return i; |
Also available in: Unified diff