1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package mergeoverlap.hack;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.beans.PropertyChangeEvent;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
11 | import org.openstreetmap.josm.command.Command;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 | import org.openstreetmap.josm.data.osm.TagCollection;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.gui.conflict.tags.CombinePrimitiveResolverDialog;
|
---|
18 | import org.openstreetmap.josm.gui.conflict.tags.TagConflictResolverModel;
|
---|
19 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * This dialog helps to resolve conflicts occurring when ways are combined or
|
---|
23 | * nodes are merged.
|
---|
24 | *
|
---|
25 | * There is a singleton instance of this dialog which can be retrieved using
|
---|
26 | * {@link #getInstance()}.
|
---|
27 | *
|
---|
28 | * The dialog uses two models: one for resolving tag conflicts, the other
|
---|
29 | * for resolving conflicts in relation memberships. For both models there are accessors,
|
---|
30 | * i.e {@link #getTagConflictResolverModel()} and {@link #getRelationMemberConflictResolverModel()}.
|
---|
31 | *
|
---|
32 | * Models have to be <strong>populated</strong> before the dialog is launched. Example:
|
---|
33 | * <pre>
|
---|
34 | * CombinePrimitiveResolverDialog dialog = CombinePrimitiveResolverDialog.getInstance();
|
---|
35 | * dialog.getTagConflictResolverModel().populate(aTagCollection);
|
---|
36 | * dialog.getRelationMemberConflictResolverModel().populate(aRelationLinkCollection);
|
---|
37 | * dialog.prepareDefaultDecisions();
|
---|
38 | * </pre>
|
---|
39 | *
|
---|
40 | * You should also set the target primitive which other primitives (ways or nodes) are
|
---|
41 | * merged to, see {@link #setTargetPrimitive(OsmPrimitive)}.
|
---|
42 | *
|
---|
43 | * After the dialog is closed use {@link #isCanceled()} to check whether the user canceled
|
---|
44 | * the dialog. If it wasn't canceled you may build a collection of {@link Command} objects
|
---|
45 | * which reflect the conflict resolution decisions the user made in the dialog:
|
---|
46 | * see {@link #buildResolutionCommands()}
|
---|
47 | */
|
---|
48 | public class MyCombinePrimitiveResolverDialog extends CombinePrimitiveResolverDialog {
|
---|
49 |
|
---|
50 | /** the unique instance of the dialog */
|
---|
51 | private static MyCombinePrimitiveResolverDialog instance;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Constructs a new {@code MyCombinePrimitiveResolverDialog}.
|
---|
55 | * @param parent The parent component in which this dialog will be displayed.
|
---|
56 | */
|
---|
57 | public MyCombinePrimitiveResolverDialog(Component parent) {
|
---|
58 | super(parent, new TagConflictResolverModel(), new MyRelationMemberConflictResolverModel());
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Replies the unique instance of the dialog
|
---|
63 | *
|
---|
64 | * @return the unique instance of the dialog
|
---|
65 | */
|
---|
66 | public static MyCombinePrimitiveResolverDialog getInstance() {
|
---|
67 | if (instance == null) {
|
---|
68 | GuiHelper.runInEDTAndWait(() -> instance = new MyCombinePrimitiveResolverDialog(MainApplication.getMainFrame()));
|
---|
69 | }
|
---|
70 | return instance;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | protected ApplyAction buildApplyAction() {
|
---|
75 | return new ApplyAction() {
|
---|
76 | @Override
|
---|
77 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
78 | super.propertyChange(evt);
|
---|
79 | if (evt.getPropertyName().equals(MyRelationMemberConflictResolverModel.NUM_CONFLICTS_PROP)) {
|
---|
80 | updateEnabledState();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | };
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Replies the relation membership conflict resolver model.
|
---|
88 | * @return The relation membership conflict resolver model.
|
---|
89 | */
|
---|
90 | @Override
|
---|
91 | public MyRelationMemberConflictResolverModel getRelationMemberConflictResolverModel() {
|
---|
92 | return (MyRelationMemberConflictResolverModel) pnlRelationMemberConflictResolver.getModel();
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Replies the list of {@link Command commands} needed to apply resolution choices.
|
---|
97 | * @return The list of {@link Command commands} needed to apply resolution choices.
|
---|
98 | */
|
---|
99 | public List<Command> buildWayResolutionCommands() {
|
---|
100 | List<Command> cmds = new LinkedList<>();
|
---|
101 |
|
---|
102 | TagCollection allResolutions = getTagConflictResolverModel().getAllResolutions();
|
---|
103 | if (!allResolutions.isEmpty()) {
|
---|
104 | cmds.addAll(buildTagChangeCommand(targetPrimitive, allResolutions));
|
---|
105 | }
|
---|
106 | if (targetPrimitive.get("created_by") != null) {
|
---|
107 | cmds.add(new ChangePropertyCommand(targetPrimitive, "created_by", null));
|
---|
108 | }
|
---|
109 |
|
---|
110 | Command cmd = pnlRelationMemberConflictResolver.buildTagApplyCommands(getRelationMemberConflictResolverModel()
|
---|
111 | .getModifiedRelations(targetPrimitive));
|
---|
112 | if (cmd != null) {
|
---|
113 | cmds.add(cmd);
|
---|
114 | }
|
---|
115 | return cmds;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void buildRelationCorrespondance(Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) {
|
---|
119 | getRelationMemberConflictResolverModel().buildRelationCorrespondance(targetPrimitive, newRelations, oldWays);
|
---|
120 | }
|
---|
121 | }
|
---|