1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package mergeoverlap.hack;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.command.Command;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
13 | import org.openstreetmap.josm.data.osm.Way;
|
---|
14 | import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictDecision;
|
---|
15 | import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictResolverModel;
|
---|
16 |
|
---|
17 | import mergeoverlap.MergeOverlapAction;
|
---|
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 | */
|
---|
24 | public 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>way</code>.
|
---|
41 | *
|
---|
42 | * @param relation the parent relation
|
---|
43 | * @param way the child way
|
---|
44 | */
|
---|
45 | protected void populate(Relation relation, Way way, Map<Way, Way> oldWays) {
|
---|
46 | for (int i =0; i<relation.getMembersCount();i++) {
|
---|
47 | RelationMember mb = relation.getMember(i);
|
---|
48 | if (mb.isWay() && MergeOverlapAction.getOld(mb.getWay(), oldWays) == MergeOverlapAction.getOld(way, oldWays)) {
|
---|
49 | decisions.add(new RelationMemberConflictDecision(relation, i));
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void populate(Collection<Relation> relations, Collection<? extends OsmPrimitive> memberPrimitives) {
|
---|
56 | throw new UnsupportedOperationException(
|
---|
57 | "Use populate(Collection<Relation>, Collection<? extends OsmPrimitive>, Map<Way, Way>) instead");
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Populates the model with the relation members belonging to one of the relations in <code>relations</code>
|
---|
62 | * and referring to one of the primitives in <code>memberPrimitives</code>.
|
---|
63 | *
|
---|
64 | * @param relations the parent relations. Empty list assumed if null.
|
---|
65 | * @param memberWays the child ways. Empty list assumed if null.
|
---|
66 | */
|
---|
67 | public void populate(Collection<Relation> relations, Collection<Way> memberWays, Map<Way, Way> oldWays) {
|
---|
68 | decisions.clear();
|
---|
69 | relations = relations == null ? new LinkedList<>() : relations;
|
---|
70 | memberWays = memberWays == null ? new LinkedList<>() : memberWays;
|
---|
71 | for (Relation r : relations) {
|
---|
72 | for (Way w : memberWays) {
|
---|
73 | populate(r, w, oldWays);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | this.relations = relations;
|
---|
77 | this.primitives = memberWays;
|
---|
78 | refresh();
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | protected Command buildResolveCommand(Relation relation, OsmPrimitive newPrimitive) {
|
---|
83 | throw new UnsupportedOperationException(
|
---|
84 | "Use buildResolveCorrespondance(Relation, OsmPrimitive, Map<Relation, Relation>, Map<Way, Way>) instead");
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected void buildResolveCorrespondance(
|
---|
88 | Relation relation, OsmPrimitive newPrimitive, Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) {
|
---|
89 |
|
---|
90 | List<RelationMember> relationsMembers = relation.getMembers();
|
---|
91 | Relation modifiedRelation = MergeOverlapAction.getNew(relation, newRelations);
|
---|
92 | modifiedRelation.setMembers(null);
|
---|
93 | for (int i=0; i < relationsMembers.size(); i++) {
|
---|
94 | RelationMember rm = relationsMembers.get(i);
|
---|
95 | RelationMemberConflictDecision decision = getDecision(relation, i);
|
---|
96 | if (decision == null) {
|
---|
97 | modifiedRelation.addMember(rm);
|
---|
98 | } else {
|
---|
99 | switch(decision.getDecision()) {
|
---|
100 | case KEEP:
|
---|
101 | if (newPrimitive instanceof Way) {
|
---|
102 | modifiedRelation.addMember(new RelationMember(decision.getRole(),
|
---|
103 | MergeOverlapAction.getOld((Way)newPrimitive, oldWays)));
|
---|
104 | } else {
|
---|
105 | modifiedRelation.addMember(new RelationMember(decision.getRole(), newPrimitive));
|
---|
106 | }
|
---|
107 | break;
|
---|
108 | case REMOVE:
|
---|
109 | // do nothing
|
---|
110 | break;
|
---|
111 | case UNDECIDED:
|
---|
112 | // FIXME: this is an error
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public List<Command> buildResolutionCommands(OsmPrimitive newPrimitive) {
|
---|
121 | throw new UnsupportedOperationException(
|
---|
122 | "Use buildRelationCorrespondance(OsmPrimitive, Map<Relation, Relation>, Map<Way, Way>) instead");
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Builds a collection of commands executing the decisions made in this model.
|
---|
127 | *
|
---|
128 | * @param newPrimitive the primitive which members shall refer to if the
|
---|
129 | * decision is {@see RelationMemberConflictDecisionType#REPLACE}
|
---|
130 | */
|
---|
131 | public void buildRelationCorrespondance(OsmPrimitive newPrimitive, Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) {
|
---|
132 | for (Relation relation : relations) {
|
---|
133 | buildResolveCorrespondance(relation, newPrimitive, newRelations, oldWays);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|