source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/validator/ValidatorTestsPreference.java @ 12841

Last change on this file since 12841 was 12841, checked in by bastiK, 6 weeks ago

see #15229 - fix deprecations caused by [12840]

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.validator;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.awt.event.ActionListener;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.LinkedList;
11import java.util.List;
12
13import javax.swing.BorderFactory;
14import javax.swing.JCheckBox;
15import javax.swing.JLabel;
16import javax.swing.JPanel;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
20import org.openstreetmap.josm.data.validation.OsmValidator;
21import org.openstreetmap.josm.data.validation.Test;
22import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
23import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
24import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
25import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
26import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
27import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
28import org.openstreetmap.josm.gui.util.GuiHelper;
29import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
30import org.openstreetmap.josm.tools.GBC;
31
32/**
33 * The general validator preferences, allowing to enable/disable tests.
34 * @since 6666
35 */
36public class ValidatorTestsPreference implements SubPreferenceSetting {
37
38    /**
39     * Factory used to create a new {@code ValidatorTestsPreference}.
40     */
41    public static class Factory implements PreferenceSettingFactory {
42        @Override
43        public PreferenceSetting createPreferenceSetting() {
44            return new ValidatorTestsPreference();
45        }
46    }
47
48    private JCheckBox prefUseIgnore;
49    private JCheckBox prefUseLayer;
50    private JCheckBox prefOtherUpload;
51    private JCheckBox prefOther;
52
53    /** The list of all tests */
54    private Collection<Test> allTests;
55
56    @Override
57    public void addGui(PreferenceTabbedPane gui) {
58        JPanel testPanel = new VerticallyScrollablePanel(new GridBagLayout());
59        testPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
60
61        prefUseIgnore = new JCheckBox(tr("Use ignore list."), ValidatorPrefHelper.PREF_USE_IGNORE.get());
62        prefUseIgnore.setToolTipText(tr("Use the ignore list to suppress warnings."));
63        testPanel.add(prefUseIgnore, GBC.eol());
64
65        prefUseLayer = new JCheckBox(tr("Use error layer."), ValidatorPrefHelper.PREF_LAYER.get());
66        prefUseLayer.setToolTipText(tr("Use the error layer to display problematic elements."));
67        testPanel.add(prefUseLayer, GBC.eol());
68
69        prefOther = new JCheckBox(tr("Show informational level."), ValidatorPrefHelper.PREF_OTHER.get());
70        prefOther.setToolTipText(tr("Show the informational tests."));
71        testPanel.add(prefOther, GBC.eol());
72
73        prefOtherUpload = new JCheckBox(tr("Show informational level on upload."),
74                ValidatorPrefHelper.PREF_OTHER_UPLOAD.get());
75        prefOtherUpload.setToolTipText(tr("Show the informational tests in the upload check windows."));
76        testPanel.add(prefOtherUpload, GBC.eol());
77
78        ActionListener otherUploadEnabled = e -> prefOtherUpload.setEnabled(prefOther.isSelected());
79        prefOther.addActionListener(otherUploadEnabled);
80        otherUploadEnabled.actionPerformed(null);
81
82        GBC a = GBC.eol().insets(-5, 0, 0, 0);
83        a.anchor = GBC.EAST;
84        testPanel.add(new JLabel(tr("On demand")), GBC.std());
85        testPanel.add(new JLabel(tr("On upload")), a);
86
87        allTests = OsmValidator.getTests();
88        for (Test test: allTests) {
89            test.addGui(testPanel);
90        }
91
92        gui.getValidatorPreference().addSubTab(this, tr("Tests"),
93                GuiHelper.embedInVerticalScrollPane(testPanel),
94                tr("Choose tests to enable"));
95    }
96
97    @Override
98    public boolean ok() {
99        List<String> tests = new LinkedList<>();
100        List<String> testsBeforeUpload = new LinkedList<>();
101
102        for (Test test : allTests) {
103            test.ok();
104            String name = test.getClass().getName();
105            if (!test.enabled)
106                tests.add(name);
107            if (!test.testBeforeUpload)
108                testsBeforeUpload.add(name);
109        }
110
111        // Initializes all tests but MapCSSTagChecker because it is initialized
112        // later in ValidatorTagCheckerRulesPreference.ok(),
113        // after its list of rules has been saved to preferences
114        List<Test> testsToInitialize = new ArrayList<>(allTests);
115        testsToInitialize.remove(OsmValidator.getTest(MapCSSTagChecker.class));
116        OsmValidator.initializeTests(testsToInitialize);
117
118        Main.pref.putList(ValidatorPrefHelper.PREF_SKIP_TESTS, tests);
119        Main.pref.putList(ValidatorPrefHelper.PREF_SKIP_TESTS_BEFORE_UPLOAD, testsBeforeUpload);
120        ValidatorPrefHelper.PREF_USE_IGNORE.put(prefUseIgnore.isSelected());
121        ValidatorPrefHelper.PREF_OTHER.put(prefOther.isSelected());
122        ValidatorPrefHelper.PREF_OTHER_UPLOAD.put(prefOtherUpload.isSelected());
123        ValidatorPrefHelper.PREF_LAYER.put(prefUseLayer.isSelected());
124        return false;
125    }
126
127    @Override
128    public boolean isExpert() {
129        return false;
130    }
131
132    @Override
133    public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
134        return gui.getValidatorPreference();
135    }
136}
Note: See TracBrowser for help on using the repository browser.