001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.validator;
003
004import static org.openstreetmap.josm.tools.I18n.marktr;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.util.ArrayList;
008import java.util.Collection;
009import java.util.Collections;
010import java.util.List;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
014import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
015import org.openstreetmap.josm.data.preferences.sources.SourceProvider;
016import org.openstreetmap.josm.data.preferences.sources.SourceType;
017import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
018import org.openstreetmap.josm.data.validation.OsmValidator;
019import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
020import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
021import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
022import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
023import org.openstreetmap.josm.gui.preferences.SourceEditor;
024import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
025import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
026import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
027
028/**
029 * The general validator preferences, allowing to enable/disable tests.
030 * @since 6669
031 */
032public class ValidatorTagCheckerRulesPreference implements SubPreferenceSetting {
033
034    /**
035     * Factory used to create a new {@code ValidatorTagCheckerRulesPreference}.
036     */
037    public static class Factory implements PreferenceSettingFactory {
038        @Override
039        public PreferenceSetting createPreferenceSetting() {
040            return new ValidatorTagCheckerRulesPreference();
041        }
042    }
043
044    private static final List<SourceProvider> ruleSourceProviders = new ArrayList<>();
045
046    /**
047     * Registers a new additional rule source provider.
048     * @param provider The rule source provider
049     * @return {@code true}, if the provider has been added, {@code false} otherwise
050     */
051    public static final boolean registerSourceProvider(SourceProvider provider) {
052        if (provider != null)
053            return ruleSourceProviders.add(provider);
054        return false;
055    }
056
057    static class TagCheckerRulesSourceEditor extends SourceEditor {
058
059        TagCheckerRulesSourceEditor() {
060            super(SourceType.TAGCHECKER_RULE, Main.getJOSMWebsite()+"/rules", ruleSourceProviders, false);
061        }
062
063        @Override
064        public Collection<? extends SourceEntry> getInitialSourcesList() {
065            return ValidatorPrefHelper.INSTANCE.get();
066        }
067
068        @Override
069        public boolean finish() {
070            return ValidatorPrefHelper.INSTANCE.put(activeSourcesModel.getSources());
071        }
072
073        @Override
074        public Collection<ExtendedSourceEntry> getDefault() {
075            return ValidatorPrefHelper.INSTANCE.getDefault();
076        }
077
078        @Override
079        public Collection<String> getInitialIconPathsList() {
080            return null;
081        }
082
083        @Override
084        public String getStr(I18nString ident) {
085            switch (ident) {
086            case AVAILABLE_SOURCES:
087                return tr("Available rules:");
088            case ACTIVE_SOURCES:
089                return tr("Active rules:");
090            case NEW_SOURCE_ENTRY_TOOLTIP:
091                return tr("Add a new rule by entering filename or URL");
092            case NEW_SOURCE_ENTRY:
093                return tr("New rule entry:");
094            case REMOVE_SOURCE_TOOLTIP:
095                return tr("Remove the selected rules from the list of active rules");
096            case EDIT_SOURCE_TOOLTIP:
097                return tr("Edit the filename or URL for the selected active rule");
098            case ACTIVATE_TOOLTIP:
099                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}