| | 1 | // License: GPL. Copyright 2010 by Hanno Hecker |
| | 2 | package org.openstreetmap.josm.actions; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
| | 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 6 | import static org.openstreetmap.josm.tools.I18n.trn; |
| | 7 | |
| | 8 | import java.awt.event.ActionEvent; |
| | 9 | import java.awt.event.KeyEvent; |
| | 10 | import java.util.ArrayList; |
| | 11 | import java.util.Collection; |
| | 12 | import java.util.Collections; |
| | 13 | import java.util.HashSet; |
| | 14 | import java.util.LinkedList; |
| | 15 | import java.util.List; |
| | 16 | |
| | 17 | import javax.swing.JOptionPane; |
| | 18 | import javax.swing.JPanel; |
| | 19 | |
| | 20 | import org.openstreetmap.josm.Main; |
| | 21 | import org.openstreetmap.josm.command.AddCommand; |
| | 22 | import org.openstreetmap.josm.command.ChangeCommand; |
| | 23 | import org.openstreetmap.josm.command.Command; |
| | 24 | import org.openstreetmap.josm.command.SequenceCommand; |
| | 25 | import org.openstreetmap.josm.data.osm.Node; |
| | 26 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 27 | import org.openstreetmap.josm.data.osm.Relation; |
| | 28 | import org.openstreetmap.josm.data.osm.RelationMember; |
| | 29 | import org.openstreetmap.josm.data.osm.Way; |
| | 30 | import org.openstreetmap.josm.gui.MapView; |
| | 31 | import org.openstreetmap.josm.tools.Shortcut; |
| | 32 | |
| | 33 | /** |
| | 34 | * Select all nodes of a selected way. |
| | 35 | * |
| | 36 | */ |
| | 37 | |
| | 38 | public class SelectWayNodes extends JosmAction { |
| | 39 | |
| | 40 | private Node selectedNode; |
| | 41 | private ArrayList<Node> selectedNodes; |
| | 42 | |
| | 43 | /** |
| | 44 | * Create a new SelectWayNodesAction |
| | 45 | */ |
| | 46 | public SelectWayNodes() { |
| | 47 | super(tr("Select Way Nodes"), null, tr("Select all nodes of a selected way."), |
| | 48 | Shortcut.registerShortcut("tools:selectwaynodes", tr("Tool: {0}", tr("Select Way Nodes")), KeyEvent.VK_N, Shortcut.GROUP_EDIT), true); |
| | 49 | putValue("help", ht("/Action/SelectWayNodes")); |
| | 50 | } |
| | 51 | |
| | 52 | /** |
| | 53 | * Called when the action is executed. |
| | 54 | * |
| | 55 | * This method does some checking on the selection and calls the matching selectWayNodes method. |
| | 56 | */ |
| | 57 | public void actionPerformed(ActionEvent e) { |
| | 58 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected(); |
| | 59 | |
| | 60 | String errMsg = null; |
| | 61 | for (OsmPrimitive p : selection) { |
| | 62 | if (p instanceof Way) { |
| | 63 | Way w = (Way) p; |
| | 64 | if (!w.isUsable() || w.getNodesCount() < 1) { |
| | 65 | continue; |
| | 66 | } |
| | 67 | selectWayNodes(w); |
| | 68 | } |
| | 69 | else if (p instanceof Node) { |
| | 70 | Node n = (Node) p; |
| | 71 | if (selectedNodes == null) { |
| | 72 | selectedNodes = new ArrayList<Node>(); |
| | 73 | } |
| | 74 | selectedNodes.add(n); |
| | 75 | } |
| | 76 | } |
| | 77 | |
| | 78 | getCurrentDataSet().setSelected(selectedNodes); |
| | 79 | selectedNodes = null; |
| | 80 | } |
| | 81 | |
| | 82 | private void selectWayNodes(Way w) { |
| | 83 | |
| | 84 | for (Node n : w.getNodes()) { |
| | 85 | if (selectedNodes == null) { |
| | 86 | selectedNodes = new ArrayList<Node>(); |
| | 87 | } |
| | 88 | selectedNodes.add(n); |
| | 89 | } |
| | 90 | } |
| | 91 | |
| | 92 | @Override |
| | 93 | protected void updateEnabledState() { |
| | 94 | if (getCurrentDataSet() == null) { |
| | 95 | setEnabled(false); |
| | 96 | } else { |
| | 97 | updateEnabledState(getCurrentDataSet().getSelected()); |
| | 98 | } |
| | 99 | } |
| | 100 | |
| | 101 | @Override |
| | 102 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
| | 103 | setEnabled(selection != null && !selection.isEmpty()); |
| | 104 | } |
| | 105 | } |