|
| 1 | +// License: GPL. For details, see LICENSE file. |
| 2 | +package org.openstreetmap.josm.actions; |
| 3 | + |
| 4 | +import static org.openstreetmap.josm.tools.I18n.tr; |
| 5 | + |
| 6 | +import java.awt.Component; |
| 7 | +import java.awt.event.ActionEvent; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import javax.swing.AbstractAction; |
| 11 | +import javax.swing.JCheckBoxMenuItem; |
| 12 | + |
| 13 | +import org.openstreetmap.josm.data.osm.Lockable; |
| 14 | +import org.openstreetmap.josm.gui.dialogs.LayerListDialog; |
| 15 | +import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer; |
| 16 | +import org.openstreetmap.josm.gui.layer.Layer; |
| 17 | +import org.openstreetmap.josm.gui.layer.Layer.LayerAction; |
| 18 | + |
| 19 | +/** |
| 20 | + * An action enabling/disabling the {@linkplain AbstractModifiableLayer#lock() read-only flag} |
| 21 | + * of the layer specified in the constructor. |
| 22 | + * |
| 23 | + * @since XXX |
| 24 | + */ |
| 25 | +public class ToggleEditLockLayerAction extends AbstractAction implements LayerAction { |
| 26 | + |
| 27 | + private final AbstractModifiableLayer layer; |
| 28 | + |
| 29 | + /** |
| 30 | + * Construct a new {@code ToggleEditLockLayerAction} |
| 31 | + * @param layer the layer for which to toggle the {@linkplain AbstractModifiableLayer#lock() read-only flag} |
| 32 | + * |
| 33 | + * @since XXX |
| 34 | + */ |
| 35 | + public ToggleEditLockLayerAction(AbstractModifiableLayer layer) { |
| 36 | + super(tr("Prevent modification")); |
| 37 | + putValue(SHORT_DESCRIPTION, tr("Prevent/allow changes being made in this layer")); |
| 38 | + this.layer = layer; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void actionPerformed(ActionEvent e) { |
| 43 | + if (layer.isLocked()) { |
| 44 | + layer.unlock(); |
| 45 | + } else { |
| 46 | + layer.lock(); |
| 47 | + } |
| 48 | + |
| 49 | + layer.invalidate(); |
| 50 | + LayerListDialog.getInstance().repaint(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Component createMenuComponent() { |
| 55 | + JCheckBoxMenuItem item = new JCheckBoxMenuItem(this); |
| 56 | + item.setSelected(layer.isLocked()); |
| 57 | + return item; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean supportLayers(List<Layer> layers) { |
| 62 | + return layers.size() == 1 && layers.get(0) instanceof Lockable; |
| 63 | + } |
| 64 | +} |
0 commit comments