1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.lang.reflect.InvocationTargetException;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Enumeration;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Set;
|
---|
18 |
|
---|
19 | import javax.swing.AbstractAction;
|
---|
20 | import javax.swing.JComponent;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPopupMenu;
|
---|
23 | import javax.swing.SwingUtilities;
|
---|
24 | import javax.swing.event.TreeSelectionEvent;
|
---|
25 | import javax.swing.event.TreeSelectionListener;
|
---|
26 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
27 | import javax.swing.tree.TreeNode;
|
---|
28 | import javax.swing.tree.TreePath;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.actions.AbstractSelectAction;
|
---|
32 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
33 | import org.openstreetmap.josm.actions.ValidateAction;
|
---|
34 | import org.openstreetmap.josm.actions.relation.EditRelationAction;
|
---|
35 | import org.openstreetmap.josm.command.Command;
|
---|
36 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
37 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
38 | import org.openstreetmap.josm.data.osm.Node;
|
---|
39 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
40 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
41 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
42 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
43 | import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
|
---|
44 | import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
|
---|
45 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
46 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
47 | import org.openstreetmap.josm.data.validation.ValidatorVisitor;
|
---|
48 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
49 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
50 | import org.openstreetmap.josm.gui.PopupMenuHandler;
|
---|
51 | import org.openstreetmap.josm.gui.SideButton;
|
---|
52 | import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
|
---|
53 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
54 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
55 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
56 | import org.openstreetmap.josm.gui.layer.ValidatorLayer;
|
---|
57 | import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
|
---|
58 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
59 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
60 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
61 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
62 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
63 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
64 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
65 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
66 | import org.xml.sax.SAXException;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * A small tool dialog for displaying the current errors. The selection manager
|
---|
70 | * respects clicks into the selection list. Ctrl-click will remove entries from
|
---|
71 | * the list while single click will make the clicked entry the only selection.
|
---|
72 | *
|
---|
73 | * @author frsantos
|
---|
74 | */
|
---|
75 | public class ValidatorDialog extends ToggleDialog implements DataSelectionListener, ActiveLayerChangeListener {
|
---|
76 |
|
---|
77 | /** The display tree */
|
---|
78 | public ValidatorTreePanel tree;
|
---|
79 |
|
---|
80 | /** The validate action */
|
---|
81 | public static final ValidateAction validateAction = new ValidateAction();
|
---|
82 |
|
---|
83 | /** The fix button */
|
---|
84 | private final SideButton fixButton;
|
---|
85 | /** The ignore button */
|
---|
86 | private final SideButton ignoreButton;
|
---|
87 | /** The select button */
|
---|
88 | private final SideButton selectButton;
|
---|
89 | /** The lookup button */
|
---|
90 | private final SideButton lookupButton;
|
---|
91 |
|
---|
92 | private final JPopupMenu popupMenu = new JPopupMenu();
|
---|
93 | private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
|
---|
94 |
|
---|
95 | /** Last selected element */
|
---|
96 | private DefaultMutableTreeNode lastSelectedNode;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Constructor
|
---|
100 | */
|
---|
101 | public ValidatorDialog() {
|
---|
102 | super(tr("Validation Results"), "validator", tr("Open the validation window."),
|
---|
103 | Shortcut.registerShortcut("subwindow:validator", tr("Toggle: {0}", tr("Validation Results")),
|
---|
104 | KeyEvent.VK_V, Shortcut.ALT_SHIFT), 150, false, ValidatorPreference.class);
|
---|
105 |
|
---|
106 | popupMenuHandler.addAction(MainApplication.getMenu().autoScaleActions.get("problem"));
|
---|
107 | popupMenuHandler.addAction(new EditRelationAction());
|
---|
108 |
|
---|
109 | tree = new ValidatorTreePanel();
|
---|
110 | tree.addMouseListener(new MouseEventHandler());
|
---|
111 | addTreeSelectionListener(new SelectionWatch());
|
---|
112 | InputMapUtils.unassignCtrlShiftUpDown(tree, JComponent.WHEN_FOCUSED);
|
---|
113 |
|
---|
114 | List<SideButton> buttons = new LinkedList<>();
|
---|
115 |
|
---|
116 | selectButton = new SideButton(new AbstractSelectAction() {
|
---|
117 | @Override
|
---|
118 | public void actionPerformed(ActionEvent e) {
|
---|
119 | setSelectedItems();
|
---|
120 | }
|
---|
121 | });
|
---|
122 | InputMapUtils.addEnterAction(tree, selectButton.getAction());
|
---|
123 |
|
---|
124 | selectButton.setEnabled(false);
|
---|
125 | buttons.add(selectButton);
|
---|
126 |
|
---|
127 | lookupButton = new SideButton(new AbstractAction() {
|
---|
128 | {
|
---|
129 | putValue(NAME, tr("Lookup"));
|
---|
130 | putValue(SHORT_DESCRIPTION, tr("Looks up the selected primitives in the error list."));
|
---|
131 | new ImageProvider("dialogs", "search").getResource().attachImageIcon(this, true);
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void actionPerformed(ActionEvent e) {
|
---|
136 | final DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
|
---|
137 | if (ds == null) {
|
---|
138 | return;
|
---|
139 | }
|
---|
140 | tree.selectRelatedErrors(ds.getSelected());
|
---|
141 | }
|
---|
142 | });
|
---|
143 |
|
---|
144 | buttons.add(lookupButton);
|
---|
145 |
|
---|
146 | buttons.add(new SideButton(validateAction));
|
---|
147 |
|
---|
148 | fixButton = new SideButton(new AbstractAction() {
|
---|
149 | {
|
---|
150 | putValue(NAME, tr("Fix"));
|
---|
151 | putValue(SHORT_DESCRIPTION, tr("Fix the selected issue."));
|
---|
152 | new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
|
---|
153 | }
|
---|
154 | @Override
|
---|
155 | public void actionPerformed(ActionEvent e) {
|
---|
156 | fixErrors();
|
---|
157 | }
|
---|
158 | });
|
---|
159 | fixButton.setEnabled(false);
|
---|
160 | buttons.add(fixButton);
|
---|
161 |
|
---|
162 | if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
|
---|
163 | ignoreButton = new SideButton(new AbstractAction() {
|
---|
164 | {
|
---|
165 | putValue(NAME, tr("Ignore"));
|
---|
166 | putValue(SHORT_DESCRIPTION, tr("Ignore the selected issue next time."));
|
---|
167 | new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
|
---|
168 | }
|
---|
169 | @Override
|
---|
170 | public void actionPerformed(ActionEvent e) {
|
---|
171 | ignoreErrors();
|
---|
172 | }
|
---|
173 | });
|
---|
174 | ignoreButton.setEnabled(false);
|
---|
175 | buttons.add(ignoreButton);
|
---|
176 | } else {
|
---|
177 | ignoreButton = null;
|
---|
178 | }
|
---|
179 | createLayout(tree, true, buttons);
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Override
|
---|
183 | public void showNotify() {
|
---|
184 | SelectionEventManager.getInstance().addSelectionListener(this);
|
---|
185 | DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
|
---|
186 | if (ds != null) {
|
---|
187 | updateSelection(ds.getAllSelected());
|
---|
188 | }
|
---|
189 | MainApplication.getLayerManager().addAndFireActiveLayerChangeListener(this);
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public void hideNotify() {
|
---|
194 | MainApplication.getLayerManager().removeActiveLayerChangeListener(this);
|
---|
195 | SelectionEventManager.getInstance().removeSelectionListener(this);
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public void setVisible(boolean v) {
|
---|
200 | if (tree != null) {
|
---|
201 | tree.setVisible(v);
|
---|
202 | }
|
---|
203 | super.setVisible(v);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Fix selected errors
|
---|
208 | */
|
---|
209 | @SuppressWarnings("unchecked")
|
---|
210 | private void fixErrors() {
|
---|
211 | TreePath[] selectionPaths = tree.getSelectionPaths();
|
---|
212 | if (selectionPaths == null)
|
---|
213 | return;
|
---|
214 |
|
---|
215 | Set<DefaultMutableTreeNode> processedNodes = new HashSet<>();
|
---|
216 |
|
---|
217 | List<TestError> errorsToFix = new LinkedList<>();
|
---|
218 | for (TreePath path : selectionPaths) {
|
---|
219 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
|
---|
220 | if (node == null) {
|
---|
221 | continue;
|
---|
222 | }
|
---|
223 |
|
---|
224 | Enumeration<TreeNode> children = node.breadthFirstEnumeration();
|
---|
225 | while (children.hasMoreElements()) {
|
---|
226 | DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
|
---|
227 | if (processedNodes.contains(childNode)) {
|
---|
228 | continue;
|
---|
229 | }
|
---|
230 |
|
---|
231 | processedNodes.add(childNode);
|
---|
232 | Object nodeInfo = childNode.getUserObject();
|
---|
233 | if (nodeInfo instanceof TestError) {
|
---|
234 | errorsToFix.add((TestError) nodeInfo);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | // run fix task asynchronously
|
---|
240 | //
|
---|
241 | FixTask fixTask = new FixTask(errorsToFix);
|
---|
242 | MainApplication.worker.submit(fixTask);
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Set selected errors to ignore state
|
---|
247 | */
|
---|
248 | @SuppressWarnings("unchecked")
|
---|
249 | private void ignoreErrors() {
|
---|
250 | int asked = JOptionPane.DEFAULT_OPTION;
|
---|
251 | boolean changed = false;
|
---|
252 | TreePath[] selectionPaths = tree.getSelectionPaths();
|
---|
253 | if (selectionPaths == null)
|
---|
254 | return;
|
---|
255 |
|
---|
256 | Set<DefaultMutableTreeNode> processedNodes = new HashSet<>();
|
---|
257 | for (TreePath path : selectionPaths) {
|
---|
258 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
|
---|
259 | if (node == null) {
|
---|
260 | continue;
|
---|
261 | }
|
---|
262 |
|
---|
263 | Object mainNodeInfo = node.getUserObject();
|
---|
264 | if (!(mainNodeInfo instanceof TestError)) {
|
---|
265 | Set<String> state = new HashSet<>();
|
---|
266 | // ask if the whole set should be ignored
|
---|
267 | if (asked == JOptionPane.DEFAULT_OPTION) {
|
---|
268 | String[] a = new String[] {tr("Whole group"), tr("Single elements"), tr("Nothing")};
|
---|
269 | asked = JOptionPane.showOptionDialog(Main.parent, tr("Ignore whole group or individual elements?"),
|
---|
270 | tr("Ignoring elements"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null,
|
---|
271 | a, a[1]);
|
---|
272 | }
|
---|
273 | if (asked == JOptionPane.YES_NO_OPTION) {
|
---|
274 | Enumeration<TreeNode> children = node.breadthFirstEnumeration();
|
---|
275 | while (children.hasMoreElements()) {
|
---|
276 | DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
|
---|
277 | if (processedNodes.contains(childNode)) {
|
---|
278 | continue;
|
---|
279 | }
|
---|
280 |
|
---|
281 | processedNodes.add(childNode);
|
---|
282 | Object nodeInfo = childNode.getUserObject();
|
---|
283 | if (nodeInfo instanceof TestError) {
|
---|
284 | TestError err = (TestError) nodeInfo;
|
---|
285 | err.setIgnored(true);
|
---|
286 | changed = true;
|
---|
287 | state.add(node.getDepth() == 1 ? err.getIgnoreSubGroup() : err.getIgnoreGroup());
|
---|
288 | }
|
---|
289 | }
|
---|
290 | for (String s : state) {
|
---|
291 | OsmValidator.addIgnoredError(s);
|
---|
292 | }
|
---|
293 | continue;
|
---|
294 | } else if (asked == JOptionPane.CANCEL_OPTION || asked == JOptionPane.CLOSED_OPTION) {
|
---|
295 | continue;
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | Enumeration<TreeNode> children = node.breadthFirstEnumeration();
|
---|
300 | while (children.hasMoreElements()) {
|
---|
301 | DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
|
---|
302 | if (processedNodes.contains(childNode)) {
|
---|
303 | continue;
|
---|
304 | }
|
---|
305 |
|
---|
306 | processedNodes.add(childNode);
|
---|
307 | Object nodeInfo = childNode.getUserObject();
|
---|
308 | if (nodeInfo instanceof TestError) {
|
---|
309 | TestError error = (TestError) nodeInfo;
|
---|
310 | String state = error.getIgnoreState();
|
---|
311 | if (state != null) {
|
---|
312 | OsmValidator.addIgnoredError(state);
|
---|
313 | }
|
---|
314 | changed = true;
|
---|
315 | error.setIgnored(true);
|
---|
316 | }
|
---|
317 | }
|
---|
318 | }
|
---|
319 | if (changed) {
|
---|
320 | tree.resetErrors();
|
---|
321 | OsmValidator.saveIgnoredErrors();
|
---|
322 | invalidateValidatorLayers();
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Sets the selection of the map to the current selected items.
|
---|
328 | */
|
---|
329 | @SuppressWarnings("unchecked")
|
---|
330 | private void setSelectedItems() {
|
---|
331 | if (tree == null)
|
---|
332 | return;
|
---|
333 |
|
---|
334 | Collection<OsmPrimitive> sel = new HashSet<>(40);
|
---|
335 |
|
---|
336 | TreePath[] selectedPaths = tree.getSelectionPaths();
|
---|
337 | if (selectedPaths == null)
|
---|
338 | return;
|
---|
339 |
|
---|
340 | for (TreePath path : selectedPaths) {
|
---|
341 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
|
---|
342 | Enumeration<TreeNode> children = node.breadthFirstEnumeration();
|
---|
343 | while (children.hasMoreElements()) {
|
---|
344 | DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
|
---|
345 | Object nodeInfo = childNode.getUserObject();
|
---|
346 | if (nodeInfo instanceof TestError) {
|
---|
347 | TestError error = (TestError) nodeInfo;
|
---|
348 | error.getPrimitives().stream()
|
---|
349 | .filter(OsmPrimitive::isSelectable)
|
---|
350 | .forEach(sel::add);
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
|
---|
355 | if (ds != null) {
|
---|
356 | ds.setSelected(sel);
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Checks for fixes in selected element and, if needed, adds to the sel
|
---|
362 | * parameter all selected elements
|
---|
363 | *
|
---|
364 | * @param sel
|
---|
365 | * The collection where to add all selected elements
|
---|
366 | * @param addSelected
|
---|
367 | * if true, add all selected elements to collection
|
---|
368 | * @return whether the selected elements has any fix
|
---|
369 | */
|
---|
370 | @SuppressWarnings("unchecked")
|
---|
371 | private boolean setSelection(Collection<OsmPrimitive> sel, boolean addSelected) {
|
---|
372 | boolean hasFixes = false;
|
---|
373 |
|
---|
374 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
|
---|
375 | if (lastSelectedNode != null && !lastSelectedNode.equals(node)) {
|
---|
376 | Enumeration<TreeNode> children = lastSelectedNode.breadthFirstEnumeration();
|
---|
377 | while (children.hasMoreElements()) {
|
---|
378 | DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
|
---|
379 | Object nodeInfo = childNode.getUserObject();
|
---|
380 | if (nodeInfo instanceof TestError) {
|
---|
381 | TestError error = (TestError) nodeInfo;
|
---|
382 | error.setSelected(false);
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | lastSelectedNode = node;
|
---|
388 | if (node == null)
|
---|
389 | return hasFixes;
|
---|
390 |
|
---|
391 | Enumeration<TreeNode> children = node.breadthFirstEnumeration();
|
---|
392 | while (children.hasMoreElements()) {
|
---|
393 | DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
|
---|
394 | Object nodeInfo = childNode.getUserObject();
|
---|
395 | if (nodeInfo instanceof TestError) {
|
---|
396 | TestError error = (TestError) nodeInfo;
|
---|
397 | error.setSelected(true);
|
---|
398 |
|
---|
399 | hasFixes = hasFixes || error.isFixable();
|
---|
400 | if (addSelected) {
|
---|
401 | error.getPrimitives().stream()
|
---|
402 | .filter(OsmPrimitive::isSelectable)
|
---|
403 | .forEach(sel::add);
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 | selectButton.setEnabled(true);
|
---|
408 | if (ignoreButton != null) {
|
---|
409 | ignoreButton.setEnabled(true);
|
---|
410 | }
|
---|
411 |
|
---|
412 | return hasFixes;
|
---|
413 | }
|
---|
414 |
|
---|
415 | @Override
|
---|
416 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
417 | OsmDataLayer editLayer = e.getSource().getEditLayer();
|
---|
418 | if (editLayer == null) {
|
---|
419 | tree.setErrorList(new ArrayList<TestError>());
|
---|
420 | } else {
|
---|
421 | tree.setErrorList(editLayer.validationErrors);
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Add a tree selection listener to the validator tree.
|
---|
427 | * @param listener the TreeSelectionListener
|
---|
428 | * @since 5958
|
---|
429 | */
|
---|
430 | public void addTreeSelectionListener(TreeSelectionListener listener) {
|
---|
431 | tree.addTreeSelectionListener(listener);
|
---|
432 | }
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * Remove the given tree selection listener from the validator tree.
|
---|
436 | * @param listener the TreeSelectionListener
|
---|
437 | * @since 5958
|
---|
438 | */
|
---|
439 | public void removeTreeSelectionListener(TreeSelectionListener listener) {
|
---|
440 | tree.removeTreeSelectionListener(listener);
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Replies the popup menu handler.
|
---|
445 | * @return The popup menu handler
|
---|
446 | * @since 5958
|
---|
447 | */
|
---|
448 | public PopupMenuHandler getPopupMenuHandler() {
|
---|
449 | return popupMenuHandler;
|
---|
450 | }
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * Replies the currently selected error, or {@code null}.
|
---|
454 | * @return The selected error, if any.
|
---|
455 | * @since 5958
|
---|
456 | */
|
---|
457 | public TestError getSelectedError() {
|
---|
458 | Object comp = tree.getLastSelectedPathComponent();
|
---|
459 | if (comp instanceof DefaultMutableTreeNode) {
|
---|
460 | Object object = ((DefaultMutableTreeNode) comp).getUserObject();
|
---|
461 | if (object instanceof TestError) {
|
---|
462 | return (TestError) object;
|
---|
463 | }
|
---|
464 | }
|
---|
465 | return null;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Watches for double clicks and launches the popup menu.
|
---|
470 | */
|
---|
471 | class MouseEventHandler extends PopupMenuLauncher {
|
---|
472 |
|
---|
473 | MouseEventHandler() {
|
---|
474 | super(popupMenu);
|
---|
475 | }
|
---|
476 |
|
---|
477 | @Override
|
---|
478 | public void mouseClicked(MouseEvent e) {
|
---|
479 | TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
|
---|
480 | if (selPath == null) {
|
---|
481 | tree.clearSelection();
|
---|
482 | }
|
---|
483 |
|
---|
484 | fixButton.setEnabled(false);
|
---|
485 | if (ignoreButton != null) {
|
---|
486 | ignoreButton.setEnabled(false);
|
---|
487 | }
|
---|
488 | selectButton.setEnabled(false);
|
---|
489 |
|
---|
490 | boolean isDblClick = isDoubleClick(e);
|
---|
491 |
|
---|
492 | Collection<OsmPrimitive> sel = isDblClick ? new HashSet<>(40) : null;
|
---|
493 |
|
---|
494 | boolean hasFixes = setSelection(sel, isDblClick);
|
---|
495 | fixButton.setEnabled(hasFixes);
|
---|
496 |
|
---|
497 | if (isDblClick) {
|
---|
498 | DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
|
---|
499 | if (ds != null) {
|
---|
500 | ds.setSelected(sel);
|
---|
501 | }
|
---|
502 | if (Config.getPref().getBoolean("validator.autozoom", false)) {
|
---|
503 | AutoScaleAction.zoomTo(sel);
|
---|
504 | }
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | @Override
|
---|
509 | public void launch(MouseEvent e) {
|
---|
510 | TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
|
---|
511 | if (selPath == null)
|
---|
512 | return;
|
---|
513 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getPathComponent(selPath.getPathCount() - 1);
|
---|
514 | if (!(node.getUserObject() instanceof TestError))
|
---|
515 | return;
|
---|
516 | super.launch(e);
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * Watches for tree selection.
|
---|
522 | */
|
---|
523 | public class SelectionWatch implements TreeSelectionListener {
|
---|
524 | @Override
|
---|
525 | public void valueChanged(TreeSelectionEvent e) {
|
---|
526 | fixButton.setEnabled(false);
|
---|
527 | if (ignoreButton != null) {
|
---|
528 | ignoreButton.setEnabled(false);
|
---|
529 | }
|
---|
530 | selectButton.setEnabled(false);
|
---|
531 |
|
---|
532 | Collection<OsmPrimitive> sel = new HashSet<>();
|
---|
533 | boolean hasFixes = setSelection(sel, true);
|
---|
534 | fixButton.setEnabled(hasFixes);
|
---|
535 | popupMenuHandler.setPrimitives(sel);
|
---|
536 | invalidateValidatorLayers();
|
---|
537 | }
|
---|
538 | }
|
---|
539 |
|
---|
540 | /**
|
---|
541 | * A visitor that is used to compute the bounds of an error.
|
---|
542 | */
|
---|
543 | public static class ValidatorBoundingXYVisitor extends BoundingXYVisitor implements ValidatorVisitor {
|
---|
544 | @Override
|
---|
545 | public void visit(OsmPrimitive p) {
|
---|
546 | if (p.isUsable()) {
|
---|
547 | p.accept((PrimitiveVisitor) this);
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 | @Override
|
---|
552 | public void visit(WaySegment ws) {
|
---|
553 | if (ws.lowerIndex < 0 || ws.lowerIndex + 1 >= ws.way.getNodesCount())
|
---|
554 | return;
|
---|
555 | visit(ws.way.getNodes().get(ws.lowerIndex));
|
---|
556 | visit(ws.way.getNodes().get(ws.lowerIndex + 1));
|
---|
557 | }
|
---|
558 |
|
---|
559 | @Override
|
---|
560 | public void visit(List<Node> nodes) {
|
---|
561 | for (Node n: nodes) {
|
---|
562 | visit(n);
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | @Override
|
---|
567 | public void visit(TestError error) {
|
---|
568 | if (error != null) {
|
---|
569 | error.visitHighlighted(this);
|
---|
570 | }
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Called when the selection was changed to update the list of displayed errors
|
---|
576 | * @param newSelection The new selection
|
---|
577 | */
|
---|
578 | public void updateSelection(Collection<? extends OsmPrimitive> newSelection) {
|
---|
579 | if (!Config.getPref().getBoolean(ValidatorPrefHelper.PREF_FILTER_BY_SELECTION, false))
|
---|
580 | return;
|
---|
581 | if (newSelection.isEmpty()) {
|
---|
582 | tree.setFilter(null);
|
---|
583 | }
|
---|
584 | tree.setFilter(new HashSet<>(newSelection));
|
---|
585 | }
|
---|
586 |
|
---|
587 | @Override
|
---|
588 | public void selectionChanged(SelectionChangeEvent event) {
|
---|
589 | updateSelection(event.getSelection());
|
---|
590 | }
|
---|
591 |
|
---|
592 | /**
|
---|
593 | * Task for fixing a collection of {@link TestError}s. Can be run asynchronously.
|
---|
594 | *
|
---|
595 | *
|
---|
596 | */
|
---|
597 | class FixTask extends PleaseWaitRunnable {
|
---|
598 | private final Collection<TestError> testErrors;
|
---|
599 | private boolean canceled;
|
---|
600 |
|
---|
601 | FixTask(Collection<TestError> testErrors) {
|
---|
602 | super(tr("Fixing errors ..."), false /* don't ignore exceptions */);
|
---|
603 | this.testErrors = testErrors == null ? new ArrayList<>() : testErrors;
|
---|
604 | }
|
---|
605 |
|
---|
606 | @Override
|
---|
607 | protected void cancel() {
|
---|
608 | this.canceled = true;
|
---|
609 | }
|
---|
610 |
|
---|
611 | @Override
|
---|
612 | protected void finish() {
|
---|
613 | // do nothing
|
---|
614 | }
|
---|
615 |
|
---|
616 | protected void fixError(TestError error) throws InterruptedException, InvocationTargetException {
|
---|
617 | if (error.isFixable()) {
|
---|
618 | final Command fixCommand = error.getFix();
|
---|
619 | if (fixCommand != null) {
|
---|
620 | SwingUtilities.invokeAndWait(() -> MainApplication.undoRedo.addNoRedraw(fixCommand));
|
---|
621 | }
|
---|
622 | // It is wanted to ignore an error if it said fixable, even if fixCommand was null
|
---|
623 | // This is to fix #5764 and #5773:
|
---|
624 | // a delete command, for example, may be null if all concerned primitives have already been deleted
|
---|
625 | error.setIgnored(true);
|
---|
626 | }
|
---|
627 | }
|
---|
628 |
|
---|
629 | @Override
|
---|
630 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
631 | ProgressMonitor monitor = getProgressMonitor();
|
---|
632 | try {
|
---|
633 | monitor.setTicksCount(testErrors.size());
|
---|
634 | final DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
|
---|
635 | int i = 0;
|
---|
636 | SwingUtilities.invokeAndWait(ds::beginUpdate);
|
---|
637 | try {
|
---|
638 | for (TestError error: testErrors) {
|
---|
639 | i++;
|
---|
640 | monitor.subTask(tr("Fixing ({0}/{1}): ''{2}''", i, testErrors.size(), error.getMessage()));
|
---|
641 | if (this.canceled)
|
---|
642 | return;
|
---|
643 | fixError(error);
|
---|
644 | monitor.worked(1);
|
---|
645 | }
|
---|
646 | } finally {
|
---|
647 | SwingUtilities.invokeAndWait(ds::endUpdate);
|
---|
648 | }
|
---|
649 | monitor.subTask(tr("Updating map ..."));
|
---|
650 | SwingUtilities.invokeAndWait(() -> {
|
---|
651 | MainApplication.undoRedo.afterAdd(null);
|
---|
652 | invalidateValidatorLayers();
|
---|
653 | tree.resetErrors();
|
---|
654 | });
|
---|
655 | } catch (InterruptedException | InvocationTargetException e) {
|
---|
656 | // FIXME: signature of realRun should have a generic checked exception we could throw here
|
---|
657 | throw new JosmRuntimeException(e);
|
---|
658 | } finally {
|
---|
659 | monitor.finishTask();
|
---|
660 | }
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | private static void invalidateValidatorLayers() {
|
---|
665 | MainApplication.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
|
---|
666 | }
|
---|
667 | }
|
---|