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

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

[josm-merge_overlap] fix sonar issues

File size: 5.7 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(
56 "Use populate(Collection<Relation>, Collection<? extends OsmPrimitive>, Map<Way, Way>) instead");
57 }
58
59 /**
60 * Populates the model with the relation members belonging to one of the relations in <code>relations</code>
61 * and referring to one of the primitives in <code>memberPrimitives</code>.
62 *
63 * @param relations the parent relations. Empty list assumed if null.
64 * @param memberPrimitives the child primitives. Empty list assumed if null.
65 */
66 public void populate(Collection<Relation> relations, Collection<? extends OsmPrimitive> memberPrimitives, Map<Way, Way> oldWays) {
67 decisions.clear();
68 relations = relations == null ? new LinkedList<Relation>() : relations;
69 memberPrimitives = memberPrimitives == null ? new LinkedList<OsmPrimitive>() : memberPrimitives;
70 for (Relation r : relations) {
71 for (OsmPrimitive p: memberPrimitives) {
72 populate(r, p, oldWays);
73 }
74 }
75 this.relations = relations;
76 refresh();
77 }
78
79 @Override
80 protected Command buildResolveCommand(Relation relation, OsmPrimitive newPrimitive) {
81 throw new UnsupportedOperationException(
82 "Use buildResolveCorrespondance(Relation, OsmPrimitive, Map<Relation, Relation>, Map<Way, Way>) instead");
83 }
84
85 protected void buildResolveCorrespondance(
86 Relation relation, OsmPrimitive newPrimitive, Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) {
87
88 List<RelationMember> relationsMembers = relation.getMembers();
89 Relation modifiedRelation = MergeOverlapAction.getNew(relation, newRelations);
90 modifiedRelation.setMembers(null);
91 for (int i=0; i < relationsMembers.size(); i++) {
92 RelationMember rm = relationsMembers.get(i);
93 RelationMemberConflictDecision decision = getDecision(relation, i);
94 if (decision == null) {
95 modifiedRelation.addMember(rm);
96 } else {
97 switch(decision.getDecision()) {
98 case KEEP:
99 if (newPrimitive instanceof Way) {
100 modifiedRelation.addMember(new RelationMember(decision.getRole(),
101 MergeOverlapAction.getOld((Way)newPrimitive, oldWays)));
102 } else {
103 modifiedRelation.addMember(new RelationMember(decision.getRole(), newPrimitive));
104 }
105 break;
106 case REMOVE:
107 // do nothing
108 break;
109 case UNDECIDED:
110 // FIXME: this is an error
111 break;
112 }
113 }
114 }
115 }
116
117 @Override
118 public List<Command> buildResolutionCommands(OsmPrimitive newPrimitive) {
119 throw new UnsupportedOperationException(
120 "Use buildRelationCorrespondance(OsmPrimitive, Map<Relation, Relation>, Map<Way, Way>) instead");
121 }
122
123 /**
124 * Builds a collection of commands executing the decisions made in this model.
125 *
126 * @param newPrimitive the primitive which members shall refer to if the
127 * decision is {@see RelationMemberConflictDecisionType#REPLACE}
128 */
129 public void buildRelationCorrespondance(OsmPrimitive newPrimitive, Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) {
130 for (Relation relation : relations) {
131 buildResolveCorrespondance(relation, newPrimitive, newRelations, oldWays);
132 }
133 }
134}
Note: See TracBrowser for help on using the repository browser.