Changeset 20527 in osm for applications
- Timestamp:
- 2010-03-17T11:17:57+01:00 (15 years ago)
- Location:
- applications/editors/josm/plugins/turnrestrictions/src/org/openstreetmap/josm/plugins/turnrestrictions
- Files:
-
- 5 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/turnrestrictions/src/org/openstreetmap/josm/plugins/turnrestrictions/dnd/PrimitiveIdListTransferHandler.java
r20489 r20527 24 24 25 25 /** 26 * Replies true if {@code transferFlavors} includes the data flavor {@see PrimitiveIdTransferable#PRIMITIVE_ID_LIST_FLAVOR}. 27 28 * @param transferFlavors an array of transferFlavors 29 * @return true if {@code transferFlavors} includes the data flavor {@see PrimitiveIdTransferable#PRIMITIVE_ID_LIST_FLAVOR}. 30 */ 31 public static boolean isSupportedFlavor(DataFlavor[] transferFlavors) { 32 for (DataFlavor df: transferFlavors) { 33 if (df.equals(PrimitiveIdTransferable.PRIMITIVE_ID_LIST_FLAVOR)) return true; 34 } 35 return false; 36 } 37 38 /** 26 39 * Creates the transfer handler 27 40 * … … 34 47 } 35 48 36 /** 37 * Replies true if {@code transferFlavors} includes the data flavor {@see PrimitiveIdTransferable#PRIMITIVE_ID_LIST_FLAVOR}. 38 39 * @param transferFlavors an array of transferFlavors 40 * @return 41 */ 42 protected boolean isSupportedFlavor(DataFlavor[] transferFlavors) { 43 for (DataFlavor df: transferFlavors) { 44 if (df.equals(PrimitiveIdTransferable.PRIMITIVE_ID_LIST_FLAVOR)) return true; 45 } 46 return false; 47 } 49 48 50 49 51 protected Transferable createTransferable(JComponent c) { -
applications/editors/josm/plugins/turnrestrictions/src/org/openstreetmap/josm/plugins/turnrestrictions/editor/JosmSelectionListModel.java
r20489 r20527 227 227 public List<PrimitiveId> getSelectedPrimitiveIds() { 228 228 List<PrimitiveId> ret = new ArrayList<PrimitiveId>(getSelected().size()); 229 for(OsmPrimitive p: getSelected()) { 230 ret.add(p.getPrimitiveId()); 229 for(int i=0; i< selection.size(); i++) { 230 if (selectionModel.isSelectedIndex(i)) { 231 ret.add(selection.get(i).getPrimitiveId()); 232 } 231 233 } 232 234 return ret; -
applications/editors/josm/plugins/turnrestrictions/src/org/openstreetmap/josm/plugins/turnrestrictions/editor/TurnRestrictionEditor.java
r20489 r20527 77 77 private JosmSelectionPanel pnlJosmSelection; 78 78 private BasicEditorPanel pnlBasicEditor; 79 private AdvancedEditorPanel pnlAdvancedEditor; 79 80 private TurnRestrictionEditorModel editorModel; 80 81 … … 117 118 tpEditors.setTitleAt(0, tr("Basic")); 118 119 tpEditors.setToolTipTextAt(0, tr("Edit basic attributes of a turn restriction")); 120 121 tpEditors.add(pnlAdvancedEditor = new AdvancedEditorPanel(editorModel)); 122 tpEditors.setTitleAt(1, tr("Advanced")); 123 tpEditors.setToolTipTextAt(1, tr("Edit the raw tags and members of this turn restriction")); 124 119 125 pnl.add(tpEditors, BorderLayout.CENTER); 120 126 return pnl; … … 161 167 */ 162 168 protected void build() { 163 editorModel = new TurnRestrictionEditorModel( );169 editorModel = new TurnRestrictionEditorModel(getLayer()); 164 170 Container c = getContentPane(); 165 171 c.setLayout(new BorderLayout()); … … 222 228 223 229 if (turnRestriction == null) { 224 editorModel.populate(new Relation(), getLayer().data); 225 } else if (turnRestriction.getDataSet() == null) { 226 editorModel.populate(turnRestriction, getLayer().data); 227 } else if (turnRestriction.getDataSet() == getLayer().data) { 230 editorModel.populate(new Relation()); 231 } else if (turnRestriction.getDataSet() == null || turnRestriction.getDataSet() == getLayer().data) { 228 232 editorModel.populate(turnRestriction); 229 233 } else { 230 throw new IllegalArgumentException(MessageFormat.format("turnRestriction must not belong to the layer {0}", getLayer().getName()));234 throw new IllegalArgumentException(MessageFormat.format("turnRestriction must belong to layer ''{0}''", getLayer().getName())); 231 235 } 232 236 setTurnRestrictionSnapshot(turnRestriction == null ? null : new Relation(turnRestriction)); -
applications/editors/josm/plugins/turnrestrictions/src/org/openstreetmap/josm/plugins/turnrestrictions/editor/TurnRestrictionEditorModel.java
r20489 r20527 2 2 3 3 import java.text.MessageFormat; 4 import java.util.ArrayList;5 import java.util.Collections;6 4 import java.util.List; 7 5 import java.util.Observable; 6 import java.util.Set; 8 7 import java.util.logging.Logger; 8 9 import javax.swing.event.TableModelEvent; 10 import javax.swing.event.TableModelListener; 9 11 10 12 import org.openstreetmap.josm.data.osm.DataSet; … … 13 15 import org.openstreetmap.josm.data.osm.PrimitiveId; 14 16 import org.openstreetmap.josm.data.osm.Relation; 15 import org.openstreetmap.josm.data.osm.RelationMember;16 17 import org.openstreetmap.josm.data.osm.TagCollection; 17 18 import org.openstreetmap.josm.data.osm.Way; … … 27 28 import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent; 28 29 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode; 30 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 31 import org.openstreetmap.josm.gui.tagging.TagEditorModel; 32 import org.openstreetmap.josm.gui.tagging.TagModel; 29 33 import org.openstreetmap.josm.tools.CheckParameterUtil; 30 34 … … 59 63 } 60 64 61 62 /** 63 * holds the relation member of turn restriction relation from which the turn 64 * restriction leg with role 'from' was initialized. This is needed if OSM 65 * data contains turn restrictions with multiple 'from' members. The 66 * field is null if a turn restriction didn't have a member with role 67 * 'from'. 68 */ 69 private RelationMember fromRelationMember; 70 71 /** 72 * holds the relation member of turn restriction relation from which the turn 73 * restriction leg with role 'to' was initialized. This is needed if OSM 74 * data contains turn restrictions with multiple 'to' members. The 75 * field is null if a turn restriction didn't have a member with role 76 * 'to'. 77 */ 78 private RelationMember toRelationMember; 79 private Way from; 80 private Way to; 81 private TagCollection tags = new TagCollection(); 82 private DataSet dataSet; 83 private final List<OsmPrimitive> vias = new ArrayList<OsmPrimitive>(); 84 85 public TurnRestrictionEditorModel(){ 65 private OsmDataLayer layer; 66 private final TagEditorModel tagEditorModel = new TagEditorModel(); 67 private RelationMemberEditorModel memberModel; 68 69 /** 70 * Creates a model in the context of a {@see OsmDataLayer} 71 * 72 * @param layer the layer. Must not be null. 73 * @throws IllegalArgumentException thrown if {@code layer} is null 74 */ 75 public TurnRestrictionEditorModel(OsmDataLayer layer) throws IllegalArgumentException{ 76 CheckParameterUtil.ensureParameterNotNull(layer, "layer"); 77 this.layer = layer; 78 memberModel = new RelationMemberEditorModel(layer); 79 memberModel.addTableModelListener(new RelationMemberModelListener()); 86 80 } 87 81 … … 96 90 public void setTurnRestrictionLeg(TurnRestrictionLegRole role, Way way) { 97 91 CheckParameterUtil.ensureParameterNotNull(role, "role"); 98 Way oldValue = null;99 92 switch(role){ 100 case FROM: 101 oldValue = this.from; 102 this.from = way; 93 case FROM: 94 memberModel.setFromPrimitive(way); 103 95 break; 104 96 case TO: 105 oldValue = this.to; 106 this.to = way; 97 memberModel.setToPrimitive(way); 107 98 break; 108 99 } 100 } 109 101 110 if (oldValue != way) {111 setChanged();112 notifyObservers();113 }114 }115 116 102 /** 117 103 * Sets the way participating in the turn restriction in a given role. … … 132 118 throw new IllegalArgumentException(MessageFormat.format("parameter ''wayId'' of type {0} expected, got {1}", OsmPrimitiveType.WAY, wayId.getType())); 133 119 } 134 135 if (dataSet == null) { 136 throw new IllegalStateException("data set not initialized"); 137 } 138 OsmPrimitive p = dataSet.getPrimitiveById(wayId); 120 121 OsmPrimitive p = layer.data.getPrimitiveById(wayId); 139 122 if (p == null) { 140 throw new IllegalStateException(MessageFormat.format("didn' t find way with id {0} in dataset {1}", wayId, dataSet));123 throw new IllegalStateException(MessageFormat.format("didn''t find way with id {0} in layer ''{1}''", wayId, layer.getName())); 141 124 } 142 125 setTurnRestrictionLeg(role, (Way)p); … … 144 127 145 128 /** 146 * Replies the turn restrictioin leg with role {@code role} 129 * "Officially" a turn restriction should have exactly one member with 130 * role {@see TurnRestrictionLegRole#FROM} and one member with role {@see TurnRestrictionLegRole#TO}, 131 * both referring to an OSM {@see Way}. In order to deals with turn restrictions where these 132 * integrity constraints are violated, this model also supports relation with multiple or no 133 * 'from' or 'to' members. 134 * 135 * Replies the turn restriction legs with role {@code role}. If no leg with this 136 * role exists, an empty set is returned. If multiple legs exists, the set of referred 137 * primitives is returned. 147 138 * 148 139 * @param role the role. Must not be null. 149 * @return the turn restrictioin leg with role {@code role}. null, if140 * @return the set of turn restriction legs with role {@code role}. The empty set, if 150 141 * no such turn restriction leg exists 151 142 * @throws IllegalArgumentException thrown if role is null 152 143 */ 153 public WaygetTurnRestrictionLeg(TurnRestrictionLegRole role){144 public Set<OsmPrimitive>getTurnRestrictionLeg(TurnRestrictionLegRole role){ 154 145 CheckParameterUtil.ensureParameterNotNull(role, "role"); 155 146 switch(role){ 156 case FROM: return from; 157 case TO: return to; 158 } 147 case FROM: return memberModel.getFromPrimitives(); 148 case TO: return memberModel.getToPrimitives(); 149 } 150 // should not happen 159 151 return null; 160 152 } … … 167 159 */ 168 160 protected void initFromTurnRestriction(Relation turnRestriction) { 169 this.from = null;170 this.to = null;171 this.fromRelationMember = null;172 this.toRelationMember = null;173 this.tags = new TagCollection();174 this.vias.clear();175 if (turnRestriction == null) return;176 for (RelationMember rm: turnRestriction.getMembers()) {177 if (rm.getRole().equals("from") && rm.isWay()) {178 this.fromRelationMember = rm;179 this.from = rm.getWay();180 break;181 }182 }183 for (RelationMember rm: turnRestriction.getMembers()) {184 if (rm.getRole().equals("to") && rm.isWay()) {185 this.toRelationMember = rm;186 this.to = rm.getWay();187 break;188 }189 }190 161 191 for (RelationMember rm: turnRestriction.getMembers()) { 192 if (rm.getRole().equals("via")) { 193 this.vias.add(rm.getMember()); 194 } 195 } 162 // populate the member model 163 memberModel.populate(turnRestriction); 196 164 197 165 // make sure we have a restriction tag 198 tags = TagCollection.from(turnRestriction);166 TagCollection tags = TagCollection.from(turnRestriction); 199 167 tags.setUniqueForKey("type", "restriction"); 168 tagEditorModel.initFromTags(tags); 200 169 201 170 setChanged(); … … 215 184 public void populate(Relation turnRestriction) { 216 185 CheckParameterUtil.ensureParameterNotNull(turnRestriction, "turnRestriction"); 217 if (turnRestriction.getDataSet() == null) {186 if (turnRestriction.getDataSet() != null && turnRestriction.getDataSet() != layer.data) { 218 187 throw new IllegalArgumentException( 219 188 // don't translate - it's a technical message 220 MessageFormat.format("turnRestriction {0} must belong to a dataset", turnRestriction.getId())189 MessageFormat.format("turnRestriction {0} must not belong to a different dataset than the dataset of layer ''{1}''", turnRestriction.getId(), layer.getName()) 221 190 ); 222 191 } 223 this.dataSet = turnRestriction.getDataSet();224 192 initFromTurnRestriction(turnRestriction); 225 193 } 226 194 227 /**228 * Populates the turn restriction editor model with a new turn restriction,229 * which isn't added to a data set yet. {@code ds} is the data set the turn230 * restriction is eventually being added to. Relation members of this231 * turn restriction must refer to objects in {@code ds}.232 *233 * {@code turnRestriction} is an arbitrary relation. A tag type=restriction234 * isn't required. If it is missing, it is added here. {@code turnRestriction}235 * is required to be a new turn restriction with a negative id. It must not be236 * part of a dataset yet237 *238 * @param turnRestriction the turn restriction. Must not be null. New turn restriction239 * required240 * @param ds the dataset. Must not be null.241 * @throws IllegalArgumentException thrown if turnRestriction is null242 * @throws IllegalArgumentException thrown if turnRestriction is part of a dataset243 * @throws IllegalArgumentException thrown if turnRestriction isn't a new turn restriction244 */245 public void populate(Relation turnRestriction, DataSet ds) {246 CheckParameterUtil.ensureParameterNotNull(turnRestriction, "turnRestriction");247 CheckParameterUtil.ensureParameterNotNull(ds, "ds");248 if (!turnRestriction.isNew()){249 throw new IllegalArgumentException(250 // don't translate - it's a technical message251 MessageFormat.format("new turn restriction expected, got turn restriction with id {0}", turnRestriction.getId())252 );253 }254 if (turnRestriction.getDataSet() != null) {255 throw new IllegalArgumentException(256 // don't translate - it's a technical message257 MessageFormat.format("expected turn restriction not assigned to a dataset, got turn restriction with id {0} assigned to {1}", turnRestriction.getId(), turnRestriction.getDataSet())258 );259 }260 for(RelationMember rm: turnRestriction.getMembers()) {261 if (rm.getMember().getDataSet() != ds) {262 throw new IllegalArgumentException(263 // don't translate - it's a technical message264 MessageFormat.format("expected all members assigned to dataset {0}, got member {1} assigned to {2}", ds, rm, rm.getMember().getDataSet())265 );266 }267 }268 this.dataSet = ds;269 initFromTurnRestriction(turnRestriction);270 }271 195 272 196 /** … … 277 201 public void apply(Relation turnRestriction) { 278 202 CheckParameterUtil.ensureParameterNotNull(turnRestriction, "turnRestriction"); 279 // apply the tags 203 204 TagCollection tags = tagEditorModel.getTagCollection(); 280 205 tags.applyTo(turnRestriction); 281 282 List<RelationMember> members = new ArrayList<RelationMember>(); 283 if (from != null){ 284 members.add(new RelationMember("from", from)); 285 } 286 if (to != null) { 287 members.add(new RelationMember("to", to)); 288 } 289 for(OsmPrimitive via: vias) { 290 members.add(new RelationMember("via", via)); 291 } 292 turnRestriction.setMembers(members); 206 memberModel.applyTo(turnRestriction); 293 207 } 294 208 … … 300 214 */ 301 215 public String getRestrictionTagValue() { 216 TagCollection tags = tagEditorModel.getTagCollection(); 302 217 if (!tags.hasTagsFor("restriction")) return null; 303 218 return tags.getJoinedValues("restriction"); … … 312 227 public void setRestrictionTagValue(String value){ 313 228 if (value == null || value.trim().equals("")) { 314 tag s.removeByKey("restriction");229 tagEditorModel.delete("restriction"); 315 230 } else { 316 tags.setUniqueForKey("restriction", value.trim()); 231 TagModel tm = tagEditorModel.get("restriction"); 232 if (tm != null){ 233 tm.setValue(value); 234 } else { 235 tagEditorModel.add(new TagModel("restriction", value)); 236 } 317 237 } 318 238 setChanged(); … … 327 247 */ 328 248 public List<OsmPrimitive> getVias() { 329 return Collections.unmodifiableList(vias);249 return memberModel.getVias(); 330 250 } 331 251 … … 342 262 */ 343 263 public void setVias(List<OsmPrimitive> vias) { 344 if (vias == null) { 345 this.vias.clear(); 346 setChanged(); 347 notifyObservers(); 348 return; 349 } 350 for (OsmPrimitive p: vias) { 351 if (p == null) 352 throw new IllegalArgumentException("a via object must not be null"); 353 if (p.getDataSet() != dataSet) 354 throw new IllegalArgumentException(MessageFormat.format("a via object must belong to dataset {1}, object {2} belongs to {3}", dataSet, p.getPrimitiveId(), p.getDataSet())); 355 } 356 this.vias.clear(); 357 this.vias.addAll(vias); 358 setChanged(); 359 notifyObservers(); 360 } 361 362 /** 363 * Replies the dataset this turn restriction editor model is 364 * working with 365 * 366 * @return the dataset 367 */ 368 public DataSet getDataSet() { 369 return this.dataSet; 370 } 371 264 memberModel.setVias(vias); 265 } 266 267 /** 268 * Replies the layer in whose context this editor is working 269 * 270 * @return the layer in whose context this editor is working 271 */ 272 public OsmDataLayer getLayer() { 273 return layer; 274 } 275 372 276 /** 373 277 * Registers this model with global event sources like {@see DatasetEventManager} … … 383 287 DatasetEventManager.getInstance().removeDatasetListener(this); 384 288 } 289 290 /** 291 * Replies the tag editor model 292 * 293 * @return the tag editor model 294 */ 295 public TagEditorModel getTagEditorModel() { 296 return tagEditorModel; 297 } 298 299 /** 300 * Replies the editor model for the relation members 301 * 302 * @return the editor model for the relation members 303 */ 304 public RelationMemberEditorModel getRelationMemberEditorModel() { 305 return memberModel; 306 } 385 307 386 308 /* ----------------------------------------------------------------------------------------- */ 387 309 /* interface DataSetListener */ 388 /* ----------------------------------------------------------------------------------------- */ 389 310 /* ----------------------------------------------------------------------------------------- */ 390 311 protected boolean isAffectedByDataSetUpdate(DataSet ds, List<? extends OsmPrimitive> updatedPrimitives) { 391 if (ds != dataSet) return false;312 if (ds != layer.data) return false; 392 313 if (updatedPrimitives == null || updatedPrimitives.isEmpty()) return false; 393 if (from != null && updatedPrimitives.contains(from)) return true; 394 if (to != null && updatedPrimitives.contains(to)) return true; 395 for (OsmPrimitive via: vias){ 396 if (updatedPrimitives.contains(via)) return true; 397 } 398 return false; 314 Set<OsmPrimitive> myPrimitives = memberModel.getMemberPrimitives(); 315 int size1 = myPrimitives.size(); 316 myPrimitives.retainAll(updatedPrimitives); 317 return size1 != myPrimitives.size(); 399 318 } 400 319 … … 402 321 // refresh the views 403 322 setChanged(); 404 notifyObservers(); 405 323 notifyObservers(); 406 324 } 407 325 … … 439 357 } 440 358 } 359 360 class RelationMemberModelListener implements TableModelListener { 361 public void tableChanged(TableModelEvent e) { 362 setChanged(); 363 notifyObservers(); 364 } 365 } 441 366 } -
applications/editors/josm/plugins/turnrestrictions/src/org/openstreetmap/josm/plugins/turnrestrictions/editor/TurnRestrictionLegEditor.java
r20489 r20527 5 5 import java.awt.BorderLayout; 6 6 import java.awt.Font; 7 import java.awt.Image;8 7 import java.awt.Toolkit; 9 8 import java.awt.datatransfer.Clipboard; … … 20 19 import java.io.IOException; 21 20 import java.util.Collections; 21 import java.util.HashSet; 22 22 import java.util.List; 23 23 import java.util.Observable; 24 24 import java.util.Observer; 25 import java.util.Set; 25 26 import java.util.logging.Logger; 26 27 … … 28 29 import javax.swing.Action; 29 30 import javax.swing.BorderFactory; 30 import javax.swing.ImageIcon;31 31 import javax.swing.JButton; 32 32 import javax.swing.JComponent; … … 39 39 import javax.swing.UIManager; 40 40 41 import org.openstreetmap.josm.data.osm.OsmPrimitive; 41 42 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 42 43 import org.openstreetmap.josm.data.osm.PrimitiveId; 43 import org.openstreetmap.josm.data.osm.Way;44 44 import org.openstreetmap.josm.gui.DefaultNameFormatter; 45 45 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; … … 58 58 59 59 private JLabel lblOsmObject; 60 private Way way;60 private final Set<OsmPrimitive> legs = new HashSet<OsmPrimitive>(); 61 61 private TurnRestrictionEditorModel model; 62 62 private TurnRestrictionLegRole role; … … 140 140 141 141 protected void refresh(){ 142 Way newWay = model.getTurnRestrictionLeg(role);143 way = newWay;144 if ( way == null) {142 legs.clear(); 143 legs.addAll(model.getTurnRestrictionLeg(role)); 144 if (legs.isEmpty()) { 145 145 lblOsmObject.setFont(UIManager.getFont("Label.font").deriveFont(Font.ITALIC)); 146 146 lblOsmObject.setIcon(null); 147 147 lblOsmObject.setText(tr("please select a way")); 148 148 lblOsmObject.setToolTipText(null); 149 } else { 149 } else if (legs.size() == 1){ 150 OsmPrimitive leg = legs.iterator().next(); 150 151 lblOsmObject.setFont(UIManager.getFont("Label.font")); 151 152 lblOsmObject.setIcon(ImageProvider.get("data", "way")); 152 lblOsmObject.setText(DefaultNameFormatter.getInstance().format(way)); 153 lblOsmObject.setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(way)); 153 lblOsmObject.setText(leg.getDisplayName(DefaultNameFormatter.getInstance())); 154 lblOsmObject.setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(leg)); 155 } else { 156 lblOsmObject.setFont(UIManager.getFont("Label.font").deriveFont(Font.ITALIC)); 157 lblOsmObject.setIcon(null); 158 lblOsmObject.setText(tr("multiple objects with role ''{0}''",this.role.toString())); 159 lblOsmObject.setToolTipText(null); 154 160 } 155 161 renderColors(); … … 201 207 /* ----------------------------------------------------------------------------- */ 202 208 public List<PrimitiveId> getSelectedPrimitiveIds() { 203 if (way == null) return Collections.emptyList(); 204 return Collections.singletonList(way.getPrimitiveId()); 209 if (legs.size() == 1) { 210 return Collections.singletonList(legs.iterator().next().getPrimitiveId()); 211 } 212 return Collections.emptyList(); 205 213 } 206 214 … … 247 255 248 256 public void updateEnabledState() { 249 setEnabled( way != null);257 setEnabled(legs.size()>0); 250 258 } 251 259 } … … 284 292 @Override 285 293 protected Transferable createTransferable(JComponent c) { 286 if ( way == null) return null;294 if (legs.size() != 1) return null; 287 295 return super.createTransferable(c); 288 296 } … … 326 334 327 335 public void updateEnabledState() { 328 setEnabled( way != null);336 setEnabled(legs.size() == 1); 329 337 } 330 338 } -
applications/editors/josm/plugins/turnrestrictions/src/org/openstreetmap/josm/plugins/turnrestrictions/editor/ViaListModel.java
r20489 r20527 171 171 if (idsToInsert == null) return; 172 172 List<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>(idsToInsert.size()); 173 DataSet ds = model.get DataSet();173 DataSet ds = model.getLayer().data; 174 174 for(PrimitiveId id: idsToInsert){ 175 175 OsmPrimitive p = ds.getPrimitiveById(id);
Note:
See TracChangeset
for help on using the changeset viewer.