| 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 javax.swing.JPanel;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.Main;
|
|---|
| 13 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
|---|
| 14 | import org.openstreetmap.josm.command.Command;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.TagCollection;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 19 | import org.openstreetmap.josm.gui.conflict.tags.CombinePrimitiveResolverDialog;
|
|---|
| 20 | import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictResolver;
|
|---|
| 21 | import 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 | */
|
|---|
| 50 | public class MyCombinePrimitiveResolverDialog extends CombinePrimitiveResolverDialog {
|
|---|
| 51 |
|
|---|
| 52 | /** the unique instance of the dialog */
|
|---|
| 53 | private static MyCombinePrimitiveResolverDialog instance;
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Constructs a new {@code MyCombinePrimitiveResolverDialog}.
|
|---|
| 57 | * @param parent The parent component in which this dialog will be displayed.
|
|---|
| 58 | */
|
|---|
| 59 | public MyCombinePrimitiveResolverDialog(Component parent) {
|
|---|
| 60 | super(parent);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Replies the unique instance of the dialog
|
|---|
| 65 | *
|
|---|
| 66 | * @return the unique instance of the dialog
|
|---|
| 67 | */
|
|---|
| 68 | public static MyCombinePrimitiveResolverDialog getInstance() {
|
|---|
| 69 | if (instance == null) {
|
|---|
| 70 | GuiHelper.runInEDTAndWait(() -> instance = new MyCombinePrimitiveResolverDialog(Main.parent));
|
|---|
| 71 | }
|
|---|
| 72 | return instance;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | @Override
|
|---|
| 76 | protected JPanel buildRelationMemberConflictResolverPanel() {
|
|---|
| 77 | pnlRelationMemberConflictResolver = new RelationMemberConflictResolver(new MyRelationMemberConflictResolverModel());
|
|---|
| 78 | return pnlRelationMemberConflictResolver;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | @Override
|
|---|
| 82 | protected ApplyAction buildApplyAction() {
|
|---|
| 83 | return new ApplyAction() {
|
|---|
| 84 | @Override
|
|---|
| 85 | public void propertyChange(PropertyChangeEvent evt) {
|
|---|
| 86 | super.propertyChange(evt);
|
|---|
| 87 | if (evt.getPropertyName().equals(MyRelationMemberConflictResolverModel.NUM_CONFLICTS_PROP)) {
|
|---|
| 88 | updateEnabledState();
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | };
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Replies the relation membership conflict resolver model.
|
|---|
| 96 | * @return The relation membership conflict resolver model.
|
|---|
| 97 | */
|
|---|
| 98 | @Override
|
|---|
| 99 | public MyRelationMemberConflictResolverModel getRelationMemberConflictResolverModel() {
|
|---|
| 100 | return (MyRelationMemberConflictResolverModel) pnlRelationMemberConflictResolver.getModel();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Replies the list of {@link Command commands} needed to apply resolution choices.
|
|---|
| 105 | * @return The list of {@link Command commands} needed to apply resolution choices.
|
|---|
| 106 | */
|
|---|
| 107 | public List<Command> buildWayResolutionCommands() {
|
|---|
| 108 | List<Command> cmds = new LinkedList<>();
|
|---|
| 109 |
|
|---|
| 110 | TagCollection allResolutions = getTagConflictResolverModel().getAllResolutions();
|
|---|
| 111 | if (!allResolutions.isEmpty()) {
|
|---|
| 112 | cmds.addAll(buildTagChangeCommand(targetPrimitive, allResolutions));
|
|---|
| 113 | }
|
|---|
| 114 | if (targetPrimitive.get("created_by") != null) {
|
|---|
| 115 | cmds.add(new ChangePropertyCommand(targetPrimitive, "created_by", null));
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | Command cmd = pnlRelationMemberConflictResolver.buildTagApplyCommands(getRelationMemberConflictResolverModel()
|
|---|
| 119 | .getModifiedRelations(targetPrimitive));
|
|---|
| 120 | if (cmd != null) {
|
|---|
| 121 | cmds.add(cmd);
|
|---|
| 122 | }
|
|---|
| 123 | return cmds;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | public void buildRelationCorrespondance(Map<Relation, Relation> newRelations, Map<Way, Way> oldWays) {
|
|---|
| 127 | getRelationMemberConflictResolverModel().buildRelationCorrespondance(targetPrimitive, newRelations, oldWays);
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|