Revision 46673 trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/ui/mdiFrame/DropDownButton.java

View differences:

DropDownButton.java
1 1
package org.gvsig.andami.ui.mdiFrame;
2 2

  
3 3
import java.awt.BorderLayout;
4
import java.awt.Point;
4
import java.awt.Insets;
5 5
import java.awt.event.ActionEvent;
6 6
import java.awt.event.ActionListener;
7 7
import java.awt.event.FocusEvent;
......
14 14
import javax.swing.JButton;
15 15
import javax.swing.JMenuItem;
16 16
import javax.swing.JPopupMenu;
17
import javax.swing.JToggleButton;
17 18
import javax.swing.SwingConstants;
19
import javax.swing.UIManager;
18 20
import javax.swing.plaf.basic.BasicArrowButton;
19 21
import org.apache.commons.lang3.BooleanUtils;
22
import org.apache.commons.lang3.StringUtils;
23
import org.gvsig.tools.swing.api.ToolsSwingManager;
20 24

  
21
public class DropDownButton extends JButton {
25
public class DropDownButton extends JToggleButton {
22 26

  
23 27
    private final List<Action> actions = new ArrayList<Action>();
24 28
    private Action currentAction = null;
25
    private final JButton mainButton;
29
    private final JToggleButton mainButton;
26 30
    private final JButton dropDownButton;
27 31
    private JPopupMenu dropDownMenu;
28 32
    private boolean showText = false;
29 33

  
30 34
    public DropDownButton() {
31 35
        super();
32
        this.mainButton = new JButton();
36
        this.mainButton = new JToggleButton();
37
        this.mainButton.setOpaque(true);
33 38
        this.dropDownMenu = null;
34 39

  
35 40
        this.dropDownButton = new BasicArrowButton(SwingConstants.SOUTH);
......
45 50
        this.setLayout(new BorderLayout());
46 51
        this.add(mainButton, BorderLayout.CENTER);
47 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);
48 60

  
61

  
49 62
        this.mainButton.addActionListener(new ActionListener() {
50 63

  
51 64
            public void actionPerformed(ActionEvent ae) {
......
80 93
    }
81 94

  
82 95
    public void updateMainButton(){
96
        if(currentAction != null && currentAction.isEnabled()){
97
            return;
98
        }
83 99
        for (Action action : actions) {
84 100
            if (action.isEnabled()){
85 101
                setMainButton(action);
......
143 159
        }
144 160
    }
145 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

  
146 177
    private class MyItemActionListener implements ActionListener {
147 178
        private Action action = null;
148 179
        MyItemActionListener(Action action) {
......
159 190
            currentAction = action;
160 191
            dropDownMenu.setVisible(false);
161 192
            mainButton.setEnabled(true);
193
            setActionCommand((String) currentAction.getValue(Action.ACTION_COMMAND_KEY));
162 194
            action.actionPerformed(ae);
163 195
        }
164 196
    }

Also available in: Unified diff