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.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.BorderFactory;
|
---|
15 | import javax.swing.JCheckBox;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
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.tools.GBC;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * The general validator preferences, allowing to enable/disable tests.
|
---|
33 | * @since 6666
|
---|
34 | */
|
---|
35 | public class ValidatorTestsPreference implements SubPreferenceSetting {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Factory used to create a new {@code ValidatorTestsPreference}.
|
---|
39 | */
|
---|
40 | public static class Factory implements PreferenceSettingFactory {
|
---|
41 | @Override
|
---|
42 | public PreferenceSetting createPreferenceSetting() {
|
---|
43 | return new ValidatorTestsPreference();
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | private JCheckBox prefUseIgnore;
|
---|
48 | private JCheckBox prefUseLayer;
|
---|
49 | private JCheckBox prefOtherUpload;
|
---|
50 | private JCheckBox prefOther;
|
---|
51 |
|
---|
52 | /** The list of all tests */
|
---|
53 | private Collection<Test> allTests;
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void addGui(PreferenceTabbedPane gui) {
|
---|
57 | JPanel testPanel = new JPanel(new GridBagLayout());
|
---|
58 | testPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
59 |
|
---|
60 | prefUseIgnore = new JCheckBox(tr("Use ignore list."), Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true));
|
---|
61 | prefUseIgnore.setToolTipText(tr("Use the ignore list to suppress warnings."));
|
---|
62 | testPanel.add(prefUseIgnore, GBC.eol());
|
---|
63 |
|
---|
64 | prefUseLayer = new JCheckBox(tr("Use error layer."), Main.pref.getBoolean(ValidatorPreference.PREF_LAYER, true));
|
---|
65 | prefUseLayer.setToolTipText(tr("Use the error layer to display problematic elements."));
|
---|
66 | testPanel.add(prefUseLayer, GBC.eol());
|
---|
67 |
|
---|
68 | prefOther = new JCheckBox(tr("Show informational level."), ValidatorPreference.PREF_OTHER.get());
|
---|
69 | prefOther.setToolTipText(tr("Show the informational tests."));
|
---|
70 | testPanel.add(prefOther, GBC.eol());
|
---|
71 |
|
---|
72 | prefOtherUpload = new JCheckBox(tr("Show informational level on upload."), Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false));
|
---|
73 | prefOtherUpload.setToolTipText(tr("Show the informational tests in the upload check windows."));
|
---|
74 | testPanel.add(prefOtherUpload, GBC.eol());
|
---|
75 |
|
---|
76 | ActionListener otherUploadEnabled = new ActionListener() {
|
---|
77 | @Override
|
---|
78 | public void actionPerformed(ActionEvent e) {
|
---|
79 | prefOtherUpload.setEnabled(prefOther.isSelected());
|
---|
80 | }
|
---|
81 | };
|
---|
82 | prefOther.addActionListener(otherUploadEnabled);
|
---|
83 | otherUploadEnabled.actionPerformed(null);
|
---|
84 |
|
---|
85 | GBC a = GBC.eol().insets(-5,0,0,0);
|
---|
86 | a.anchor = GBC.EAST;
|
---|
87 | testPanel.add( new JLabel(tr("On demand")), GBC.std() );
|
---|
88 | testPanel.add( new JLabel(tr("On upload")), a );
|
---|
89 |
|
---|
90 | allTests = OsmValidator.getTests();
|
---|
91 | for (Test test: allTests) {
|
---|
92 | test.addGui(testPanel);
|
---|
93 | }
|
---|
94 |
|
---|
95 | gui.getValidatorPreference().addSubTab(this, tr("Tests"),
|
---|
96 | GuiHelper.embedInVerticalScrollPane(testPanel),
|
---|
97 | tr("Choose tests to enable"));
|
---|
98 | }
|
---|
99 |
|
---|
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 | }
|
---|