Ignore:
Timestamp:
2012-03-29T23:22:07+02:00 (12 years ago)
Author:
simon04
Message:

fix #7513 - Warn non-experts when combining ways with conflicting tags or ways being part of relations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/conflict/tags/CombinePrimitiveResolverDialog.java

    r4310 r5132  
    44import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    55import static org.openstreetmap.josm.tools.I18n.tr;
     6import static org.openstreetmap.josm.tools.I18n.trn;
    67
    78import java.awt.BorderLayout;
     
    1617import java.beans.PropertyChangeEvent;
    1718import java.beans.PropertyChangeListener;
     19import java.util.Collection;
    1820import java.util.HashSet;
    1921import java.util.LinkedList;
     
    3032
    3133import org.openstreetmap.josm.Main;
     34import org.openstreetmap.josm.actions.ExpertToggleAction;
    3235import org.openstreetmap.josm.command.ChangePropertyCommand;
    3336import org.openstreetmap.josm.command.Command;
     37import org.openstreetmap.josm.corrector.UserCancelException;
     38import org.openstreetmap.josm.data.osm.NameFormatter;
    3439import org.openstreetmap.josm.data.osm.Node;
    3540import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    3742import org.openstreetmap.josm.data.osm.TagCollection;
    3843import org.openstreetmap.josm.data.osm.Way;
     44import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    3945import org.openstreetmap.josm.gui.DefaultNameFormatter;
    4046import org.openstreetmap.josm.gui.SideButton;
     
    4248import org.openstreetmap.josm.gui.help.HelpUtil;
    4349import org.openstreetmap.josm.tools.ImageProvider;
     50import org.openstreetmap.josm.tools.Utils;
     51import org.openstreetmap.josm.tools.Utils.Function;
    4452import org.openstreetmap.josm.tools.WindowGeometry;
    4553
     
    4755 * This dialog helps to resolve conflicts occurring when ways are combined or
    4856 * nodes are merged.
     57 *
     58 * Usage: {@link #launchIfNecessary} followed by {@link #buildResolutionCommands}.
     59 *
     60 * Prior to {@link #launchIfNecessary}, the following usage sequence was needed:
    4961 *
    5062 * There is a singleton instance of this dialog which can be retrieved using
     
    8294     *
    8395     * @return the unique instance of the dialog
     96     * @deprecated use {@link #launchIfNecessary} instead.
    8497     */
     98    @Deprecated
    8599    public static CombinePrimitiveResolverDialog getInstance() {
    86100        if (instance == null) {
     
    421435        }
    422436    }
     437
     438    public static List<Command> launchIfNecessary(
     439            final TagCollection tagsOfPrimitives,
     440            final Collection<? extends OsmPrimitive> primitives,
     441            final Collection<? extends OsmPrimitive> targetPrimitives) throws UserCancelException {
     442
     443        final TagCollection completeWayTags = new TagCollection(tagsOfPrimitives);
     444        TagConflictResolutionUtil.combineTigerTags(completeWayTags);
     445        TagConflictResolutionUtil.normalizeTagCollectionBeforeEditing(completeWayTags, primitives);
     446        final TagCollection tagsToEdit = new TagCollection(completeWayTags);
     447        TagConflictResolutionUtil.completeTagCollectionForEditing(tagsToEdit);
     448
     449        final CombinePrimitiveResolverDialog dialog = CombinePrimitiveResolverDialog.getInstance();
     450
     451        dialog.getTagConflictResolverModel().populate(tagsToEdit, completeWayTags.getKeysWithMultipleValues());
     452
     453        final Set<Relation> parentRelations = OsmPrimitive.getParentRelations(primitives);
     454        dialog.getRelationMemberConflictResolverModel().populate(parentRelations, primitives);
     455        dialog.prepareDefaultDecisions();
     456
     457        // show information dialog to non-experts
     458        if (!completeWayTags.isApplicableToPrimitive() && !ExpertToggleAction.isExpert()) {
     459            String conflicts = Utils.joinAsHtmlUnorderedList(Utils.transform(completeWayTags.getKeysWithMultipleValues(), new Function<String, String>() {
     460
     461                @Override
     462                public String apply(String key) {
     463                    return tr("{0} ({1})", key, Utils.join(tr(", "), Utils.transform(completeWayTags.getValues(key), new Function<String, String>() {
     464
     465                        @Override
     466                        public String apply(String x) {
     467                            return x == null || x.isEmpty() ? tr("<i>missing</i>") : x;
     468                        }
     469                    })));
     470                }
     471            }));
     472            String msg = tr("You are about to combine {0} objects, "
     473                    + "but the following tags are used conflictingly:<br/>{1}"
     474                    + "If these objects are combined, the resulting object may have unwanted tags.<br/>"
     475                    + "If you want to continue, you are shown a dialog to fix the conflicting tags.<br/><br/>"
     476                    + "Do you want to continue?",
     477                    primitives.size(), conflicts);
     478            if (!ConditionalOptionPaneUtil.showConfirmationDialog(
     479                    "combine_tags",
     480                    Main.parent,
     481                    "<html>" + msg + "</html>",
     482                    tr("Combine confirmation"),
     483                    JOptionPane.YES_NO_OPTION,
     484                    JOptionPane.QUESTION_MESSAGE,
     485                    JOptionPane.YES_OPTION)) {
     486                throw new UserCancelException();
     487            }
     488        }
     489
     490        if (!parentRelations.isEmpty() && !ExpertToggleAction.isExpert()) {
     491            String msg = trn("You are about to combine {1} objects, "
     492                    + "which are part of {0} relation:<br/>{2}"
     493                    + "Combining these objects may break this relation. If you are unsure, please cancel this operation.<br/>"
     494                    + "If you want to continue, you are shown a dialog to decide how to adapt the relation.<br/><br/>"
     495                    + "Do you want to continue?",
     496                    "You are about to combine {1} objects, "
     497                    + "which are part of {0} relations:<br/>{2}"
     498                    + "Combining these objects may break these relations. If you are unsure, please cancel this operation.<br/>"
     499                    + "If you want to continue, you are shown a dialog to decide how to adapt the relations.<br/><br/>"
     500                    + "Do you want to continue?",
     501                    parentRelations.size(), parentRelations.size(), primitives.size(),
     502                    DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(parentRelations));
     503            if (!ConditionalOptionPaneUtil.showConfirmationDialog(
     504                    "combine_tags",
     505                    Main.parent,
     506                    "<html>" + msg + "</html>",
     507                    tr("Combine confirmation"),
     508                    JOptionPane.YES_NO_OPTION,
     509                    JOptionPane.QUESTION_MESSAGE,
     510                    JOptionPane.YES_OPTION)) {
     511                throw new UserCancelException();
     512            }
     513        }
     514
     515        // resolve tag conflicts if necessary
     516        if (!completeWayTags.isApplicableToPrimitive() || !parentRelations.isEmpty()) {
     517            dialog.setVisible(true);
     518            if (dialog.isCanceled()) {
     519                throw new UserCancelException();
     520            }
     521        }
     522        List<Command> cmds = new LinkedList<Command>();
     523        for (OsmPrimitive i : targetPrimitives) {
     524            dialog.setTargetPrimitive(i);
     525            cmds.addAll(dialog.buildResolutionCommands());
     526        }
     527        return cmds;
     528    }
    423529}
Note: See TracChangeset for help on using the changeset viewer.