source: josm/trunk/src/org/openstreetmap/josm/corrector/ReverseWayNoTagCorrector.java@ 5724

Last change on this file since 5724 was 5724, checked in by Don-vip, 11 years ago

fix #4664 - warn when reverting a way with direction defined by tag

File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.corrector;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.util.Arrays;
8
9import javax.swing.JOptionPane;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.Tag;
13import org.openstreetmap.josm.data.osm.TagCollection;
14import org.openstreetmap.josm.data.osm.Way;
15import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
16import org.openstreetmap.josm.gui.DefaultNameFormatter;
17
18/**
19 * A ReverseWayNoTagCorrector warns about ways that should not be reversed
20 * because their semantic meaning cannot be preserved in that case.
21 * E.g. natural=coastline, natural=cliff, barrier=retaining_wall cannot be changed.
22 * @see ReverseWayTagCorrector for handling of tags that can be modified (oneway=yes, etc.)
23 * @since 5724
24 */
25public class ReverseWayNoTagCorrector {
26
27 /**
28 * Tags that imply a semantic meaning from the way direction and cannot be changed.
29 */
30 public static final TagCollection directionalTags = new TagCollection(Arrays.asList(new Tag[]{
31 new Tag("natural", "coastline"),
32 new Tag("natural", "cliff"),
33 new Tag("barrier", "retaining_wall"),
34 new Tag("waterway", "stream"),
35 new Tag("waterway", "river"),
36 new Tag("waterway", "ditch"),
37 new Tag("waterway", "drain"),
38 new Tag("waterway", "canal")
39 }));
40
41 /**
42 * Replies the tags that imply a semantic meaning from <code>way</code> direction and cannot be changed.
43 * @param way The way to look for
44 * @return tags that imply a semantic meaning from <code>way</code> direction and cannot be changed
45 */
46 public static final TagCollection getDirectionalTags(Way way) {
47 return directionalTags.intersect(TagCollection.from(way));
48 }
49
50 /**
51 * Tests whether way can be reversed without semantic change.
52 * Looks for tags like natural=cliff, barrier=retaining_wall.
53 * @param way The way to check
54 * @return false if the semantic meaning change if the way is reversed, true otherwise.
55 */
56 public static boolean isReversible(Way way) {
57 return getDirectionalTags(way).isEmpty();
58 }
59
60 protected static String getHTML(TagCollection tags) {
61 if (tags.size() == 1) {
62 return tags.iterator().next().toString();
63 } else if (tags.size() > 1) {
64 String s = "<ul>";
65 for (Tag t : tags) {
66 s += "<li>" + t.toString() + "</li>";
67 }
68 s += "</ul>";
69 return s;
70 } else {
71 return "";
72 }
73 }
74
75 protected static boolean confirmReverseWay(Way way, TagCollection tags) {
76 String msg = trn(
77 // Singular, if a single tag is impacted
78 "<html>You are going to reverse the way ''{0}'',"
79 + "<br/> whose semantic meaning of its tag ''{1}'' is defined by its direction.<br/>"
80 + "Do you really want to change the way direction, thus its semantic meaning?</html>",
81 // Plural, if several tags are impacted
82 "<html>You are going to reverse the way ''{0}'',"
83 + "<br/> whose semantic meaning of these tags are defined by its direction:<br/>{1}"
84 + "Do you really want to change the way direction, thus its semantic meaning?</html>",
85 tags.size(),
86 way.getDisplayName(DefaultNameFormatter.getInstance()),
87 getHTML(tags)
88 );
89 int ret = ConditionalOptionPaneUtil.showOptionDialog(
90 "reverse_directional_way",
91 Main.parent,
92 msg,
93 tr("Reverse directional way."),
94 JOptionPane.YES_NO_CANCEL_OPTION,
95 JOptionPane.WARNING_MESSAGE,
96 null,
97 null
98 );
99 switch(ret) {
100 case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION : return true;
101 case JOptionPane.YES_OPTION: return true;
102 default: return false;
103 }
104 }
105
106 /**
107 * Checks the given way can be safely reversed and asks user to confirm the operation if it not the case.
108 * @param way The way to check
109 * @throws UserCancelException If the user cancels the operation
110 */
111 public static void checkAndConfirmReverseWay(Way way) throws UserCancelException {
112 TagCollection tags = getDirectionalTags(way);
113 if (!tags.isEmpty() && !confirmReverseWay(way, tags)) {
114 throw new UserCancelException();
115 }
116 }
117}
Note: See TracBrowser for help on using the repository browser.