Ticket #22623: 22623.2.patch
File 22623.2.patch, 13.0 KB (added by , 2 years ago) |
---|
-
src/org/openstreetmap/josm/plugins/buildings_tools/AdvancedSettingsDialog.java
19 19 private final JCheckBox cBigMode = new JCheckBox(tr("Big buildings mode")); 20 20 private final JCheckBox cSoftCur = new JCheckBox(tr("Rotate crosshair")); 21 21 private final JCheckBox cNoClickDrag = new JCheckBox(tr("Disable click+drag")); 22 private final JCheckBox cToggleMapMode = new JCheckBox(tr("Switch between circle and rectangle modes")); 22 23 23 24 public AdvancedSettingsDialog() { 24 25 super(tr("Advanced settings")); … … 33 34 panel.add(cBigMode, GBC.eol().fill(GBC.HORIZONTAL)); 34 35 panel.add(cSoftCur, GBC.eol().fill(GBC.HORIZONTAL)); 35 36 panel.add(cNoClickDrag, GBC.eol().fill(GBC.HORIZONTAL)); 37 panel.add(cToggleMapMode, GBC.eol().fill(GBC.HORIZONTAL)); 36 38 37 39 cBigMode.setSelected(ToolSettings.isBBMode()); 38 40 cSoftCur.setSelected(ToolSettings.isSoftCursor()); 39 41 cNoClickDrag.setSelected(ToolSettings.isNoClickAndDrag()); 42 cToggleMapMode.setSelected(ToolSettings.isTogglingBuildingTypeOnRepeatedKeyPress()); 40 43 44 cToggleMapMode.setToolTipText(tr("This is similar to the select action toggling between lasso and rectangle select modes")); 45 41 46 setupDialog(); 42 47 showDialog(); 43 48 } … … 47 52 ToolSettings.setBBMode(cBigMode.isSelected()); 48 53 ToolSettings.setSoftCursor(cSoftCur.isSelected()); 49 54 ToolSettings.setNoClickAndDrag(cNoClickDrag.isSelected()); 55 ToolSettings.setTogglingBuildingTypeOnRepeatedKeyPress(cToggleMapMode.isSelected()); 50 56 } 51 57 } -
src/org/openstreetmap/josm/plugins/buildings_tools/BuildingsToolsPlugin.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.plugins.buildings_tools; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 4 6 import javax.swing.JMenu; 5 7 6 8 import org.openstreetmap.josm.data.coor.EastNorth; … … 14 16 import org.openstreetmap.josm.gui.MapFrame; 15 17 import org.openstreetmap.josm.plugins.Plugin; 16 18 import org.openstreetmap.josm.plugins.PluginInformation; 19 import org.openstreetmap.josm.tools.ImageProvider; 17 20 21 /** 22 * The entry point for the buildings tools plugin 23 */ 18 24 public class BuildingsToolsPlugin extends Plugin { 19 25 public static final Projection MERCATOR = Projections.getProjectionByCode("EPSG:3857"); // Mercator 20 26 … … 28 34 29 35 public BuildingsToolsPlugin(PluginInformation info) { 30 36 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()); 37 JMenu moreToolsMenu = MainApplication.getMenu().moreToolsMenu; 38 if (moreToolsMenu.getMenuComponentCount() > 0) { 39 moreToolsMenu.addSeparator(); 40 } 41 MainMenu.add(moreToolsMenu, new DrawBuildingAction()); 42 JMenu optionMenu = new JMenu(tr("Draw buildings modes")); 43 optionMenu.setIcon(ImageProvider.get("preference_small", ImageProvider.ImageSizes.MENU)); 44 moreToolsMenu.add(optionMenu); 45 MainMenu.add(optionMenu, new BuildingSizeAction()); 46 MainMenu.add(optionMenu, new BuildingCircleAction()); 47 MainMenu.add(optionMenu, new BuildingRectangleAction()); 48 MainMenu.add(optionMenu, new MergeAddrPointsAction()); 36 49 } 37 50 38 51 @Override 39 52 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 40 53 if (oldFrame == null && newFrame != null) { 41 MainApplication.getMap().addMapMode(new IconToggleButton(new DrawBuildingAction()));54 newFrame.addMapMode(new IconToggleButton(new DrawBuildingAction())); 42 55 } 43 56 } 44 57 } -
src/org/openstreetmap/josm/plugins/buildings_tools/DrawBuildingAction.java
44 44 import static org.openstreetmap.josm.tools.I18n.marktr; 45 45 import static org.openstreetmap.josm.tools.I18n.tr; 46 46 47 /** 48 * The action for drawing the building 49 */ 47 50 public class DrawBuildingAction extends MapMode implements MapViewPaintable, DataSelectionListener, 48 51 KeyPressReleaseListener, ModifierExListener { 52 private static final long serialVersionUID = -3515263157730927711L; 49 53 // We need to avoid opening many file descriptors on Linux under Wayland -- see JOSM #21929. This will probably also 50 54 // improve performance, since we aren't creating cursors all the time. 51 55 private static final Cursor CURSOR_SILO = ImageProvider.getCursor("crosshair", "silo"); … … 72 76 73 77 private final PreferenceChangedListener shapeChangeListener = event -> updCursor(); 74 78 79 /** 80 * Create a new {@link DrawBuildingAction} object 81 */ 75 82 public DrawBuildingAction() { 76 83 super(tr("Draw buildings"), "building", tr("Draw buildings"), 77 84 Shortcut.registerShortcut("mapmode:buildings", … … 193 200 194 201 cancelDrawing(); 195 202 } 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 } 196 215 } 197 216 198 217 @Override -
src/org/openstreetmap/josm/plugins/buildings_tools/ToolSettings.java
133 133 Config.getPref().putBoolean("buildings_tools.autoselect_replace_selection", autoSelectReplace); 134 134 } 135 135 136 /** 137 * Check if we are toggling between {@link Shape} types if the user toggles the mapmode with a keypress 138 * @return {@code true} if we want to change the shape type 139 */ 140 public static boolean isTogglingBuildingTypeOnRepeatedKeyPress() { 141 return Config.getPref().getBoolean("buildings_tools.toggle_building_type", false); 142 } 143 144 /** 145 * Set whether or not we are toggling between {@link Shape} types if the user toggles the mapmode with a keypress 146 * @param toggle {@code true} if we want to change the shape type 147 */ 148 public static void setTogglingBuildingTypeOnRepeatedKeyPress(boolean toggle) { 149 Config.getPref().putBoolean("buildings_tools.toggle_building_type", toggle); 150 } 151 136 152 public static boolean isNoClickAndDrag() { 137 153 return Config.getPref().getBoolean("buildings_tools.noclickdrag", false); 138 154 } -
test/unit/org/openstreetmap/josm/plugins/buildings_tools/BuildingsToolsPluginTest.java
1 package org.openstreetmap.josm.plugins.buildings_tools; 2 3 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 4 5 import java.util.jar.Attributes; 6 7 import org.junit.jupiter.api.Test; 8 import org.junit.jupiter.api.extension.RegisterExtension; 9 import org.openstreetmap.josm.data.osm.DataSet; 10 import org.openstreetmap.josm.gui.MainApplication; 11 import org.openstreetmap.josm.gui.layer.Layer; 12 import org.openstreetmap.josm.gui.layer.LayerManager; 13 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 14 import org.openstreetmap.josm.plugins.PluginException; 15 import org.openstreetmap.josm.plugins.PluginInformation; 16 import org.openstreetmap.josm.testutils.JOSMTestRules; 17 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 18 19 /** 20 * Test class for {@link BuildingsToolsPlugin} 21 */ 22 @BasicPreferences 23 class BuildingsToolsPluginTest { 24 @RegisterExtension 25 static JOSMTestRules rule = new JOSMTestRules().projection().main(); 26 27 /** 28 * This makes certain we don't have an IAE after removing last layer and adding a new one 29 * @throws PluginException If something occurs during {@link PluginInformation} construction 30 */ 31 @Test 32 void testMapReinitialization() throws PluginException { 33 final BuildingsToolsPlugin plugin = 34 new BuildingsToolsPlugin(new PluginInformation(new Attributes(), "buildings_tools", "https://example.com")); 35 MainApplication.getMainPanel().addMapFrameListener(plugin); 36 try { 37 final LayerManager layerManager = MainApplication.getLayerManager(); 38 for (int i = 0; i < 20; i++) { 39 final Layer layer = new OsmDataLayer(new DataSet(), "testMapReinitialization", null); 40 assertDoesNotThrow(() -> layerManager.addLayer(layer)); 41 assertDoesNotThrow(() -> layerManager.removeLayer(layer)); 42 } 43 } finally { 44 MainApplication.getMainPanel().removeMapFrameListener(plugin); 45 } 46 } 47 48 } 49 No newline at end of file -
test/unit/org/openstreetmap/josm/plugins/buildings_tools/DrawBuildingActionTest.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.buildings_tools; 3 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 import static org.junit.jupiter.api.Assertions.assertNotEquals; 6 import static org.junit.jupiter.api.Assertions.assertNotNull; 7 8 import java.awt.event.KeyEvent; 9 import java.time.Instant; 10 11 import javax.swing.JLabel; 12 import javax.swing.KeyStroke; 13 14 import org.junit.jupiter.api.AfterAll; 15 import org.junit.jupiter.api.BeforeAll; 16 import org.junit.jupiter.api.extension.RegisterExtension; 17 import org.junit.jupiter.params.ParameterizedTest; 18 import org.junit.jupiter.params.provider.ValueSource; 19 import org.openstreetmap.josm.data.osm.DataSet; 20 import org.openstreetmap.josm.gui.MainApplication; 21 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 22 import org.openstreetmap.josm.testutils.JOSMTestRules; 23 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 24 25 /** 26 * Test class for {@link DrawBuildingAction} 27 */ 28 @BasicPreferences 29 class DrawBuildingActionTest { 30 @RegisterExtension 31 static JOSMTestRules rule = new JOSMTestRules().main().projection(); 32 33 private static DrawBuildingAction action; 34 35 @BeforeAll 36 static void setup() { 37 action = new DrawBuildingAction(); 38 } 39 40 @AfterAll 41 static void tearDown() { 42 action.destroy(); 43 action = null; 44 } 45 46 47 /** 48 * Ensure that we are toggling the map mode properly 49 */ 50 @ParameterizedTest 51 @ValueSource(booleans = {false, true}) 52 void testToggle(boolean setToggle) { 53 // Ensure we are showing the main map 54 MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(), "testToggle", null)); 55 ToolSettings.setTogglingBuildingTypeOnRepeatedKeyPress(setToggle); 56 assertEquals(setToggle, ToolSettings.isTogglingBuildingTypeOnRepeatedKeyPress()); 57 final ToolSettings.Shape shape = ToolSettings.getShape(); 58 final KeyStroke keyStroke = action.getShortcut().getKeyStroke(); 59 MainApplication.getMap().selectMapMode(action); 60 action.doKeyPressed(getKeyEvent(keyStroke)); 61 assertNotEquals(setToggle, shape == ToolSettings.getShape()); 62 action.doKeyPressed(getKeyEvent(keyStroke)); 63 assertEquals(shape, ToolSettings.getShape()); 64 } 65 66 /** 67 * Get a key event to send the action 68 * @param keyStroke The keystroke to use 69 * @return The event 70 */ 71 private KeyEvent getKeyEvent(KeyStroke keyStroke) { 72 assertNotNull(keyStroke); 73 return new KeyEvent(new JLabel(), KeyEvent.KEY_PRESSED, Instant.now().toEpochMilli(), 74 keyStroke.getModifiers(), keyStroke.getKeyCode(), keyStroke.getKeyChar()); 75 } 76 }