1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.validator;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionListener;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.BorderFactory;
|
---|
14 | import javax.swing.JCheckBox;
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
|
---|
20 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
21 | import org.openstreetmap.josm.data.validation.Test;
|
---|
22 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
26 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
---|
27 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
---|
28 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
29 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
30 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
31 | import org.openstreetmap.josm.tools.GBC;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * The general validator preferences, allowing to enable/disable tests.
|
---|
35 | * @since 6666
|
---|
36 | */
|
---|
37 | public class ValidatorTestsPreference implements SubPreferenceSetting {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Factory used to create a new {@code ValidatorTestsPreference}.
|
---|
41 | */
|
---|
42 | public static class Factory implements PreferenceSettingFactory {
|
---|
43 | @Override
|
---|
44 | public PreferenceSetting createPreferenceSetting() {
|
---|
45 | return new ValidatorTestsPreference();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private JCheckBox prefUseIgnore;
|
---|
50 | private JCheckBox prefUseLayer;
|
---|
51 | private JCheckBox prefOtherUpload;
|
---|
52 | private JCheckBox prefOther;
|
---|
53 |
|
---|
54 | /** The list of all tests */
|
---|
55 | private Collection<Test> allTests;
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public void addGui(PreferenceTabbedPane gui) {
|
---|
59 | JPanel testPanel = new VerticallyScrollablePanel(new GridBagLayout());
|
---|
60 | testPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
61 |
|
---|
62 | prefUseIgnore = new JCheckBox(tr("Use ignore list."), ValidatorPrefHelper.PREF_USE_IGNORE.get());
|
---|
63 | prefUseIgnore.setToolTipText(tr("Use the ignore list to suppress warnings."));
|
---|
64 | testPanel.add(prefUseIgnore, GBC.eol());
|
---|
65 |
|
---|
66 | prefUseLayer = new JCheckBox(tr("Use error layer."), ValidatorPrefHelper.PREF_LAYER.get());
|
---|
67 | prefUseLayer.setToolTipText(tr("Use the error layer to display problematic elements."));
|
---|
68 | testPanel.add(prefUseLayer, GBC.eol());
|
---|
69 |
|
---|
70 | prefOther = new JCheckBox(tr("Show informational level."), ValidatorPrefHelper.PREF_OTHER.get());
|
---|
71 | prefOther.setToolTipText(tr("Show the informational tests."));
|
---|
72 | testPanel.add(prefOther, GBC.eol());
|
---|
73 |
|
---|
74 | prefOtherUpload = new JCheckBox(tr("Show informational level on upload."),
|
---|
75 | ValidatorPrefHelper.PREF_OTHER_UPLOAD.get());
|
---|
76 | prefOtherUpload.setToolTipText(tr("Show the informational tests in the upload check windows."));
|
---|
77 | testPanel.add(prefOtherUpload, GBC.eol());
|
---|
78 |
|
---|
79 | ActionListener otherUploadEnabled = e -> prefOtherUpload.setEnabled(prefOther.isSelected());
|
---|
80 | prefOther.addActionListener(otherUploadEnabled);
|
---|
81 | otherUploadEnabled.actionPerformed(null);
|
---|
82 |
|
---|
83 | GBC a = GBC.eol().insets(-5, 0, 0, 0);
|
---|
84 | a.anchor = GBC.EAST;
|
---|
85 | testPanel.add(new JLabel(tr("On demand")), GBC.std());
|
---|
86 | testPanel.add(new JLabel(tr("On upload")), a);
|
---|
87 |
|
---|
88 | allTests = OsmValidator.getTests();
|
---|
89 | for (Test test: allTests) {
|
---|
90 | test.addGui(testPanel);
|
---|
91 | }
|
---|
92 |
|
---|
93 | JScrollPane scrollPane = GuiHelper.embedInVerticalScrollPane(testPanel);
|
---|
94 | scrollPane.setBorder(BorderFactory.createEmptyBorder());
|
---|
95 | gui.getValidatorPreference().addSubTab(this, tr("Tests"),
|
---|
96 | scrollPane,
|
---|
97 | tr("Choose tests to enable"));
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public boolean ok() {
|
---|
102 | List<String> tests = new LinkedList<>();
|
---|
103 | List<String> testsBeforeUpload = new LinkedList<>();
|
---|
104 |
|
---|
105 | for (Test test : allTests) {
|
---|
106 | test.ok();
|
---|
107 | String name = test.getClass().getName();
|
---|
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 | Config.getPref().putList(ValidatorPrefHelper.PREF_SKIP_TESTS, tests);
|
---|
122 | Config.getPref().putList(ValidatorPrefHelper.PREF_SKIP_TESTS_BEFORE_UPLOAD, testsBeforeUpload);
|
---|
123 | ValidatorPrefHelper.PREF_USE_IGNORE.put(prefUseIgnore.isSelected());
|
---|
124 | ValidatorPrefHelper.PREF_OTHER.put(prefOther.isSelected());
|
---|
125 | ValidatorPrefHelper.PREF_OTHER_UPLOAD.put(prefOtherUpload.isSelected());
|
---|
126 | ValidatorPrefHelper.PREF_LAYER.put(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 | }
|
---|