Changeset 5845 in josm for trunk/src/org


Ignore:
Timestamp:
2013-04-13T14:19:56+02:00 (11 years ago)
Author:
akks
Message:

fix #8599: Remote control command add_way now creates closed ways correctly
Show old values and tooltips in add_tags dialog
All nodes added by add_node and add_way are merged with existing ones

Location:
trunk/src/org/openstreetmap/josm/io/remotecontrol
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/AddTagsDialog.java

    r5844 r5845  
    66import java.awt.Color;
    77import java.awt.Component;
    8 import java.awt.Dimension;
    98import java.awt.Font;
    109import java.awt.GridBagLayout;
    1110import java.awt.event.ActionEvent;
    1211import java.awt.event.KeyEvent;
     12import java.awt.event.MouseEvent;
    1313import java.util.Collection;
     14import java.util.HashMap;
    1415import javax.swing.AbstractAction;
    15 import javax.swing.DefaultCellEditor;
    1616
    1717import javax.swing.JPanel;
    1818import javax.swing.JTable;
    19 import javax.swing.JTextField;
    2019import javax.swing.KeyStroke;
     20import javax.swing.event.CellEditorListener;
     21import javax.swing.event.ChangeEvent;
    2122import javax.swing.table.DefaultTableModel;
    2223import javax.swing.table.TableCellEditor;
     
    3031import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3132import org.openstreetmap.josm.gui.ExtendedDialog;
    32 import org.openstreetmap.josm.gui.util.TableCellEditorSupport;
    3333import org.openstreetmap.josm.gui.util.TableHelper;
    3434import org.openstreetmap.josm.tools.GBC;
     
    5050    int[] count;
    5151
     52    /**
     53     * Class for displaying "delete from ... objects" in the table
     54     */
    5255    static class DeleteTagMarker {
    5356        int num;
     
    6063    }
    6164   
     65    /**
     66     * Class for displaying list of existing tag values in the table
     67     */
     68    static class ExistingValues {
     69        String tag;
     70        HashMap<String, Integer> valueCount;
     71        public ExistingValues(String tag) {
     72            this.tag=tag; valueCount=new HashMap<String, Integer>();
     73        }
     74       
     75        int addValue(String val) {
     76            Integer c = valueCount.get(val);
     77            int r = c==null? 1 : (c.intValue()+1);
     78            valueCount.put(val, r);
     79            return r;
     80        }
     81
     82        @Override
     83        public String toString() {
     84            StringBuilder sb=new StringBuilder();
     85            for (String k: valueCount.keySet()) {
     86                if (sb.length()>0) sb.append(", ");
     87                sb.append(k);
     88            }
     89            return sb.toString();
     90        }
     91
     92        private String getToolTip() {
     93            StringBuilder sb=new StringBuilder();
     94            sb.append("<html>");
     95            sb.append(tr("Old values of"));
     96            sb.append(" <b>");
     97            sb.append(tag);
     98            sb.append("</b><br/>");
     99            for (String k: valueCount.keySet()) {
     100                sb.append("<b>");
     101                sb.append(valueCount.get(k));
     102                sb.append(" x </b>");
     103                sb.append(k);
     104                sb.append("<br/>");
     105            }
     106            sb.append("</html>");
     107            return sb.toString();
     108           
     109        }
     110    }
    62111           
    63112    public AddTagsDialog(String[][] tags) {
     
    70119
    71120
    72         DefaultTableModel tm = new DefaultTableModel(new String[] {tr("Assume"), tr("Key"), tr("Value")}, tags.length) {
    73             final Class<?> types[] = {Boolean.class, String.class, Object.class};
     121        final DefaultTableModel tm = new DefaultTableModel(new String[] {tr("Assume"), tr("Key"), tr("Value"), tr("Existing values")}, tags.length) {
     122            final Class<?> types[] = {Boolean.class, String.class, Object.class, ExistingValues.class};
    74123            @Override
    75124            public Class getColumnClass(int c) {
     
    85134            count[i] = 0;
    86135            String key = tags[i][0];
    87             String value = tags[i][1];
     136            String value = tags[i][1], oldValue;
    88137            Boolean b = Boolean.TRUE;
     138            ExistingValues old = new ExistingValues(key);
    89139            for (OsmPrimitive osm : sel) {
    90                 if (osm.keySet().contains(key) && !osm.get(key).equals(value)) {
    91                     b = Boolean.FALSE;
    92                     count[i]++;
    93                     break;
     140                oldValue  = osm.get(key);
     141                if (oldValue!=null) {
     142                    old.addValue(oldValue);
     143                    if (!oldValue.equals(value)) {
     144                        b = Boolean.FALSE;
     145                        count[i]++;
     146                    }
    94147                }
    95148            }
     
    97150            tm.setValueAt(tags[i][0], i, 1);
    98151            tm.setValueAt(tags[i][1].isEmpty() ? new DeleteTagMarker(count[i]) : tags[i][1], i, 2);
    99         }
    100 
     152            tm.setValueAt(old , i, 3);
     153        }
     154       
    101155        propertyTable = new JTable(tm) {
    102156
     
    120174            public TableCellEditor getCellEditor(int row, int column) {
    121175                Object value = getValueAt(row,column);
    122                 System.out.println(value);
    123176                if (value instanceof DeleteTagMarker) return null;
     177                if (value instanceof ExistingValues) return null;
    124178                return getDefaultEditor(value.getClass());
    125179            }
     180
     181            @Override
     182            public String getToolTipText(MouseEvent event) {
     183                int r = rowAtPoint(event.getPoint());
     184                int c = columnAtPoint(event.getPoint());
     185                Object o = getValueAt(r, c);
     186                if (c==1 || c==2) return o.toString();
     187                if (c==3) return ((ExistingValues)o).getToolTip();
     188                return tr("Enable the checkbox to accept the value");
     189            }
     190           
    126191        };
    127192       
     
    129194        // a checkbox has a size of 15 px
    130195        propertyTable.getColumnModel().getColumn(0).setMaxWidth(15);
    131         TableHelper.adjustColumnWidth(propertyTable, 1, 200);
    132         TableHelper.adjustColumnWidth(propertyTable, 2, 700);
     196        TableHelper.adjustColumnWidth(propertyTable, 1, 150);
     197        TableHelper.adjustColumnWidth(propertyTable, 2, 400);
     198        TableHelper.adjustColumnWidth(propertyTable, 3, 300);
    133199        // get edit results if the table looses the focus, for example if a user clicks "add tags"
    134200        propertyTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
     
    139205            }
    140206        });
    141 
     207       
    142208        // set the content of this AddTagsDialog consisting of the tableHeader and the table itself.
    143209        JPanel tablePanel = new JPanel();
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java

    r5844 r5845  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Point;
    67import java.util.HashMap;
    78import org.openstreetmap.josm.Main;
     
    1011import org.openstreetmap.josm.data.coor.LatLon;
    1112import org.openstreetmap.josm.data.osm.Node;
     13import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1214import org.openstreetmap.josm.gui.util.GuiHelper;
    1315import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
     
    6466        // Create a new node
    6567        LatLon ll = new LatLon(lat, lon);
    66         Node nnew = new Node(ll);
    6768
    68         // Now execute the commands to add this node.
    69         Main.main.undoRedo.add(new AddCommand(nnew));
    70         Main.main.getCurrentDataSet().setSelected(nnew);
     69        Node nd = null;
     70       
     71        if (Main.map != null &&  Main.map.mapView != null) {
     72            Point p = Main.map.mapView.getPoint(ll);
     73            nd = Main.map.mapView.getNearestNode(p, OsmPrimitive.isUsablePredicate);
     74            if (nd!=null && nd.getCoor().greatCircleDistance(ll) > Main.pref.getDouble("remotecontrol.tolerance", 0.1)) {
     75                nd = null; // node is too far
     76            }
     77        }
     78
     79        if (nd==null) {
     80            nd = new Node(ll);
     81            // Now execute the commands to add this node.
     82            Main.main.undoRedo.add(new AddCommand(nd));
     83        }
     84       
     85        Main.main.getCurrentDataSet().setSelected(nd);
    7186        if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
    7287            AutoScaleAction.autoScale("selection");
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java

    r5844 r5845  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
     5import java.awt.Point;
    56import java.util.ArrayList;
    67import java.util.Arrays;
     8import java.util.HashMap;
    79import java.util.LinkedList;
    810import java.util.List;
     
    1517import org.openstreetmap.josm.data.coor.LatLon;
    1618import org.openstreetmap.josm.data.osm.Node;
     19import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1720import org.openstreetmap.josm.data.osm.Way;
    1821import org.openstreetmap.josm.gui.util.GuiHelper;
     
    3134   
    3235    private final List<LatLon> allCoordinates = new ArrayList<LatLon>();
     36
     37    /**
     38     * The place to remeber already added nodes (they are reused if needed @since 5845
     39     */
     40    HashMap<LatLon, Node> addedNodes;
    3341
    3442    @Override
     
    8492        }
    8593    }
     94   
     95    /**
     96     * Find the node with almost the same ccords in dataset or in already added nodes
     97     * @since 5845
     98     **/
     99    Node findOrCreateNode(LatLon ll,  List<Command> commands) {
     100        Node nd = null;     
     101         
     102        if (Main.map != null && Main.map.mapView != null) {
     103            Point p = Main.map.mapView.getPoint(ll);
     104            nd = Main.map.mapView.getNearestNode(p, OsmPrimitive.isUsablePredicate);
     105            if (nd!=null && nd.getCoor().greatCircleDistance(ll) > Main.pref.getDouble("remote.tolerance", 0.1)) {
     106                nd = null; // node is too far
     107            }
     108        }
     109       
     110        Node prev = null;
     111        for (LatLon lOld: addedNodes.keySet()) {
     112            if (lOld.greatCircleDistance(ll) < Main.pref.getDouble("remotecontrol.tolerance", 0.1)) {
     113                prev = addedNodes.get(lOld);
     114                break;
     115            }
     116        }
    86117
     118        if (prev!=null) {
     119            nd = prev;
     120        } else if (nd==null) {
     121            nd = new Node(ll);
     122            // Now execute the commands to add this node.
     123            commands.add(new AddCommand(nd));
     124            addedNodes.put(ll, nd);
     125        }
     126        return nd;
     127    }
     128   
    87129    /*
    88130     * This function creates the way with given coordinates of nodes
    89131     */
    90132    private void addWay() {
     133        addedNodes = new HashMap<LatLon, Node>();
    91134        Way way = new Way();
    92135        List<Command> commands = new LinkedList<Command>();
    93136        for (LatLon ll : allCoordinates) {
    94             Node node = new Node(ll);
     137            Node node = findOrCreateNode(ll, commands);
    95138            way.addNode(node);
    96             commands.add(new AddCommand(node));
    97139        }
    98140        allCoordinates.clear();
Note: See TracChangeset for help on using the changeset viewer.