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