| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs.layer;
|
|---|
| 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.JMenuItem;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.gui.dialogs.IEnabledStateUpdating;
|
|---|
| 14 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
|
|---|
| 15 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
|---|
| 16 | import org.openstreetmap.josm.gui.io.SaveLayersDialog;
|
|---|
| 17 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 18 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
|
|---|
| 19 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * The action to delete the currently selected layer
|
|---|
| 23 | */
|
|---|
| 24 | public final class DeleteLayerAction extends AbstractAction implements IEnabledStateUpdating, LayerAction {
|
|---|
| 25 |
|
|---|
| 26 | private final LayerListModel model;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Creates a {@link DeleteLayerAction} which will delete the currently selected layers in the layer dialog.
|
|---|
| 30 | * @param model layer list model
|
|---|
| 31 | */
|
|---|
| 32 | public DeleteLayerAction(LayerListModel model) {
|
|---|
| 33 | this.model = model;
|
|---|
| 34 | new ImageProvider("dialogs", "delete").getResource().attachImageIcon(this, true);
|
|---|
| 35 | putValue(SHORT_DESCRIPTION, tr("Delete the selected layers."));
|
|---|
| 36 | putValue(NAME, tr("Delete"));
|
|---|
| 37 | putValue("help", HelpUtil.ht("/Dialog/LayerList#DeleteLayer"));
|
|---|
| 38 | updateEnabledState();
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | @Override
|
|---|
| 42 | public void actionPerformed(ActionEvent e) {
|
|---|
| 43 | List<Layer> selectedLayers = model.getSelectedLayers();
|
|---|
| 44 | if (selectedLayers.isEmpty())
|
|---|
| 45 | return;
|
|---|
| 46 | if (!SaveLayersDialog.saveUnsavedModifications(selectedLayers, SaveLayersDialog.Reason.DELETE))
|
|---|
| 47 | return;
|
|---|
| 48 | for (Layer l: selectedLayers) {
|
|---|
| 49 | if (model.getLayerManager().containsLayer(l)) {
|
|---|
| 50 | // it may happen that this call removes other layers.
|
|---|
| 51 | // this is why we need to check if every layer is still in the list of selected layers.
|
|---|
| 52 | model.getLayerManager().removeLayer(l);
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | @Override
|
|---|
| 58 | public void updateEnabledState() {
|
|---|
| 59 | setEnabled(!model.getSelectedLayers().isEmpty());
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | @Override
|
|---|
| 63 | public Component createMenuComponent() {
|
|---|
| 64 | return new JMenuItem(this);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | @Override
|
|---|
| 68 | public boolean supportLayers(List<Layer> layers) {
|
|---|
| 69 | return true;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | @Override
|
|---|
| 73 | public boolean equals(Object obj) {
|
|---|
| 74 | return obj instanceof DeleteLayerAction;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | @Override
|
|---|
| 78 | public int hashCode() {
|
|---|
| 79 | return getClass().hashCode();
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|