1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.awt.event.MouseAdapter;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 | import java.beans.PropertyChangeEvent;
|
---|
7 | import java.beans.PropertyChangeListener;
|
---|
8 |
|
---|
9 | import javax.swing.AbstractAction;
|
---|
10 | import javax.swing.Action;
|
---|
11 | import javax.swing.Icon;
|
---|
12 | import javax.swing.JToggleButton;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import 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 | */
|
---|
23 | public class IconToggleButton extends JToggleButton implements HideableButton, PropertyChangeListener, Destroyable {
|
---|
24 |
|
---|
25 | public boolean groupbutton;
|
---|
26 | private ShowHideButtonListener listener;
|
---|
27 | private boolean hideIfDisabled=false;
|
---|
28 | private boolean hiddenByDefault;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Construct the toggle button with the given action.
|
---|
32 | */
|
---|
33 | public IconToggleButton(Action action) {
|
---|
34 | this(action, false);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Construct the toggle button with the given action.
|
---|
39 | */
|
---|
40 | public IconToggleButton(Action action, boolean hiddenByDefault) {
|
---|
41 | super(action);
|
---|
42 | this.hiddenByDefault = hiddenByDefault;
|
---|
43 | setText(null);
|
---|
44 |
|
---|
45 | Object o = action.getValue(Action.SHORT_DESCRIPTION);
|
---|
46 | if (o != null) {
|
---|
47 | setToolTipText(o.toString());
|
---|
48 | }
|
---|
49 |
|
---|
50 | action.addPropertyChangeListener(this);
|
---|
51 |
|
---|
52 | addMouseListener(new MouseAdapter(){
|
---|
53 | @Override public void mousePressed(MouseEvent e) {
|
---|
54 | groupbutton = e.getX() > getWidth()/2 && e.getY() > getHeight()/2;
|
---|
55 | }
|
---|
56 | });
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
60 | if (evt.getPropertyName().equals("active")) {
|
---|
61 | setSelected((Boolean)evt.getNewValue());
|
---|
62 | requestFocusInWindow();
|
---|
63 | } else if (evt.getPropertyName().equals("selected")) {
|
---|
64 | setSelected((Boolean)evt.getNewValue());
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void destroy() {
|
---|
69 | Action action = getAction();
|
---|
70 | if (action instanceof Destroyable) {
|
---|
71 | ((Destroyable) action).destroy();
|
---|
72 | }
|
---|
73 | if (action != null) {
|
---|
74 | action.removePropertyChangeListener(this);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | String getPreferenceKey() {
|
---|
79 | String s = (String) getSafeActionValue("toolbar");
|
---|
80 | if (s==null) {
|
---|
81 | if (getAction()!=null) s=getAction().getClass().getName();
|
---|
82 | }
|
---|
83 | return "sidetoolbar.hidden."+s;
|
---|
84 |
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void applyButtonHiddenPreferences() {
|
---|
89 | boolean alwaysHideDisabled = Main.pref.getBoolean("sidetoolbar.hideDisabledButtons", false);
|
---|
90 | boolean hiddenFlag = Main.pref.getBoolean(getPreferenceKey(), hiddenByDefault);
|
---|
91 | if (!isEnabled() && (hideIfDisabled || alwaysHideDisabled))
|
---|
92 | setVisible(false); // hide because of disabled button
|
---|
93 | else
|
---|
94 | setVisible( !hiddenFlag ); // show or hide, do what preferences say
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public void setButtonHidden(boolean b) {
|
---|
99 | setVisible(!b);
|
---|
100 | if (listener!=null) { // if someone wants to know about changes of visibility
|
---|
101 | if (!b) listener.buttonShown(); else listener.buttonHidden();
|
---|
102 | }
|
---|
103 | Main.pref.put(getPreferenceKey(), b);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * This fuction should be called for plugins that want to enable auto-hiding
|
---|
108 | * custom buttons when they are disabled (because of incorrect layer, for example)
|
---|
109 | */
|
---|
110 | public void setAutoHideDisabledButton(boolean b) {
|
---|
111 | hideIfDisabled=b;
|
---|
112 | if (b && !isEnabled()) setVisible(false);
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public void showButton() {
|
---|
117 | setButtonHidden(false);
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void hideButton() {
|
---|
122 | setButtonHidden(true);
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public String getActionName() {
|
---|
127 | return (String) getSafeActionValue(Action.NAME);
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public Icon getIcon() {
|
---|
132 | return (Icon) getSafeActionValue(Action.SMALL_ICON);
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public boolean isButtonVisible() {
|
---|
137 | return isVisible();
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public void setShowHideButtonListener(ShowHideButtonListener l) {
|
---|
142 | listener = l;
|
---|
143 | }
|
---|
144 |
|
---|
145 | protected final Object getSafeActionValue(String key) {
|
---|
146 | // Mac OS X Aqua L&F can call accessors from constructor, so getAction() can be null in those cases
|
---|
147 | return getAction() != null ? getAction().getValue(key) : null;
|
---|
148 | }
|
---|
149 | }
|
---|