/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openstreetmap.josm.actions;

import org.openstreetmap.josm.command.Command;
import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
import static org.openstreetmap.josm.tools.I18n.tr;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Collection;

import javax.swing.JOptionPane;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.command.DeleteCommand;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.tools.Shortcut;

/**
 *
 * @author araygorodskiy
 */
public class CutAction extends JosmAction {

    public CutAction() {
        super(tr("Cut"), "cut",
                tr("Cut selected objects to paste buffer."),
                Shortcut.registerShortcut("system:cut", tr("Edit: {0}", tr("Cut")), KeyEvent.VK_X, Shortcut.GROUP_MENU), true);
        putValue("help", ht("/Action/Cut"));
    }

    public void actionPerformed(ActionEvent e) {
        if (isEmptySelection()) {
            return;
        }
        Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();

        CopyAction.copy(getEditLayer(), selection);
        final Command c = DeleteCommand.delete(getEditLayer(),selection, true);
        if (c != null) {
            Main.main.undoRedo.add(c);
        }
    }

    @Override
    protected void updateEnabledState() {
        if (getCurrentDataSet() == null) {
            setEnabled(false);
        } else {
            updateEnabledState(getCurrentDataSet().getSelected());
        }
    }

    @Override
    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
        setEnabled(selection != null && !selection.isEmpty());
    }

    private boolean isEmptySelection() {
        Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
        if (sel.isEmpty()) {
            JOptionPane.showMessageDialog(
                    Main.parent,
                    tr("Please select something to cut."),
                    tr("Information"),
                    JOptionPane.INFORMATION_MESSAGE);
            return true;
        }
        return false;
    }
}
