1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.Graphics;
|
---|
7 | import java.awt.Graphics2D;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.geom.Path2D;
|
---|
11 |
|
---|
12 | import javax.swing.Action;
|
---|
13 | import javax.swing.Icon;
|
---|
14 | import javax.swing.JButton;
|
---|
15 | import javax.swing.JPopupMenu;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Button triggering the appearance of a JPopupMenu when activated.
|
---|
19 | * @since 12955
|
---|
20 | */
|
---|
21 | public class PopupMenuButton extends JButton implements ActionListener {
|
---|
22 | private JPopupMenu menu;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @see JButton#JButton()
|
---|
26 | */
|
---|
27 | public PopupMenuButton() {
|
---|
28 | super();
|
---|
29 | this.initialize();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @see JButton#JButton(Action)
|
---|
34 | */
|
---|
35 | public PopupMenuButton(Action a) {
|
---|
36 | super(a);
|
---|
37 | this.initialize();
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * @see JButton#JButton(Icon)
|
---|
42 | */
|
---|
43 | public PopupMenuButton(Icon i) {
|
---|
44 | super(i);
|
---|
45 | this.initialize();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * @see JButton#JButton(String)
|
---|
50 | */
|
---|
51 | public PopupMenuButton(String t) {
|
---|
52 | super(t);
|
---|
53 | this.initialize();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * @see JButton#JButton(String, Icon)
|
---|
58 | */
|
---|
59 | public PopupMenuButton(String t, Icon i) {
|
---|
60 | super(t, i);
|
---|
61 | this.initialize();
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Pass-through to {@link JButton#JButton()} allowing associated popup menu to be set
|
---|
66 | */
|
---|
67 | public PopupMenuButton(JPopupMenu m) {
|
---|
68 | super();
|
---|
69 | this.initialize(m);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Pass-through to {@link JButton#JButton(Action)} allowing associated popup menu to be set
|
---|
74 | */
|
---|
75 | public PopupMenuButton(Action a, JPopupMenu m) {
|
---|
76 | super(a);
|
---|
77 | this.initialize(m);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Pass-through to {@link JButton#JButton(Icon)} allowing associated popup menu to be set
|
---|
82 | */
|
---|
83 | public PopupMenuButton(Icon i, JPopupMenu m) {
|
---|
84 | super(i);
|
---|
85 | this.initialize(m);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Pass-through to {@link JButton#JButton(String)} allowing associated popup menu to be set
|
---|
90 | */
|
---|
91 | public PopupMenuButton(String t, JPopupMenu m) {
|
---|
92 | super(t);
|
---|
93 | this.initialize(m);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Pass-through to {@link JButton#JButton(String, Icon)} allowing associated popup menu to be set
|
---|
98 | */
|
---|
99 | public PopupMenuButton(String t, Icon i, JPopupMenu m) {
|
---|
100 | super(t, i);
|
---|
101 | this.initialize(m);
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void initialize(JPopupMenu m) {
|
---|
105 | this.menu = m;
|
---|
106 | this.initialize();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void initialize() {
|
---|
110 | this.addActionListener(this);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Get the popup menu associated with this button
|
---|
115 | */
|
---|
116 | public JPopupMenu getPopupMenu() {
|
---|
117 | return this.menu;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Set the popup menu associated with this button
|
---|
122 | * @param m Menu to show when button is triggered
|
---|
123 | */
|
---|
124 | public void setPopupMenu(JPopupMenu m) {
|
---|
125 | this.menu = m;
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public void actionPerformed(ActionEvent e) {
|
---|
130 | this.menu.show(this, 0, this.getHeight());
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public void paint(Graphics g) {
|
---|
135 | super.paint(g);
|
---|
136 | Graphics2D g2d = (Graphics2D) g;
|
---|
137 |
|
---|
138 | //
|
---|
139 | // paint small arrow in bottom right corner
|
---|
140 | //
|
---|
141 | Dimension size = this.getSize();
|
---|
142 |
|
---|
143 | Path2D p = new Path2D.Float();
|
---|
144 | p.moveTo(size.getWidth() - 7, size.getHeight() - 4);
|
---|
145 | p.lineTo(size.getWidth() - 1, size.getHeight() - 4);
|
---|
146 | p.lineTo(size.getWidth() - 4, size.getHeight() - 1);
|
---|
147 | p.closePath();
|
---|
148 |
|
---|
149 | g2d.setPaint(Color.BLACK);
|
---|
150 | g2d.fill(p);
|
---|
151 | }
|
---|
152 | }
|
---|