source: osm/applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/AddIntersectionsAction.java@ 27852

Last change on this file since 27852 was 27852, checked in by stoecker, 12 years ago

fix shortcut deprecation

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package utilsplugin2;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Set;
14
15import javax.swing.JOptionPane;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.actions.JosmAction;
19import org.openstreetmap.josm.command.AddCommand;
20import org.openstreetmap.josm.command.Command;
21import org.openstreetmap.josm.command.SequenceCommand;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.Way;
25import org.openstreetmap.josm.tools.Geometry;
26import org.openstreetmap.josm.tools.Shortcut;
27
28public class AddIntersectionsAction extends JosmAction {
29 public AddIntersectionsAction() {
30 super(tr("Add nodes at intersections"), "addintersect", tr("Add missing nodes at intersections of selected ways."),
31 Shortcut.registerShortcut("tools:addintersect", tr("Tool: {0}", tr("Add nodes at intersections")), KeyEvent.VK_N, Shortcut.ALT_CTRL_SHIFT), true);
32 putValue("help", ht("/Action/AddIntersections"));
33 }
34
35 @Override
36 public void actionPerformed(ActionEvent arg0) {
37 if (!isEnabled())
38 return;
39 List<Way> ways = OsmPrimitive.getFilteredList(getCurrentDataSet().getSelected(), Way.class);
40 if (ways.isEmpty()) {
41 JOptionPane.showMessageDialog(
42 Main.parent,
43 tr("Please select one or more ways with intersections of segments."),
44 tr("Information"),
45 JOptionPane.INFORMATION_MESSAGE
46 );
47 return;
48 }
49
50 LinkedList<Command> cmds = new LinkedList<Command>();
51 Geometry.addIntersections(ways, false, cmds);
52 if (!cmds.isEmpty()) {
53 Main.main.undoRedo.add(new SequenceCommand(tr("Add nodes at intersections"),cmds));
54 Set<Node> nodes = new HashSet<Node>(10);
55 // find and select newly added nodes
56 for (Command cmd: cmds) if (cmd instanceof AddCommand){
57 Collection<? extends OsmPrimitive> pp = cmd.getParticipatingPrimitives();
58 for ( OsmPrimitive p : pp) { // find all affected nodes
59 if (p instanceof Node && p.isNew()) nodes.add((Node)p);
60 }
61 if (!nodes.isEmpty()) {
62 getCurrentDataSet().setSelected(nodes);
63 }
64 }
65 }
66 }
67
68 @Override
69 protected void updateEnabledState() {
70 if (getCurrentDataSet() == null) {
71 setEnabled(false);
72 } else {
73 updateEnabledState(getCurrentDataSet().getSelected());
74 }
75 }
76
77 @Override
78 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
79 setEnabled(selection != null && !selection.isEmpty());
80 }
81}
Note: See TracBrowser for help on using the repository browser.