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

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

sonar - squid:S2325 - "private" methods that don't access instance data should be "static"

  • Property svn:eol-style set to native
File size: 8.7 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;
27import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
28
29/**
30 * The general validator preferences, allowing to enable/disable tests.
31 * @since 6669
32 */
33public 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}
Note: See TracBrowser for help on using the repository browser.