-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JOSM #22308 - Add option to toggle layer read-only status to popup menu #99
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason why you are requiring layer size to be
Suggested change
EDIT: You'll want to implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I modeled this after the prevent upload toggle action which works exactly the same way. Not sure if locking/unlocking multiple layers at once is ever really needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me. |
||||||
} | ||||||
} |
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()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed since this is an entirely new class -- the
@since xxx
on L25 covers everything in the file.