Changeset 6674 in josm
- Timestamp:
- 2014-01-12T10:41:20+01:00 (11 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java
r6429 r6674 22 22 import org.openstreetmap.josm.data.osm.Node; 23 23 import org.openstreetmap.josm.data.osm.OsmPrimitive; 24 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 24 25 import org.openstreetmap.josm.data.osm.OsmUtils; 25 26 import org.openstreetmap.josm.data.osm.Relation; … … 746 747 } 747 748 748 // TODO: change how we handle this749 749 private static class ExactType extends Match { 750 private final Class<?>type;750 private final OsmPrimitiveType type; 751 751 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) 759 754 throw new ParseError(tr("Unknown primitive type: {0}. Allowed values are node, way or relation", 760 755 type)); 761 756 } 762 757 @Override public boolean match(OsmPrimitive osm) { 763 return osm.getClass() == type;758 return type.equals(osm.getType()); 764 759 } 765 760 @Override public String toString() {return "type="+type;} -
trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java
r6084 r6674 3 3 4 4 import java.io.Serializable; 5 import java.util.ArrayList; 6 import java.util.List; 5 7 import java.util.regex.Matcher; 6 8 import java.util.regex.Pattern; … … 9 11 private final long id; 10 12 private final OsmPrimitiveType type; 13 14 public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)"); 11 15 12 16 public SimplePrimitiveId(long id, OsmPrimitiveType type) { … … 64 68 65 69 /** 66 * Parses a {@code OsmPrimitiveType} from the string {@code s}.70 * Parses a {@code SimplePrimitiveId} from the string {@code s}. 67 71 * @param s the string to be parsed, e.g., {@code n1}, {@code node1}, 68 72 * {@code w1}, {@code way1}, {@code r1}, {@code rel1}, {@code relation1}. 69 * @return the parsed {@code OsmPrimitiveType}73 * @return the parsed {@code SimplePrimitiveId} 70 74 * @throws IllegalArgumentException if the string does not match the pattern 71 75 */ 72 76 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); 75 78 if (m.matches()) { 76 79 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); 80 85 } 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); 82 87 } 83 88 } 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 } 84 109 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java
r6577 r6674 3 3 4 4 import org.openstreetmap.josm.Main; 5 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 5 6 import org.openstreetmap.josm.data.osm.PrimitiveId; 7 import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 6 8 import org.openstreetmap.josm.gui.ExtendedDialog; 7 9 import org.openstreetmap.josm.gui.widgets.HistoryComboBox; … … 29 31 import java.util.Collection; 30 32 import java.util.Collections; 33 import java.util.HashSet; 31 34 import java.util.LinkedList; 32 35 import java.util.List; 36 import java.util.Set; 33 37 34 38 import static org.openstreetmap.josm.tools.I18n.tr; … … 187 191 String buf = Utils.getClipboardContent(); 188 192 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 } 197 215 } 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 } 202 225 } 203 226 } -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java
r6643 r6674 22 22 import org.openstreetmap.josm.data.osm.OsmPrimitive; 23 23 import org.openstreetmap.josm.data.osm.Relation; 24 import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 24 25 import org.openstreetmap.josm.data.osm.Way; 25 26 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; … … 52 53 53 54 // 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>(); 57 56 58 57 @Override … … 60 59 String msg = tr("Remote Control has been asked to load data from the API.") + 61 60 "<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()); 64 63 } 65 64 return msg; … … 168 167 if(ds == null) // e.g. download failed 169 168 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(); 188 176 ds.setSelected(newSel); 189 177 if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) { … … 277 265 // Process optional argument 'select' 278 266 if (args.containsKey("select")) { 279 ways.clear(); 280 nodes.clear(); 281 relations.clear(); 267 toSelect.clear(); 282 268 for (String item : args.get("select").split(",")) { 283 269 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"); 297 273 } 298 274 }
Note:
See TracChangeset
for help on using the changeset viewer.