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 |
|
---|
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 | }
|
---|