Changeset 36081 in osm for applications/editors


Ignore:
Timestamp:
2023-05-12T14:34:31+02:00 (20 months ago)
Author:
taylor.smock
Message:

Fix #22623: BuildingTools: simpler mode switching

This additionally moves the building tool from Data to More tools and fixes
some SonarLint issues.

Location:
applications/editors/josm/plugins/buildings_tools
Files:
9 added
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/buildings_tools/src/org/openstreetmap/josm/plugins/buildings_tools/AdvancedSettingsDialog.java

    r35669 r36081  
    1414import org.openstreetmap.josm.tools.GBC;
    1515
     16/**
     17 * A dialog for users to set "advanced" settings for the tool
     18 */
    1619public class AdvancedSettingsDialog extends MyDialog {
    1720    private final TagEditorModel tagsModel = new TagEditorModel();
     
    2023    private final JCheckBox cSoftCur = new JCheckBox(tr("Rotate crosshair"));
    2124    private final JCheckBox cNoClickDrag = new JCheckBox(tr("Disable click+drag"));
     25    private final JCheckBox cToggleMapMode = new JCheckBox(tr("Switch between circle and rectangle modes"));
    2226
     27    /**
     28     * Create a new advanced settings dialog
     29     */
    2330    public AdvancedSettingsDialog() {
    2431        super(tr("Advanced settings"));
     
    3441        panel.add(cSoftCur, GBC.eol().fill(GBC.HORIZONTAL));
    3542        panel.add(cNoClickDrag, GBC.eol().fill(GBC.HORIZONTAL));
     43        panel.add(cToggleMapMode, GBC.eol().fill(GBC.HORIZONTAL));
    3644
    3745        cBigMode.setSelected(ToolSettings.isBBMode());
    3846        cSoftCur.setSelected(ToolSettings.isSoftCursor());
    3947        cNoClickDrag.setSelected(ToolSettings.isNoClickAndDrag());
     48        cToggleMapMode.setSelected(ToolSettings.isTogglingBuildingTypeOnRepeatedKeyPress());
     49
     50        cToggleMapMode.setToolTipText(tr("This is similar to the select action toggling between lasso and rectangle select modes"));
    4051
    4152        setupDialog();
     
    4354    }
    4455
     56    /**
     57     * Save the settings
     58     */
    4559    public final void saveSettings() {
    4660        ToolSettings.saveTags(tagsModel.getTags());
     
    4862        ToolSettings.setSoftCursor(cSoftCur.isSelected());
    4963        ToolSettings.setNoClickAndDrag(cNoClickDrag.isSelected());
     64        ToolSettings.setTogglingBuildingTypeOnRepeatedKeyPress(cToggleMapMode.isSelected());
    5065    }
    5166}
  • applications/editors/josm/plugins/buildings_tools/src/org/openstreetmap/josm/plugins/buildings_tools/BuildingsToolsPlugin.java

    r35978 r36081  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.plugins.buildings_tools;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import javax.swing.JMenu;
     
    1517import org.openstreetmap.josm.plugins.Plugin;
    1618import org.openstreetmap.josm.plugins.PluginInformation;
     19import org.openstreetmap.josm.tools.ImageProvider;
    1720
     21/**
     22 * The entry point for the buildings tools plugin
     23 */
    1824public class BuildingsToolsPlugin extends Plugin {
    1925    public static final Projection MERCATOR = Projections.getProjectionByCode("EPSG:3857"); // Mercator
    2026
     27    /**
     28     * Convert a latlon to east north
     29     * @param p The latlon to convert
     30     * @return The east-north ({@link #MERCATOR})
     31     */
    2132    public static EastNorth latlon2eastNorth(ILatLon p) {
    2233        return MERCATOR.latlon2eastNorth(p);
    2334    }
    2435
     36    /**
     37     * Convert an east-north to a latlon
     38     * @param p The east north to convert (from {@link #MERCATOR})
     39     * @return The latlon
     40     */
    2541    public static LatLon eastNorth2latlon(EastNorth p) {
    2642        return MERCATOR.eastNorth2latlon(p);
     
    2945    public BuildingsToolsPlugin(PluginInformation info) {
    3046        super(info);
    31         JMenu dataMenu = MainApplication.getMenu().dataMenu;
    32         MainMenu.add(dataMenu, new BuildingSizeAction());
    33         MainMenu.add(dataMenu, new BuildingCircleAction());
    34         MainMenu.add(dataMenu, new BuildingRectangleAction());
    35         MainMenu.add(dataMenu, new MergeAddrPointsAction());
     47        JMenu moreToolsMenu = MainApplication.getMenu().moreToolsMenu;
     48        if (moreToolsMenu.getMenuComponentCount() > 0) {
     49            moreToolsMenu.addSeparator();
     50        }
     51        MainMenu.add(moreToolsMenu, new DrawBuildingAction());
     52        JMenu optionMenu = new JMenu(tr("Draw buildings modes"));
     53        optionMenu.setIcon(ImageProvider.get("preference_small", ImageProvider.ImageSizes.MENU));
     54        moreToolsMenu.add(optionMenu);
     55        MainMenu.add(optionMenu, new BuildingSizeAction());
     56        MainMenu.add(optionMenu, new BuildingCircleAction());
     57        MainMenu.add(optionMenu, new BuildingRectangleAction());
     58        MainMenu.add(optionMenu, new MergeAddrPointsAction());
    3659    }
    3760
     
    3962    public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
    4063        if (oldFrame == null && newFrame != null) {
    41             MainApplication.getMap().addMapMode(new IconToggleButton(new DrawBuildingAction()));
     64            newFrame.addMapMode(new IconToggleButton(new DrawBuildingAction()));
    4265        }
    4366    }
  • applications/editors/josm/plugins/buildings_tools/src/org/openstreetmap/josm/plugins/buildings_tools/DrawBuildingAction.java

    r35997 r36081  
    2323import org.openstreetmap.josm.tools.Geometry;
    2424import org.openstreetmap.josm.tools.ImageProvider;
    25 import org.openstreetmap.josm.tools.Logging;
    2625import org.openstreetmap.josm.tools.Shortcut;
    2726
     
    4544import static org.openstreetmap.josm.tools.I18n.tr;
    4645
     46/**
     47 * The action for drawing the building
     48 */
    4749public class DrawBuildingAction extends MapMode implements MapViewPaintable, DataSelectionListener,
    4850        KeyPressReleaseListener, ModifierExListener {
     51    private static final long serialVersionUID = -3515263157730927711L;
    4952    // We need to avoid opening many file descriptors on Linux under Wayland -- see JOSM #21929. This will probably also
    5053    // improve performance, since we aren't creating cursors all the time.
     
    7376    private final PreferenceChangedListener shapeChangeListener = event -> updCursor();
    7477
     78    /**
     79     * Create a new {@link DrawBuildingAction} object
     80     */
    7581    public DrawBuildingAction() {
    7682        super(tr("Draw buildings"), "building", tr("Draw buildings"),
     
    8894
    8995    private static Cursor getCursor() {
    90         try {
    91             if (ToolSettings.Shape.CIRCLE == ToolSettings.getShape()) {
     96        switch (ToolSettings.getShape()) {
     97            case RECTANGLE:
     98                return CURSOR_BUILDING;
     99            case CIRCLE:
    92100                return CURSOR_SILO;
    93             } else {
    94                 return CURSOR_BUILDING;
    95             }
    96         } catch (Exception e) {
    97             Logging.error(e);
    98         }
    99         return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
     101            default:
     102                return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
     103        }
    100104    }
    101105
     
    161165    }
    162166
     167    /**
     168     * Cancel the drawing of a building
     169     */
    163170    public final void cancelDrawing() {
    164171        mode = Mode.None;
     
    194201            cancelDrawing();
    195202        }
     203        if (!ToolSettings.isTogglingBuildingTypeOnRepeatedKeyPress()
     204                || !MainApplication.isDisplayingMapView() || !getShortcut().isEvent(e)) {
     205            return;
     206        }
     207        e.consume();
     208        switch (ToolSettings.getShape()) {
     209            case CIRCLE:
     210                ToolSettings.saveShape(ToolSettings.Shape.RECTANGLE);
     211                break;
     212            case RECTANGLE:
     213                ToolSettings.saveShape(ToolSettings.Shape.CIRCLE);
     214        }
    196215    }
    197216
    198217    @Override
    199218    public void doKeyReleased(KeyEvent e) {
     219        // Do nothing
    200220    }
    201221
     
    437457    }
    438458
     459    /**
     460     * Update the snap for drawing
     461     * @param newSelection The selection to use
     462     */
    439463    public final void updateSnap(Collection<? extends OsmPrimitive> newSelection) {
    440464        building.clearAngleSnap();
  • applications/editors/josm/plugins/buildings_tools/src/org/openstreetmap/josm/plugins/buildings_tools/ToolSettings.java

    r35978 r36081  
    55import java.util.Arrays;
    66import java.util.Collection;
     7import java.util.Collections;
    78import java.util.HashMap;
    89import java.util.Iterator;
     
    1415import org.openstreetmap.josm.tools.Logging;
    1516
     17/**
     18 * A holder for plugin settings
     19 */
    1620public final class ToolSettings {
    1721
     
    2226    public static final BooleanProperty PROP_USE_ADDR_NODE = new BooleanProperty("buildings_tools.addrNode", false);
    2327
     28    /**
     29     * The shapes that users can create
     30     */
    2431    public enum Shape {
    2532            CIRCLE, RECTANGLE
     
    3239    private static final Map<String, String> TAGS = new HashMap<>();
    3340
     41    /**
     42     * Get the current shape that will be created
     43     * @return The shape
     44     */
    3445    public static Shape getShape() {
    3546        loadShape();
     
    3748    }
    3849
     50    /**
     51     * Set whether to use the address dialog
     52     * @param useAddr {@code true} if the address dialog should be used
     53     */
    3954    public static void setAddrDialog(boolean useAddr) {
    4055        ToolSettings.useAddr = useAddr;
    4156    }
    4257
     58    /**
     59     * Set some constraints for new buildings
     60     * @param newwidth The max width ({@link Shape#RECTANGLE}) or max diameter ({@link Shape#CIRCLE})
     61     * @param newlenstep The step sizes to use when setting the length of a {@link Shape#RECTANGLE}
     62     */
    4363    public static void setSizes(double newwidth, double newlenstep) {
    4464        width = newwidth;
     
    4666    }
    4767
     68    /**
     69     * Get the width/diameter
     70     * @return The max width ({@link Shape#RECTANGLE}) or max diameter ({@link Shape#CIRCLE})
     71     */
    4872    public static double getWidth() {
    4973        return width;
    5074    }
    5175
     76    /**
     77     * Get the step length for {@link Shape#RECTANGLE}
     78     * @return The amount to increase the length of a {@link Shape#RECTANGLE}.
     79     */
    5280    public static double getLenStep() {
    5381        return lenstep;
    5482    }
    5583
     84    /**
     85     * Check if we want to show the user an address dialog
     86     * @return {@code true} if the user should be shown the address dialog
     87     */
    5688    public static boolean isUsingAddr() {
    5789        return useAddr;
    5890    }
    5991
     92    /**
     93     * Get the tags that the user wants to set on all new objects
     94     * @return The tag map
     95     */
    6096    public static Map<String, String> getTags() {
    6197        loadTags();
    62         return TAGS;
    63     }
    64 
     98        return Collections.unmodifiableMap(TAGS);
     99    }
     100
     101    /**
     102     * Set the tags to put on all new objects
     103     * @param tags The tags to set
     104     */
    65105    public static void saveTags(Map<String, String> tags) {
    66106        TAGS.clear();
     
    74114    }
    75115
     116    /**
     117     * Load tags from preferences
     118     */
    76119    private static void loadTags() {
    77120        TAGS.clear();
     
    87130    }
    88131
     132    /**
     133     * Set the shape to use
     134     * @param shape The shape
     135     */
    89136    public static void saveShape(Shape shape) {
    90137        Config.getPref().put("buildings_tool.shape", shape.name());
    91138    }
    92139
     140    /**
     141     * Load the shape to use from preferences
     142     * @return The shape to use
     143     */
    93144    private static Shape loadShape() {
    94145        String shape = Config.getPref().get("buildings_tool.shape");
     
    102153    }
    103154
     155    /**
     156     * Set the Big buildings mode
     157     * @param bbmode {@code true} if big building mode should be used
     158     */
    104159    public static void setBBMode(boolean bbmode) {
    105160        Config.getPref().putBoolean("buildings_tools.bbmode", bbmode);
    106161    }
    107162
     163    /**
     164     * Get the Big buildings mode
     165     * @return {@code true} if big building mode should be used
     166     */
    108167    public static boolean isBBMode() {
    109168        return Config.getPref().getBoolean("buildings_tools.bbmode", false);
     
    134193    }
    135194
     195    /**
     196     * Check if we are toggling between {@link Shape} types if the user toggles the mapmode with a keypress
     197     * @return {@code true} if we want to change the shape type
     198     */
     199    public static boolean isTogglingBuildingTypeOnRepeatedKeyPress() {
     200        return Config.getPref().getBoolean("buildings_tools.toggle_building_type", false);
     201    }
     202
     203    /**
     204     * Set whether or not we are toggling between {@link Shape} types if the user toggles the mapmode with a keypress
     205     * @param toggle {@code true} if we want to change the shape type
     206     */
     207    public static void setTogglingBuildingTypeOnRepeatedKeyPress(boolean toggle) {
     208        Config.getPref().putBoolean("buildings_tools.toggle_building_type", toggle);
     209    }
     210
    136211    public static boolean isNoClickAndDrag() {
    137212        return Config.getPref().getBoolean("buildings_tools.noclickdrag", false);
Note: See TracChangeset for help on using the changeset viewer.