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