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