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.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
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 | * Constructs a new {@code PopupMenuButton}.
|
---|
26 | * @see JButton#JButton()
|
---|
27 | */
|
---|
28 | public PopupMenuButton() {
|
---|
29 | super();
|
---|
30 | this.initialize();
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Constructs a new {@code PopupMenuButton}.
|
---|
35 | * @param a the <code>Action</code> used to specify the new button
|
---|
36 | * @see JButton#JButton(Action)
|
---|
37 | */
|
---|
38 | public PopupMenuButton(Action a) {
|
---|
39 | super(a);
|
---|
40 | this.initialize();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code PopupMenuButton}.
|
---|
45 | * @param i the Icon image to display on the button
|
---|
46 | * @see JButton#JButton(Icon)
|
---|
47 | */
|
---|
48 | public PopupMenuButton(Icon i) {
|
---|
49 | super(i);
|
---|
50 | this.initialize();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Constructs a new {@code PopupMenuButton}.
|
---|
55 | * @param t the text of the button
|
---|
56 | * @see JButton#JButton(String)
|
---|
57 | */
|
---|
58 | public PopupMenuButton(String t) {
|
---|
59 | super(t);
|
---|
60 | this.initialize();
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Constructs a new {@code PopupMenuButton}.
|
---|
65 | * @param t the text of the button
|
---|
66 | * @param i the Icon image to display on the button
|
---|
67 | * @see JButton#JButton(String, Icon)
|
---|
68 | */
|
---|
69 | public PopupMenuButton(String t, Icon i) {
|
---|
70 | super(t, i);
|
---|
71 | this.initialize();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Pass-through to {@link JButton#JButton()} allowing associated popup menu to be set
|
---|
76 | * @param m the associated popup menu
|
---|
77 | */
|
---|
78 | public PopupMenuButton(JPopupMenu m) {
|
---|
79 | super();
|
---|
80 | this.initialize(m);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Pass-through to {@link JButton#JButton(Action)} allowing associated popup menu to be set
|
---|
85 | * @param a the <code>Action</code> used to specify the new button
|
---|
86 | * @param m the associated popup menu
|
---|
87 | */
|
---|
88 | public PopupMenuButton(Action a, JPopupMenu m) {
|
---|
89 | super(a);
|
---|
90 | this.initialize(m);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Pass-through to {@link JButton#JButton(Icon)} allowing associated popup menu to be set
|
---|
95 | * @param i the Icon image to display on the button
|
---|
96 | * @param m the associated popup menu
|
---|
97 | */
|
---|
98 | public PopupMenuButton(Icon i, JPopupMenu m) {
|
---|
99 | super(i);
|
---|
100 | this.initialize(m);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Pass-through to {@link JButton#JButton(String)} allowing associated popup menu to be set
|
---|
105 | * @param t the text of the button
|
---|
106 | * @param m the associated popup menu
|
---|
107 | */
|
---|
108 | public PopupMenuButton(String t, JPopupMenu m) {
|
---|
109 | super(t);
|
---|
110 | this.initialize(m);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Pass-through to {@link JButton#JButton(String, Icon)} allowing associated popup menu to be set
|
---|
115 | * @param t the text of the button
|
---|
116 | * @param i the Icon image to display on the button
|
---|
117 | * @param m the associated popup menu
|
---|
118 | */
|
---|
119 | public PopupMenuButton(String t, Icon i, JPopupMenu m) {
|
---|
120 | super(t, i);
|
---|
121 | this.initialize(m);
|
---|
122 | }
|
---|
123 |
|
---|
124 | private void initialize(JPopupMenu m) {
|
---|
125 | this.menu = m;
|
---|
126 | this.initialize();
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void initialize() {
|
---|
130 | this.addActionListener(this);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Get the popup menu associated with this button
|
---|
135 | * @return the popup menu associated with this button
|
---|
136 | */
|
---|
137 | public JPopupMenu getPopupMenu() {
|
---|
138 | return this.menu;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Set the popup menu associated with this button
|
---|
143 | * @param m Menu to show when button is triggered
|
---|
144 | */
|
---|
145 | public void setPopupMenu(JPopupMenu m) {
|
---|
146 | this.menu = m;
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void actionPerformed(ActionEvent e) {
|
---|
151 | this.menu.show(this, 0, this.getHeight());
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public void paint(Graphics g) {
|
---|
156 | super.paint(g);
|
---|
157 | Graphics2D g2d = (Graphics2D) g;
|
---|
158 |
|
---|
159 | //
|
---|
160 | // paint small arrow in bottom right corner
|
---|
161 | //
|
---|
162 | Dimension size = this.getSize();
|
---|
163 |
|
---|
164 | Path2D p = new Path2D.Float();
|
---|
165 | p.moveTo(size.getWidth() - 7, size.getHeight() - 4);
|
---|
166 | p.lineTo(size.getWidth() - 1, size.getHeight() - 4);
|
---|
167 | p.lineTo(size.getWidth() - 4, size.getHeight() - 1);
|
---|
168 | p.closePath();
|
---|
169 |
|
---|
170 | g2d.setPaint(Color.BLACK);
|
---|
171 | g2d.fill(p);
|
---|
172 | }
|
---|
173 | }
|
---|