Changeset 1927 in josm


Ignore:
Timestamp:
2009-08-08T10:53:46+02:00 (15 years ago)
Author:
Gubaer
Message:

fixed #3213: Adding objects to a relation breaks selecting multiple objects

Location:
trunk
Files:
7 added
2 edited

Legend:

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

    r1925 r1927  
    334334        editor.setAutoCompletionList(acList);
    335335
    336         SelectionSynchronizer synchronizer = new SelectionSynchronizer();
    337         memberTable.getSelectionModel().addListSelectionListener(synchronizer);
    338         DataSet.selListeners.add(synchronizer);
    339336        memberTable.addMouseListener(new MemberTableDblClickAdapter());
    340337        memberTableModel.addMemberModelListener(memberTable);
     
    489486        gc.weightx = 0.0;
    490487        gc.weighty = 0.0;
     488
     489        // -----
     490        gc.gridy = 0;
    491491        MoveUpAction moveUpAction = new MoveUpAction();
    492492        memberTableModel.getSelectionModel().addListSelectionListener(moveUpAction);
     
    511511        pnl.add(new JButton(removeSelectedAction), gc);
    512512
     513
    513514        // ------
    514515        gc.gridy = 4;
     516        SelectPrimitivesForSelectedMembersAction selectAction = new SelectPrimitivesForSelectedMembersAction();
     517        memberTable.getSelectionModel().addListSelectionListener(selectAction);
     518        pnl.add(new JButton(selectAction), gc);
     519
     520        // ------
     521        gc.gridy = 5;
    515522        SortAction sortAction = new SortAction();
    516523        pnl.add(new JButton(sortAction), gc);
     
    518525        // ------
    519526        // just grab the remaining space
    520         gc.gridy = 5;
     527        gc.gridy = 6;
    521528        gc.weighty = 1.0;
    522529        gc.fill = GridBagConstraints.BOTH;
     
    550557        // -----
    551558        gc.gridy = 1;
     559        SelectedMembersForSelectionAction selectMembersForSelectionAction = new SelectedMembersForSelectionAction();
     560        selectionTableModel.addTableModelListener(selectMembersForSelectionAction);
     561        memberTableModel.addTableModelListener(selectMembersForSelectionAction);
     562        pnl.add(new JButton(selectMembersForSelectionAction), gc);
     563
     564        // -----
     565        gc.gridy = 2;
    552566        RemoveSelectedAction removeSelectedAction = new RemoveSelectedAction();
    553567        selectionTableModel.addTableModelListener(removeSelectedAction);
     
    556570        // ------
    557571        // just grab the remaining space
    558         gc.gridy = 2;
     572        gc.gridy = 3;
    559573        gc.weighty = 1.0;
    560574        gc.fill = GridBagConstraints.BOTH;
     
    562576
    563577        // -----
    564         gc.gridy = 3;
     578        gc.gridy = 4;
    565579        gc.weighty = 0.0;
    566580        AddSelectedAtStartAction addSelectionAction = new AddSelectedAtStartAction();
     
    569583
    570584        // -----
    571         gc.gridy = 4;
     585        gc.gridy = 5;
    572586        AddSelectedBeforeSelection addSelectedBeforeSelectionAction = new AddSelectedBeforeSelection();
    573587        selectionTableModel.addTableModelListener(addSelectedBeforeSelectionAction);
     
    576590
    577591        // -----
    578         gc.gridy = 5;
     592        gc.gridy = 6;
    579593        AddSelectedAfterSelection addSelectedAfterSelectionAction = new AddSelectedAfterSelection();
    580594        selectionTableModel.addTableModelListener(addSelectedAfterSelectionAction);
     
    887901        public RemoveSelectedAction() {
    888902            putValue(SHORT_DESCRIPTION, tr("Remove all members referring to one of the selected primitives"));
    889             putValue(SMALL_ICON, ImageProvider.get("dialogs", "removeselected"));
     903            putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "deletemembers"));
    890904            // putValue(NAME, tr("Remove Selected"));
    891905            Shortcut.registerShortcut("relationeditor:removeselected", tr("Relation Editor: Remove Selected"),
     
    912926
    913927        public void tableChanged(TableModelEvent e) {
     928            updateEnabledState();
     929        }
     930    }
     931
     932    /**
     933     * Selects  members in the relation editor which refer to primitives in the current
     934     * selection of the context layer.
     935     *
     936     */
     937    class SelectedMembersForSelectionAction extends AbstractAction implements TableModelListener {
     938        public SelectedMembersForSelectionAction() {
     939            putValue(SHORT_DESCRIPTION, tr("Select relation members which refer to primitives in the current selection"));
     940            putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "selectmembers"));
     941            updateEnabledState();
     942        }
     943
     944        protected void updateEnabledState() {
     945            boolean enabled = selectionTableModel.getRowCount() > 0
     946            &&  !memberTableModel.getChildPrimitives(getLayer().data.getSelected()).isEmpty();
     947
     948            if (enabled) {
     949                putValue(SHORT_DESCRIPTION, tr("Select relation members which refer to {0} primitives in the current selection",memberTableModel.getChildPrimitives(getLayer().data.getSelected()).size()));
     950            } else {
     951                putValue(SHORT_DESCRIPTION, tr("Select relation members which refer to primitives in the current selection"));
     952            }
     953            setEnabled(enabled);
     954        }
     955
     956        public void actionPerformed(ActionEvent e) {
     957            memberTableModel.selectMembersReferringTo(getLayer().data.getSelected());
     958        }
     959
     960        public void tableChanged(TableModelEvent e) {
     961            updateEnabledState();
     962
     963        }
     964    }
     965
     966    /**
     967     * Selects primitives in the layer this editor belongs to. The selected primitives are
     968     * equal to the set of primitives the currently selected relation members refer to.
     969     *
     970     */
     971    class SelectPrimitivesForSelectedMembersAction extends AbstractAction implements ListSelectionListener {
     972        public SelectPrimitivesForSelectedMembersAction() {
     973            putValue(SHORT_DESCRIPTION, tr("Select primitives for selected relation members"));
     974            putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "selectprimitives"));
     975            updateEnabledState();
     976        }
     977
     978        protected void updateEnabledState() {
     979            setEnabled(memberTable.getSelectedRowCount() > 0);
     980        }
     981
     982        public void actionPerformed(ActionEvent e) {
     983            getLayer().data.setSelected(memberTableModel.getSelectedChildPrimitives());
     984            DataSet.fireSelectionChanged(getLayer().data.getSelected());
     985        }
     986
     987        public void valueChanged(ListSelectionEvent e) {
    914988            updateEnabledState();
    915989        }
     
    14441518            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
    14451519                new EditAction().run();
    1446             }
    1447         }
    1448     }
    1449 
    1450     /**
    1451      * Updates the selection in the current data set with the selected referers in
    1452      * in the member table.
    1453      */
    1454     class SelectionSynchronizer implements ListSelectionListener, SelectionChangedListener{
    1455         public void valueChanged(ListSelectionEvent e) {
    1456             // as long as the model is computing the final selection it emits selection
    1457             // change events with getValueIsAdjusting() true. Ignore these events, only
    1458             // handle the final selection update. Otherwise, infinite loops of property
    1459             // change events occur.
    1460             //
    1461             if (e.getValueIsAdjusting())
    1462                 return;
    1463 
    1464             // Avoid infinite loop.  Only update the selection if it is not in sync with what
    1465             // is already selected. Avoids infinite loops of property change events.
    1466             //
    1467             if (!memberTableModel.selectionsAreInSync()) {
    1468                 getLayer().data.setSelected(memberTableModel.getSelectedReferers());
    1469             }
    1470         }
    1471 
    1472         public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
    1473             // ignore selection change events if they happen for a dataset in another
    1474             // layer
    1475             if (!memberTableModel.isActiveLayer()) return;
    1476             if (!memberTableModel.selectionsAreInSync()) {
    1477                 memberTableModel.selectMembersReferringTo(newSelection);
    14781520            }
    14791521        }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

    r1926 r1927  
    376376     * @return the set of selected referers
    377377     */
    378     public Set<OsmPrimitive> getSelectedReferers() {
     378    public Set<OsmPrimitive> getSelectedChildPrimitives() {
    379379        HashSet<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
    380380        for (RelationMember m: getSelectedMembers()) {
     
    385385
    386386    /**
     387     * Replies the set of selected referers. Never null, but may be empty.
     388     *
     389     * @return the set of selected referers
     390     */
     391    public Set<OsmPrimitive> getChildPrimitives(Collection<? extends OsmPrimitive> referenceSet) {
     392        HashSet<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
     393        if (referenceSet == null) return null;
     394        for (RelationMember m: members) {
     395            if (referenceSet.contains(m.member)) {
     396                ret.add(m.member);
     397            }
     398        }
     399        return ret;
     400    }
     401
     402
     403    /**
    387404     * Replies true, if the selected {@see OsmPrimitive}s in the layer belonging
    388405     * to this model are in sync with the selected referers in this model.
     
    391408     */
    392409    public boolean selectionsAreInSync() {
    393         HashSet<OsmPrimitive> s1 = new HashSet<OsmPrimitive>(getSelectedReferers());
     410        HashSet<OsmPrimitive> s1 = new HashSet<OsmPrimitive>(getSelectedChildPrimitives());
    394411        if (s1.size() != layer.data.getSelected().size()) return false;
    395412        s1.removeAll(layer.data.getSelected());
Note: See TracChangeset for help on using the changeset viewer.