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.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 | */
|
---|
26 | public 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("waterway", "stream"),
|
---|
42 | new Tag("waterway", "river"),
|
---|
43 | new Tag("waterway", "ditch"),
|
---|
44 | new Tag("waterway", "drain"),
|
---|
45 | new Tag("waterway", "canal")
|
---|
46 | }));
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Replies the tags that imply a semantic meaning from <code>way</code> direction and cannot be changed.
|
---|
50 | * @param way The way to look for
|
---|
51 | * @return tags that imply a semantic meaning from <code>way</code> direction and cannot be changed
|
---|
52 | */
|
---|
53 | public static final TagCollection getDirectionalTags(Way way) {
|
---|
54 | return directionalTags.intersect(TagCollection.from(way));
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Tests whether way can be reversed without semantic change.
|
---|
59 | * Looks for tags like natural=cliff, barrier=retaining_wall.
|
---|
60 | * @param way The way to check
|
---|
61 | * @return false if the semantic meaning change if the way is reversed, true otherwise.
|
---|
62 | */
|
---|
63 | public static boolean isReversible(Way way) {
|
---|
64 | return getDirectionalTags(way).isEmpty();
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected static String getHTML(TagCollection tags) {
|
---|
68 | if (tags.size() == 1) {
|
---|
69 | return tags.iterator().next().toString();
|
---|
70 | } else if (tags.size() > 1) {
|
---|
71 | return Utils.joinAsHtmlUnorderedList(tags);
|
---|
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 | }
|
---|