1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.validator;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
16 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
|
---|
17 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
18 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
19 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
20 | import org.openstreetmap.josm.gui.preferences.SourceEditor;
|
---|
21 | import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
|
---|
22 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.SourceProvider;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.SourceType;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
---|
26 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
---|
27 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The general validator preferences, allowing to enable/disable tests.
|
---|
31 | * @since 6669
|
---|
32 | */
|
---|
33 | public class ValidatorTagCheckerRulesPreference implements SubPreferenceSetting {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Factory used to create a new {@code ValidatorTagCheckerRulesPreference}.
|
---|
37 | */
|
---|
38 | public static class Factory implements PreferenceSettingFactory {
|
---|
39 | @Override
|
---|
40 | public PreferenceSetting createPreferenceSetting() {
|
---|
41 | return new ValidatorTagCheckerRulesPreference();
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private static final List<SourceProvider> ruleSourceProviders = new ArrayList<>();
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Registers a new additional rule source provider.
|
---|
49 | * @param provider The rule source provider
|
---|
50 | * @return {@code true}, if the provider has been added, {@code false} otherwise
|
---|
51 | */
|
---|
52 | public static final boolean registerSourceProvider(SourceProvider provider) {
|
---|
53 | if (provider != null)
|
---|
54 | return ruleSourceProviders.add(provider);
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static class TagCheckerRulesSourceEditor extends SourceEditor {
|
---|
59 |
|
---|
60 | TagCheckerRulesSourceEditor() {
|
---|
61 | super(SourceType.TAGCHECKER_RULE, Main.getJOSMWebsite()+"/rules", ruleSourceProviders, false);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public Collection<? extends SourceEntry> getInitialSourcesList() {
|
---|
66 | return RulePrefHelper.INSTANCE.get();
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public boolean finish() {
|
---|
71 | return RulePrefHelper.INSTANCE.put(activeSourcesModel.getSources());
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public Collection<ExtendedSourceEntry> getDefault() {
|
---|
76 | return RulePrefHelper.INSTANCE.getDefault();
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public Collection<String> getInitialIconPathsList() {
|
---|
81 | return null;
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public String getStr(I18nString ident) {
|
---|
86 | switch (ident) {
|
---|
87 | case AVAILABLE_SOURCES:
|
---|
88 | return tr("Available rules:");
|
---|
89 | case ACTIVE_SOURCES:
|
---|
90 | return tr("Active rules:");
|
---|
91 | case NEW_SOURCE_ENTRY_TOOLTIP:
|
---|
92 | return tr("Add a new rule by entering filename or URL");
|
---|
93 | case NEW_SOURCE_ENTRY:
|
---|
94 | return tr("New rule entry:");
|
---|
95 | case REMOVE_SOURCE_TOOLTIP:
|
---|
96 | return tr("Remove the selected rules from the list of active rules");
|
---|
97 | case EDIT_SOURCE_TOOLTIP:
|
---|
98 | return tr("Edit the filename or URL for the selected active rule");
|
---|
99 | case ACTIVATE_TOOLTIP:
|
---|
100 | return tr("Add the selected available rules to the list of active rules");
|
---|
101 | case RELOAD_ALL_AVAILABLE:
|
---|
102 | return marktr("Reloads the list of available rules from ''{0}''");
|
---|
103 | case LOADING_SOURCES_FROM:
|
---|
104 | return marktr("Loading rule sources from ''{0}''");
|
---|
105 | case FAILED_TO_LOAD_SOURCES_FROM:
|
---|
106 | return marktr("<html>Failed to load the list of rule sources from<br>"
|
---|
107 | + "''{0}''.<br>"
|
---|
108 | + "<br>"
|
---|
109 | + "Details (untranslated):<br>{1}</html>");
|
---|
110 | case FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC:
|
---|
111 | return "/Preferences/Rules#FailedToLoadRuleSources";
|
---|
112 | case ILLEGAL_FORMAT_OF_ENTRY:
|
---|
113 | return marktr("Warning: illegal format of entry in rule list ''{0}''. Got ''{1}''");
|
---|
114 | default: throw new AssertionError();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | protected String getTitleForSourceEntry(SourceEntry entry) {
|
---|
120 | final String title = MapPaintPreference.getTitleFromSourceEntry(entry);
|
---|
121 | return title != null ? title : super.getTitleForSourceEntry(entry);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Helper class for validator tag checker rules preferences.
|
---|
127 | */
|
---|
128 | public static class RulePrefHelper extends SourceEditor.SourcePrefHelper {
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * The unique instance.
|
---|
132 | */
|
---|
133 | public static final RulePrefHelper INSTANCE = new RulePrefHelper();
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Constructs a new {@code PresetPrefHelper}.
|
---|
137 | */
|
---|
138 | public RulePrefHelper() {
|
---|
139 | super(MapCSSTagChecker.ENTRIES_PREF_KEY);
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public Collection<ExtendedSourceEntry> getDefault() {
|
---|
144 | List<ExtendedSourceEntry> def = new ArrayList<>();
|
---|
145 |
|
---|
146 | addDefault(def, "addresses", tr("Addresses"), tr("Checks for errors on addresses"));
|
---|
147 | addDefault(def, "combinations", tr("Tag combinations"), tr("Checks for missing tag or suspicious combinations"));
|
---|
148 | addDefault(def, "deprecated", tr("Deprecated features"), tr("Checks for deprecated features"));
|
---|
149 | addDefault(def, "geometry", tr("Geometry"), tr("Checks for geometry errors"));
|
---|
150 | addDefault(def, "highway", tr("Highways"), tr("Checks for errors on highways"));
|
---|
151 | addDefault(def, "multiple", tr("Multiple values"), tr("Checks for wrong multiple values"));
|
---|
152 | addDefault(def, "numeric", tr("Numeric values"), tr("Checks for wrong numeric values"));
|
---|
153 | addDefault(def, "power", tr("Power"), tr("Checks for errors on power infrastructures"));
|
---|
154 | addDefault(def, "religion", tr("Religion"), tr("Checks for errors on religious objects"));
|
---|
155 | addDefault(def, "relation", tr("Relations"), tr("Checks for errors on relations"));
|
---|
156 | addDefault(def, "unnecessary", tr("Unnecessary tags"), tr("Checks for unnecessary tags"));
|
---|
157 | addDefault(def, "wikipedia", tr("Wikipedia"), tr("Checks for wrong wikipedia tags"));
|
---|
158 |
|
---|
159 | return def;
|
---|
160 | }
|
---|
161 |
|
---|
162 | private static void addDefault(List<ExtendedSourceEntry> defaults, String filename, String title, String description) {
|
---|
163 | ExtendedSourceEntry i = new ExtendedSourceEntry(filename+".mapcss", "resource://data/validator/"+filename+".mapcss");
|
---|
164 | i.title = title;
|
---|
165 | i.description = description;
|
---|
166 | defaults.add(i);
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override
|
---|
170 | public Map<String, String> serialize(SourceEntry entry) {
|
---|
171 | Map<String, String> res = new HashMap<>();
|
---|
172 | res.put("url", entry.url);
|
---|
173 | res.put("title", entry.title == null ? "" : entry.title);
|
---|
174 | res.put("active", Boolean.toString(entry.active));
|
---|
175 | return res;
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Override
|
---|
179 | public SourceEntry deserialize(Map<String, String> s) {
|
---|
180 | return new SourceEntry(s.get("url"), null, s.get("title"), Boolean.parseBoolean(s.get("active")));
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | private SourceEditor sources;
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public void addGui(PreferenceTabbedPane gui) {
|
---|
188 | final ValidatorPreference valPref = gui.getValidatorPreference();
|
---|
189 | sources = new TagCheckerRulesSourceEditor();
|
---|
190 |
|
---|
191 | valPref.addSubTab(this, tr("Tag checker rules"),
|
---|
192 | sources, tr("Choose Tag checker rules to enable"));
|
---|
193 | sources.deferLoading(valPref, sources);
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public boolean ok() {
|
---|
198 | if (sources.finish()) {
|
---|
199 | // Reload sources
|
---|
200 | MapCSSTagChecker tagChecker = OsmValidator.getTest(MapCSSTagChecker.class);
|
---|
201 | if (tagChecker != null) {
|
---|
202 | OsmValidator.initializeTests(Collections.singleton(tagChecker));
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | return false;
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public boolean isExpert() {
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
|
---|
216 | return gui.getValidatorPreference();
|
---|
217 | }
|
---|
218 | }
|
---|