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.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 JPanel(new GridBagLayout());
058        testPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
059
060        prefUseIgnore = new JCheckBox(tr("Use ignore list."), Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true));
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."), Main.pref.getBoolean(ValidatorPreference.PREF_LAYER, true));
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."), Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false));
073        prefOtherUpload.setToolTipText(tr("Show the informational tests in the upload check windows."));
074        testPanel.add(prefOtherUpload, GBC.eol());
075
076        ActionListener otherUploadEnabled = new ActionListener() {
077            @Override
078            public void actionPerformed(ActionEvent e) {
079                prefOtherUpload.setEnabled(prefOther.isSelected());
080            }
081        };
082        prefOther.addActionListener(otherUploadEnabled);
083        otherUploadEnabled.actionPerformed(null);
084
085        GBC a = GBC.eol().insets(-5,0,0,0);
086        a.anchor = GBC.EAST;
087        testPanel.add( new JLabel(tr("On demand")), GBC.std() );
088        testPanel.add( new JLabel(tr("On upload")), a );
089
090        allTests = OsmValidator.getTests();
091        for (Test test: allTests) {
092            test.addGui(testPanel);
093        }
094        
095        gui.getValidatorPreference().addSubTab(this, tr("Tests"),
096                GuiHelper.embedInVerticalScrollPane(testPanel),
097                tr("Choose tests to enable"));
098    }
099
100    @Override
101    public boolean ok() {
102        Collection<String> tests = new LinkedList<>();
103        Collection<String> testsBeforeUpload = new LinkedList<>();
104
105        for (Test test : allTests) {
106            test.ok();
107            String name = test.getClass().getSimpleName();
108            if(!test.enabled)
109                tests.add(name);
110            if(!test.testBeforeUpload)
111                testsBeforeUpload.add(name);
112        }
113        
114        // Initializes all tests but MapCSSTagChecker because it is initialized
115        // later in ValidatorTagCheckerRulesPreference.ok(),
116        // after its list of rules has been saved to preferences
117        List<Test> testsToInitialize = new ArrayList<>(allTests);
118        testsToInitialize.remove(OsmValidator.getTest(MapCSSTagChecker.class));
119        OsmValidator.initializeTests(testsToInitialize);
120
121        Main.pref.putCollection(ValidatorPreference.PREF_SKIP_TESTS, tests);
122        Main.pref.putCollection(ValidatorPreference.PREF_SKIP_TESTS_BEFORE_UPLOAD, testsBeforeUpload);
123        Main.pref.put(ValidatorPreference.PREF_USE_IGNORE, prefUseIgnore.isSelected());
124        ValidatorPreference.PREF_OTHER.put(prefOther.isSelected());
125        Main.pref.put(ValidatorPreference.PREF_OTHER_UPLOAD, prefOtherUpload.isSelected());
126        Main.pref.put(ValidatorPreference.PREF_LAYER, prefUseLayer.isSelected());
127        return false;
128    }
129
130    @Override
131    public boolean isExpert() {
132        return false;
133    }
134
135    @Override
136    public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
137        return gui.getValidatorPreference();
138    }
139}