source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/validator/ValidatorTagCheckerRulesPreference.java@ 6927

Last change on this file since 6927 was 6927, checked in by Don-vip, 10 years ago

fix #9667 - Verify addresses interpolation range/values via MapCSS (patch by simon04)

File size: 8.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.validator;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.validation.OsmValidator;
16import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
17import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
18import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
19import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
20import org.openstreetmap.josm.gui.preferences.SourceEditor;
21import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
22import org.openstreetmap.josm.gui.preferences.SourceEntry;
23import org.openstreetmap.josm.gui.preferences.SourceProvider;
24import org.openstreetmap.josm.gui.preferences.SourceType;
25import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
26import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
27
28/**
29 * The general validator preferences, allowing to enable/disable tests.
30 * @since 6669
31 */
32public 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.getJOSMWebsite()+"/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 static final 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, "addresses", tr("Addresses"), tr("Checks for errors on addresses"));
140 addDefault(def, "combinations", tr("Tag combinations"), tr("Checks for missing tag or suspicious combinations"));
141 addDefault(def, "deprecated", tr("Deprecated features"), tr("Checks for deprecated features"));
142 addDefault(def, "geometry", tr("Geometry"), tr("Checks for geometry errors"));
143 addDefault(def, "highway", tr("Highways"), tr("Checks for errors on highways"));
144 addDefault(def, "numeric", tr("Numeric values"), tr("Checks for wrong numeric values"));
145 addDefault(def, "power", tr("Power"), tr("Checks for errors on power infrastructures"));
146 addDefault(def, "religion", tr("Religion"), tr("Checks for errors on religious objects"));
147 addDefault(def, "relation", tr("Relations"), tr("Checks for errors on relations"));
148 addDefault(def, "unnecessary", tr("Unnecessary tags"), tr("Checks for unnecessary tags"));
149 addDefault(def, "wikipedia", tr("Wikipedia"), tr("Checks for wrong wikipedia tags"));
150
151 return def;
152 }
153
154 private void addDefault(List<ExtendedSourceEntry> defaults, String filename, String title, String description) {
155 ExtendedSourceEntry i = new ExtendedSourceEntry(filename+".mapcss", "resource://data/validator/"+filename+".mapcss");
156 i.title = title;
157 i.description = description;
158 defaults.add(i);
159 }
160
161 @Override
162 public Map<String, String> serialize(SourceEntry entry) {
163 Map<String, String> res = new HashMap<String, String>();
164 res.put("url", entry.url);
165 res.put("title", entry.title == null ? "" : entry.title);
166 res.put("active", Boolean.toString(entry.active));
167 return res;
168 }
169
170 @Override
171 public SourceEntry deserialize(Map<String, String> s) {
172 return new SourceEntry(s.get("url"), null, s.get("title"), Boolean.parseBoolean(s.get("active")));
173 }
174 }
175
176 private SourceEditor sources;
177
178 @Override
179 public void addGui(PreferenceTabbedPane gui) {
180 final ValidatorPreference valPref = gui.getValidatorPreference();
181 sources = new TagCheckerRulesSourceEditor();
182
183 valPref.addSubTab(this, tr("Tag checker rules"),
184 sources, tr("Choose Tag checker rules to enable"));
185 sources.deferLoading(valPref, sources);
186 }
187
188 @Override
189 public boolean ok() {
190 if (sources.finish()) {
191 // Reload sources
192 MapCSSTagChecker tagChecker = OsmValidator.getTest(MapCSSTagChecker.class);
193 if (tagChecker != null) {
194 OsmValidator.initializeTests(Collections.singleton(tagChecker));
195 }
196 }
197
198 return false;
199 }
200
201 @Override
202 public boolean isExpert() {
203 return false;
204 }
205
206 @Override
207 public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
208 return gui.getValidatorPreference();
209 }
210}
Note: See TracBrowser for help on using the repository browser.