source: osm/applications/editors/josm/plugins/merge-overlap/src/mergeoverlap/hack/MyRelationMemberConflictResolverModel.java@ 30766

Last change on this file since 30766 was 30766, checked in by donvip, 11 years ago

[josm-merge_overlap] fix #josm10668 - ClassCastException while merging ways

File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package mergeoverlap.hack;
3
4import java.util.Collection;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.Map;
8
9import mergeoverlap.MergeOverlapAction;
10
11import org.openstreetmap.josm.command.Command;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Relation;
14import org.openstreetmap.josm.data.osm.RelationMember;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictDecision;
17import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictResolverModel;
18
19/**
20 * This model manages a list of conflicting relation members.
21 *
22 * It can be used as {@link javax.swing.table.TableModel}.
23 */
24public class MyRelationMemberConflictResolverModel extends RelationMemberConflictResolverModel {
25 /** the property name for the number conflicts managed by this model */
26 public static final String NUM_CONFLICTS_PROP = MyRelationMemberConflictResolverModel.class.getName() + ".numConflicts";
27
28 @Override
29 protected String getProperty() {
30 return NUM_CONFLICTS_PROP;
31 }
32
33 @Override
34 protected void populate(Relation relation, OsmPrimitive primitive) {
35 throw new UnsupportedOperationException("Use populate(Relation, OsmPrimitive, Map<Way, Way>) instead");
36 }
37
38 /**
39 * Populates the model with the members of the relation <code>relation</code>
40 * referring to <code>primitive</code>.
41 *
42 * @param relation the parent relation
43 * @param primitive the child primitive
44 */
45 protected void populate(Relation relation, OsmPrimitive primitive, Map<Way, Way> oldWays) {
46 for (int i =0; i<relation.getMembersCount();i++) {
47 if (MergeOverlapAction.getOld(relation.getMember(i).getWay(), oldWays) == MergeOverlapAction.getOld((Way)primitive, oldWays)) {
48 decisions.add(new RelationMemberConflictDecision(relation, i));
49 }
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");
56 }
57
58 /**
59 * Populates the model with the relation members belonging to one of the relations in <code>relations</code>
60 * and referring to one of the primitives in <code>memberPrimitives</code>.
61 *
62 * @param relations the parent relations. Empty list assumed if null.
63 * @param memberPrimitives the child primitives. Empty list assumed if null.
64 */
65 public void populate(Collection<Relation> relations, Collection<? extends OsmPrimitive> memberPrimitives, Map<Way, Way> oldWays) {
66 decisions.clear();
67 relations = relations == null ? new LinkedList<Relation>() : relations;
68 memberPrimitives = memberPrimitives == null ? new LinkedList<OsmPrimitive>() : memberPrimitives;
69 for (Relation r : relations) {
70 for (OsmPrimitive p: memberPrimitives) {
71 populate(r, p, oldWays);
72 }
73 }
74 this.relations = relations;
75 refresh();
76 }
77
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");
81 }
82
83 protected void buildResolveCorrespondance(Relation relation, OsmPrimitive newPrimitive, Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) {
84
85 List<RelationMember> relationsMembers = relation.getMembers();
86 Relation modifiedRelation = MergeOverlapAction.getNew(relation, newRelations);
87 modifiedRelation.setMembers(null);
88 for (int i=0; i < relationsMembers.size(); i++) {
89 RelationMember rm = relationsMembers.get(i);
90 RelationMemberConflictDecision decision = getDecision(relation, i);
91 if (decision == null) {
92 modifiedRelation.addMember(rm);
93 } else {
94 switch(decision.getDecision()) {
95 case KEEP:
96 if (newPrimitive instanceof Way) {
97 modifiedRelation.addMember(new RelationMember(decision.getRole(), MergeOverlapAction.getOld((Way)newPrimitive, oldWays)));
98 }
99 else {
100 modifiedRelation.addMember(new RelationMember(decision.getRole(), newPrimitive));
101 }
102 break;
103 case REMOVE:
104 // do nothing
105 break;
106 case UNDECIDED:
107 // FIXME: this is an error
108 break;
109 }
110 }
111 }
112 }
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
119 /**
120 * Builds a collection of commands executing the decisions made in this model.
121 *
122 * @param newPrimitive the primitive which members shall refer to if the
123 * decision is {@see RelationMemberConflictDecisionType#REPLACE}
124 */
125 public void buildRelationCorrespondance(OsmPrimitive newPrimitive, Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) {
126 for (Relation relation : relations) {
127 buildResolveCorrespondance(relation, newPrimitive, newRelations, oldWays);
128 }
129 }
130}
Note: See TracBrowser for help on using the repository browser.