svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / mdiFrame / DropDownButton.java @ 47249
History | View | Annotate | Download (7.05 KB)
1 |
package org.gvsig.andami.ui.mdiFrame; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Insets; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.FocusEvent; |
8 |
import java.awt.event.FocusListener; |
9 |
import java.util.ArrayList; |
10 |
import java.util.List; |
11 |
import javax.swing.Action; |
12 |
import javax.swing.BorderFactory; |
13 |
import javax.swing.Icon; |
14 |
import javax.swing.JButton; |
15 |
import javax.swing.JMenuItem; |
16 |
import javax.swing.JPopupMenu; |
17 |
import javax.swing.JToggleButton; |
18 |
import javax.swing.SwingConstants; |
19 |
import javax.swing.UIManager; |
20 |
import javax.swing.plaf.basic.BasicArrowButton; |
21 |
import org.apache.commons.lang3.BooleanUtils; |
22 |
import org.apache.commons.lang3.StringUtils; |
23 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
24 |
|
25 |
public class DropDownButton extends JToggleButton { |
26 |
|
27 |
private final List<Action> actions = new ArrayList<Action>(); |
28 |
private Action currentAction = null; |
29 |
private final JToggleButton mainButton; |
30 |
private final JButton dropDownButton; |
31 |
private JPopupMenu dropDownMenu; |
32 |
private boolean showText = false; |
33 |
|
34 |
public DropDownButton() {
|
35 |
super();
|
36 |
this.mainButton = new JToggleButton(); |
37 |
this.mainButton.setOpaque(true); |
38 |
this.dropDownMenu = null; |
39 |
|
40 |
this.dropDownButton = new BasicArrowButton(SwingConstants.SOUTH); |
41 |
dropDownButton.addActionListener(new ActionListener() { |
42 |
public void actionPerformed(ActionEvent ae) { |
43 |
doToggleDropDownMenu(); |
44 |
} |
45 |
}); |
46 |
|
47 |
this.setBorder(BorderFactory.createEmptyBorder()); |
48 |
this.dropDownButton.setBorder(BorderFactory.createEmptyBorder()); |
49 |
this.mainButton.setBorder(BorderFactory.createEmptyBorder()); |
50 |
this.setLayout(new BorderLayout()); |
51 |
this.add(mainButton, BorderLayout.CENTER); |
52 |
this.add(dropDownButton, BorderLayout.EAST); |
53 |
this.mainButton.setMargin(new Insets(0, 2, 0, 2)); |
54 |
this.mainButton.setBackground(UIManager.getColor(ToolsSwingManager.COLOR_PANEL_BACKGROUND)); |
55 |
this.dropDownButton.setBackground(UIManager.getColor(ToolsSwingManager.COLOR_PANEL_BACKGROUND)); |
56 |
this.setBackground(UIManager.getColor(ToolsSwingManager.COLOR_PANEL_BACKGROUND)); |
57 |
|
58 |
this.mainButton.setFocusable(false); |
59 |
this.setFocusable(false); |
60 |
|
61 |
|
62 |
this.mainButton.addActionListener(new ActionListener() { |
63 |
|
64 |
public void actionPerformed(ActionEvent ae) { |
65 |
if (currentAction != null) { |
66 |
currentAction.actionPerformed(ae); |
67 |
} |
68 |
} |
69 |
}); |
70 |
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public void setVisible(boolean aFlag) { |
75 |
super.setVisible(aFlag);
|
76 |
} |
77 |
|
78 |
public void add(final Action action) { |
79 |
this.actions.add(action);
|
80 |
if (this.actions.size() == 1) { |
81 |
setMainButton(action); |
82 |
} |
83 |
} |
84 |
|
85 |
private void setMainButton(Action action){ |
86 |
mainButton.setIcon((Icon) action.getValue(Action.SMALL_ICON)); |
87 |
if (showText) {
|
88 |
mainButton.setText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
89 |
} else {
|
90 |
mainButton.setText("");
|
91 |
} |
92 |
currentAction = action; |
93 |
} |
94 |
|
95 |
public void updateMainButton(){ |
96 |
if(currentAction != null && currentAction.isEnabled()){ |
97 |
return;
|
98 |
} |
99 |
for (Action action : actions) { |
100 |
if (action.isEnabled()){
|
101 |
setMainButton(action); |
102 |
return;
|
103 |
} |
104 |
} |
105 |
} |
106 |
|
107 |
public boolean isAllHiden() { |
108 |
for (Action action : actions) { |
109 |
Boolean isVisible = (Boolean) action.getValue("VISIBLE"); |
110 |
if( BooleanUtils.isTrue(isVisible) ) {
|
111 |
return false; |
112 |
} |
113 |
// if( isVisible==null ) {
|
114 |
// return false;
|
115 |
// }
|
116 |
// if( isVisible.booleanValue() ) {
|
117 |
// return false;
|
118 |
// }
|
119 |
} |
120 |
return true; |
121 |
} |
122 |
|
123 |
|
124 |
public boolean isAllDisabled() { |
125 |
this.mainButton.setEnabled(this.currentAction.isEnabled()); |
126 |
for (Action action : actions) { |
127 |
if( action.isEnabled() ) {
|
128 |
return false; |
129 |
} |
130 |
} |
131 |
return true; |
132 |
} |
133 |
|
134 |
private void createDropDownMenu() { |
135 |
this.dropDownMenu = null; |
136 |
for (Action action : actions) { |
137 |
Boolean isVisible = (Boolean) action.getValue("VISIBLE"); |
138 |
if( !BooleanUtils.isTrue(isVisible) ) {
|
139 |
continue;
|
140 |
} |
141 |
|
142 |
JMenuItem mi = new JMenuItem( |
143 |
(String) action.getValue(Action.SHORT_DESCRIPTION), |
144 |
(Icon) action.getValue(Action.SMALL_ICON) |
145 |
); |
146 |
mi.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
147 |
mi.addActionListener(new MyItemActionListener(action));
|
148 |
mi.setEnabled(action.isEnabled()); |
149 |
if( this.dropDownMenu==null ) { |
150 |
this.dropDownMenu = new JPopupMenu(); |
151 |
this.dropDownMenu.addFocusListener(new FocusListener() { |
152 |
public void focusGained(FocusEvent fe) { |
153 |
} |
154 |
public void focusLost(FocusEvent fe) { |
155 |
if( dropDownMenu!=null ) { |
156 |
dropDownMenu.setVisible(false);
|
157 |
} |
158 |
} |
159 |
}); |
160 |
} |
161 |
this.dropDownMenu.add(mi);
|
162 |
} |
163 |
} |
164 |
|
165 |
void setSelected(String actionCommand, boolean b) { |
166 |
for (Action action : actions) { |
167 |
String s = (String) action.getValue(Action.ACTION_COMMAND_KEY); |
168 |
if(StringUtils.equals(s, actionCommand)){
|
169 |
setMainButton(action); |
170 |
if (action.isEnabled()){
|
171 |
this.mainButton.setSelected(b);
|
172 |
this.setSelected(b);
|
173 |
} |
174 |
return;
|
175 |
} |
176 |
} |
177 |
this.mainButton.setSelected(false); |
178 |
} |
179 |
|
180 |
private class MyItemActionListener implements ActionListener { |
181 |
private Action action = null; |
182 |
MyItemActionListener(Action action) {
|
183 |
this.action = action;
|
184 |
} |
185 |
|
186 |
public void actionPerformed(ActionEvent ae) { |
187 |
mainButton.setIcon((Icon) action.getValue(Action.SMALL_ICON)); |
188 |
if (showText) {
|
189 |
mainButton.setText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
190 |
} else {
|
191 |
mainButton.setText("");
|
192 |
} |
193 |
currentAction = action; |
194 |
dropDownMenu.setVisible(false);
|
195 |
mainButton.setEnabled(true);
|
196 |
setActionCommand((String) currentAction.getValue(Action.ACTION_COMMAND_KEY)); |
197 |
action.actionPerformed(ae); |
198 |
} |
199 |
} |
200 |
|
201 |
private void doToggleDropDownMenu() { |
202 |
if (dropDownMenu==null || !dropDownMenu.isVisible()) { |
203 |
createDropDownMenu(); |
204 |
if(dropDownMenu != null){ |
205 |
dropDownMenu.show(this, 0,this.getHeight()); |
206 |
} |
207 |
} else {
|
208 |
dropDownMenu.setVisible(false);
|
209 |
} |
210 |
} |
211 |
|
212 |
public void setShowText(boolean showText) { |
213 |
this.showText = showText;
|
214 |
} |
215 |
|
216 |
public boolean isShowText() { |
217 |
return showText;
|
218 |
} |
219 |
|
220 |
} |