Changeset 30766 in osm for applications/editors/josm/plugins/merge-overlap/src/mergeoverlap/hack/MyRelationMemberConflictResolverModel.java
- Timestamp:
- 2014-10-27T23:30:49+01:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/merge-overlap/src/mergeoverlap/hack/MyRelationMemberConflictResolverModel.java
r30714 r30766 2 2 package mergeoverlap.hack; 3 3 4 import java.beans.PropertyChangeListener;5 import java.beans.PropertyChangeSupport;6 import java.util.ArrayList;7 4 import java.util.Collection; 8 import java.util.HashSet;9 5 import java.util.LinkedList; 10 6 import java.util.List; 11 7 import java.util.Map; 12 import java.util.Set;13 14 import javax.swing.table.DefaultTableModel;15 8 16 9 import mergeoverlap.MergeOverlapAction; 17 10 11 import org.openstreetmap.josm.command.Command; 18 12 import org.openstreetmap.josm.data.osm.OsmPrimitive; 19 13 import org.openstreetmap.josm.data.osm.Relation; 20 14 import org.openstreetmap.josm.data.osm.RelationMember; 21 import org.openstreetmap.josm.data.osm.RelationToChildReference;22 15 import org.openstreetmap.josm.data.osm.Way; 23 16 import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictDecision; 24 import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictDecisionType; 25 import org.openstreetmap.josm.gui.util.GuiHelper; 17 import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictResolverModel; 26 18 27 19 /** … … 30 22 * It can be used as {@link javax.swing.table.TableModel}. 31 23 */ 32 public class MyRelationMemberConflictResolverModel extends DefaultTableModel {24 public class MyRelationMemberConflictResolverModel extends RelationMemberConflictResolverModel { 33 25 /** the property name for the number conflicts managed by this model */ 34 26 public static final String NUM_CONFLICTS_PROP = MyRelationMemberConflictResolverModel.class.getName() + ".numConflicts"; 35 27 36 /** the list of conflict decisions */ 37 private List<RelationMemberConflictDecision> decisions; 38 /** the collection of relations for which we manage conflicts */ 39 private Collection<Relation> relations; 40 /** the number of conflicts */ 41 private int numConflicts; 42 private PropertyChangeSupport support; 43 44 /** 45 * Replies true if each {@link MultiValueResolutionDecision} is decided. 46 * 47 * @return true if each {@link MultiValueResolutionDecision} is decided; false 48 * otherwise 49 */ 50 public boolean isResolvedCompletely() { 51 return numConflicts == 0; 28 @Override 29 protected String getProperty() { 30 return NUM_CONFLICTS_PROP; 52 31 } 53 54 /** 55 * Replies the current number of conflicts 56 * 57 * @return the current number of conflicts 58 */ 59 public int getNumConflicts() { 60 return numConflicts; 61 } 62 63 /** 64 * Updates the current number of conflicts from list of decisions and emits 65 * a property change event if necessary. 66 * 67 */ 68 protected void updateNumConflicts() { 69 int count = 0; 70 for (RelationMemberConflictDecision decision: decisions) { 71 if (!decision.isDecided()) { 72 count++; 73 } 74 } 75 int oldValue = numConflicts; 76 numConflicts = count; 77 if (numConflicts != oldValue) { 78 support.firePropertyChange(NUM_CONFLICTS_PROP, oldValue, numConflicts); 79 } 80 } 81 82 public void addPropertyChangeListener(PropertyChangeListener l) { 83 support.addPropertyChangeListener(l); 84 } 85 86 public void removePropertyChangeListener(PropertyChangeListener l) { 87 support.removePropertyChangeListener(l); 88 } 89 90 public MyRelationMemberConflictResolverModel() { 91 decisions = new ArrayList<>(); 92 support = new PropertyChangeSupport(this); 93 } 94 32 95 33 @Override 96 public int getRowCount() { 97 return getNumDecisions(); 98 } 99 100 @Override 101 public Object getValueAt(int row, int column) { 102 if (decisions == null) return null; 103 104 RelationMemberConflictDecision d = decisions.get(row); 105 switch(column) { 106 case 0: /* relation */ return d.getRelation(); 107 case 1: /* pos */ return Integer.toString(d.getPos() + 1); // position in "user space" starting at 1 108 case 2: /* role */ return d.getRole(); 109 case 3: /* original */ return d.getOriginalPrimitive(); 110 case 4: /* decision */ return d.getDecision(); 111 } 112 return null; 113 } 114 115 @Override 116 public void setValueAt(Object value, int row, int column) { 117 RelationMemberConflictDecision d = decisions.get(row); 118 switch(column) { 119 case 2: /* role */ 120 d.setRole((String)value); 121 break; 122 case 4: /* decision */ 123 d.decide((RelationMemberConflictDecisionType)value); 124 refresh(); 125 break; 126 } 127 fireTableDataChanged(); 34 protected void populate(Relation relation, OsmPrimitive primitive) { 35 throw new UnsupportedOperationException("Use populate(Relation, OsmPrimitive, Map<Way, Way>) instead"); 128 36 } 129 37 … … 141 49 } 142 50 } 51 } 52 53 @Override 54 public void populate(Collection<Relation> relations, Collection<? extends OsmPrimitive> memberPrimitives) { 55 throw new UnsupportedOperationException("Use populate(Collection<Relation>, Collection<? extends OsmPrimitive>, Map<Way, Way>) instead"); 143 56 } 144 57 … … 163 76 } 164 77 165 /** 166 * Populates the model with the relation members represented as a collection of 167 * {@link RelationToChildReference}s. 168 * 169 * @param references the references. Empty list assumed if null. 170 */ 171 public void populate(Collection<RelationToChildReference> references) { 172 references = references == null ? new LinkedList<RelationToChildReference>() : references; 173 decisions.clear(); 174 this.relations = new HashSet<>(references.size()); 175 for (RelationToChildReference reference: references) { 176 decisions.add(new RelationMemberConflictDecision(reference.getParent(), reference.getPosition())); 177 relations.add(reference.getParent()); 178 } 179 refresh(); 180 } 181 182 /** 183 * Replies the decision at position <code>row</code> 184 * 185 * @param row 186 * @return the decision at position <code>row</code> 187 */ 188 public RelationMemberConflictDecision getDecision(int row) { 189 return decisions.get(row); 190 } 191 192 /** 193 * Replies the number of decisions managed by this model 194 * 195 * @return the number of decisions managed by this model 196 */ 197 public int getNumDecisions() { 198 return decisions == null ? 0 : decisions.size(); 199 } 200 201 /** 202 * Refreshes the model state. Invoke this method to trigger necessary change 203 * events after an update of the model data. 204 * 205 */ 206 public void refresh() { 207 updateNumConflicts(); 208 GuiHelper.runInEDTAndWait(new Runnable() { 209 @Override public void run() { 210 fireTableDataChanged(); 211 } 212 }); 213 } 214 215 /** 216 * Apply a role to all member managed by this model. 217 * 218 * @param role the role. Empty string assumed if null. 219 */ 220 public void applyRole(String role) { 221 role = role == null ? "" : role; 222 for (RelationMemberConflictDecision decision : decisions) { 223 decision.setRole(role); 224 } 225 refresh(); 226 } 227 228 protected RelationMemberConflictDecision getDecision(Relation relation, int pos) { 229 for(RelationMemberConflictDecision decision: decisions) { 230 if (decision.matches(relation, pos)) return decision; 231 } 232 return null; 78 @Override 79 protected Command buildResolveCommand(Relation relation, OsmPrimitive newPrimitive) { 80 throw new UnsupportedOperationException("Use buildResolveCorrespondance(Relation, OsmPrimitive, Map<Relation, Relation>, Map<Way, Way>) instead"); 233 81 } 234 82 … … 238 86 Relation modifiedRelation = MergeOverlapAction.getNew(relation, newRelations); 239 87 modifiedRelation.setMembers(null); 240 // boolean isChanged = false;241 88 for (int i=0; i < relationsMembers.size(); i++) { 242 89 RelationMember rm = relationsMembers.get(i); 243 // RelationMember rm = relation.getMember(i);244 // RelationMember rmNew;245 90 RelationMemberConflictDecision decision = getDecision(relation, i); 246 91 if (decision == null) { 247 92 modifiedRelation.addMember(rm); 248 93 } else { 249 System.out.println(modifiedRelation);250 System.out.println(111);251 94 switch(decision.getDecision()) { 252 95 case KEEP: 253 // modifiedRelation.removeMembersFor(newPrimitive);254 System.out.println(222);255 96 if (newPrimitive instanceof Way) { 256 97 modifiedRelation.addMember(new RelationMember(decision.getRole(), MergeOverlapAction.getOld((Way)newPrimitive, oldWays))); … … 259 100 modifiedRelation.addMember(new RelationMember(decision.getRole(), newPrimitive)); 260 101 } 261 // modifiedRelation.addMember(new RelationMember(decision.getRole(), newPrimitive));262 102 break; 263 103 case REMOVE: 264 System.out.println(333);265 // modifiedRelation.removeMembersFor(rm.getMember());266 // isChanged = true;267 104 // do nothing 268 105 break; … … 275 112 } 276 113 114 @Override 115 public List<Command> buildResolutionCommands(OsmPrimitive newPrimitive) { 116 throw new UnsupportedOperationException("Use buildRelationCorrespondance(OsmPrimitive, Map<Relation, Relation>, Map<Way, Way>) instead"); 117 } 118 277 119 /** 278 120 * Builds a collection of commands executing the decisions made in this model. … … 280 122 * @param newPrimitive the primitive which members shall refer to if the 281 123 * decision is {@see RelationMemberConflictDecisionType#REPLACE} 282 * @return a list of commands283 124 */ 284 125 public void buildRelationCorrespondance(OsmPrimitive newPrimitive, Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) { … … 287 128 } 288 129 } 289 290 protected boolean isChanged(Relation relation, OsmPrimitive newPrimitive) {291 for (int i=0; i < relation.getMembersCount(); i++) {292 RelationMemberConflictDecision decision = getDecision(relation, i);293 if (decision == null) {294 continue;295 }296 switch(decision.getDecision()) {297 case REMOVE: return true;298 case KEEP:299 if (!relation.getMember(i).getRole().equals(decision.getRole()))300 return true;301 if (relation.getMember(i).getMember() != newPrimitive)302 return true;303 case UNDECIDED:304 // FIXME: handle error305 }306 }307 return false;308 }309 310 /**311 * Replies the set of relations which have to be modified according312 * to the decisions managed by this model.313 *314 * @param newPrimitive the primitive which members shall refer to if the315 * decision is {@see RelationMemberConflictDecisionType#REPLACE}316 *317 * @return the set of relations which have to be modified according318 * to the decisions managed by this model319 */320 public Set<Relation> getModifiedRelations(OsmPrimitive newPrimitive) {321 HashSet<Relation> ret = new HashSet<>();322 for (Relation relation: relations) {323 if (isChanged(relation, newPrimitive)) {324 ret.add(relation);325 }326 }327 return ret;328 }329 130 }
Note:
See TracChangeset
for help on using the changeset viewer.
