1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.autofilter;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Graphics2D;
|
---|
7 | import java.awt.RenderingHints;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 |
|
---|
10 | import javax.swing.AbstractAction;
|
---|
11 | import javax.swing.BorderFactory;
|
---|
12 | import javax.swing.JButton;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
15 | import org.openstreetmap.josm.actions.PreferencesAction;
|
---|
16 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.preferences.display.DrawingPreference;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * A button associated to an auto filter. If clicked twice, the filter is reset.
|
---|
22 | * @since 12400
|
---|
23 | */
|
---|
24 | public class AutoFilterButton extends JButton {
|
---|
25 |
|
---|
26 | private static final NamedColorProperty PROP_COLOR = new NamedColorProperty("auto.filter.button.color", new Color(0, 160, 160));
|
---|
27 |
|
---|
28 | private final AutoFilter filter;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Constructs a new {@code AutoFilterButton}.
|
---|
32 | * @param filter auto filter associated to this button
|
---|
33 | */
|
---|
34 | public AutoFilterButton(final AutoFilter filter) {
|
---|
35 | super(new JosmAction(filter.getLabel(), null, filter.getDescription(), null, false) {
|
---|
36 | @Override
|
---|
37 | public synchronized void actionPerformed(ActionEvent e) {
|
---|
38 | AutoFilterManager afm = AutoFilterManager.getInstance();
|
---|
39 | if (filter.equals(afm.getCurrentAutoFilter())) {
|
---|
40 | afm.setCurrentAutoFilter(null);
|
---|
41 | MainApplication.getMap().filterDialog.getFilterModel().executeFilters(true);
|
---|
42 | } else {
|
---|
43 | afm.setCurrentAutoFilter(filter);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | });
|
---|
47 | this.filter = filter;
|
---|
48 | setForeground(Color.WHITE);
|
---|
49 | setContentAreaFilled(false);
|
---|
50 | setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | protected void paintComponent(Graphics g) {
|
---|
55 | if (getModel().isPressed()) {
|
---|
56 | g.setColor(PROP_COLOR.get().darker().darker());
|
---|
57 | } else if (getModel().isRollover() || AutoFilterManager.getInstance().getCurrentAutoFilter() == filter) {
|
---|
58 | g.setColor(PROP_COLOR.get().darker());
|
---|
59 | } else {
|
---|
60 | g.setColor(PROP_COLOR.get());
|
---|
61 | }
|
---|
62 | if (g instanceof Graphics2D) {
|
---|
63 | ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
64 | }
|
---|
65 | g.fillRoundRect(0, 0, getWidth(), getHeight(), 3, 3);
|
---|
66 | super.paintComponent(g);
|
---|
67 | }
|
---|
68 |
|
---|
69 | static AutoFilterButton forOsmKey(String key) {
|
---|
70 | final String name = key + ":";
|
---|
71 | final AutoFilterButton button = new AutoFilterButton(new AutoFilter(key, "", null));
|
---|
72 | button.setAction(new AbstractAction(name) {
|
---|
73 | @Override
|
---|
74 | public void actionPerformed(ActionEvent e) {
|
---|
75 | PreferencesAction.forPreferenceTab("", "null", DrawingPreference.class).actionPerformed(e);
|
---|
76 | }
|
---|
77 | });
|
---|
78 | return button;
|
---|
79 | }
|
---|
80 | }
|
---|