source: josm/trunk/src/org/openstreetmap/josm/gui/SideButton.java@ 13434

Last change on this file since 13434 was 13188, checked in by Don-vip, 6 years ago

fix Checkstyle/PMD issues, increase some test timeouts, update to Groovy 2.4.13

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.awt.BorderLayout;
5import java.awt.Color;
6import java.awt.Insets;
7import java.awt.event.ActionListener;
8import java.beans.PropertyChangeListener;
9
10import javax.swing.AbstractAction;
11import javax.swing.Action;
12import javax.swing.BorderFactory;
13import javax.swing.JButton;
14import javax.swing.SwingConstants;
15import javax.swing.plaf.basic.BasicArrowButton;
16
17import org.openstreetmap.josm.tools.Destroyable;
18import org.openstreetmap.josm.tools.ImageProvider;
19import org.openstreetmap.josm.tools.ImageResource;
20
21/**
22 * Button that is usually used in toggle dialogs.
23 * @since 744
24 */
25public class SideButton extends JButton implements Destroyable {
26
27 private transient PropertyChangeListener propertyChangeListener;
28
29 /**
30 * Constructs a new {@code SideButton}.
31 * @param action action used to specify the new button
32 * an icon must be provided with {@link ImageResource#attachImageIcon(AbstractAction, boolean)}
33 * @throws IllegalArgumentException if no icon provided
34 * @since 744
35 */
36 public SideButton(Action action) {
37 super(action);
38 ImageResource icon = (ImageResource) action.getValue("ImageResource");
39 if (icon != null) {
40 setIcon(icon.getImageIconBounded(
41 ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension()));
42 } else {
43 throw new IllegalArgumentException("No icon provided");
44 }
45 doStyle();
46 }
47
48 /**
49 * Constructs a new {@code SideButton}.
50 * @param action action used to specify the new button
51 * @param usename use action name
52 * @since 2710
53 */
54 public SideButton(Action action, boolean usename) {
55 this(action);
56 if (!usename) {
57 setText(null);
58 }
59 }
60
61 /**
62 * Constructs a new {@code SideButton}.
63 * @param action action used to specify the new button
64 * @param imagename image name in "dialogs" directory
65 * @since 2747
66 */
67 public SideButton(Action action, String imagename) {
68 super(action);
69 setIcon(ImageProvider.get("dialogs", imagename, ImageProvider.ImageSizes.SIDEBUTTON));
70 doStyle();
71 }
72
73 /**
74 * Do the style settings for the side button layout
75 */
76 private void doStyle() {
77 setLayout(new BorderLayout());
78 setIconTextGap(2);
79 setMargin(new Insets(0, 0, 0, 0));
80 }
81
82 /**
83 * Create the arrow for opening a drop-down menu
84 * @param listener listener to use for button actions (e.g. pressing)
85 * @return the created button
86 * @since 9668
87 */
88 public BasicArrowButton createArrow(ActionListener listener) {
89 setMargin(new Insets(0, 0, 0, 0));
90 BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
91 arrowButton.setBorder(BorderFactory.createEmptyBorder());
92 add(arrowButton, BorderLayout.EAST);
93 arrowButton.addActionListener(listener);
94 return arrowButton;
95 }
96
97 @Override
98 public void destroy() {
99 Action action = getAction();
100 if (action instanceof Destroyable) {
101 ((Destroyable) action).destroy();
102 }
103 if (action != null) {
104 if (propertyChangeListener != null) {
105 action.removePropertyChangeListener(propertyChangeListener);
106 }
107 setAction(null);
108 }
109 }
110}
Note: See TracBrowser for help on using the repository browser.