001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.conflict.pair.properties;
003
004import static org.openstreetmap.josm.gui.conflict.pair.MergeDecisionType.UNDECIDED;
005
006import java.beans.PropertyChangeListener;
007import java.beans.PropertyChangeSupport;
008import java.util.ArrayList;
009import java.util.Collections;
010import java.util.List;
011
012import org.openstreetmap.josm.command.Command;
013import org.openstreetmap.josm.command.conflict.CoordinateConflictResolveCommand;
014import org.openstreetmap.josm.command.conflict.DeletedStateConflictResolveCommand;
015import org.openstreetmap.josm.data.conflict.Conflict;
016import org.openstreetmap.josm.data.coor.ILatLon;
017import org.openstreetmap.josm.data.coor.LatLon;
018import org.openstreetmap.josm.data.osm.Node;
019import org.openstreetmap.josm.data.osm.OsmPrimitive;
020import org.openstreetmap.josm.gui.conflict.pair.MergeDecisionType;
021import org.openstreetmap.josm.gui.util.ChangeNotifier;
022import org.openstreetmap.josm.tools.CheckParameterUtil;
023
024/**
025 * This is the model for resolving conflicts in the properties of the
026 * {@link OsmPrimitive}s. In particular, it represents conflicts in the coordinates of {@link Node}s and
027 * the deleted or visible state of {@link OsmPrimitive}s.
028 *
029 * This model is a {@link ChangeNotifier}. It notifies registered {@link javax.swing.event.ChangeListener}s whenever the
030 * internal state changes.
031 *
032 * This model also emits property changes for {@link #RESOLVED_COMPLETELY_PROP}. Property change
033 * listeners may register themselves using {@link #addPropertyChangeListener(PropertyChangeListener)}.
034 *
035 * @see Node#getCoor()
036 * @see OsmPrimitive#isDeleted
037 * @see OsmPrimitive#isVisible
038 *
039 */
040public class PropertiesMergeModel extends ChangeNotifier {
041
042    public static final String RESOLVED_COMPLETELY_PROP = PropertiesMergeModel.class.getName() + ".resolvedCompletely";
043    public static final String DELETE_PRIMITIVE_PROP = PropertiesMergeModel.class.getName() + ".deletePrimitive";
044
045    private OsmPrimitive my;
046
047    private LatLon myCoords;
048    private LatLon theirCoords;
049    private MergeDecisionType coordMergeDecision;
050
051    private boolean myDeletedState;
052    private boolean theirDeletedState;
053    private List<OsmPrimitive> myReferrers;
054    private List<OsmPrimitive> theirReferrers;
055    private MergeDecisionType deletedMergeDecision;
056    private final PropertyChangeSupport support;
057    private Boolean resolvedCompletely;
058
059    public void addPropertyChangeListener(PropertyChangeListener listener) {
060        support.addPropertyChangeListener(listener);
061    }
062
063    public void removePropertyChangeListener(PropertyChangeListener listener) {
064        support.removePropertyChangeListener(listener);
065    }
066
067    public void fireCompletelyResolved() {
068        Boolean oldValue = resolvedCompletely;
069        resolvedCompletely = isResolvedCompletely();
070        support.firePropertyChange(RESOLVED_COMPLETELY_PROP, oldValue, resolvedCompletely);
071    }
072
073    /**
074     * Constructs a new {@code PropertiesMergeModel}.
075     */
076    public PropertiesMergeModel() {
077        coordMergeDecision = UNDECIDED;
078        deletedMergeDecision = UNDECIDED;
079        support = new PropertyChangeSupport(this);
080        resolvedCompletely = null;
081    }
082
083    /**
084     * replies true if there is a coordinate conflict and if this conflict is resolved
085     *
086     * @return true if there is a coordinate conflict and if this conflict is resolved; false, otherwise
087     */
088    public boolean isDecidedCoord() {
089        return coordMergeDecision != UNDECIDED;
090    }
091
092    /**
093     * replies true if there is a  conflict in the deleted state and if this conflict is resolved
094     *
095     * @return true if there is a conflict in the deleted state and if this conflict is
096     * resolved; false, otherwise
097     */
098    public boolean isDecidedDeletedState() {
099        return deletedMergeDecision != UNDECIDED;
100    }
101
102    /**
103     * replies true if the current decision for the coordinate conflict is <code>decision</code>
104     * @param decision conflict resolution decision
105     *
106     * @return true if the current decision for the coordinate conflict is <code>decision</code>;
107     *  false, otherwise
108     */
109    public boolean isCoordMergeDecision(MergeDecisionType decision) {
110        return coordMergeDecision == decision;
111    }
112
113    /**
114     * replies true if the current decision for the deleted state conflict is <code>decision</code>
115     * @param decision conflict resolution decision
116     *
117     * @return true if the current decision for the deleted state conflict is <code>decision</code>;
118     *  false, otherwise
119     */
120    public boolean isDeletedStateDecision(MergeDecisionType decision) {
121        return deletedMergeDecision == decision;
122    }
123
124    /**
125     * Populates the model with the differences between local and server version
126     *
127     * @param conflict The conflict information
128     */
129    public void populate(Conflict<? extends OsmPrimitive> conflict) {
130        this.my = conflict.getMy();
131        OsmPrimitive their = conflict.getTheir();
132        if (my instanceof Node) {
133            myCoords = ((Node) my).getCoor();
134            theirCoords = ((Node) their).getCoor();
135        } else {
136            myCoords = null;
137            theirCoords = null;
138        }
139
140        myDeletedState = conflict.isMyDeleted() || my.isDeleted();
141        theirDeletedState = their.isDeleted();
142
143        myReferrers = my.getDataSet() == null ? Collections.<OsmPrimitive>emptyList() : my.getReferrers();
144        theirReferrers = their.getDataSet() == null ? Collections.<OsmPrimitive>emptyList() : their.getReferrers();
145
146        coordMergeDecision = UNDECIDED;
147        deletedMergeDecision = UNDECIDED;
148        fireStateChanged();
149        fireCompletelyResolved();
150    }
151
152    /**
153     * replies the coordinates of my {@link OsmPrimitive}. null, if my primitive hasn't
154     * coordinates (i.e. because it is a {@link org.openstreetmap.josm.data.osm.Way}).
155     *
156     * @return the coordinates of my {@link OsmPrimitive}. null, if my primitive hasn't
157     *  coordinates (i.e. because it is a {@link org.openstreetmap.josm.data.osm.Way}).
158     */
159    public LatLon getMyCoords() {
160        return myCoords;
161    }
162
163    /**
164     * replies the coordinates of their {@link OsmPrimitive}. null, if their primitive hasn't
165     * coordinates (i.e. because it is a {@link org.openstreetmap.josm.data.osm.Way}).
166     *
167     * @return the coordinates of my {@link OsmPrimitive}. null, if my primitive hasn't
168     * coordinates (i.e. because it is a {@link org.openstreetmap.josm.data.osm.Way}).
169     */
170    public LatLon getTheirCoords() {
171        return theirCoords;
172    }
173
174    /**
175     * replies the coordinates of the merged {@link OsmPrimitive}. null, if the current primitives
176     * have no coordinates or if the conflict is yet {@link MergeDecisionType#UNDECIDED}
177     *
178     * @return the coordinates of the merged {@link OsmPrimitive}. null, if the current primitives
179     * have no coordinates or if the conflict is yet {@link MergeDecisionType#UNDECIDED}
180     */
181    public LatLon getMergedCoords() {
182        switch (coordMergeDecision) {
183        case KEEP_MINE:
184            return myCoords;
185        case KEEP_THEIR:
186            return theirCoords;
187        default:
188            return null;
189        }
190    }
191
192    /**
193     * Decides a conflict between local and server coordinates
194     *
195     * @param decision the decision
196     */
197    public void decideCoordsConflict(MergeDecisionType decision) {
198        coordMergeDecision = decision;
199        fireStateChanged();
200        fireCompletelyResolved();
201    }
202
203    /**
204     * Replies deleted state of local dataset
205     * @return The state of deleted flag
206     */
207    public Boolean getMyDeletedState() {
208        return myDeletedState;
209    }
210
211    /**
212     * Replies deleted state of Server dataset
213     * @return The state of deleted flag
214     */
215    public Boolean getTheirDeletedState() {
216        return theirDeletedState;
217    }
218
219    /**
220     * Replies deleted state of combined dataset
221     * @return The state of deleted flag
222     */
223    public Boolean getMergedDeletedState() {
224        switch (deletedMergeDecision) {
225        case KEEP_MINE:
226            return myDeletedState;
227        case KEEP_THEIR:
228            return theirDeletedState;
229        default:
230            return null;
231        }
232    }
233
234    /**
235     * Returns local referrers
236     * @return The referrers
237     */
238    public List<OsmPrimitive> getMyReferrers() {
239        return myReferrers;
240    }
241
242    /**
243     * Returns server referrers
244     * @return The referrers
245     */
246    public List<OsmPrimitive> getTheirReferrers() {
247        return theirReferrers;
248    }
249
250    private boolean getMergedDeletedState(MergeDecisionType decision) {
251        switch (decision) {
252        case KEEP_MINE:
253            return myDeletedState;
254        case KEEP_THEIR:
255            return theirDeletedState;
256        default:
257            return false;
258        }
259    }
260
261    /**
262     * decides the conflict between two deleted states
263     * @param decision the decision (must not be null)
264     *
265     * @throws IllegalArgumentException if decision is null
266     */
267    public void decideDeletedStateConflict(MergeDecisionType decision) {
268        CheckParameterUtil.ensureParameterNotNull(decision, "decision");
269
270        boolean oldMergedDeletedState = getMergedDeletedState(this.deletedMergeDecision);
271        boolean newMergedDeletedState = getMergedDeletedState(decision);
272
273        this.deletedMergeDecision = decision;
274        fireStateChanged();
275        fireCompletelyResolved();
276
277        if (oldMergedDeletedState != newMergedDeletedState) {
278            support.firePropertyChange(DELETE_PRIMITIVE_PROP, oldMergedDeletedState, newMergedDeletedState);
279        }
280    }
281
282    /**
283     * replies true if my and their primitive have a conflict between
284     * their coordinate values
285     *
286     * @return true if my and their primitive have a conflict between
287     * their coordinate values; false otherwise
288     */
289    public boolean hasCoordConflict() {
290        if (myCoords == null && theirCoords != null) return true;
291        if (myCoords != null && theirCoords == null) return true;
292        if (myCoords == null && theirCoords == null) return false;
293        return myCoords != null && !myCoords.equalsEpsilon(theirCoords, ILatLon.MAX_SERVER_PRECISION);
294    }
295
296    /**
297     * replies true if my and their primitive have a conflict between
298     * their deleted states
299     *
300     * @return <code>true</code> if my and their primitive have a conflict between
301     * their deleted states
302     */
303    public boolean hasDeletedStateConflict() {
304        return myDeletedState != theirDeletedState;
305    }
306
307    /**
308     * replies true if all conflict in this model are resolved
309     *
310     * @return <code>true</code> if all conflict in this model are resolved; <code>false</code> otherwise
311     */
312    public boolean isResolvedCompletely() {
313        boolean ret = true;
314        if (hasCoordConflict()) {
315            ret = ret && coordMergeDecision != UNDECIDED;
316        }
317        if (hasDeletedStateConflict()) {
318            ret = ret && deletedMergeDecision != UNDECIDED;
319        }
320        return ret;
321    }
322
323    /**
324     * Builds the command(s) to apply the conflict resolutions to my primitive
325     *
326     * @param conflict The conflict information
327     * @return The list of commands
328     */
329    public List<Command> buildResolveCommand(Conflict<? extends OsmPrimitive> conflict) {
330        List<Command> cmds = new ArrayList<>();
331        if (hasCoordConflict() && isDecidedCoord()) {
332            cmds.add(new CoordinateConflictResolveCommand(conflict, coordMergeDecision));
333        }
334        if (hasDeletedStateConflict() && isDecidedDeletedState()) {
335            cmds.add(new DeletedStateConflictResolveCommand(conflict, deletedMergeDecision));
336        }
337        return cmds;
338    }
339
340    public OsmPrimitive getMyPrimitive() {
341        return my;
342    }
343
344}