Changeset 6674 in josm


Ignore:
Timestamp:
2014-01-12T10:41:20+01:00 (10 years ago)
Author:
simon04
Message:

Make strings like foo relation/123 and way/345 but also node/789 paste-able in "Download object", some refactoring of related code

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r6429 r6674  
    2222import org.openstreetmap.josm.data.osm.Node;
    2323import org.openstreetmap.josm.data.osm.OsmPrimitive;
     24import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    2425import org.openstreetmap.josm.data.osm.OsmUtils;
    2526import org.openstreetmap.josm.data.osm.Relation;
     
    746747    }
    747748
    748     // TODO: change how we handle this
    749749    private static class ExactType extends Match {
    750         private final Class<?> type;
     750        private final OsmPrimitiveType type;
    751751        public ExactType(String type) throws ParseError {
    752             if ("node".equals(type)) {
    753                 this.type = Node.class;
    754             } else if ("way".equals(type)) {
    755                 this.type = Way.class;
    756             } else if ("relation".equals(type)) {
    757                 this.type = Relation.class;
    758             } else
     752            this.type = OsmPrimitiveType.from(type);
     753            if (this.type == null)
    759754                throw new ParseError(tr("Unknown primitive type: {0}. Allowed values are node, way or relation",
    760755                        type));
    761756        }
    762757        @Override public boolean match(OsmPrimitive osm) {
    763             return osm.getClass() == type;
     758            return type.equals(osm.getType());
    764759        }
    765760        @Override public String toString() {return "type="+type;}
  • trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java

    r6084 r6674  
    33
    44import java.io.Serializable;
     5import java.util.ArrayList;
     6import java.util.List;
    57import java.util.regex.Matcher;
    68import java.util.regex.Pattern;
     
    911    private final long id;
    1012    private final OsmPrimitiveType type;
     13
     14    public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)");
    1115
    1216    public SimplePrimitiveId(long id, OsmPrimitiveType type) {
     
    6468
    6569    /**
    66      * Parses a {@code OsmPrimitiveType} from the string {@code s}.
     70     * Parses a {@code SimplePrimitiveId} from the string {@code s}.
    6771     * @param s the string to be parsed, e.g., {@code n1}, {@code node1},
    6872     * {@code w1}, {@code way1}, {@code r1}, {@code rel1}, {@code relation1}.
    69      * @return the parsed {@code OsmPrimitiveType}
     73     * @return the parsed {@code SimplePrimitiveId}
    7074     * @throws IllegalArgumentException if the string does not match the pattern
    7175     */
    7276    public static SimplePrimitiveId fromString(String s) {
    73         final Pattern p = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)");
    74         final Matcher m = p.matcher(s);
     77        final Matcher m = ID_PATTERN.matcher(s);
    7578        if (m.matches()) {
    7679            return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
    77                     s.charAt(0) == 'n' ? OsmPrimitiveType.NODE
    78                     : s.charAt(0) == 'w' ? OsmPrimitiveType.WAY
    79                     : OsmPrimitiveType.RELATION);
     80                    s.charAt(0) == 'n'
     81                            ? OsmPrimitiveType.NODE
     82                            : s.charAt(0) == 'w'
     83                            ? OsmPrimitiveType.WAY
     84                            : OsmPrimitiveType.RELATION);
    8085        } else {
    81             throw new IllegalArgumentException("The string " + s + " does not match the pattern " + p);
     86            throw new IllegalArgumentException("The string " + s + " does not match the pattern " + ID_PATTERN);
    8287        }
    8388    }
     89
     90    /**
     91     * Attempts to parse extract any primitive id from the string {@code s}.
     92     * @param s the string to be parsed, e.g., {@code n1, w1}, {@code node1 and rel2}.
     93     * @return the parsed list of {@code OsmPrimitiveType}s.
     94     */
     95    public static List<SimplePrimitiveId> fuzzyParse(String s) {
     96        final ArrayList<SimplePrimitiveId> ids = new ArrayList<SimplePrimitiveId>();
     97        final Matcher m = ID_PATTERN.matcher(s);
     98        while (m.find()) {
     99            final char firstChar = s.charAt(m.start());
     100            ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
     101                    firstChar == 'n'
     102                            ? OsmPrimitiveType.NODE
     103                            : firstChar == 'w'
     104                            ? OsmPrimitiveType.WAY
     105                            : OsmPrimitiveType.RELATION));
     106        }
     107        return ids;
     108    }
    84109}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java

    r6577 r6674  
    33
    44import org.openstreetmap.josm.Main;
     5import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    56import org.openstreetmap.josm.data.osm.PrimitiveId;
     7import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    68import org.openstreetmap.josm.gui.ExtendedDialog;
    79import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
     
    2931import java.util.Collection;
    3032import java.util.Collections;
     33import java.util.HashSet;
    3134import java.util.LinkedList;
    3235import java.util.List;
     36import java.util.Set;
    3337
    3438import static org.openstreetmap.josm.tools.I18n.tr;
     
    187191        String buf = Utils.getClipboardContent();
    188192        if (buf != null) {
    189             if (buf.contains("node")) cbType.setSelectedIndex(0);
    190             if (buf.contains("way")) cbType.setSelectedIndex(1);
    191             if (buf.contains("relation")) cbType.setSelectedIndex(2);
    192             String[] res = buf.split("/");
    193             String txt;
    194             if (res.length > 0) {
    195                 txt = res[res.length - 1];
    196                 if (txt.isEmpty() && txt.length() > 1) txt = res[res.length - 2];
     193            if (buf.length() <= Main.pref.getInteger("downloadprimitive.max-autopaste-length", 2000)) {
     194                final List<SimplePrimitiveId> ids = SimplePrimitiveId.fuzzyParse(buf);
     195                final String parsedText = Utils.join(", ", Utils.transform(ids, new Utils.Function<SimplePrimitiveId, String>() {
     196                    @Override
     197                    public String apply(SimplePrimitiveId x) {
     198                        return x.getType().getAPIName().charAt(0) + String.valueOf(x.getUniqueId());
     199                    }
     200                }));
     201                tfId.tryToPasteFrom(parsedText);
     202                final Set<OsmPrimitiveType> types = new HashSet<OsmPrimitiveType>(Utils.transform(ids, new Utils.Function<SimplePrimitiveId, OsmPrimitiveType>() {
     203                    @Override
     204                    public OsmPrimitiveType apply(SimplePrimitiveId x) {
     205                        return x.getType();
     206                    }
     207                }));
     208                if (types.size() == 1) {
     209                    // select corresponding type
     210                    cbType.setSelectedItem(types.iterator().next());
     211                } else {
     212                    // select "mixed"
     213                    cbType.setSelectedIndex(3);
     214                }
    197215            } else {
    198                 txt = buf;
    199             }
    200             if (buf.length() <= Main.pref.getInteger("downloadprimitive.max-autopaste-length", 2000)) {
    201                 tfId.tryToPasteFrom(txt.replaceAll("[^0-9]+", " ").replaceAll("\\s\\s+", " "));
     216                if (buf.contains("node")) cbType.setSelectedIndex(0);
     217                if (buf.contains("way")) cbType.setSelectedIndex(1);
     218                if (buf.contains("relation")) cbType.setSelectedIndex(2);
     219                String[] res = buf.split("/");
     220                String txt;
     221                if (res.length > 0) {
     222                    txt = res[res.length - 1];
     223                    if (txt.isEmpty() && txt.length() > 1) txt = res[res.length - 2];
     224                }
    202225            }
    203226        }
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java

    r6643 r6674  
    2222import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2323import org.openstreetmap.josm.data.osm.Relation;
     24import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    2425import org.openstreetmap.josm.data.osm.Way;
    2526import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     
    5253
    5354    // Optional argument 'select'
    54     private final Set<Long> ways = new HashSet<Long>();
    55     private final Set<Long> nodes = new HashSet<Long>();
    56     private final Set<Long> relations = new HashSet<Long>();
     55    private final HashSet<SimplePrimitiveId> toSelect = new HashSet<SimplePrimitiveId>();
    5756
    5857    @Override
     
    6059        String msg = tr("Remote Control has been asked to load data from the API.") +
    6160                "<br>" + tr("Bounding box: ") + new BBox(minlon, minlat, maxlon, maxlat).toStringCSV(", ");
    62         if (args.containsKey("select") && ways.size()+nodes.size()+relations.size() > 0) {
    63             msg += "<br>" + tr("Sel.: Rel.:{0} / Ways:{1} / Nodes:{2}", relations.size(), ways.size(), nodes.size());
     61        if (args.containsKey("select") && toSelect.size() > 0) {
     62            msg += "<br>" + tr("Selection: {0}", toSelect.size());
    6463        }
    6564        return msg;
     
    168167                    if(ds == null) // e.g. download failed
    169168                        return;
    170                     for (Way w : ds.getWays()) {
    171                         if (ways.contains(w.getId())) {
    172                             newSel.add(w);
    173                         }
    174                     }
    175                     ways.clear();
    176                     for (Node n : ds.getNodes()) {
    177                         if (nodes.contains(n.getId())) {
    178                             newSel.add(n);
    179                         }
    180                     }
    181                     nodes.clear();
    182                     for (Relation r : ds.getRelations()) {
    183                         if (relations.contains(r.getId())) {
    184                             newSel.add(r);
    185                         }
    186                     }
    187                     relations.clear();
     169                    for (SimplePrimitiveId id : toSelect) {
     170                        final OsmPrimitive p = ds.getPrimitiveById(id);
     171                        if (p != null) {
     172                            newSel.add(p);
     173                        }
     174                    }
     175                    toSelect.clear();
    188176                    ds.setSelected(newSel);
    189177                    if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
     
    277265        // Process optional argument 'select'
    278266        if (args.containsKey("select")) {
    279             ways.clear();
    280             nodes.clear();
    281             relations.clear();
     267            toSelect.clear();
    282268            for (String item : args.get("select").split(",")) {
    283269                try {
    284                     if (item.startsWith("way")) {
    285                         ways.add(Long.parseLong(item.substring(3)));
    286                     } else if (item.startsWith("node")) {
    287                         nodes.add(Long.parseLong(item.substring(4)));
    288                     } else if (item.startsWith("relation")) {
    289                         relations.add(Long.parseLong(item.substring(8)));
    290                     } else if (item.startsWith("rel")) {
    291                         relations.add(Long.parseLong(item.substring(3)));
    292                     } else {
    293                         Main.warn("RemoteControl: invalid selection '"+item+"' ignored");
    294                     }
    295                 } catch (NumberFormatException e) {
    296                     Main.warn("RemoteControl: invalid selection '"+item+"' ignored");
     270                    toSelect.add(SimplePrimitiveId.fromString(item));
     271                } catch (IllegalArgumentException ex) {
     272                    Main.warn("RemoteControl: invalid selection '" + item + "' ignored");
    297273                }
    298274            }
Note: See TracChangeset for help on using the changeset viewer.