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

Last change on this file since 4669 was 4669, checked in by stoecker, 12 years ago

fix #6963 - patch by akks - fixes for left hand buttons

  • Property svn:eol-style set to native
File size: 4.2 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 private boolean hideIfDisabled=false;
28
29 /**
30 * Construct the toggle button with the given action.
31 */
32 public IconToggleButton(Action action) {
33 super(action);
34 setText(null);
35
36 Object o = action.getValue(Action.SHORT_DESCRIPTION);
37 if (o != null) {
38 setToolTipText(o.toString());
39 }
40
41 action.addPropertyChangeListener(this);
42
43 addMouseListener(new MouseAdapter(){
44 @Override public void mousePressed(MouseEvent e) {
45 groupbutton = e.getX() > getWidth()/2 && e.getY() > getHeight()/2;
46 }
47 });
48 }
49
50 public void propertyChange(PropertyChangeEvent evt) {
51 if (evt.getPropertyName().equals("active")) {
52 setSelected((Boolean)evt.getNewValue());
53 requestFocusInWindow();
54 } else if (evt.getPropertyName().equals("selected")) {
55 setSelected((Boolean)evt.getNewValue());
56 }
57 }
58
59 public void destroy() {
60 Action action = getAction();
61 if (action instanceof Destroyable) {
62 ((Destroyable) action).destroy();
63 }
64 if (action != null) {
65 action.removePropertyChangeListener(this);
66 }
67 }
68
69 String getPreferenceKey() {
70 String s = (String) getSafeActionValue("toolbar");
71 if (s==null) {
72 if (getAction()!=null) s=getAction().getClass().getName();
73 }
74 return "sidetoolbar.hidden."+s;
75
76 }
77
78 @Override
79 public void applyButtonHiddenPreferences() {
80 boolean alwaysHideDisabled = Main.pref.getBoolean("sidetoolbar.hideDisabledButtons", false);
81 boolean hiddenFlag = Main.pref.getBoolean(getPreferenceKey(), false);
82 if (!isEnabled() && (hideIfDisabled || alwaysHideDisabled))
83 setVisible(false); // hide because of disabled button
84 else
85 setVisible( !hiddenFlag ); // show or hide, do what preferences say
86 }
87
88 @Override
89 public void setButtonHidden(boolean b) {
90 setVisible(!b);
91 if (listener!=null) { // if someone wants to know about changes of visibility
92 if (!b) listener.buttonShown(); else listener.buttonHidden();
93 }
94 Main.pref.put(getPreferenceKey(), b);
95 }
96
97 /*
98 * This fuction should be called for plugins that want to enable auto-hiding
99 * custom buttons when they are disabled (because of incorrect layer, for example)
100 */
101 public void setAutoHideDisabledButton(boolean b) {
102 hideIfDisabled=b;
103 if (b && !isEnabled()) setVisible(false);
104 }
105
106 @Override
107 public void showButton() {
108 setButtonHidden(false);
109 }
110
111 @Override
112 public void hideButton() {
113 setButtonHidden(true);
114 }
115
116 @Override
117 public String getActionName() {
118 return (String) getSafeActionValue(Action.NAME);
119 }
120
121 @Override
122 public Icon getIcon() {
123 return (Icon) getSafeActionValue(Action.SMALL_ICON);
124 }
125
126 @Override
127 public boolean isButtonVisible() {
128 return isVisible();
129 }
130
131 @Override
132 public void setShowHideButtonListener(ShowHideButtonListener l) {
133 listener = l;
134 }
135
136 protected final Object getSafeActionValue(String key) {
137 // Mac OS X Aqua L&F can call accessors from constructor, so getAction() can be null in those cases
138 return getAction() != null ? getAction().getValue(key) : null;
139 }
140}
Note: See TracBrowser for help on using the repository browser.