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

Last change on this file since 12649 was 12581, checked in by bastiK, 7 years ago

see #14794 - javadoc

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