1 | // License: PD
|
---|
2 | package utilsplugin2.selection;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.*;
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
12 | import org.openstreetmap.josm.data.osm.*;
|
---|
13 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Select all connected ways for a street if one way is selected (determine by name/ref),
|
---|
17 | * select highway ways between two selected ways.
|
---|
18 | *
|
---|
19 | * @author zverik
|
---|
20 | */
|
---|
21 | public class SelectHighwayAction extends JosmAction {
|
---|
22 |
|
---|
23 | public SelectHighwayAction() {
|
---|
24 | super(tr("Select Highway"), "selecthighway", tr("Select highway for the name/ref given"),
|
---|
25 | Shortcut.registerShortcut("tools:selecthighway", tr("Tool: {0}","Select Highway"),
|
---|
26 | KeyEvent.VK_W, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT), true);
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public void actionPerformed(ActionEvent e) {
|
---|
31 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
32 | List<Way> selectedWays = OsmPrimitive.getFilteredList(getCurrentDataSet().getSelected(), Way.class);
|
---|
33 | Set<Way> newWays = new HashSet<Way>();
|
---|
34 |
|
---|
35 | if( selectedWays.size() == 1 ) {
|
---|
36 | Way firstWay = selectedWays.get(0);
|
---|
37 | String key = firstWay.hasKey("name") ? "name" : "ref";
|
---|
38 | if( !firstWay.hasKey(key) ) {
|
---|
39 | JOptionPane.showMessageDialog(Main.parent, "The selected way should have either name or ref tag.", "Select Highway", JOptionPane.ERROR_MESSAGE);
|
---|
40 | return;
|
---|
41 | }
|
---|
42 | String value = firstWay.get(key);
|
---|
43 |
|
---|
44 | Queue<Node> nodeQueue = new LinkedList<Node>();
|
---|
45 | nodeQueue.add(firstWay.firstNode());
|
---|
46 |
|
---|
47 | while( !nodeQueue.isEmpty() ) {
|
---|
48 | Node node = nodeQueue.remove();
|
---|
49 | for( Way p : OsmPrimitive.getFilteredList(node.getReferrers(), Way.class) ) {
|
---|
50 | if( !newWays.contains(p) && p.hasKey(key) && p.get(key).equals(value) ) {
|
---|
51 | newWays.add(p);
|
---|
52 | nodeQueue.add(p.firstNode().equals(node) ? p.lastNode() : p.firstNode());
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | } else if( selectedWays.size() == 2 ) {
|
---|
57 | JOptionPane.showMessageDialog(Main.parent, "Sorry, two ways are not supported yet", "Select Highway", JOptionPane.ERROR_MESSAGE);
|
---|
58 | return;
|
---|
59 | } else {
|
---|
60 | JOptionPane.showMessageDialog(Main.parent, "Please select exactly one way for this action", "Select Highway", JOptionPane.ERROR_MESSAGE);
|
---|
61 | return;
|
---|
62 | }
|
---|
63 |
|
---|
64 | getCurrentDataSet().setSelected(newWays);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | protected void updateEnabledState() {
|
---|
69 | if (getCurrentDataSet() == null)
|
---|
70 | setEnabled(false);
|
---|
71 | else
|
---|
72 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
77 | if (selection == null) {
|
---|
78 | setEnabled(false);
|
---|
79 | return;
|
---|
80 | }
|
---|
81 | int count = 0;
|
---|
82 | for( OsmPrimitive p : selection )
|
---|
83 | if( p instanceof Way )
|
---|
84 | count++;
|
---|
85 | setEnabled(count == 1); // todo: or 2
|
---|
86 | }
|
---|
87 | }
|
---|