Changeset 17585 in osm for applications/editors/josm


Ignore:
Timestamp:
2009-09-12T06:29:25+02:00 (15 years ago)
Author:
guggis
Message:

Fixing errors is now an asynchronous, cancelable task.
Batch processing for fixing duplicate nodes improved.

Location:
applications/editors/josm/plugins/validator
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/validator/build.xml

    r17540 r17585  
    2626                <attribute name="Plugin-Description" value="An OSM data validator. It checks for problems in data, and provides fixes for the common ones. Spellcheck integrated for tag names."/>
    2727                <attribute name="Plugin-Link" value="http://wiki.openstreetmap.org/index.php/JOSM/Plugins/Validator"/>
    28                 <attribute name="Plugin-Mainversion" value="2082"/>
     28                <attribute name="Plugin-Mainversion" value="2095"/>
    2929                <attribute name="Plugin-Version" value="${version.entry.commit.revision}"/>
    3030            </manifest>
  • applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/ValidatorDialog.java

    r17479 r17585  
    1111import java.awt.event.MouseAdapter;
    1212import java.awt.event.MouseEvent;
     13import java.io.IOException;
     14import java.lang.reflect.InvocationTargetException;
     15import java.util.ArrayList;
    1316import java.util.Collection;
    1417import java.util.Enumeration;
    1518import java.util.HashSet;
     19import java.util.LinkedList;
    1620import java.util.Set;
    1721
     
    2125import javax.swing.JPopupMenu;
    2226import javax.swing.JScrollPane;
     27import javax.swing.SwingUtilities;
    2328import javax.swing.event.TreeSelectionEvent;
    2429import javax.swing.event.TreeSelectionListener;
     
    3338import org.openstreetmap.josm.data.osm.WaySegment;
    3439import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     40import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    3541import org.openstreetmap.josm.gui.SideButton;
    3642import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
     43import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     44import org.openstreetmap.josm.io.OsmTransferException;
     45import org.openstreetmap.josm.plugins.validator.tests.DuplicateNode;
    3746import org.openstreetmap.josm.tools.Shortcut;
     47import org.xml.sax.SAXException;
    3848
    3949/**
     
    133143
    134144        Set<DefaultMutableTreeNode> processedNodes = new HashSet<DefaultMutableTreeNode>();
     145               
     146        DuplicateNode.clearBackreferences();
     147        LinkedList<TestError> errorsToFix = new LinkedList<TestError>();
    135148        for (TreePath path : selectionPaths) {
    136149            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
     
    147160                Object nodeInfo = childNode.getUserObject();
    148161                if (nodeInfo instanceof TestError) {
    149                     TestError error = (TestError) nodeInfo;
    150                     Command fixCommand = error.getFix();
    151                     if (fixCommand != null) {
    152                         Main.main.undoRedo.add(fixCommand);
    153                         error.setIgnored(true);
    154                     }
     162                        errorsToFix.add((TestError)nodeInfo);
    155163                }
    156164            }
    157165        }
    158 
    159         Main.map.repaint();
    160         tree.resetErrors();
    161         DataSet.fireSelectionChanged(Main.main.getCurrentDataSet().getSelected());
     166       
     167        // run fix task asynchronously
     168        //
     169        FixTask fixTask = new FixTask(errorsToFix);
     170        Main.worker.submit(fixTask);
    162171    }
    163172
     
    436445        tree.setFilter(filter);
    437446    }
     447   
     448    /**
     449     * Task for fixing a collection of {@see TestError}s. Can be run asynchronously.
     450     *
     451     *
     452     */
     453    class FixTask extends PleaseWaitRunnable {
     454        private Collection<TestError> testErrors;
     455        private boolean canceled;
     456        private LinkedList<Command> fixCommands;
     457   
     458       
     459        public FixTask(Collection<TestError> testErrors) {
     460                super(tr("Fixing errors ..."), false /* don't ignore exceptions */);
     461                this.testErrors = testErrors == null ? new ArrayList<TestError> (): testErrors;
     462                fixCommands = new LinkedList<Command>();
     463        }
     464
     465                @Override
     466                protected void cancel() {
     467                        this.canceled = true;                   
     468                }
     469
     470                @Override
     471                protected void finish() {
     472                        // do nothing
     473                }
     474               
     475                @Override
     476                protected void realRun() throws SAXException, IOException,
     477                                OsmTransferException {
     478                        ProgressMonitor monitor = getProgressMonitor();
     479                        try {                           
     480                                monitor.setTicksCount(testErrors.size());
     481                                int i=0;
     482                                for (TestError error: testErrors) {
     483                                        i++;
     484                                        monitor.subTask(tr("Fixing ({0}/{1}): ''{2}''", i, testErrors.size(),error.getMessage()));
     485                                        if (this.canceled)
     486                                                return;
     487                                        final Command fixCommand = error.getFix();
     488                    if (fixCommand != null) {
     489                        fixCommands.add(fixCommand);
     490                                        SwingUtilities.invokeAndWait(
     491                                                        new Runnable() {
     492                                                                public void run() {
     493                                                                        Main.main.undoRedo.add(fixCommand);
     494                                                                }
     495                                                        }
     496                                        );
     497                        error.setIgnored(true);
     498                    }
     499                    monitor.worked(1);
     500                                }               
     501                                monitor.subTask(tr("Updating map ..."));
     502                                SwingUtilities.invokeAndWait(new Runnable() {
     503                                        public void run() {
     504                                                Main.map.repaint();
     505                                                tree.resetErrors();
     506                                                DataSet.fireSelectionChanged(Main.main.getCurrentDataSet().getSelected());
     507                                        }
     508                                });
     509                        } catch(InterruptedException e) {
     510                                // FIXME: signature of realRun should have a generic checked exception we
     511                                // could throw here
     512                                throw new RuntimeException(e);
     513                        } catch(InvocationTargetException e) {
     514                                throw new RuntimeException(e);
     515                        } finally {
     516                                monitor.finishTask();
     517                        }                       
     518                }
     519    }
    438520}
  • applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/DuplicateNode.java

    r17481 r17585  
    1616import org.openstreetmap.josm.command.Command;
    1717import org.openstreetmap.josm.data.coor.LatLon;
     18import org.openstreetmap.josm.data.osm.BackreferencedDataSet;
    1819import org.openstreetmap.josm.data.osm.Node;
    1920import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    2829 * @author frsantos
    2930 */
    30 public class DuplicateNode extends Test
    31 {
     31public class DuplicateNode extends Test{
     32       
     33        private static BackreferencedDataSet backreferences;
     34       
     35        public static BackreferencedDataSet getBackreferenceDataSet() {
     36                if (backreferences == null) {
     37                        backreferences = new BackreferencedDataSet(Main.main.getEditLayer().data);
     38                        backreferences.build();
     39                }
     40                return backreferences;
     41        }
     42       
     43        public static void clearBackreferences() {
     44                backreferences = null;
     45        }
     46       
    3247    protected static int DUPLICATE_NODE = 1;
    3348
     
    92107    public Command fixError(TestError testError)
    93108    {
    94         Collection<? extends OsmPrimitive> sel = testError.getPrimitives();
    95         LinkedList<Node> nodes = new LinkedList<Node>();
     109        Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>(testError.getPrimitives());
     110        LinkedList<Node> nodes = new LinkedList<Node>(OsmPrimitive.getFilteredList(sel, Node.class));
     111        MergeNodesAction mergeAction  = new MergeNodesAction();
     112        Node target = mergeAction.selectTargetNode(nodes);
     113        if(checkAndConfirmOutlyingDeletes(nodes))
     114            return mergeAction.mergeNodes(Main.main.getEditLayer(),getBackreferenceDataSet(), nodes, target);
    96115
    97         for (OsmPrimitive osm : sel)
    98             if (osm instanceof Node)
    99                 nodes.add((Node)osm);
    100 
    101         if( nodes.size() < 2 )
    102             return null;
    103 
    104         Node target = null;
    105         // select the target node in the same way as in the core action MergeNodesAction, rev.1084
    106         for (Node n: nodes) {
    107             if (n.getId() > 0) {
    108                 target = n;
    109                 break;
    110             }
    111         }
    112         if (target == null)
    113             target = nodes.iterator().next();
    114 
    115         if(checkAndConfirmOutlyingDeletes(nodes))
    116             new MergeNodesAction().mergeNodes(nodes, target);
    117 
    118         return null; // undoRedo handling done in mergeNodes
     116        return null;// undoRedo handling done in mergeNodes
    119117    }
    120118
Note: See TracChangeset for help on using the changeset viewer.