| 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.event.ActionEvent;
|
|---|
| 7 |
|
|---|
| 8 | import javax.swing.AbstractAction;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 11 | import org.openstreetmap.josm.gui.dialogs.IEnabledStateUpdating;
|
|---|
| 12 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
|
|---|
| 13 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
|---|
| 14 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 17 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * The action to duplicate the given selected layer into another layer.
|
|---|
| 21 | */
|
|---|
| 22 | public final class DuplicateAction extends AbstractAction implements IEnabledStateUpdating {
|
|---|
| 23 | private transient Layer layer;
|
|---|
| 24 | private final LayerListModel model;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Constructs a new {@code DuplicateAction}.
|
|---|
| 28 | * @param layer the layer
|
|---|
| 29 | * @param model layer list model
|
|---|
| 30 | * @throws IllegalArgumentException if {@code layer} is null
|
|---|
| 31 | */
|
|---|
| 32 | public DuplicateAction(Layer layer, LayerListModel model) {
|
|---|
| 33 | this(model);
|
|---|
| 34 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
|---|
| 35 | this.layer = layer;
|
|---|
| 36 | updateEnabledState();
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Constructs a new {@code DuplicateAction}.
|
|---|
| 41 | * @param model layer list model
|
|---|
| 42 | */
|
|---|
| 43 | public DuplicateAction(LayerListModel model) {
|
|---|
| 44 | this.model = model;
|
|---|
| 45 | putValue(NAME, tr("Duplicate"));
|
|---|
| 46 | new ImageProvider("dialogs", "duplicatelayer").getResource().attachImageIcon(this, true);
|
|---|
| 47 | putValue(SHORT_DESCRIPTION, tr("Duplicate this layer"));
|
|---|
| 48 | putValue("help", HelpUtil.ht("/Dialog/LayerList#DuplicateLayer"));
|
|---|
| 49 | updateEnabledState();
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | private static void duplicate(Layer layer) {
|
|---|
| 53 | if (layer instanceof OsmDataLayer) {
|
|---|
| 54 | String newName = LayerListTransferHandler.suggestNewLayerName(layer.getName(), MainApplication.getLayerManager().getLayers());
|
|---|
| 55 | MainApplication.getLayerManager().addLayer(((OsmDataLayer) layer).duplicate(newName));
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | @Override
|
|---|
| 60 | public void actionPerformed(ActionEvent e) {
|
|---|
| 61 | if (layer != null) {
|
|---|
| 62 | duplicate(layer);
|
|---|
| 63 | } else {
|
|---|
| 64 | duplicate(model.getSelectedLayers().get(0));
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | @Override
|
|---|
| 69 | public void updateEnabledState() {
|
|---|
| 70 | if (layer == null) {
|
|---|
| 71 | if (model != null && model.getSelectedLayers().size() == 1) {
|
|---|
| 72 | setEnabled(model.getSelectedLayers().get(0) instanceof OsmDataLayer);
|
|---|
| 73 | } else {
|
|---|
| 74 | setEnabled(false);
|
|---|
| 75 | }
|
|---|
| 76 | } else {
|
|---|
| 77 | setEnabled(layer instanceof OsmDataLayer);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|