Logo Search packages:      
Sourcecode: josm-plugins version File versions  Download package

ValidatorDialog.java

package org.openstreetmap.josm.plugins.validator;

import static org.openstreetmap.josm.tools.I18n.tr;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.*;
import java.util.Map.Entry;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.command.Command;
import org.openstreetmap.josm.command.SequenceCommand;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
import org.openstreetmap.josm.plugins.validator.util.Bag;
import org.openstreetmap.josm.plugins.validator.util.Util;

/**
 * A small tool dialog for displaying the current errors. The selection manager
 * respects clicks into the selection list. Ctrl-click will remove entries from
 * the list while single click will make the clicked entry the only selection.
 *
 * @author frsantos
 */
00032 public class ValidatorDialog extends ToggleDialog implements ActionListener
{
      private OSMValidatorPlugin plugin;

    /** Serializable ID */
00037     private static final long serialVersionUID = 2952292777351992696L;

      /**
     * The display tree.
     */
00042     protected ErrorTreePanel tree;

    /** 
     * The fix button
     */
00047     private JButton fixButton;
    
    /** 
     * The select button
     */
00052     private JButton selectButton;
    
    /** Last selected element */
00055     private DefaultMutableTreeNode lastSelectedNode = null;

    /**
     * Constructor
     */
00060     public ValidatorDialog(OSMValidatorPlugin plugin) {
        super(tr("Validation errors"), "validator", tr("Open the validation window."), KeyEvent.VK_V, 150);

            this.plugin = plugin;
        
        tree = new ErrorTreePanel();
            tree.addMouseListener(new ClickWatch());
            tree.addTreeSelectionListener(new SelectionWatch());

            add(new JScrollPane(tree), BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new GridLayout(1,2));

        selectButton = Util.createButton("Select", "mapmode/selection/select", "Set the selected elements on the map to the selected items in the list above.", this);
        selectButton.setEnabled(false);
        buttonPanel.add(selectButton); 
        //add(buttonPanel, BorderLayout.SOUTH);
        buttonPanel.add(Util.createButton("Validate", "dialogs/refresh", "Validate the data.", this)); 
        // add(buttonPanel, BorderLayout.SOUTH);
        fixButton = Util.createButton("Fix", "dialogs/fix", "Fix the selected errors.", this);
        fixButton.setEnabled(false);
        buttonPanel.add(fixButton); 
        add(buttonPanel, BorderLayout.SOUTH);
    }

    @Override 
    public void setVisible(boolean v) 
    {
        if( tree != null )
            tree.setVisible(v);
            if( action != null && action.button != null )
                  action.button.setSelected(v);
            super.setVisible(v);
            Main.map.repaint();
      }
    
    
      /**
       * Fix selected errors
       * @param e 
       */
      @SuppressWarnings("unchecked")
00102       private void fixErrors(ActionEvent e) 
      {
        TreePath[] selectionPaths = tree.getSelectionPaths();
        if( selectionPaths == null )
            return;
        
        Set<DefaultMutableTreeNode> processedNodes = new HashSet<DefaultMutableTreeNode>();
        for( TreePath path : selectionPaths )
        {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
            if( node == null )
                  continue;
            
            Enumeration<DefaultMutableTreeNode> children = node.breadthFirstEnumeration();
            while( children.hasMoreElements() )
            {
                  DefaultMutableTreeNode childNode = children.nextElement();
                if( processedNodes.contains(childNode) )
                    continue;
                
                processedNodes.add(childNode);
                  Object nodeInfo = childNode.getUserObject();
                  if( nodeInfo instanceof TestError)
                  {
                        TestError error = (TestError)nodeInfo;
                        Command fixCommand = error.getFix();
                        if( fixCommand != null )
                        {
                              Main.main.undoRedo.add(fixCommand);
                        }
                  }
            }
        }
            
            Main.map.repaint();
            DataSet.fireSelectionChanged(Main.ds.getSelected());
                   
      plugin.validateAction.doValidate(e, false);
      }     
      
    /**
     * Sets the selection of the map to the current selected items.
     */
    @SuppressWarnings("unchecked")
00146     private void setSelectedItems() 
    {
        if( tree == null )
            return;
        
        Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>(40);

        TreePath[] selectedPaths = tree.getSelectionPaths();
        if( selectedPaths == null)
            return;
        
        for( TreePath path : selectedPaths)
        {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
            Enumeration<DefaultMutableTreeNode> children = node.breadthFirstEnumeration();
            while( children.hasMoreElements() )
            {
                  DefaultMutableTreeNode childNode = children.nextElement();
                  Object nodeInfo = childNode.getUserObject();
                  if( nodeInfo instanceof TestError)
                  {
                        TestError error = (TestError)nodeInfo;
                        sel.addAll( error.getPrimitives() );
                  }
            }
        }
        
        Main.ds.setSelected(sel);
    }

      public void actionPerformed(ActionEvent e) 
      {
            String actionCommand = e.getActionCommand();
            if( actionCommand.equals("Select"))
                  setSelectedItems();
            else if( actionCommand.equals("Validate"))
            plugin.validateAction.actionPerformed(e);
            else if( actionCommand.equals("Fix"))
            fixErrors(e); 
      }

    /**
     * Checks for fixes in selected element and, if needed, adds to the sel parameter all selected elements
     * @param sel The collection where to add all selected elements
     * @param addSelected if true, add all selected elements to collection
     * @return whether the selected elements has any fix
     */
    @SuppressWarnings("unchecked")
00194       private boolean setSelection(Collection<OsmPrimitive> sel, boolean addSelected)
      {
            boolean hasFixes = false;

        DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
        if( lastSelectedNode != null && !lastSelectedNode.equals(node) )
        {
            Enumeration<DefaultMutableTreeNode> children = lastSelectedNode.breadthFirstEnumeration();
            while( children.hasMoreElements() )
            {
                DefaultMutableTreeNode childNode = children.nextElement();
                Object nodeInfo = childNode.getUserObject();
                if( nodeInfo instanceof TestError)
                {
                    TestError error = (TestError)nodeInfo;
                    error.setSelected(false);
                }
            }  
        }
        
        lastSelectedNode = node;
      if( node == null ) 
            return hasFixes;

            Enumeration<DefaultMutableTreeNode> children = node.breadthFirstEnumeration();
            while( children.hasMoreElements() )
            {
            DefaultMutableTreeNode childNode = children.nextElement();
            Object nodeInfo = childNode.getUserObject();
            if( nodeInfo instanceof TestError)
            {
                  TestError error = (TestError)nodeInfo;
                error.setSelected(true);
                
                  hasFixes = hasFixes || error.isFixable();
                  if( addSelected )
                  {
                        sel.addAll( error.getPrimitives() );
                  }
            }
            }
        selectButton.setEnabled(true);
            
            return hasFixes;
      }
    
      /**
       * Watches for clicks.
       */
00243       public class ClickWatch extends MouseAdapter 
      {
        @Override 
            public void mouseClicked(MouseEvent e) 
            {
            fixButton.setEnabled(false);
            selectButton.setEnabled(false);
            
                  boolean isDblClick = e.getClickCount() > 1;
            Collection<OsmPrimitive> sel = isDblClick ? new HashSet<OsmPrimitive>(40) : null;
                  
                  boolean hasFixes = setSelection(sel, isDblClick);
              fixButton.setEnabled(hasFixes);
              
              if( isDblClick)
                  {
                  Main.ds.setSelected(sel);
                  }
            }
      }
      
      /**
       * Watches for tree selection.
       */
00267       public class SelectionWatch implements TreeSelectionListener 
      {
        @SuppressWarnings("unchecked")
            public void valueChanged(TreeSelectionEvent e) 
            {
              fixButton.setEnabled(false);
            selectButton.setEnabled(false);
            
                  if(e.getSource() instanceof JScrollPane)
            {
                System.out.println(e.getSource());
                return;
            }
            
                  boolean hasFixes = setSelection(null, false);
              fixButton.setEnabled(hasFixes);
            Main.map.repaint();            
            }
      }
}

Generated by  Doxygen 1.6.0   Back to index