source: josm/trunk/src/org/openstreetmap/josm/actions/PreferenceToggleAction.java @ 12885

Last change on this file since 12885 was 12885, checked in by bastiK, 15 months ago

see #15229 - fix checkstyle warnings

  • Property svn:eol-style set to native
File size: 1.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import java.awt.event.ActionEvent;
5
6import javax.swing.JCheckBoxMenuItem;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent;
10import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener;
11import org.openstreetmap.josm.data.preferences.BooleanProperty;
12
13/**
14 * User action to toggle a custom boolean preference value.
15 *
16 * A user action will just change a preference value. To take any real action,
17 * register another {@link PreferenceChangedListener} for the given preference key.
18 */
19public class PreferenceToggleAction extends JosmAction implements PreferenceChangedListener {
20
21    private final JCheckBoxMenuItem checkbox;
22    private final BooleanProperty pref;
23
24    /**
25     * Create a new PreferenceToggleAction.
26     * @param name the (translated) title
27     * @param tooltip tooltip text
28     * @param prefKey the preference key to toggle
29     * @param prefDefault default value for the preference entry
30     */
31    public PreferenceToggleAction(String name, String tooltip, String prefKey, boolean prefDefault) {
32        super(name, null, tooltip, null, false);
33        putValue("toolbar", "toggle-" + prefKey);
34        this.pref = new BooleanProperty(prefKey, prefDefault);
35        checkbox = new JCheckBoxMenuItem(this);
36        checkbox.setSelected(pref.get());
37        Main.pref.addWeakKeyPreferenceChangeListener(prefKey, this);
38    }
39
40    @Override
41    public void actionPerformed(ActionEvent e) {
42        pref.put(checkbox.isSelected());
43    }
44
45    /**
46     * Get the checkbox that can be used for this action. It can only be used at one place.
47     * @return The checkbox.
48     */
49    public JCheckBoxMenuItem getCheckbox() {
50        return checkbox;
51    }
52
53    @Override
54    public void preferenceChanged(PreferenceChangeEvent e) {
55        checkbox.setSelected(pref.get());
56    }
57}
Note: See TracBrowser for help on using the repository browser.