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

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

fix #4664 - Add new tags for directional tags

File size: 4.8 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", "guard_rail"),
34 new Tag("barrier", "kerb"),
35 new Tag("barrier", "retaining_wall"),
36 new Tag("waterway", "stream"),
37 new Tag("waterway", "river"),
38 new Tag("waterway", "ditch"),
39 new Tag("waterway", "drain"),
40 new Tag("waterway", "canal")
41 }));
42
43 /**
44 * Replies the tags that imply a semantic meaning from <code>way</code> direction and cannot be changed.
45 * @param way The way to look for
46 * @return tags that imply a semantic meaning from <code>way</code> direction and cannot be changed
47 */
48 public static final TagCollection getDirectionalTags(Way way) {
49 return directionalTags.intersect(TagCollection.from(way));
50 }
51
52 /**
53 * Tests whether way can be reversed without semantic change.
54 * Looks for tags like natural=cliff, barrier=retaining_wall.
55 * @param way The way to check
56 * @return false if the semantic meaning change if the way is reversed, true otherwise.
57 */
58 public static boolean isReversible(Way way) {
59 return getDirectionalTags(way).isEmpty();
60 }
61
62 protected static String getHTML(TagCollection tags) {
63 if (tags.size() == 1) {
64 return tags.iterator().next().toString();
65 } else if (tags.size() > 1) {
66 String s = "<ul>";
67 for (Tag t : tags) {
68 s += "<li>" + t.toString() + "</li>";
69 }
70 s += "</ul>";
71 return s;
72 } else {
73 return "";
74 }
75 }
76
77 protected static boolean confirmReverseWay(Way way, TagCollection tags) {
78 String msg = trn(
79 // Singular, if a single tag is impacted
80 "<html>You are going to reverse the way ''{0}'',"
81 + "<br/> whose semantic meaning of its tag ''{1}'' is defined by its direction.<br/>"
82 + "Do you really want to change the way direction, thus its semantic meaning?</html>",
83 // Plural, if several tags are impacted
84 "<html>You are going to reverse the way ''{0}'',"
85 + "<br/> whose semantic meaning of these tags are defined by its direction:<br/>{1}"
86 + "Do you really want to change the way direction, thus its semantic meaning?</html>",
87 tags.size(),
88 way.getDisplayName(DefaultNameFormatter.getInstance()),
89 getHTML(tags)
90 );
91 int ret = ConditionalOptionPaneUtil.showOptionDialog(
92 "reverse_directional_way",
93 Main.parent,
94 msg,
95 tr("Reverse directional way."),
96 JOptionPane.YES_NO_CANCEL_OPTION,
97 JOptionPane.WARNING_MESSAGE,
98 null,
99 null
100 );
101 switch(ret) {
102 case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION : return true;
103 case JOptionPane.YES_OPTION: return true;
104 default: return false;
105 }
106 }
107
108 /**
109 * Checks the given way can be safely reversed and asks user to confirm the operation if it not the case.
110 * @param way The way to check
111 * @throws UserCancelException If the user cancels the operation
112 */
113 public static void checkAndConfirmReverseWay(Way way) throws UserCancelException {
114 TagCollection tags = getDirectionalTags(way);
115 if (!tags.isEmpty() && !confirmReverseWay(way, tags)) {
116 throw new UserCancelException();
117 }
118 }
119}
Note: See TracBrowser for help on using the repository browser.