Changeset 1839 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2009-07-25T17:18:21+02:00 (15 years ago)
Author:
Gubaer
Message:

fixed #3022: Add confirmation dialog if adding way twice in a relation

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

Legend:

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

    r1832 r1839  
    2020import java.awt.event.WindowAdapter;
    2121import java.awt.event.WindowEvent;
     22import java.beans.PropertyChangeEvent;
     23import java.beans.PropertyChangeListener;
    2224import java.io.IOException;
    2325import java.util.ArrayList;
    2426import java.util.Collection;
     27import java.util.Collections;
     28import java.util.HashSet;
     29import java.util.Iterator;
     30import java.util.List;
    2531import java.util.logging.Logger;
    2632
     
    4046import javax.swing.KeyStroke;
    4147import javax.swing.SwingUtilities;
     48import javax.swing.event.AncestorEvent;
     49import javax.swing.event.AncestorListener;
    4250import javax.swing.event.ChangeEvent;
    4351import javax.swing.event.ChangeListener;
     
    6068import org.openstreetmap.josm.data.osm.RelationMember;
    6169import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
     70import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    6271import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     72import org.openstreetmap.josm.gui.PrimitiveNameFormatter;
    6373import org.openstreetmap.josm.gui.SideButton;
    6474import org.openstreetmap.josm.gui.dialogs.relation.ac.AutoCompletionCache;
     
    310320     */
    311321    protected JPanel buildMemberEditorPanel() {
    312         JPanel pnl = new JPanel();
     322        final JPanel pnl = new JPanel();
    313323        pnl.setLayout(new GridBagLayout());
    314324        // setting up the member table
     
    403413            }
    404414        });
     415
    405416
    406417        JPanel pnl3 = new JPanel();
     
    617628    }
    618629
    619     class AddSelectedAtStartAction extends AbstractAction implements TableModelListener {
     630    class AddAbortException extends Exception  {
     631    }
     632
     633    abstract class  AddFromSelectionAction extends AbstractAction {
     634        private PrimitiveNameFormatter nameFormatter = new PrimitiveNameFormatter();
     635
     636        protected boolean isPotentialDuplicate(OsmPrimitive primitive) {
     637            return memberTableModel.hasMembersReferringTo(Collections.singleton(primitive));
     638        }
     639
     640        protected boolean confirmAddingPrimtive(OsmPrimitive primitive)  throws AddAbortException {
     641            String msg = tr("<html>This relation already has one or more members referring to<br>"
     642                    + "the primitive ''{0}''<br>"
     643                    + "<br>"
     644                    + "Do you really want to add another relation member?</html>",
     645                    nameFormatter.getName(primitive)
     646            );
     647            int ret = ConditionalOptionPaneUtil.showOptionDialog(
     648                    "add_primitive_to_relation",
     649                    Main.parent,
     650                    msg,
     651                    tr("Multiple members referring to same primitive"),
     652                    JOptionPane.YES_NO_CANCEL_OPTION,
     653                    JOptionPane.WARNING_MESSAGE,
     654                    null,
     655                    null
     656            );
     657            switch(ret) {
     658            case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION : return true;
     659            case JOptionPane.YES_OPTION: return true;
     660            case JOptionPane.NO_OPTION: return false;
     661            case JOptionPane.CLOSED_OPTION: return false;
     662            case JOptionPane.CANCEL_OPTION: throw new AddAbortException();
     663            }
     664            // should not happen
     665            return false;
     666        }
     667
     668        protected List<OsmPrimitive> filterConfirmedPrimitives(List<OsmPrimitive> primitives) throws AddAbortException {
     669            if (primitives == null || primitives.isEmpty())
     670                return primitives;
     671            ArrayList<OsmPrimitive> ret = new ArrayList<OsmPrimitive>();
     672            Iterator<OsmPrimitive> it = primitives.iterator();
     673            while(it.hasNext()) {
     674                OsmPrimitive primitive = it.next();
     675                if (isPotentialDuplicate(primitive) && confirmAddingPrimtive(primitive)) {
     676                    ret.add(primitive);
     677                }
     678            }
     679            return ret;
     680        }
     681    }
     682
     683    class AddSelectedAtStartAction extends AddFromSelectionAction implements TableModelListener {
    620684        public AddSelectedAtStartAction() {
    621685            putValue(SHORT_DESCRIPTION,
     
    631695
    632696        public void actionPerformed(ActionEvent e) {
    633             memberTableModel.addMembersAtBeginning(selectionTableModel.getSelection());
     697            try {
     698                List<OsmPrimitive> toAdd = filterConfirmedPrimitives(selectionTableModel.getSelection());
     699                memberTableModel.addMembersAtBeginning(toAdd);
     700            } catch(AddAbortException ex) {
     701                // do nothing
     702            }
    634703        }
    635704
     
    639708    }
    640709
    641     class AddSelectedAtEndAction extends AbstractAction implements TableModelListener {
     710    class AddSelectedAtEndAction extends AddFromSelectionAction implements TableModelListener {
    642711        public AddSelectedAtEndAction() {
    643712            putValue(SHORT_DESCRIPTION, tr("Add all primitives selected in the current dataset after the last member"));
     
    652721
    653722        public void actionPerformed(ActionEvent e) {
    654             memberTableModel.addMembersAtEnd(selectionTableModel.getSelection());
     723            try {
     724                List<OsmPrimitive> toAdd = filterConfirmedPrimitives(selectionTableModel.getSelection());
     725                memberTableModel.addMembersAtEnd(toAdd);
     726            } catch(AddAbortException ex) {
     727                // do nothing
     728            }
    655729        }
    656730
     
    660734    }
    661735
    662     class AddSelectedBeforeSelection extends AbstractAction implements TableModelListener, ListSelectionListener {
     736    class AddSelectedBeforeSelection extends AddFromSelectionAction implements TableModelListener, ListSelectionListener {
    663737        public AddSelectedBeforeSelection() {
    664738            putValue(SHORT_DESCRIPTION,
     
    675749
    676750        public void actionPerformed(ActionEvent e) {
    677             memberTableModel.addMembersBeforeIdx(selectionTableModel.getSelection(), memberTableModel
    678                     .getSelectionModel().getMinSelectionIndex());
     751            try {
     752                List<OsmPrimitive> toAdd = filterConfirmedPrimitives(selectionTableModel.getSelection());
     753                memberTableModel.addMembersBeforeIdx(toAdd, memberTableModel
     754                        .getSelectionModel().getMinSelectionIndex());
     755            } catch(AddAbortException ex) {
     756                // do nothing
     757            }
     758
     759
    679760        }
    680761
     
    688769    }
    689770
    690     class AddSelectedAfterSelection extends AbstractAction implements TableModelListener, ListSelectionListener {
     771    class AddSelectedAfterSelection extends AddFromSelectionAction implements TableModelListener, ListSelectionListener {
    691772        public AddSelectedAfterSelection() {
    692773            putValue(SHORT_DESCRIPTION,
     
    703784
    704785        public void actionPerformed(ActionEvent e) {
    705             memberTableModel.addMembersAfterIdx(selectionTableModel.getSelection(), memberTableModel
    706                     .getSelectionModel().getMaxSelectionIndex());
     786            try {
     787                List<OsmPrimitive> toAdd = filterConfirmedPrimitives(selectionTableModel.getSelection());
     788                memberTableModel.addMembersAfterIdx(toAdd, memberTableModel
     789                        .getSelectionModel().getMaxSelectionIndex());
     790            } catch(AddAbortException ex) {
     791                // do nothing
     792            }
    707793        }
    708794
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

    r1831 r1839  
    250250    }
    251251
    252     public void addMembersAtBeginning(List<? extends OsmPrimitive> primitives) {
     252    public void addMembersAtBeginning(List<OsmPrimitive> primitives) {
    253253        if (primitives == null)
    254254            return;
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/SelectionTableModel.java

    r1801 r1839  
    9090    }
    9191
    92     public List<? extends OsmPrimitive> getSelection() {
     92    public List<OsmPrimitive> getSelection() {
    9393        return cache;
    9494    }
Note: See TracChangeset for help on using the changeset viewer.