001    // License: GPL. See LICENSE file for details.
002    package org.openstreetmap.josm.gui.preferences;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.awt.GridBagLayout;
007    import java.awt.event.ActionEvent;
008    import java.awt.event.ActionListener;
009    import java.util.Collection;
010    
011    import javax.swing.BorderFactory;
012    import javax.swing.JCheckBox;
013    import javax.swing.JLabel;
014    import javax.swing.JPanel;
015    
016    import org.openstreetmap.josm.Main;
017    import org.openstreetmap.josm.data.validation.OsmValidator;
018    import org.openstreetmap.josm.data.validation.Test;
019    import org.openstreetmap.josm.tools.GBC;
020    
021    /**
022     * Preference settings for the validator
023     *
024     * @author frsantos
025     */
026    public 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                public void actionPerformed(ActionEvent e) {
099                    prefOtherUpload.setEnabled(prefOther.isSelected());
100                }
101            };
102            prefOther.addActionListener(otherUploadEnabled);
103            otherUploadEnabled.actionPerformed(null);
104    
105            GBC a = GBC.eol().insets(-5,0,0,0);
106            a.anchor = GBC.EAST;
107            testPanel.add( new JLabel(tr("On demand")), GBC.std() );
108            testPanel.add( new JLabel(tr("On upload")), a );
109    
110            allTests = OsmValidator.getTests();
111            for (Test test: allTests) {
112                test.addGui(testPanel);
113            }
114    
115            createPreferenceTabWithScrollPane(gui, testPanel);
116        }
117    
118        @Override
119        public boolean ok() {
120            StringBuilder tests = new StringBuilder();
121            StringBuilder testsBeforeUpload = new StringBuilder();
122    
123            for (Test test : allTests) {
124                test.ok();
125                String name = test.getClass().getSimpleName();
126                tests.append(',').append(name).append('=').append(test.enabled);
127                testsBeforeUpload.append(',').append(name).append('=').append(test.testBeforeUpload);
128            }
129    
130            if (tests.length() > 0) {
131                tests = tests.deleteCharAt(0);
132            }
133            if (testsBeforeUpload.length() > 0) {
134                testsBeforeUpload = testsBeforeUpload.deleteCharAt(0);
135            }
136    
137            OsmValidator.initializeTests(allTests);
138    
139            Main.pref.put(PREF_TESTS, tests.toString());
140            Main.pref.put(PREF_TESTS_BEFORE_UPLOAD, testsBeforeUpload.toString());
141            Main.pref.put(PREF_USE_IGNORE, prefUseIgnore.isSelected());
142            Main.pref.put(PREF_OTHER, prefOther.isSelected());
143            Main.pref.put(PREF_OTHER_UPLOAD, prefOtherUpload.isSelected());
144            Main.pref.put(PREF_LAYER, prefUseLayer.isSelected());
145            return false;
146        }
147    }