source: josm/trunk/src/org/openstreetmap/josm/gui/IconToggleButton.java@ 4666

Last change on this file since 4666 was 4666, checked in by Don-vip, 12 years ago

see #7136 and #7128 - Downloading data on Mac OS X produces an exception

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui;
3
4import java.awt.event.MouseAdapter;
5import java.awt.event.MouseEvent;
6import java.beans.PropertyChangeEvent;
7import java.beans.PropertyChangeListener;
8
9import javax.swing.AbstractAction;
10import javax.swing.Action;
11import javax.swing.Icon;
12import javax.swing.JToggleButton;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.tools.Destroyable;
16
17/**
18 * Just a toggle button, with smaller border and icon only to display in
19 * MapFrame toolbars.
20 * Also provides methods for storing hidden state in preferences
21 * @author imi, akks
22 */
23public class IconToggleButton extends JToggleButton implements HideableButton, PropertyChangeListener, Destroyable {
24
25 public boolean groupbutton;
26 private ShowHideButtonListener listener;
27
28 /**
29 * Construct the toggle button with the given action.
30 */
31 public IconToggleButton(Action action) {
32 super(action);
33 setText(null);
34
35 Object o = action.getValue(Action.SHORT_DESCRIPTION);
36 if (o != null) {
37 setToolTipText(o.toString());
38 }
39
40 action.addPropertyChangeListener(this);
41
42 addMouseListener(new MouseAdapter(){
43 @Override public void mousePressed(MouseEvent e) {
44 groupbutton = e.getX() > getWidth()/2 && e.getY() > getHeight()/2;
45 }
46 });
47 }
48
49 public void propertyChange(PropertyChangeEvent evt) {
50 if (evt.getPropertyName().equals("active")) {
51 setSelected((Boolean)evt.getNewValue());
52 requestFocusInWindow();
53 } else if (evt.getPropertyName().equals("selected")) {
54 setSelected((Boolean)evt.getNewValue());
55 }
56 }
57
58 public void destroy() {
59 Action action = getAction();
60 if (action instanceof Destroyable) {
61 ((Destroyable) action).destroy();
62 }
63 if (action != null) {
64 action.removePropertyChangeListener(this);
65 }
66 }
67
68 @Override
69 public void applyButtonHiddenPreferences() {
70 String actionName = (String) getSafeActionValue(AbstractAction.NAME);
71 boolean hiddenFlag = Main.pref.getBoolean(actionName + ".itbutton_hidden", false);
72 setVisible(!hiddenFlag);
73 }
74
75 @Override
76 public void setButtonHidden(boolean b) {
77 String actionName = (String) getSafeActionValue(AbstractAction.NAME);
78 setVisible(!b);
79 if (listener!=null) { // if someone wants to know about changes of visibility
80 if (!b) listener.buttonShown(); else listener.buttonHidden();
81 }
82 Main.pref.put(actionName + ".itbutton_hidden", b);
83 }
84
85 @Override
86 public void showButton() {
87 setButtonHidden(false);
88 }
89
90 @Override
91 public void hideButton() {
92 setButtonHidden(true);
93 }
94
95 @Override
96 public String getActionName() {
97 return (String) getSafeActionValue(Action.NAME);
98 }
99
100 @Override
101 public Icon getIcon() {
102 return (Icon) getSafeActionValue(Action.SMALL_ICON);
103 }
104
105 @Override
106 public boolean isButtonVisible() {
107 return isVisible();
108 }
109
110 @Override
111 public void setShowHideButtonListener(ShowHideButtonListener l) {
112 listener = l;
113 }
114
115 protected final Object getSafeActionValue(String key) {
116 // Mac OS X Aqua L&F can call accessors from constructor, so getAction() can be null in those cases
117 return getAction() != null ? getAction().getValue(key) : null;
118 }
119}
Note: See TracBrowser for help on using the repository browser.