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