Changeset 6220 in josm for trunk


Ignore:
Timestamp:
2013-09-07T15:50:39+02:00 (11 years ago)
Author:
Don-vip
Message:

refactor Toggle actions, use Action.SELECTED_KEY since we have switched to Java 6 long ago, javadoc

Location:
trunk/src/org/openstreetmap/josm/actions
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/ChangesetManagerToggleAction.java

    r6084 r6220  
    1010import java.awt.event.WindowEvent;
    1111import java.awt.event.WindowListener;
    12 import java.util.ArrayList;
    13 import java.util.List;
    14 
    15 import javax.swing.ButtonModel;
    1612
    1713import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager;
    1814import org.openstreetmap.josm.tools.Shortcut;
     15
    1916/**
    2017 * This action toggles the visibility of the {@link ChangesetCacheManager} dialog.
    21  *
     18 * @since 2685
    2219 */
    23 public class ChangesetManagerToggleAction extends JosmAction {
    24     private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
    25     //FIXME: replace with property Action.SELECTED_KEY when migrating to
    26     // Java 6
    27     private boolean selected;
     20public class ChangesetManagerToggleAction extends ToggleAction {
    2821    private WindowListener changesetCacheManagerClosedHandler;
    2922
     23    /**
     24     * Constructs a new {@code ChangesetManagerToggleAction}.
     25     */
    3026    public ChangesetManagerToggleAction() {
    31         super(
    32                 tr("Changeset Manager"),
    33                 "dialogs/changeset/changesetmanager",
    34                 tr("Toggle visibility of Changeset Manager window"),
    35                 Shortcut.registerShortcut("menu:windows:changesetdialog",
    36                         tr("Toggle visibility of Changeset Manager window"),
    37                         KeyEvent.VK_C, Shortcut.ALT_CTRL),
    38                 true /* register shortcut */
     27        super(tr("Changeset Manager"),
     28              "dialogs/changeset/changesetmanager",
     29              tr("Toggle visibility of Changeset Manager window"),
     30              Shortcut.registerShortcut("menu:windows:changesetdialog",
     31                      tr("Toggle visibility of Changeset Manager window"),
     32                      KeyEvent.VK_C, Shortcut.ALT_CTRL),
     33              true /* register shortcut */
    3934        );
    4035        notifySelectedState();
     
    4338    }
    4439
    45     public void addButtonModel(ButtonModel model) {
    46         if (model != null && !buttonModels.contains(model)) {
    47             buttonModels.add(model);
    48         }
    49     }
    50 
    51     public void removeButtonModel(ButtonModel model) {
    52         if (model != null && buttonModels.contains(model)) {
    53             buttonModels.remove(model);
    54         }
    55     }
    56 
    57     protected void notifySelectedState() {
    58         for (ButtonModel model: buttonModels) {
    59             if (model.isSelected() != selected) {
    60                 model.setSelected(selected);
    61             }
    62         }
    63     }
    64 
    65     protected void toggleSelectedState() {
    66         selected = !selected;
     40    @Override
     41    public void actionPerformed(ActionEvent e) {
     42        toggleSelectedState();
    6743        notifySelectedState();
    68         if (selected) {
     44        if (isSelected()) {
    6945            ChangesetCacheManager.getInstance().addWindowListener(changesetCacheManagerClosedHandler);
    7046            ChangesetCacheManager.getInstance().setVisible(true);
     
    7551    }
    7652
    77     @Override
    78     public void actionPerformed(ActionEvent e) {
    79         toggleSelectedState();
    80     }
    81 
    8253    private class ChangesetCacheManagerClosedHandler extends WindowAdapter {
    8354        @Override
    8455        public void windowClosed(WindowEvent e) {
    85             selected = false;
     56            setSelected(false);
    8657            notifySelectedState();
    8758            ChangesetCacheManager.getInstance().removeWindowListener(changesetCacheManagerClosedHandler);
  • trunk/src/org/openstreetmap/josm/actions/DialogsToggleAction.java

    r6069 r6220  
    77import java.awt.event.ActionEvent;
    88import java.awt.event.KeyEvent;
    9 import java.util.ArrayList;
    10 import java.util.List;
    119
    12 import javax.swing.ButtonModel;
    1310import javax.swing.Icon;
    1411
     
    1613import org.openstreetmap.josm.tools.Shortcut;
    1714
    18 /*
    19 * Action that hides or shows toggle dialogs panel and other panels
    20 * to free more space for drawing (GIMP-like)
     15/**
     16* This action toggles visibility of dialogs panel and other panels to free more space for drawing (GIMP-like)
    2117* @author cmuelle8
     18* @since 5965
    2219*/
    23 public class DialogsToggleAction extends JosmAction {
    24     private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
    25     private boolean selected;
     20public class DialogsToggleAction extends ToggleAction {
    2621
     22    /**
     23     * Constructs a new {@code DialogsToggleAction}.
     24     */
    2725    public DialogsToggleAction() {
    28         super(
    29                 tr("Toggle dialogs panel"),
    30                 (Icon) null, /* no icon */
    31                 tr("Toggle dialogs panel, maximize mapview"),
    32                 Shortcut.registerShortcut("menu:view:dialogspanel", tr("Toggle dialogs panel"),KeyEvent.VK_TAB, Shortcut.DIRECT),
    33                 true, "dialogspanel", /* register in toolbar */
    34                 false
     26        super(tr("Toggle dialogs panel"),
     27              (Icon) null, /* no icon */
     28              tr("Toggle dialogs panel, maximize mapview"),
     29              Shortcut.registerShortcut("menu:view:dialogspanel", tr("Toggle dialogs panel"),KeyEvent.VK_TAB, Shortcut.DIRECT),
     30              true, "dialogspanel", /* register in toolbar */
     31              false
    3532        );
    3633        putValue("help", ht("/Action/ToggleDialogsPanel"));
    37         selected = Main.pref.getBoolean("draw.dialogspanel", true);
     34        setSelected(Main.pref.getBoolean("draw.dialogspanel", true));
    3835        notifySelectedState();
    3936    }
    4037
    41     public void addButtonModel(ButtonModel model) {
    42         if (model != null && !buttonModels.contains(model)) {
    43             buttonModels.add(model);
    44         }
    45     }
    46 
    47     public void removeButtonModel(ButtonModel model) {
    48         if (model != null && buttonModels.contains(model)) {
    49             buttonModels.remove(model);
    50         }
    51     }
    52 
    53     protected void notifySelectedState() {
    54         for (ButtonModel model: buttonModels) {
    55             if (model.isSelected() != selected) {
    56                 model.setSelected(selected);
    57             }
    58         }
    59     }
    60 
    61     protected void toggleSelectedState() {
    62         selected = !selected;
    63         Main.pref.put("draw.dialogspanel", selected);
     38    @Override
     39    public void actionPerformed(ActionEvent e) {
     40        toggleSelectedState();
     41        Main.pref.put("draw.dialogspanel", isSelected());
    6442        notifySelectedState();
    6543        setMode();
    6644    }
    6745
     46    /**
     47     * To call if this action must be initially run at JOSM startup.
     48     */
    6849    public void initial() {
    69         if(selected) {
     50        if (isSelected()) {
    7051            setMode();
    7152        }
     
    7455    protected void setMode() {
    7556        if (Main.isDisplayingMapView()) {
     57            boolean selected = isSelected();
    7658            Main.map.setDialogsPanelVisible(selected);
    7759            Main.map.statusLine.setVisible(selected || Main.pref.getBoolean("statusbar.always-visible", true));
     
    8567        }
    8668    }
    87 
    88     @Override
    89     public void actionPerformed(ActionEvent e) {
    90         toggleSelectedState();
    91     }
    9269}
  • trunk/src/org/openstreetmap/josm/actions/ExpertToggleAction.java

    r6084 r6220  
    1111import java.util.List;
    1212
    13 import javax.swing.ButtonModel;
    14 
    1513import org.openstreetmap.josm.Main;
    1614
    17 public class ExpertToggleAction extends JosmAction {
    18 
    19     private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
    20     private boolean selected;
     15/**
     16 * This action toggles the Expert mode.
     17 * @since 4840
     18 */
     19public class ExpertToggleAction extends ToggleAction {
    2120
    2221    public interface ExpertModeChangeListener {
     
    118117    }
    119118
     119    /**
     120     * Constructs a new {@code ExpertToggleAction}.
     121     */
    120122    public ExpertToggleAction() {
    121         super(
    122                 tr("Expert Mode"),
    123                 "expert",
    124                 tr("Enable/disable expert mode"),
    125                 null,
    126                 false /* register toolbar */
     123        super(tr("Expert Mode"),
     124              "expert",
     125              tr("Enable/disable expert mode"),
     126              null,
     127              false /* register toolbar */
    127128        );
    128129        putValue("toolbar", "expertmode");
    129130        Main.toolbar.register(this);
    130         selected = Main.pref.getBoolean("expert", false);
     131        setSelected(Main.pref.getBoolean("expert", false));
    131132        notifySelectedState();
    132133    }
    133134
    134     public void addButtonModel(ButtonModel model) {
    135         if (model != null && !buttonModels.contains(model)) {
    136             buttonModels.add(model);
    137             model.setSelected(selected);
    138         }
    139     }
    140 
    141     public void removeButtonModel(ButtonModel model) {
    142         if (model != null && buttonModels.contains(model)) {
    143             buttonModels.remove(model);
    144         }
    145     }
    146 
    147135    protected void notifySelectedState() {
    148         for (ButtonModel model: buttonModels) {
    149             if (model.isSelected() != selected) {
    150                 model.setSelected(selected);
    151             }
    152         }
    153         fireExpertModeChanged(selected);
    154     }
    155 
    156     protected void toggleSelectedState() {
    157         selected = !selected;
    158         Main.pref.put("expert", selected);
    159         notifySelectedState();
     136        super.notifySelectedState();
     137        fireExpertModeChanged(isSelected());
    160138    }
    161139
     
    163141    public void actionPerformed(ActionEvent e) {
    164142        toggleSelectedState();
     143        Main.pref.put("expert", isSelected());
     144        notifySelectedState();
    165145    }
    166146
    167     public boolean isSelected() {
    168         return selected;
    169     }
    170 
     147    /**
     148     * Replies the unique instance of this action.
     149     * @return The unique instance of this action
     150     */
    171151    public static ExpertToggleAction getInstance() {
    172152        return INSTANCE;
    173153    }
    174154
     155    /**
     156     * Determines if expert mode is enabled.
     157     * @return {@code true} if expert mode is enabled, {@code false} otherwise.
     158     */
    175159    public static boolean isExpert() {
    176160        return INSTANCE.isSelected();
  • trunk/src/org/openstreetmap/josm/actions/FullscreenToggleAction.java

    r6084 r6220  
    1616import java.util.List;
    1717
    18 import javax.swing.ButtonModel;
    1918import javax.swing.JComponent;
    2019import javax.swing.JFrame;
     
    2524import org.openstreetmap.josm.tools.Shortcut;
    2625
    27 public class FullscreenToggleAction extends JosmAction {
    28     private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
    29     private boolean selected;
     26/**
     27 * This class toggles the full-screen mode.
     28 * @since 2533
     29 */
     30public class FullscreenToggleAction extends ToggleAction {
    3031    private GraphicsDevice gd;
    3132    private Rectangle prevBounds;
    3233
     34    /**
     35     * Constructs a new {@code FullscreenToggleAction}.
     36     */
    3337    public FullscreenToggleAction() {
    34         super(
    35                 tr("Fullscreen view"),
    36                 null, /* no icon */
    37                 tr("Toggle fullscreen view"),
    38                 Shortcut.registerShortcut("menu:view:fullscreen", tr("Toggle fullscreen view"),KeyEvent.VK_F11, Shortcut.DIRECT),
    39                 false /* register */
     38        super(tr("Fullscreen view"),
     39              null, /* no icon */
     40              tr("Toggle fullscreen view"),
     41              Shortcut.registerShortcut("menu:view:fullscreen", tr("Toggle fullscreen view"),KeyEvent.VK_F11, Shortcut.DIRECT),
     42              false /* register */
    4043        );
    4144        putValue("help", ht("/Action/FullscreenView"));
     
    4346        Main.toolbar.register(this);
    4447        gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    45         selected = Main.pref.getBoolean("draw.fullscreen", false);
     48        setSelected(Main.pref.getBoolean("draw.fullscreen", false));
    4649        notifySelectedState();
    4750    }
    4851
    49     public void addButtonModel(ButtonModel model) {
    50         if (model != null && !buttonModels.contains(model)) {
    51             buttonModels.add(model);
    52         }
    53     }
    54 
    55     public void removeButtonModel(ButtonModel model) {
    56         if (model != null && buttonModels.contains(model)) {
    57             buttonModels.remove(model);
    58         }
    59     }
    60 
    61     protected void notifySelectedState() {
    62         for (ButtonModel model: buttonModels) {
    63             if (model.isSelected() != selected) {
    64                 model.setSelected(selected);
    65             }
    66         }
    67     }
    68 
    69     protected void toggleSelectedState() {
    70         selected = !selected;
    71         Main.pref.put("draw.fullscreen", selected);
     52    @Override
     53    public void actionPerformed(ActionEvent e) {
     54        toggleSelectedState();
     55        Main.pref.put("draw.fullscreen", isSelected());
    7256        notifySelectedState();
    7357        setMode();
    7458    }
    7559
     60    /**
     61     * To call if this action must be initially run at JOSM startup.
     62     */
    7663    public void initial() {
    77         if(selected)
     64        if (isSelected()) {
    7865            setMode();
     66        }
    7967    }
    8068
     
    8977            }
    9078        }
     79       
     80        boolean selected = isSelected();
    9181
    9282        frame.dispose();
     
    120110        frame.getJMenuBar().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");
    121111    }
    122 
    123     @Override
    124     public void actionPerformed(ActionEvent e) {
    125         toggleSelectedState();
    126     }
    127112}
  • trunk/src/org/openstreetmap/josm/actions/ViewportFollowToggleAction.java

    r6084 r6220  
    77import java.awt.event.ActionEvent;
    88import java.awt.event.KeyEvent;
    9 import java.util.ArrayList;
    10 import java.util.List;
    11 
    12 import javax.swing.ButtonModel;
    139
    1410import org.openstreetmap.josm.Main;
    1511import org.openstreetmap.josm.tools.Shortcut;
    1612
    17 public class ViewportFollowToggleAction extends JosmAction {
    18     private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
    19     private boolean selected;
     13/**
     14 * This action toggles automatic moving of the map view to last placed node
     15 * @since 3837
     16 */
     17public class ViewportFollowToggleAction extends ToggleAction {
     18   
     19    /**
     20     * Constructs a new {@code ViewportFollowToggleAction}.
     21     */
    2022    public ViewportFollowToggleAction() {
    21         super(
    22                 tr("Viewport Following"),
    23                 "viewport-follow",
    24                 tr("Enable/disable automatic moving of the map view to last placed node"),
    25                 Shortcut.registerShortcut("menu:view:viewportfollow", tr("Toggle Viewport Following"),
    26                 KeyEvent.VK_F, Shortcut.CTRL_SHIFT),
    27                 true /* register shortcut */
     23        super(tr("Viewport Following"),
     24              "viewport-follow",
     25              tr("Enable/disable automatic moving of the map view to last placed node"),
     26              Shortcut.registerShortcut("menu:view:viewportfollow", tr("Toggle Viewport Following"),
     27              KeyEvent.VK_F, Shortcut.CTRL_SHIFT),
     28              true /* register shortcut */
    2829        );
    2930        putValue("help", ht("/Action/ViewportFollowing"));
    30         selected = false;
     31        setSelected(false);
    3132        notifySelectedState();
    3233    }
    3334
    34     public void addButtonModel(ButtonModel model) {
    35         if (model != null && !buttonModels.contains(model)) {
    36             buttonModels.add(model);
    37             model.setSelected(selected);
    38         }
    39     }
    40 
    41     public void removeButtonModel(ButtonModel model) {
    42         if (model != null && buttonModels.contains(model)) {
    43             buttonModels.remove(model);
    44         }
    45     }
    46 
    47     protected void notifySelectedState() {
    48         for (ButtonModel model: buttonModels) {
    49             if (model.isSelected() != selected) {
    50                 model.setSelected(selected);
    51             }
    52         }
    53     }
    54 
    55     protected void toggleSelectedState() {
    56         selected = !selected;
    57         Main.map.mapView.viewportFollowing = selected;
    58         notifySelectedState();
    59     }
    6035    @Override
    6136    public void actionPerformed(ActionEvent e) {
    6237        toggleSelectedState();
     38        Main.map.mapView.viewportFollowing = isSelected();
     39        notifySelectedState();
    6340    }
    6441
  • trunk/src/org/openstreetmap/josm/actions/WireframeToggleAction.java

    r6084 r6220  
    66import java.awt.event.ActionEvent;
    77import java.awt.event.KeyEvent;
    8 import java.util.ArrayList;
    9 import java.util.List;
    10 
    11 import javax.swing.ButtonModel;
    128
    139import org.openstreetmap.josm.Main;
     
    1713import org.openstreetmap.josm.tools.Shortcut;
    1814
    19 public class WireframeToggleAction extends JosmAction {
    20     private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
    21     //FIXME: replace with property Action.SELECTED_KEY when migrating to
    22     // Java 6
    23     private boolean selected;
     15/**
     16 * This class toggles the wireframe rendering mode.
     17 * @since 2530
     18 */
     19public class WireframeToggleAction extends ToggleAction {
     20
     21    /**
     22     * Constructs a new {@code WireframeToggleAction}.
     23     */
    2424    public WireframeToggleAction() {
    25         super(
    26                 tr("Wireframe View"),
    27                 null, /* no icon */
    28                 tr("Enable/disable rendering the map as wireframe only"),
    29                 Shortcut.registerShortcut("menu:view:wireframe", tr("Toggle Wireframe view"),KeyEvent.VK_W, Shortcut.CTRL),
    30                 false /* register toolbar */
     25        super(tr("Wireframe View"),
     26              null, /* no icon */
     27              tr("Enable/disable rendering the map as wireframe only"),
     28              Shortcut.registerShortcut("menu:view:wireframe", tr("Toggle Wireframe view"),KeyEvent.VK_W, Shortcut.CTRL),
     29              false /* register toolbar */
    3130        );
    3231        putValue("toolbar", "wireframe");
    3332        Main.toolbar.register(this);
    34         selected = MapRendererFactory.getInstance().isWireframeMapRendererActive();
     33        setSelected(MapRendererFactory.getInstance().isWireframeMapRendererActive());
    3534        notifySelectedState();
    36     }
    37 
    38     public void addButtonModel(ButtonModel model) {
    39         if (model != null && !buttonModels.contains(model)) {
    40             buttonModels.add(model);
    41             model.setSelected(selected);
    42         }
    43     }
    44 
    45     public void removeButtonModel(ButtonModel model) {
    46         if (model != null && buttonModels.contains(model)) {
    47             buttonModels.remove(model);
    48         }
    49     }
    50 
    51     protected void notifySelectedState() {
    52         for (ButtonModel model: buttonModels) {
    53             if (model.isSelected() != selected) {
    54                 model.setSelected(selected);
    55             }
    56         }
    57     }
    58 
    59     protected void toggleSelectedState() {
    60         selected = !selected;
    61         //Main.pref.put("draw.wireframe", selected);
    62          if (selected){
    63          MapRendererFactory.getInstance().activate(WireframeMapRenderer.class);
    64             } else {
    65                 MapRendererFactory.getInstance().activate(StyledMapRenderer.class);
    66             }
    67 
    68         notifySelectedState();
    69         if (Main.isDisplayingMapView()) {
    70             Main.map.mapView.repaint();
    71         }
    72     }
    73 
    74     @Override
    75     public void actionPerformed(ActionEvent e) {
    76         toggleSelectedState();
    7735    }
    7836
     
    8240    }
    8341
    84     public boolean isSelected() {
    85         return selected;
     42    @Override
     43    public void actionPerformed(ActionEvent e) {
     44        toggleSelectedState();
     45        if (isSelected()) {
     46            MapRendererFactory.getInstance().activate(WireframeMapRenderer.class);
     47        } else {
     48            MapRendererFactory.getInstance().activate(StyledMapRenderer.class);
     49        }
     50
     51        notifySelectedState();
     52        if (Main.isDisplayingMapView()) {
     53            Main.map.mapView.repaint();
     54        }
    8655    }
    8756}
Note: See TracChangeset for help on using the changeset viewer.