source: josm/trunk/src/org/openstreetmap/josm/actions/ValidateAction.java @ 5241

Revision 4982, 6.9 KB checked in by stoecker, 3 months ago (diff)

see #7226 - patch by akks (fixed a bit) - fix shortcut deprecations

  • Property svn:eol-style set to native
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.List;
12
13import javax.swing.SwingUtilities;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.validation.OsmValidator;
18import org.openstreetmap.josm.data.validation.Test;
19import org.openstreetmap.josm.data.validation.TestError;
20import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
23import org.openstreetmap.josm.io.OsmTransferException;
24import org.openstreetmap.josm.tools.Shortcut;
25import org.xml.sax.SAXException;
26
27/**
28 * The action that does the validate thing.
29 * <p>
30 * This action iterates through all active tests and give them the data, so that
31 * each one can test it.
32 *
33 * @author frsantos
34 */
35public class ValidateAction extends JosmAction {
36
37    /** Serializable ID */
38    private static final long serialVersionUID = -2304521273582574603L;
39
40    /** Last selection used to validate */
41    private Collection<OsmPrimitive> lastSelection;
42
43    /**
44     * Constructor
45     */
46    public ValidateAction() {
47        super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"),
48            Shortcut.registerShortcut("tools:validate", tr("Tool: {0}", tr("Validation")),
49            KeyEvent.VK_V, Shortcut.SHIFT), true);
50    }
51
52    public void actionPerformed(ActionEvent ev) {
53        doValidate(ev, true);
54    }
55
56    /**
57     * Does the validation.
58     * <p>
59     * If getSelectedItems is true, the selected items (or all items, if no one
60     * is selected) are validated. If it is false, last selected items are
61     * revalidated
62     *
63     * @param ev The event
64     * @param getSelectedItems If selected or last selected items must be validated
65     */
66    public void doValidate(ActionEvent ev, boolean getSelectedItems) {
67        if (Main.main.validator.validateAction == null || Main.map == null || !Main.map.isVisible())
68            return;
69
70        OsmValidator.initializeErrorLayer();
71
72        Collection<Test> tests = OsmValidator.getEnabledTests(false);
73        if (tests.isEmpty())
74            return;
75
76        Collection<OsmPrimitive> selection;
77        if (getSelectedItems) {
78            selection = Main.main.getCurrentDataSet().getSelected();
79            if (selection.isEmpty()) {
80                selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
81                lastSelection = null;
82            } else {
83                AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
84                selection = v.visit(selection);
85                lastSelection = selection;
86            }
87        } else {
88            if (lastSelection == null) {
89                selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
90            } else {
91                selection = lastSelection;
92            }
93        }
94
95        ValidationTask task = new ValidationTask(tests, selection, lastSelection);
96        Main.worker.submit(task);
97    }
98
99    @Override
100    public void updateEnabledState() {
101        setEnabled(getEditLayer() != null);
102    }
103
104    /**
105     * Asynchronous task for running a collection of tests against a collection
106     * of primitives
107     *
108     */
109    static class ValidationTask extends PleaseWaitRunnable {
110        private Collection<Test> tests;
111        private Collection<OsmPrimitive> validatedPrimitives;
112        private Collection<OsmPrimitive> formerValidatedPrimitives;
113        private boolean canceled;
114        private List<TestError> errors;
115
116        /**
117         *
118         * @param tests  the tests to run
119         * @param validatedPrimitives the collection of primitives to validate.
120         * @param formerValidatedPrimitives the last collection of primitives being validates. May be null.
121         */
122        public ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives, Collection<OsmPrimitive> formerValidatedPrimitives) {
123            super(tr("Validating"), false /*don't ignore exceptions */);
124            this.validatedPrimitives  = validatedPrimitives;
125            this.formerValidatedPrimitives = formerValidatedPrimitives;
126            this.tests = tests;
127        }
128
129        @Override
130        protected void cancel() {
131            this.canceled = true;
132        }
133
134        @Override
135        protected void finish() {
136            if (canceled) return;
137
138            // update GUI on Swing EDT
139            //
140            Runnable r = new Runnable()  {
141                @Override
142                public void run() {
143                    Main.map.validatorDialog.tree.setErrors(errors);
144                    Main.map.validatorDialog.unfurlDialog();
145                    Main.main.getCurrentDataSet().fireSelectionChanged();
146                }
147            };
148            if (SwingUtilities.isEventDispatchThread()) {
149                r.run();
150            } else {
151                SwingUtilities.invokeLater(r);
152            }
153        }
154
155        @Override
156        protected void realRun() throws SAXException, IOException,
157        OsmTransferException {
158            if (tests == null || tests.isEmpty())
159                return;
160            errors = new ArrayList<TestError>(200);
161            getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
162            int testCounter = 0;
163            for (Test test : tests) {
164                if (canceled)
165                    return;
166                testCounter++;
167                getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(),test.getName()));
168                test.setPartialSelection(formerValidatedPrimitives != null);
169                test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
170                test.visit(validatedPrimitives);
171                test.endTest();
172                errors.addAll(test.getErrors());
173            }
174            tests = null;
175            if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
176                getProgressMonitor().subTask(tr("Updating ignored errors ..."));
177                for (TestError error : errors) {
178                    if (canceled) return;
179                    List<String> s = new ArrayList<String>();
180                    s.add(error.getIgnoreState());
181                    s.add(error.getIgnoreGroup());
182                    s.add(error.getIgnoreSubGroup());
183                    for (String state : s) {
184                        if (state != null && OsmValidator.hasIgnoredError(state)) {
185                            error.setIgnored(true);
186                        }
187                    }
188                }
189            }
190        }
191    }
192}
Note: See TracBrowser for help on using the repository browser.