svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / mdiFrame / DropDownButton.java @ 42803
History | View | Annotate | Download (5.56 KB)
1 | 41962 | jjdelcerro | package org.gvsig.andami.ui.mdiFrame; |
---|---|---|---|
2 | |||
3 | import java.awt.BorderLayout; |
||
4 | import java.awt.Point; |
||
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.SwingConstants; |
||
18 | import javax.swing.plaf.basic.BasicArrowButton; |
||
19 | import org.apache.commons.lang3.BooleanUtils; |
||
20 | |||
21 | public class DropDownButton extends JButton { |
||
22 | |||
23 | private final List<Action> actions = new ArrayList<Action>(); |
||
24 | private Action currentAction = null; |
||
25 | private final JButton mainButton; |
||
26 | private final JButton dropDownButton; |
||
27 | private JPopupMenu dropDownMenu; |
||
28 | private boolean showText = false; |
||
29 | |||
30 | public DropDownButton() {
|
||
31 | super();
|
||
32 | this.mainButton = new JButton(); |
||
33 | this.dropDownMenu = null; |
||
34 | |||
35 | this.dropDownButton = new BasicArrowButton(SwingConstants.SOUTH); |
||
36 | dropDownButton.addActionListener(new ActionListener() { |
||
37 | public void actionPerformed(ActionEvent ae) { |
||
38 | doToggleDropDownMenu(); |
||
39 | } |
||
40 | }); |
||
41 | |||
42 | this.setBorder(BorderFactory.createEmptyBorder()); |
||
43 | this.dropDownButton.setBorder(BorderFactory.createEmptyBorder()); |
||
44 | this.mainButton.setBorder(BorderFactory.createEmptyBorder()); |
||
45 | this.setLayout(new BorderLayout()); |
||
46 | this.add(mainButton, BorderLayout.CENTER); |
||
47 | this.add(dropDownButton, BorderLayout.EAST); |
||
48 | |||
49 | this.mainButton.addActionListener(new ActionListener() { |
||
50 | |||
51 | public void actionPerformed(ActionEvent ae) { |
||
52 | if (currentAction != null) { |
||
53 | currentAction.actionPerformed(ae); |
||
54 | } |
||
55 | } |
||
56 | }); |
||
57 | |||
58 | } |
||
59 | |||
60 | @Override
|
||
61 | public void setVisible(boolean aFlag) { |
||
62 | super.setVisible(aFlag);
|
||
63 | } |
||
64 | |||
65 | public void add(final Action action) { |
||
66 | this.actions.add(action);
|
||
67 | if (this.actions.size() == 1) { |
||
68 | 42548 | dmartinezizquierdo | setMainButton(action); |
69 | } |
||
70 | } |
||
71 | |||
72 | private void setMainButton(Action action){ |
||
73 | mainButton.setIcon((Icon) action.getValue(Action.SMALL_ICON)); |
||
74 | if (showText) {
|
||
75 | mainButton.setText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
||
76 | } else {
|
||
77 | mainButton.setText("");
|
||
78 | } |
||
79 | currentAction = action; |
||
80 | } |
||
81 | |||
82 | public void updateMainButton(){ |
||
83 | for (Action action : actions) { |
||
84 | if (action.isEnabled()){
|
||
85 | setMainButton(action); |
||
86 | return;
|
||
87 | 41962 | jjdelcerro | } |
88 | } |
||
89 | } |
||
90 | |||
91 | public boolean isAllHiden() { |
||
92 | for (Action action : actions) { |
||
93 | Boolean isVisible = (Boolean) action.getValue("VISIBLE"); |
||
94 | if( isVisible==null ) { |
||
95 | return false; |
||
96 | } |
||
97 | if( isVisible.booleanValue() ) {
|
||
98 | return false; |
||
99 | } |
||
100 | } |
||
101 | return true; |
||
102 | } |
||
103 | |||
104 | 42001 | fdiaz | |
105 | 41962 | jjdelcerro | public boolean isAllDisabled() { |
106 | this.mainButton.setEnabled(this.currentAction.isEnabled()); |
||
107 | for (Action action : actions) { |
||
108 | if( action.isEnabled() ) {
|
||
109 | return false; |
||
110 | } |
||
111 | } |
||
112 | return true; |
||
113 | 42001 | fdiaz | } |
114 | 41962 | jjdelcerro | |
115 | private void createDropDownMenu() { |
||
116 | this.dropDownMenu = null; |
||
117 | for (Action action : actions) { |
||
118 | Boolean isVisible = (Boolean) action.getValue("VISIBLE"); |
||
119 | if( !BooleanUtils.isTrue(isVisible) ) {
|
||
120 | continue;
|
||
121 | } |
||
122 | |||
123 | JMenuItem mi = new JMenuItem( |
||
124 | (String) action.getValue(Action.SHORT_DESCRIPTION), |
||
125 | (Icon) action.getValue(Action.SMALL_ICON) |
||
126 | ); |
||
127 | mi.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
||
128 | mi.addActionListener(new MyItemActionListener(action));
|
||
129 | mi.setEnabled(action.isEnabled()); |
||
130 | if( this.dropDownMenu==null ) { |
||
131 | this.dropDownMenu = new JPopupMenu(); |
||
132 | this.dropDownMenu.addFocusListener(new FocusListener() { |
||
133 | public void focusGained(FocusEvent fe) { |
||
134 | } |
||
135 | public void focusLost(FocusEvent fe) { |
||
136 | if( dropDownMenu!=null ) { |
||
137 | dropDownMenu.setVisible(false);
|
||
138 | } |
||
139 | } |
||
140 | }); |
||
141 | } |
||
142 | this.dropDownMenu.add(mi);
|
||
143 | } |
||
144 | } |
||
145 | |||
146 | private class MyItemActionListener implements ActionListener { |
||
147 | private Action action = null; |
||
148 | MyItemActionListener(Action action) {
|
||
149 | this.action = action;
|
||
150 | } |
||
151 | |||
152 | public void actionPerformed(ActionEvent ae) { |
||
153 | mainButton.setIcon((Icon) action.getValue(Action.SMALL_ICON)); |
||
154 | if (showText) {
|
||
155 | mainButton.setText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
||
156 | } else {
|
||
157 | mainButton.setText("");
|
||
158 | } |
||
159 | currentAction = action; |
||
160 | dropDownMenu.setVisible(false);
|
||
161 | mainButton.setEnabled(true);
|
||
162 | action.actionPerformed(ae); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | private void doToggleDropDownMenu() { |
||
167 | if (dropDownMenu==null || !dropDownMenu.isVisible()) { |
||
168 | createDropDownMenu(); |
||
169 | dropDownMenu.show(this, 0,this.getHeight()); |
||
170 | } else {
|
||
171 | dropDownMenu.setVisible(false);
|
||
172 | } |
||
173 | } |
||
174 | |||
175 | public void setShowText(boolean showText) { |
||
176 | this.showText = showText;
|
||
177 | } |
||
178 | |||
179 | public boolean isShowText() { |
||
180 | return showText;
|
||
181 | } |
||
182 | |||
183 | } |