Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / mdiFrame / DropDownButton.java @ 46673

History | View | Annotate | Download (6.89 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( isVisible==null ) {
111
                return false;
112
            }
113
            if( isVisible.booleanValue() ) {
114
                return false;
115
            }
116
        }
117
        return true;
118
    }
119

    
120

    
121
    public boolean isAllDisabled() {
122
        this.mainButton.setEnabled(this.currentAction.isEnabled());
123
        for (Action action : actions) {
124
            if( action.isEnabled() ) {
125
                return false;
126
            }
127
        }
128
        return true;
129
    }
130

    
131
    private void createDropDownMenu() {
132
        this.dropDownMenu = null;
133
        for (Action action : actions) {
134
            Boolean isVisible = (Boolean) action.getValue("VISIBLE");
135
            if( !BooleanUtils.isTrue(isVisible) ) {
136
                continue;
137
            }
138

    
139
            JMenuItem mi = new JMenuItem(
140
                    (String) action.getValue(Action.SHORT_DESCRIPTION),
141
                    (Icon) action.getValue(Action.SMALL_ICON)
142
            );
143
            mi.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
144
            mi.addActionListener(new MyItemActionListener(action));
145
            mi.setEnabled(action.isEnabled());
146
            if( this.dropDownMenu==null ) {
147
                this.dropDownMenu = new JPopupMenu();
148
                this.dropDownMenu.addFocusListener(new FocusListener() {
149
                    public void focusGained(FocusEvent fe) {
150
                    }
151
                    public void focusLost(FocusEvent fe) {
152
                        if( dropDownMenu!=null ) {
153
                            dropDownMenu.setVisible(false);
154
                        }
155
                    }
156
                });
157
            }
158
            this.dropDownMenu.add(mi);
159
        }
160
    }
161

    
162
    void setSelected(String actionCommand, boolean b) {
163
        for (Action action : actions) {
164
            String s = (String) action.getValue(Action.ACTION_COMMAND_KEY);
165
            if(StringUtils.equals(s, actionCommand)){
166
                setMainButton(action);
167
                if (action.isEnabled()){
168
                    this.mainButton.setSelected(b);
169
                    this.setSelected(b);
170
                }
171
                return;
172
            }
173
        }
174
        this.mainButton.setSelected(false);
175
    }
176

    
177
    private class MyItemActionListener implements ActionListener {
178
        private Action action = null;
179
        MyItemActionListener(Action action) {
180
            this.action = action;
181
        }
182

    
183
        public void actionPerformed(ActionEvent ae) {
184
            mainButton.setIcon((Icon) action.getValue(Action.SMALL_ICON));
185
            if (showText) {
186
                mainButton.setText((String) action.getValue(Action.SHORT_DESCRIPTION));
187
            } else {
188
                mainButton.setText("");
189
            }
190
            currentAction = action;
191
            dropDownMenu.setVisible(false);
192
            mainButton.setEnabled(true);
193
            setActionCommand((String) currentAction.getValue(Action.ACTION_COMMAND_KEY));
194
            action.actionPerformed(ae);
195
        }
196
    }
197

    
198
    private void doToggleDropDownMenu() {
199
        if (dropDownMenu==null || !dropDownMenu.isVisible()) {
200
            createDropDownMenu();
201
            dropDownMenu.show(this, 0,this.getHeight());
202
        } else {
203
            dropDownMenu.setVisible(false);
204
        }
205
    }
206

    
207
    public void setShowText(boolean showText) {
208
        this.showText = showText;
209
    }
210

    
211
    public boolean isShowText() {
212
        return showText;
213
    }
214

    
215
}