Ticket #6118: CutAction.java

File CutAction.java, 2.2 KB (added by andrey.raygorodskiy@…, 14 years ago)
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package org.openstreetmap.josm.actions;
6
7import org.openstreetmap.josm.command.Command;
8import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
9import static org.openstreetmap.josm.tools.I18n.tr;
10
11import java.awt.event.ActionEvent;
12import java.awt.event.KeyEvent;
13import java.util.Collection;
14
15import javax.swing.JOptionPane;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.command.DeleteCommand;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 *
24 * @author araygorodskiy
25 */
26public class CutAction extends JosmAction {
27
28 public CutAction() {
29 super(tr("Cut"), "cut",
30 tr("Cut selected objects to paste buffer."),
31 Shortcut.registerShortcut("system:cut", tr("Edit: {0}", tr("Cut")), KeyEvent.VK_X, Shortcut.GROUP_MENU), true);
32 putValue("help", ht("/Action/Cut"));
33 }
34
35 public void actionPerformed(ActionEvent e) {
36 if (isEmptySelection()) {
37 return;
38 }
39 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
40
41 CopyAction.copy(getEditLayer(), selection);
42 final Command c = DeleteCommand.delete(getEditLayer(),selection, true);
43 if (c != null) {
44 Main.main.undoRedo.add(c);
45 }
46 }
47
48 @Override
49 protected void updateEnabledState() {
50 if (getCurrentDataSet() == null) {
51 setEnabled(false);
52 } else {
53 updateEnabledState(getCurrentDataSet().getSelected());
54 }
55 }
56
57 @Override
58 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
59 setEnabled(selection != null && !selection.isEmpty());
60 }
61
62 private boolean isEmptySelection() {
63 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
64 if (sel.isEmpty()) {
65 JOptionPane.showMessageDialog(
66 Main.parent,
67 tr("Please select something to cut."),
68 tr("Information"),
69 JOptionPane.INFORMATION_MESSAGE);
70 return true;
71 }
72 return false;
73 }
74}