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.BorderFactory;
|
---|
11 | import javax.swing.JButton;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
14 | import org.openstreetmap.josm.data.preferences.ColorProperty;
|
---|
15 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * A button associated to an auto filter. If clicked twice, the filter is reset.
|
---|
19 | * @since 12400
|
---|
20 | */
|
---|
21 | public class AutoFilterButton extends JButton {
|
---|
22 |
|
---|
23 | private static final ColorProperty PROP_COLOR = new ColorProperty("auto.filter.button.color", new Color(0, 160, 160));
|
---|
24 |
|
---|
25 | private final AutoFilter filter;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code AutoFilterButton}.
|
---|
29 | * @param filter auto filter associated to this button
|
---|
30 | */
|
---|
31 | public AutoFilterButton(final AutoFilter filter) {
|
---|
32 | super(new JosmAction(filter.getLabel(), null, filter.getDescription(), null, false) {
|
---|
33 | @Override
|
---|
34 | public synchronized void actionPerformed(ActionEvent e) {
|
---|
35 | AutoFilterManager afm = AutoFilterManager.getInstance();
|
---|
36 | if (filter.equals(afm.getCurrentAutoFilter())) {
|
---|
37 | afm.setCurrentAutoFilter(null);
|
---|
38 | MainApplication.getMap().filterDialog.getFilterModel().executeFilters();
|
---|
39 | } else {
|
---|
40 | afm.setCurrentAutoFilter(filter);
|
---|
41 | }
|
---|
42 | }
|
---|
43 | });
|
---|
44 | this.filter = filter;
|
---|
45 | setForeground(Color.WHITE);
|
---|
46 | setContentAreaFilled(false);
|
---|
47 | setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void paintComponent(Graphics g) {
|
---|
52 | if (getModel().isPressed()) {
|
---|
53 | g.setColor(PROP_COLOR.get().darker().darker());
|
---|
54 | } else if (getModel().isRollover() || AutoFilterManager.getInstance().getCurrentAutoFilter() == filter) {
|
---|
55 | g.setColor(PROP_COLOR.get().darker());
|
---|
56 | } else {
|
---|
57 | g.setColor(PROP_COLOR.get());
|
---|
58 | }
|
---|
59 | if (g instanceof Graphics2D) {
|
---|
60 | ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
61 | }
|
---|
62 | g.fillRoundRect(0, 0, getWidth(), getHeight(), 3, 3);
|
---|
63 | super.paintComponent(g);
|
---|
64 | }
|
---|
65 | }
|
---|