source: josm/trunk/src/org/openstreetmap/josm/gui/autofilter/AutoFilterButton.java @ 14206

Last change on this file since 14206 was 14206, checked in by Don-vip, 3 months ago

fix #16698, see #15670 - make sure filters are executed (costly operation) only when necessary:

  • data changes imply execution of filters only when at least a filter is enabled
  • filter changes imply execution of filters even is no filter is enabled
  • filter dataset change events should not trigger a new filter execution!
File size: 2.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.autofilter;
3
4import java.awt.Color;
5import java.awt.Graphics;
6import java.awt.Graphics2D;
7import java.awt.RenderingHints;
8import java.awt.event.ActionEvent;
9
10import javax.swing.BorderFactory;
11import javax.swing.JButton;
12
13import org.openstreetmap.josm.actions.JosmAction;
14import org.openstreetmap.josm.data.preferences.NamedColorProperty;
15import 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 */
21public class AutoFilterButton extends JButton {
22
23    private static final NamedColorProperty PROP_COLOR = new NamedColorProperty("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(true);
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}
Note: See TracBrowser for help on using the repository browser.