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

Last change on this file since 8443 was 8408, checked in by Klumbumbus, 9 years ago

fix #11246 - add warning for way direction change of man_made_embankment

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