File: PopdownButton.java

package info (click to toggle)
mac-widgets 0.9.5%2Bsvn369-dfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,920 kB
  • sloc: java: 8,318; makefile: 13; sh: 12
file content (124 lines) | stat: -rw-r--r-- 3,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.explodingpixels.widgets;

import com.explodingpixels.painter.Painter;
import com.explodingpixels.swingx.EPToggleButton;

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.event.*;

public class PopdownButton {

    private final EPToggleButton fButton;

    private PopupMenuCustomizer fPopupMenuCustomizer;

    private JPopupMenu fPopupMenu = new JPopupMenu();

    public PopdownButton(Icon icon, PopupMenuCustomizer popupMenuCustomizer) {

        if (popupMenuCustomizer == null) {
            throw new IllegalArgumentException("The list of items to add to" +
                    "the popup menu cannot be null.");
        }

        fButton = new EPToggleButton(icon);
        fButton.setPressedIcon(icon);

        fPopupMenuCustomizer = popupMenuCustomizer;

        init();
    }

    private void init() {

        fButton.addMouseListener(createToggleButtonMouseListener());
        fButton.addMouseMotionListener(createToggleButtonMouseMotionListener());
        fPopupMenu.addPopupMenuListener(createPopupMenuListener());

        // this is a trick to get hold of the client property which prevents
        // closing of the popup when the button is pressed.
        JComboBox box = new JComboBox();
        Object preventHide = box.getClientProperty("doNotCancelPopup");
        fButton.putClientProperty("doNotCancelPopup", preventHide);
    }

    public void setBackgroundPainter(Painter<AbstractButton> painter) {
        fButton.setBackgroundPainter(painter);
    }

    public JComponent getComponent() {
        return fButton;
    }

    public void setPressedIcon(Icon pressedIcon) {
        fButton.setPressedIcon(pressedIcon);
    }

    private void showPopupMenu() {
        fPopupMenu.pack();
        fPopupMenu.show(fButton, 0, fButton.getHeight());
    }

    private void hidePopupMenu() {
        fPopupMenu.setVisible(false);
        fButton.setSelected(false);
    }

    private MouseListener createToggleButtonMouseListener() {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // if the button is not in the selected state, then hide the
                // popup menu. note that showing the popup menu is always
                // handled by the mousePressed method.
                if (!fButton.isSelected()) {
                    hidePopupMenu();
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // allways show the menu on a mouse pressed. the mouse clicked
                // event will take care of dismissing the popup menu if the
                // button has gone into an unselected state.
                showPopupMenu();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                MenuSelectionManager manager = MenuSelectionManager.defaultManager();
                manager.processMouseEvent(e);
            }
        };
    }

    private MouseMotionListener createToggleButtonMouseMotionListener() {
        return new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                MenuSelectionManager.defaultManager().processMouseEvent(e);
            }
        };
    }

    private PopupMenuListener createPopupMenuListener() {
        return new PopupMenuListener() {

            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                fPopupMenuCustomizer.customizePopup(fPopupMenu);
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                // make sure the button accuratley reflects the visibility of
                // the popup menu.
                fButton.setSelected(false);
            }

            public void popupMenuCanceled(PopupMenuEvent e) {
                // no implementation.
            }
        };
    }
}