[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[1] | 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
[91] | 4 | import java.awt.event.MouseAdapter;
|
---|
| 5 | import java.awt.event.MouseEvent;
|
---|
[7] | 6 | import java.beans.PropertyChangeEvent;
|
---|
| 7 | import java.beans.PropertyChangeListener;
|
---|
| 8 |
|
---|
[1] | 9 | import javax.swing.Action;
|
---|
| 10 | import javax.swing.JToggleButton;
|
---|
| 11 |
|
---|
[2871] | 12 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
| 13 |
|
---|
[1] | 14 | /**
|
---|
| 15 | * Just a toggle button, with smaller border and icon only to display in
|
---|
| 16 | * MapFrame toolbars.
|
---|
| 17 | *
|
---|
| 18 | * @author imi
|
---|
| 19 | */
|
---|
[2871] | 20 | public class IconToggleButton extends JToggleButton implements PropertyChangeListener, Destroyable {
|
---|
[1] | 21 |
|
---|
[1169] | 22 | public boolean groupbutton;
|
---|
[91] | 23 |
|
---|
[1169] | 24 | /**
|
---|
| 25 | * Construct the toggle button with the given action.
|
---|
| 26 | */
|
---|
| 27 | public IconToggleButton(Action action) {
|
---|
| 28 | super(action);
|
---|
| 29 | setText(null);
|
---|
[80] | 30 |
|
---|
[1169] | 31 | Object o = action.getValue(Action.SHORT_DESCRIPTION);
|
---|
[2005] | 32 | if (o != null) {
|
---|
[1169] | 33 | setToolTipText(o.toString());
|
---|
[2005] | 34 | }
|
---|
[91] | 35 |
|
---|
[1169] | 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;
|
---|
[91] | 41 | }
|
---|
[1169] | 42 | });
|
---|
| 43 | }
|
---|
[7] | 44 |
|
---|
[1169] | 45 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
| 46 | if (evt.getPropertyName().equals("active")) {
|
---|
| 47 | setSelected((Boolean)evt.getNewValue());
|
---|
| 48 | requestFocusInWindow();
|
---|
[2005] | 49 | } else if (evt.getPropertyName().equals("selected")) {
|
---|
| 50 | setSelected((Boolean)evt.getNewValue());
|
---|
[1169] | 51 | }
|
---|
| 52 | }
|
---|
[2871] | 53 |
|
---|
| 54 | public void destroy() {
|
---|
| 55 | Action action = getAction();
|
---|
| 56 | if (action instanceof Destroyable) {
|
---|
| 57 | ((Destroyable) action).destroy();
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
[1] | 60 | }
|
---|