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

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

[josm-merge_overlap] fix sonar issues

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