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.List;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
|
---|
13 | import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
|
---|
14 | import org.openstreetmap.josm.data.preferences.sources.SourceProvider;
|
---|
15 | import org.openstreetmap.josm.data.preferences.sources.SourceType;
|
---|
16 | import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
|
---|
17 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
18 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
|
---|
19 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
20 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
21 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
22 | import org.openstreetmap.josm.gui.preferences.SourceEditor;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
26 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
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<>();
|
---|
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 | TagCheckerRulesSourceEditor() {
|
---|
60 | super(SourceType.TAGCHECKER_RULE, Config.getUrls().getJOSMWebsite()+"/rules", ruleSourceProviders, false);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public Collection<? extends SourceEntry> getInitialSourcesList() {
|
---|
65 | return ValidatorPrefHelper.INSTANCE.get();
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public boolean finish() {
|
---|
70 | return ValidatorPrefHelper.INSTANCE.put(activeSourcesModel.getSources());
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public Collection<ExtendedSourceEntry> getDefault() {
|
---|
75 | return ValidatorPrefHelper.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 | @Override
|
---|
118 | protected String getTitleForSourceEntry(SourceEntry entry) {
|
---|
119 | final String title = MapPaintPreference.getTitleFromSourceEntry(entry);
|
---|
120 | return title != null ? title : super.getTitleForSourceEntry(entry);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | private SourceEditor sources;
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public void addGui(PreferenceTabbedPane gui) {
|
---|
128 | final ValidatorPreference valPref = gui.getValidatorPreference();
|
---|
129 | sources = new TagCheckerRulesSourceEditor();
|
---|
130 |
|
---|
131 | valPref.addSubTab(this, tr("Tag checker rules"),
|
---|
132 | sources, tr("Choose Tag checker rules to enable"));
|
---|
133 | sources.deferLoading(valPref, sources);
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public boolean ok() {
|
---|
138 | if (sources.finish()) {
|
---|
139 | // Reload sources
|
---|
140 | MapCSSTagChecker tagChecker = OsmValidator.getTest(MapCSSTagChecker.class);
|
---|
141 | if (tagChecker != null) {
|
---|
142 | OsmValidator.initializeTests(Collections.singleton(tagChecker));
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public boolean isExpert() {
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
|
---|
156 | return gui.getValidatorPreference();
|
---|
157 | }
|
---|
158 | }
|
---|