001// License: GPL. See LICENSE file for details.
002package org.openstreetmap.josm.gui.preferences;
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.Collection;
010
011import javax.swing.BorderFactory;
012import javax.swing.JCheckBox;
013import javax.swing.JLabel;
014import javax.swing.JPanel;
015
016import org.openstreetmap.josm.Main;
017import org.openstreetmap.josm.data.validation.OsmValidator;
018import org.openstreetmap.josm.data.validation.Test;
019import org.openstreetmap.josm.tools.GBC;
020
021/**
022 * Preference settings for the validator
023 *
024 * @author frsantos
025 */
026public final class ValidatorPreference extends DefaultTabPreferenceSetting {
027
028    public static class Factory implements PreferenceSettingFactory {
029        @Override
030        public PreferenceSetting createPreferenceSetting() {
031            return new ValidatorPreference();
032        }
033    }
034
035    private ValidatorPreference() {
036        super("validator", tr("Data validator"),
037                tr("An OSM data validator that checks for common errors made by users and editor programs."));
038    }
039
040    /** The preferences prefix */
041    public static final String PREFIX = "validator";
042
043    /** The preferences key for error layer */
044    public static final String PREF_LAYER = PREFIX + ".layer";
045
046    /** The preferences key for enabled tests */
047    public static final String PREF_TESTS = PREFIX + ".tests";
048
049    /** The preferences key for enabled tests */
050    public static final String PREF_USE_IGNORE = PREFIX + ".ignore";
051
052    /** The preferences key for enabled tests before upload*/
053    public static final String PREF_TESTS_BEFORE_UPLOAD = PREFIX + ".testsBeforeUpload";
054
055    /** The preferences key for ignored severity other on upload */
056    public static final String PREF_OTHER_UPLOAD = PREFIX + ".otherUpload";
057
058    /** The preferences key for ignored severity other */
059    public static final String PREF_OTHER = PREFIX + ".other";
060
061    /**
062     * The preferences key for enabling the permanent filtering
063     * of the displayed errors in the tree regarding the current selection
064     */
065    public static final String PREF_FILTER_BY_SELECTION = PREFIX + ".selectionFilter";
066
067    private JCheckBox prefUseIgnore;
068    private JCheckBox prefUseLayer;
069    private JCheckBox prefOtherUpload;
070    private JCheckBox prefOther;
071
072    /** The list of all tests */
073    private Collection<Test> allTests;
074
075    @Override
076    public void addGui(PreferenceTabbedPane gui)
077    {
078        JPanel testPanel = new JPanel(new GridBagLayout());
079        testPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
080
081        prefUseIgnore = new JCheckBox(tr("Use ignore list."), Main.pref.getBoolean(PREF_USE_IGNORE, true));
082        prefUseIgnore.setToolTipText(tr("Use the ignore list to suppress warnings."));
083        testPanel.add(prefUseIgnore, GBC.eol());
084
085        prefUseLayer = new JCheckBox(tr("Use error layer."), Main.pref.getBoolean(PREF_LAYER, true));
086        prefUseLayer.setToolTipText(tr("Use the error layer to display problematic elements."));
087        testPanel.add(prefUseLayer, GBC.eol());
088
089        prefOther = new JCheckBox(tr("Show informational level."), Main.pref.getBoolean(PREF_OTHER, false));
090        prefOther.setToolTipText(tr("Show the informational tests."));
091        testPanel.add(prefOther, GBC.eol());
092
093        prefOtherUpload = new JCheckBox(tr("Show informational level on upload."), Main.pref.getBoolean(PREF_OTHER_UPLOAD, false));
094        prefOtherUpload.setToolTipText(tr("Show the informational tests in the upload check windows."));
095        testPanel.add(prefOtherUpload, GBC.eol());
096
097        ActionListener otherUploadEnabled = new ActionListener() {
098            @Override
099            public void actionPerformed(ActionEvent e) {
100                prefOtherUpload.setEnabled(prefOther.isSelected());
101            }
102        };
103        prefOther.addActionListener(otherUploadEnabled);
104        otherUploadEnabled.actionPerformed(null);
105
106        GBC a = GBC.eol().insets(-5,0,0,0);
107        a.anchor = GBC.EAST;
108        testPanel.add( new JLabel(tr("On demand")), GBC.std() );
109        testPanel.add( new JLabel(tr("On upload")), a );
110
111        allTests = OsmValidator.getTests();
112        for (Test test: allTests) {
113            test.addGui(testPanel);
114        }
115
116        createPreferenceTabWithScrollPane(gui, testPanel);
117    }
118
119    @Override
120    public boolean ok() {
121        StringBuilder tests = new StringBuilder();
122        StringBuilder testsBeforeUpload = new StringBuilder();
123
124        for (Test test : allTests) {
125            test.ok();
126            String name = test.getClass().getSimpleName();
127            tests.append(',').append(name).append('=').append(test.enabled);
128            testsBeforeUpload.append(',').append(name).append('=').append(test.testBeforeUpload);
129        }
130
131        if (tests.length() > 0) {
132            tests = tests.deleteCharAt(0);
133        }
134        if (testsBeforeUpload.length() > 0) {
135            testsBeforeUpload = testsBeforeUpload.deleteCharAt(0);
136        }
137
138        OsmValidator.initializeTests(allTests);
139
140        Main.pref.put(PREF_TESTS, tests.toString());
141        Main.pref.put(PREF_TESTS_BEFORE_UPLOAD, testsBeforeUpload.toString());
142        Main.pref.put(PREF_USE_IGNORE, prefUseIgnore.isSelected());
143        Main.pref.put(PREF_OTHER, prefOther.isSelected());
144        Main.pref.put(PREF_OTHER_UPLOAD, prefOtherUpload.isSelected());
145        Main.pref.put(PREF_LAYER, prefUseLayer.isSelected());
146        return false;
147    }
148}