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.Action;
|
---|
10 | import javax.swing.JToggleButton;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Just a toggle button, with smaller border and icon only to display in
|
---|
16 | * MapFrame toolbars.
|
---|
17 | *
|
---|
18 | * @author imi
|
---|
19 | */
|
---|
20 | public class IconToggleButton extends JToggleButton implements PropertyChangeListener, Destroyable {
|
---|
21 |
|
---|
22 | public boolean groupbutton;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Construct the toggle button with the given action.
|
---|
26 | */
|
---|
27 | public IconToggleButton(Action action) {
|
---|
28 | super(action);
|
---|
29 | setText(null);
|
---|
30 |
|
---|
31 | Object o = action.getValue(Action.SHORT_DESCRIPTION);
|
---|
32 | if (o != null) {
|
---|
33 | setToolTipText(o.toString());
|
---|
34 | }
|
---|
35 |
|
---|
36 | action.addPropertyChangeListener(this);
|
---|
37 |
|
---|
38 | addMouseListener(new MouseAdapter(){
|
---|
39 | @Override public void mousePressed(MouseEvent e) {
|
---|
40 | groupbutton = e.getX() > getWidth()/2 && e.getY() > getHeight()/2;
|
---|
41 | }
|
---|
42 | });
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
46 | if (evt.getPropertyName().equals("active")) {
|
---|
47 | setSelected((Boolean)evt.getNewValue());
|
---|
48 | requestFocusInWindow();
|
---|
49 | } else if (evt.getPropertyName().equals("selected")) {
|
---|
50 | setSelected((Boolean)evt.getNewValue());
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void destroy() {
|
---|
55 | Action action = getAction();
|
---|
56 | if (action instanceof Destroyable) {
|
---|
57 | ((Destroyable) action).destroy();
|
---|
58 | }
|
---|
59 | action.removePropertyChangeListener(this);
|
---|
60 | }
|
---|
61 | }
|
---|