-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to toggle layer read-only status to popup menu
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/org/openstreetmap/josm/actions/ToggleEditLockLayerAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.actions; | ||
|
||
import static org.openstreetmap.josm.tools.I18n.tr; | ||
|
||
import java.awt.Component; | ||
import java.awt.event.ActionEvent; | ||
import java.util.List; | ||
|
||
import javax.swing.AbstractAction; | ||
import javax.swing.JCheckBoxMenuItem; | ||
|
||
import org.openstreetmap.josm.data.osm.Lockable; | ||
import org.openstreetmap.josm.gui.dialogs.LayerListDialog; | ||
import org.openstreetmap.josm.gui.layer.Layer; | ||
import org.openstreetmap.josm.gui.layer.Layer.LayerAction; | ||
import org.openstreetmap.josm.tools.CheckParameterUtil; | ||
import org.openstreetmap.josm.tools.ImageProvider; | ||
|
||
/** | ||
* An action enabling/disabling the {@linkplain Lockable#lock() read-only flag} | ||
* of the layer specified in the constructor. | ||
* @param <L> Type of layer the action should be instantiated for | ||
* | ||
* @since xxx | ||
*/ | ||
public class ToggleEditLockLayerAction<L extends Layer & Lockable> extends AbstractAction implements LayerAction { | ||
|
||
private final L layer; | ||
|
||
/** | ||
* Construct a new {@code ToggleEditLockLayerAction} | ||
* @param layer the layer for which to toggle the {@linkplain Lockable#lock() read-only flag} | ||
* | ||
* @since xxx | ||
*/ | ||
public ToggleEditLockLayerAction(L layer) { | ||
super(tr("Prevent modification")); | ||
CheckParameterUtil.ensureParameterNotNull(layer, "layer"); | ||
putValue(SHORT_DESCRIPTION, tr("Prevent/allow changes being made in this layer")); | ||
new ImageProvider("lock").getResource().attachImageIcon(this, true); | ||
this.layer = layer; | ||
} | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
if (layer.isLocked()) { | ||
layer.unlock(); | ||
} else { | ||
layer.lock(); | ||
} | ||
|
||
layer.invalidate(); | ||
LayerListDialog.getInstance().repaint(); | ||
} | ||
|
||
@Override | ||
public Component createMenuComponent() { | ||
JCheckBoxMenuItem item = new JCheckBoxMenuItem(this); | ||
item.setSelected(layer.isLocked()); | ||
return item; | ||
} | ||
|
||
@Override | ||
public boolean supportLayers(List<Layer> layers) { | ||
return layers.size() == 1 && layers.get(0) instanceof Lockable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
test/unit/org/openstreetmap/josm/actions/ToggleEditLockLayerActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.actions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.openstreetmap.josm.data.osm.DataSet; | ||
import org.openstreetmap.josm.gui.MainApplication; | ||
import org.openstreetmap.josm.gui.layer.OsmDataLayer; | ||
import org.openstreetmap.josm.testutils.JOSMTestRules; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
/** | ||
* Unit tests for class {@link ToggleEditLockLayerAction}. | ||
*/ | ||
final class ToggleEditLockLayerActionTest { | ||
|
||
/** | ||
* Setup test. | ||
*/ | ||
@RegisterExtension | ||
@SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") | ||
public static JOSMTestRules test = new JOSMTestRules().main().projection(); | ||
|
||
/** | ||
* Test null parameter | ||
*/ | ||
@Test | ||
void testNullLayer() { | ||
assertThrows(IllegalArgumentException.class, () -> new ToggleEditLockLayerAction<OsmDataLayer>(null)); | ||
} | ||
|
||
/** | ||
* Test edit lock toggle functionality | ||
* @param locked Initial layer lock status | ||
*/ | ||
@ParameterizedTest | ||
@ValueSource(booleans = {true, false}) | ||
void testLayerLockToggle(boolean locked) { | ||
OsmDataLayer testLayer = new OsmDataLayer(new DataSet(), "testLayerLock", null); | ||
MainApplication.getLayerManager().addLayer(testLayer); | ||
if (locked) { | ||
testLayer.lock(); | ||
} | ||
// Sanity check | ||
assertEquals(locked, testLayer.isLocked()); | ||
ToggleEditLockLayerAction<OsmDataLayer> action = new ToggleEditLockLayerAction<>(testLayer); | ||
action.actionPerformed(null); | ||
assertNotEquals(locked, testLayer.isLocked()); | ||
action.actionPerformed(null); | ||
assertEquals(locked, testLayer.isLocked()); | ||
} | ||
} |