001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.validator;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagLayout;
007import java.awt.event.ActionListener;
008import java.util.ArrayList;
009import java.util.Collection;
010import java.util.LinkedList;
011import java.util.List;
012
013import javax.swing.BorderFactory;
014import javax.swing.JCheckBox;
015import javax.swing.JLabel;
016import javax.swing.JPanel;
017
018import org.openstreetmap.josm.Main;
019import org.openstreetmap.josm.data.validation.OsmValidator;
020import org.openstreetmap.josm.data.validation.Test;
021import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
022import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
023import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
024import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
025import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
026import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
027import org.openstreetmap.josm.gui.util.GuiHelper;
028import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
029import org.openstreetmap.josm.tools.GBC;
030
031/**
032 * The general validator preferences, allowing to enable/disable tests.
033 * @since 6666
034 */
035public class ValidatorTestsPreference implements SubPreferenceSetting {
036
037    /**
038     * Factory used to create a new {@code ValidatorTestsPreference}.
039     */
040    public static class Factory implements PreferenceSettingFactory {
041        @Override
042        public PreferenceSetting createPreferenceSetting() {
043            return new ValidatorTestsPreference();
044        }
045    }
046
047    private JCheckBox prefUseIgnore;
048    private JCheckBox prefUseLayer;
049    private JCheckBox prefOtherUpload;
050    private JCheckBox prefOther;
051
052    /** The list of all tests */
053    private Collection<Test> allTests;
054
055    @Override
056    public void addGui(PreferenceTabbedPane gui) {
057        JPanel testPanel = new VerticallyScrollablePanel(new GridBagLayout());
058        testPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
059
060        prefUseIgnore = new JCheckBox(tr("Use ignore list."), ValidatorPreference.PREF_USE_IGNORE.get());
061        prefUseIgnore.setToolTipText(tr("Use the ignore list to suppress warnings."));
062        testPanel.add(prefUseIgnore, GBC.eol());
063
064        prefUseLayer = new JCheckBox(tr("Use error layer."), ValidatorPreference.PREF_LAYER.get());
065        prefUseLayer.setToolTipText(tr("Use the error layer to display problematic elements."));
066        testPanel.add(prefUseLayer, GBC.eol());
067
068        prefOther = new JCheckBox(tr("Show informational level."), ValidatorPreference.PREF_OTHER.get());
069        prefOther.setToolTipText(tr("Show the informational tests."));
070        testPanel.add(prefOther, GBC.eol());
071
072        prefOtherUpload = new JCheckBox(tr("Show informational level on upload."),
073                ValidatorPreference.PREF_OTHER_UPLOAD.get());
074        prefOtherUpload.setToolTipText(tr("Show the informational tests in the upload check windows."));
075        testPanel.add(prefOtherUpload, GBC.eol());
076
077        ActionListener otherUploadEnabled = e -> prefOtherUpload.setEnabled(prefOther.isSelected());
078        prefOther.addActionListener(otherUploadEnabled);
079        otherUploadEnabled.actionPerformed(null);
080
081        GBC a = GBC.eol().insets(-5, 0, 0, 0);
082        a.anchor = GBC.EAST;
083        testPanel.add(new JLabel(tr("On demand")), GBC.std());
084        testPanel.add(new JLabel(tr("On upload")), a);
085
086        allTests = OsmValidator.getTests();
087        for (Test test: allTests) {
088            test.addGui(testPanel);
089        }
090
091        gui.getValidatorPreference().addSubTab(this, tr("Tests"),
092                GuiHelper.embedInVerticalScrollPane(testPanel),
093                tr("Choose tests to enable"));
094    }
095
096    @Override
097    public boolean ok() {
098        Collection<String> tests = new LinkedList<>();
099        Collection<String> testsBeforeUpload = new LinkedList<>();
100
101        for (Test test : allTests) {
102            test.ok();
103            String name = test.getClass().getName();
104            if (!test.enabled)
105                tests.add(name);
106            if (!test.testBeforeUpload)
107                testsBeforeUpload.add(name);
108        }
109
110        // Initializes all tests but MapCSSTagChecker because it is initialized
111        // later in ValidatorTagCheckerRulesPreference.ok(),
112        // after its list of rules has been saved to preferences
113        List<Test> testsToInitialize = new ArrayList<>(allTests);
114        testsToInitialize.remove(OsmValidator.getTest(MapCSSTagChecker.class));
115        OsmValidator.initializeTests(testsToInitialize);
116
117        Main.pref.putCollection(ValidatorPreference.PREF_SKIP_TESTS, tests);
118        Main.pref.putCollection(ValidatorPreference.PREF_SKIP_TESTS_BEFORE_UPLOAD, testsBeforeUpload);
119        ValidatorPreference.PREF_USE_IGNORE.put(prefUseIgnore.isSelected());
120        ValidatorPreference.PREF_OTHER.put(prefOther.isSelected());
121        ValidatorPreference.PREF_OTHER_UPLOAD.put(prefOtherUpload.isSelected());
122        ValidatorPreference.PREF_LAYER.put(prefUseLayer.isSelected());
123        return false;
124    }
125
126    @Override
127    public boolean isExpert() {
128        return false;
129    }
130
131    @Override
132    public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
133        return gui.getValidatorPreference();
134    }
135}