|
| 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 | +import org.openstreetmap.josm.tools.ImageProvider; |
| 19 | + |
| 20 | +/** |
| 21 | + * An action enabling/disabling the {@linkplain AbstractModifiableLayer#lock() read-only flag} |
| 22 | + * of the layer specified in the constructor. |
| 23 | + * |
| 24 | + * @since XXX |
| 25 | + */ |
| 26 | +public class ToggleEditLockLayerAction extends AbstractAction implements LayerAction { |
| 27 | + |
| 28 | + private final AbstractModifiableLayer layer; |
| 29 | + |
| 30 | + /** |
| 31 | + * Construct a new {@code ToggleEditLockLayerAction} |
| 32 | + * @param layer the layer for which to toggle the {@linkplain AbstractModifiableLayer#lock() read-only flag} |
| 33 | + * |
| 34 | + * @since XXX |
| 35 | + */ |
| 36 | + public ToggleEditLockLayerAction(AbstractModifiableLayer layer) { |
| 37 | + super(tr("Prevent modification")); |
| 38 | + putValue(SHORT_DESCRIPTION, tr("Prevent/allow changes being made in this layer")); |
| 39 | + new ImageProvider("lock").getResource().attachImageIcon(this, true); |
| 40 | + this.layer = layer; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void actionPerformed(ActionEvent e) { |
| 45 | + if (layer.isLocked()) { |
| 46 | + layer.unlock(); |
| 47 | + } else { |
| 48 | + layer.lock(); |
| 49 | + } |
| 50 | + |
| 51 | + layer.invalidate(); |
| 52 | + LayerListDialog.getInstance().repaint(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Component createMenuComponent() { |
| 57 | + JCheckBoxMenuItem item = new JCheckBoxMenuItem(this); |
| 58 | + item.setSelected(layer.isLocked()); |
| 59 | + return item; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean supportLayers(List<Layer> layers) { |
| 64 | + return layers.size() == 1 && layers.get(0) instanceof Lockable; |
| 65 | + } |
| 66 | +} |
0 commit comments