1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.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 | import java.util.Map;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
13 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
14 | import org.openstreetmap.josm.data.osm.TagCollection;
|
---|
15 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
20 | import org.openstreetmap.josm.tools.Utils;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * A ReverseWayNoTagCorrector warns about ways that should not be reversed
|
---|
24 | * because their semantic meaning cannot be preserved in that case.
|
---|
25 | * E.g. natural=coastline, natural=cliff, barrier=retaining_wall cannot be changed.
|
---|
26 | * @see ReverseWayTagCorrector for handling of tags that can be modified (oneway=yes, etc.)
|
---|
27 | * @since 5724
|
---|
28 | */
|
---|
29 | public final class ReverseWayNoTagCorrector {
|
---|
30 |
|
---|
31 | private ReverseWayNoTagCorrector() {
|
---|
32 | // Hide default constructor for utils classes
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Tags that imply a semantic meaning from the way direction and cannot be changed.
|
---|
37 | */
|
---|
38 | private static final TagCollection DIRECTIONAL_TAGS = new TagCollection(Arrays.asList(
|
---|
39 | new Tag("natural", "coastline"),
|
---|
40 | new Tag("natural", "cliff"),
|
---|
41 | new Tag("barrier", "guard_rail"),
|
---|
42 | new Tag("barrier", "kerb"),
|
---|
43 | new Tag("barrier", "retaining_wall"),
|
---|
44 | new Tag("barrier", "city_wall"),
|
---|
45 | new Tag("man_made", "embankment")
|
---|
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 TagCollection getDirectionalTags(Tagged way) {
|
---|
54 | final TagCollection collection = new TagCollection();
|
---|
55 | for (Map.Entry<String, String> entry : way.getKeys().entrySet()) {
|
---|
56 | final Tag tag = new Tag(entry.getKey(), entry.getValue());
|
---|
57 | final boolean isDirectional = DIRECTIONAL_TAGS.contains(tag) || tag.isDirectionKey();
|
---|
58 | if (isDirectional) {
|
---|
59 | final boolean cannotBeCorrected = ReverseWayTagCorrector.getTagCorrections(tag).isEmpty();
|
---|
60 | if (cannotBeCorrected) {
|
---|
61 | collection.add(tag);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | return collection;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Tests whether way can be reversed without semantic change.
|
---|
70 | * Looks for tags like natural=cliff, barrier=retaining_wall.
|
---|
71 | * @param way The way to check
|
---|
72 | * @return false if the semantic meaning change if the way is reversed, true otherwise.
|
---|
73 | */
|
---|
74 | public static boolean isReversible(Tagged way) {
|
---|
75 | return getDirectionalTags(way).isEmpty();
|
---|
76 | }
|
---|
77 |
|
---|
78 | private 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 | Utils.escapeReservedCharactersHTML(way.getDisplayName(DefaultNameFormatter.getInstance())),
|
---|
90 | Utils.joinAsHtmlUnorderedList(tags)
|
---|
91 | );
|
---|
92 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
93 | "reverse_directional_way",
|
---|
94 | MainApplication.getMainFrame(),
|
---|
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 | }
|
---|