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