Changeset 5082 in josm for trunk


Ignore:
Timestamp:
2012-03-14T21:39:27+01:00 (13 years ago)
Author:
simon04
Message:

fix #5395 - add "Add selection to relation" to popup menu of relation toggle dialog

Location:
trunk/src/org/openstreetmap/josm/gui/dialogs
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java

    r5028 r5082  
    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.Point;
     
    2627import javax.swing.JList;
    2728import javax.swing.JMenuItem;
    28 import javax.swing.KeyStroke;
    2929import javax.swing.ListSelectionModel;
    3030import javax.swing.SwingUtilities;
     
    3434
    3535import org.openstreetmap.josm.Main;
     36import org.openstreetmap.josm.command.Command;
     37import org.openstreetmap.josm.command.SequenceCommand;
     38import org.openstreetmap.josm.data.SelectionChangedListener;
     39import org.openstreetmap.josm.data.osm.DataSet;
    3640import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3741import org.openstreetmap.josm.data.osm.Relation;
     
    5559import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
    5660import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
     61import org.openstreetmap.josm.gui.dialogs.relation.GenericRelationEditor;
    5762import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
    5863import org.openstreetmap.josm.gui.layer.Layer;
     
    8085    private DeleteAction deleteAction;
    8186    private NewAction newAction;
     87    private AddToRelation addToRelation;
    8288    /** the popup menu */
    8389    private RelationDialogPopupMenu popupMenu;
     
    145151        //displaylist.getActionMap().put("deleteRelation", deleteAction);
    146152
     153        addToRelation = new AddToRelation();
    147154        popupMenu = new RelationDialogPopupMenu(displaylist);
    148155    }
     
    152159        newAction.updateEnabledState();
    153160        DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IN_EDT);
     161        DataSet.addSelectionListener(addToRelation);
    154162        dataChanged(null);
    155163    }
     
    158166        MapView.removeLayerChangeListener(newAction);
    159167        DatasetEventManager.getInstance().removeDatasetListener(this);
     168        DataSet.removeSelectionListener(addToRelation);
    160169    }
    161170
     
    578587        public void valueChanged(ListSelectionEvent e) {
    579588            updateEnabledState();
     589        }
     590    }
     591
     592    class AddToRelation extends AbstractAction implements ListSelectionListener, SelectionChangedListener {
     593
     594        public AddToRelation() {
     595            super("", ImageProvider.get("dialogs/conflict", "copyendright"));
     596            putValue(SHORT_DESCRIPTION, tr("Add all objects selected in the current dataset after the last member"));
     597            setEnabled(false);
     598        }
     599
     600        @Override
     601        public void actionPerformed(ActionEvent e) {
     602            Collection<Command> cmds = new LinkedList<Command>();
     603            for (Relation orig : getSelectedRelations()) {
     604                Command c = GenericRelationEditor.addPrimitivesToRelation(orig, Main.main.getCurrentDataSet().getSelected());
     605                if (c != null) {
     606                    cmds.add(c);
     607                }
     608            }
     609            if (!cmds.isEmpty()) {
     610                Main.main.undoRedo.add(new SequenceCommand(tr("Add selection to relation"), cmds));
     611            }
     612        }
     613
     614        @Override
     615        public void valueChanged(ListSelectionEvent e) {
     616            putValue(NAME, trn("Add selection to {0} relation", "Add selection to {0} relations",
     617                    getSelectedRelations().size(), getSelectedRelations().size()));
     618        }
     619
     620        @Override
     621        public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
     622            setEnabled(newSelection != null && !newSelection.isEmpty());
    580623        }
    581624    }
     
    795838
    796839            // -- download members action
    797             //
    798840            add(new DownloadMembersAction());
    799841
    800842            // -- download incomplete members action
    801             //
    802843            add(new DownloadSelectedIncompleteMembersAction());
    803844
     
    805846
    806847            // -- select members action
    807             //
    808848            add(new SelectMembersAction(false));
    809849            add(new SelectMembersAction(true));
    810850
    811851            // -- select action
    812             //
    813852            add(new SelectAction(false));
    814853            add(new SelectAction(true));
     854
     855            addSeparator();
     856
     857            add(addToRelation);
    815858        }
    816859    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java

    r4982 r5082  
    4747import org.openstreetmap.josm.command.AddCommand;
    4848import org.openstreetmap.josm.command.ChangeCommand;
     49import org.openstreetmap.josm.command.Command;
    4950import org.openstreetmap.josm.command.ConflictAddCommand;
    5051import org.openstreetmap.josm.data.conflict.Conflict;
     
    688689    }
    689690
    690     static class AddAbortException extends Exception  {
    691     }
    692 
    693     abstract class  AddFromSelectionAction extends AbstractAction {
     691    static class AddAbortException extends Exception {
     692    }
     693
     694    static boolean confirmAddingPrimtive(OsmPrimitive primitive) throws AddAbortException {
     695        String msg = tr("<html>This relation already has one or more members referring to<br>"
     696                + "the object ''{0}''<br>"
     697                + "<br>"
     698                + "Do you really want to add another relation member?</html>",
     699                primitive.getDisplayName(DefaultNameFormatter.getInstance())
     700            );
     701        int ret = ConditionalOptionPaneUtil.showOptionDialog(
     702                "add_primitive_to_relation",
     703                Main.parent,
     704                msg,
     705                tr("Multiple members referring to same object."),
     706                JOptionPane.YES_NO_CANCEL_OPTION,
     707                JOptionPane.WARNING_MESSAGE,
     708                null,
     709                null
     710        );
     711        switch(ret) {
     712        case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION : return true;
     713        case JOptionPane.YES_OPTION: return true;
     714        case JOptionPane.NO_OPTION: return false;
     715        case JOptionPane.CLOSED_OPTION: return false;
     716        case JOptionPane.CANCEL_OPTION: throw new AddAbortException();
     717        }
     718        // should not happen
     719        return false;
     720    }
     721
     722    static void warnOfCircularReferences(OsmPrimitive primitive) {
     723        String msg = tr("<html>You are trying to add a relation to itself.<br>"
     724                + "<br>"
     725                + "This creates circular references and is therefore discouraged.<br>"
     726                + "Skipping relation ''{0}''.</html>",
     727                primitive.getDisplayName(DefaultNameFormatter.getInstance()));
     728        JOptionPane.showMessageDialog(
     729                Main.parent,
     730                msg,
     731                tr("Warning"),
     732                JOptionPane.WARNING_MESSAGE);
     733    }
     734
     735    public static Command addPrimitivesToRelation(final Relation orig, Collection<? extends OsmPrimitive> primitivesToAdd) {
     736        try {
     737            Relation relation = new Relation(orig);
     738            boolean modified = false;
     739            for (OsmPrimitive p : primitivesToAdd) {
     740                if (p instanceof Relation && orig != null && orig.equals(p)) {
     741                    warnOfCircularReferences(p);
     742                    continue;
     743                } else if (MemberTableModel.hasMembersReferringTo(relation.getMembers(), Collections.singleton(p))
     744                        && !confirmAddingPrimtive(p)) {
     745                    continue;
     746                }
     747                relation.addMember(new RelationMember("", p));
     748                modified = true;
     749            }
     750            return modified ? new ChangeCommand(orig, relation) : null;
     751        } catch (AddAbortException ign) {
     752            return null;
     753        }
     754    }
     755
     756    abstract class AddFromSelectionAction extends AbstractAction {
    694757        protected boolean isPotentialDuplicate(OsmPrimitive primitive) {
    695758            return memberTableModel.hasMembersReferringTo(Collections.singleton(primitive));
    696         }
    697 
    698         protected boolean confirmAddingPrimtive(OsmPrimitive primitive)  throws AddAbortException {
    699             String msg = tr("<html>This relation already has one or more members referring to<br>"
    700                     + "the object ''{0}''<br>"
    701                     + "<br>"
    702                     + "Do you really want to add another relation member?</html>",
    703                     primitive.getDisplayName(DefaultNameFormatter.getInstance())
    704             );
    705             int ret = ConditionalOptionPaneUtil.showOptionDialog(
    706                     "add_primitive_to_relation",
    707                     Main.parent,
    708                     msg,
    709                     tr("Multiple members referring to same object."),
    710                     JOptionPane.YES_NO_CANCEL_OPTION,
    711                     JOptionPane.WARNING_MESSAGE,
    712                     null,
    713                     null
    714             );
    715             switch(ret) {
    716             case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION : return true;
    717             case JOptionPane.YES_OPTION: return true;
    718             case JOptionPane.NO_OPTION: return false;
    719             case JOptionPane.CLOSED_OPTION: return false;
    720             case JOptionPane.CANCEL_OPTION: throw new AddAbortException();
    721             }
    722             // should not happen
    723             return false;
    724         }
    725 
    726         protected void warnOfCircularReferences(OsmPrimitive primitive) {
    727             String msg = tr("<html>You are trying to add a relation to itself.<br>"
    728                     + "<br>"
    729                     + "This creates circular references and is therefore discouraged.<br>"
    730                     + "Skipping relation ''{0}''.</html>",
    731                     primitive.getDisplayName(DefaultNameFormatter.getInstance())
    732             );
    733             JOptionPane.showMessageDialog(
    734                     Main.parent,
    735                     msg,
    736                     tr("Warning"),
    737                     JOptionPane.WARNING_MESSAGE
    738             );
    739759        }
    740760
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

    r4792 r5082  
    560560
    561561    /**
    562      * Replies true if there is at least one relation member in this model
    563      * which refers to at least on the primitives in <code>primitives</code>.
    564      *
     562     * Replies true if there is at least one relation member given as {@code members}
     563     * which refers to at least on the primitives in {@code primitives}.
     564     *
     565     * @param members the members
    565566     * @param primitives the collection of primitives
    566567     * @return true if there is at least one relation member in this model
     
    568569     * otherwise
    569570     */
     571    public static boolean hasMembersReferringTo(Collection<RelationMember> members, Collection<OsmPrimitive> primitives) {
     572        if (primitives == null || primitives.isEmpty()) {
     573            return false;
     574        }
     575        HashSet<OsmPrimitive> referrers = new HashSet<OsmPrimitive>();
     576        for (RelationMember member : members) {
     577            referrers.add(member.getMember());
     578        }
     579        for (OsmPrimitive referred : primitives) {
     580            if (referrers.contains(referred)) {
     581                return true;
     582            }
     583        }
     584        return false;
     585    }
     586
     587    /**
     588     * Replies true if there is at least one relation member in this model
     589     * which refers to at least on the primitives in <code>primitives</code>.
     590     *
     591     * @param primitives the collection of primitives
     592     * @return true if there is at least one relation member in this model
     593     * which refers to at least on the primitives in <code>primitives</code>; false
     594     * otherwise
     595     */
    570596    public boolean hasMembersReferringTo(Collection<OsmPrimitive> primitives) {
    571         if (primitives == null || primitives.isEmpty())
    572             return false;
    573         HashSet<OsmPrimitive> referrers = new HashSet<OsmPrimitive>();
    574         for(RelationMember member : members) {
    575             referrers.add(member.getMember());
    576         }
    577         Iterator<OsmPrimitive> it = primitives.iterator();
    578         while(it.hasNext()) {
    579             OsmPrimitive referred = it.next();
    580             if (referrers.contains(referred))
    581                 return true;
    582         }
    583         return false;
     597        return hasMembersReferringTo(members, primitives);
    584598    }
    585599
Note: See TracChangeset for help on using the changeset viewer.