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