Changeset 5793 in josm


Ignore:
Timestamp:
2013-03-22T19:53:11+01:00 (12 years ago)
Author:
akks
Message:

Big refactoring of relation context menu actions, removing duplicate and similar code. Any behavior change is a bug!
see #7846: Harmonize Relation popup menus and actions,
Please check relation-actions in Selection List, Relation List and Properties dialogs.

Location:
trunk/src/org/openstreetmap/josm
Files:
8 added
3 edited

Legend:

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

    r5744 r5793  
    3131import javax.swing.JMenuItem;
    3232import javax.swing.JPanel;
     33import javax.swing.JPopupMenu;
    3334import javax.swing.JScrollPane;
    3435import javax.swing.JTextField;
     
    4445
    4546import org.openstreetmap.josm.Main;
     47import org.openstreetmap.josm.actions.relation.DownloadMembersAction;
     48import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
     49import org.openstreetmap.josm.actions.relation.EditRelationAction;
     50import org.openstreetmap.josm.actions.relation.SelectMembersAction;
     51import org.openstreetmap.josm.actions.relation.SelectRelationAction;
    4652import org.openstreetmap.josm.actions.search.SearchCompiler;
    4753import org.openstreetmap.josm.command.Command;
     
    5157import org.openstreetmap.josm.data.osm.OsmPrimitive;
    5258import org.openstreetmap.josm.data.osm.Relation;
    53 import org.openstreetmap.josm.data.osm.RelationMember;
    5459import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
    5560import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
     
    6873import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
    6974import org.openstreetmap.josm.gui.SideButton;
    70 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
    71 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
    7275import org.openstreetmap.josm.gui.dialogs.relation.GenericRelationEditor;
    7376import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
     
    7679import org.openstreetmap.josm.gui.util.GuiHelper;
    7780import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
    78 import org.openstreetmap.josm.gui.widgets.ListPopupMenu;
    7981import org.openstreetmap.josm.tools.ImageProvider;
    8082import org.openstreetmap.josm.tools.InputMapUtils;
     
    9698    private final RelationListModel model;
    9799
    98     /** the edit action */
    99     private final EditAction editAction;
    100100    /** the delete action */
    101101    private final DeleteAction deleteAction;
    102102    private final NewAction newAction;
    103103    private final AddToRelation addToRelation;
     104    private final DuplicateAction duplicateAction;
     105   
    104106    /** the popup menu */
    105107    private final RelationDialogPopupMenu popupMenu;
     
    107109    private final JTextField filter;
    108110   
     111    // Actions
     112    /** the edit action */
     113    private final EditRelationAction editAction;
     114    private final DownloadMembersAction downloadMembersAction = new DownloadMembersAction();
     115    private final DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction = new DownloadSelectedIncompleteMembersAction();
     116    private final SelectMembersAction selectMemebersAction = new SelectMembersAction(false);
     117    private final SelectMembersAction addMembersToSelectionAction = new SelectMembersAction(true);
     118    private final SelectRelationAction selectRelationAction = new SelectRelationAction(false);
     119    private final SelectRelationAction addRelationToSelectionAction = new SelectRelationAction(true);
     120
    109121    /**
    110122     * constructor
     
    139151        // the edit action
    140152        //
    141         editAction = new EditAction();
    142         displaylist.addListSelectionListener(editAction);
    143 
     153        editAction = new EditRelationAction();
     154       
    144155        // the duplicate action
    145156        //
    146         DuplicateAction duplicateAction = new DuplicateAction();
    147         displaylist.addListSelectionListener(duplicateAction);
    148 
     157        duplicateAction = new DuplicateAction();
     158       
    149159        // the delete action
    150160        //
    151161        deleteAction = new DeleteAction();
    152         displaylist.addListSelectionListener(deleteAction);
    153 
    154         // the select action
     162
     163        // Add to realaion action
    155164        //
    156         SelectAction selectAction = new SelectAction(false);
    157         displaylist.addListSelectionListener(selectAction);
    158 
    159         filter = new DisableShortcutsOnFocusGainedTextField();
    160         filter.setToolTipText(tr("Relation list filter"));
    161         filter.getDocument().addDocumentListener(new DocumentListener() {
    162 
    163             private void setFilter() {
    164                 try {
    165                     filter.setBackground(UIManager.getColor("TextField.background"));
    166                     filter.setToolTipText(tr("Relation list filter"));
    167                     model.setFilter(SearchCompiler.compile(filter.getText(), false, false));
    168                 } catch (SearchCompiler.ParseError ex) {
    169                     filter.setBackground(new Color(255, 224, 224));
    170                     filter.setToolTipText(ex.getMessage());
    171                     model.setFilter(new SearchCompiler.Always());
    172                 }
    173             }
    174 
     165        addToRelation = new AddToRelation();
     166
     167        filter = setupFilter();
     168
     169        displaylist.addListSelectionListener(new ListSelectionListener() {
    175170            @Override
    176             public void insertUpdate(DocumentEvent e) {
    177                 setFilter();
    178             }
    179 
    180             @Override
    181             public void removeUpdate(DocumentEvent e) {
    182                 setFilter();
    183             }
    184 
    185             @Override
    186             public void changedUpdate(DocumentEvent e) {
    187                 setFilter();
     171            public void valueChanged(ListSelectionEvent e) {
     172                duplicateAction.valueChanged(e);
     173                deleteAction.valueChanged(e);
     174                addToRelation.valueChanged(e);
     175
     176                List<Relation> rels;
     177                rels = model.getSelectedNonNewRelations();
     178                downloadMembersAction.setRelations(rels);
     179
     180                rels = model.getSelectedRelationsWithIncompleteMembers();
     181                downloadSelectedIncompleteMembersAction.setRelations(rels);
     182
     183                rels = model.getSelectedRelations();
     184                selectMemebersAction.setRelations(rels);
     185                addMembersToSelectionAction.setRelations(rels);
     186                selectRelationAction.setRelations(rels);
     187                addRelationToSelectionAction.setRelations(rels);
    188188            }
    189189        });
    190 
     190       
    191191        JPanel pane = new JPanel(new BorderLayout());
    192192        pane.add(filter, BorderLayout.NORTH);
     
    197197                new SideButton(duplicateAction, false),
    198198                new SideButton(deleteAction, false),
    199                 new SideButton(selectAction, false)
     199                new SideButton(selectRelationAction, false)
    200200        }));
    201201
     
    207207       
    208208        // Select relation on Ctrl-Enter
    209         InputMapUtils.addEnterAction(displaylist, selectAction);
    210 
    211         addToRelation = new AddToRelation();
    212         popupMenu = new RelationDialogPopupMenu(displaylist);
     209        InputMapUtils.addEnterAction(displaylist, selectRelationAction);
     210
     211        popupMenu = new RelationDialogPopupMenu();
    213212
    214213        // Edit relation on Ctrl-Enter
     
    216215        displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK), "edit");
    217216    }
    218 
     217   
    219218    @Override public void showNotify() {
    220219        MapView.addLayerChangeListener(newAction);
     
    254253
    255254    /**
    256      * Adds a selection listener to the relation list.
    257      *
    258      * @param listener the listener to add
    259      */
    260     public void addListSelectionListener(ListSelectionListener listener) {
    261         displaylist.addListSelectionListener(listener);
    262     }
    263 
    264     /**
    265      * Removes a selection listener from the relation list.
    266      *
    267      * @param listener the listener to remove
    268      */
    269     public void removeListSelectionListener(ListSelectionListener listener) {
    270         displaylist.removeListSelectionListener(listener);
    271     }
    272 
    273     /**
    274255     * @return The selected relation in the list
    275256     */
     
    306287    }
    307288
     289    private JTextField  setupFilter() {
     290        final JTextField f = new DisableShortcutsOnFocusGainedTextField();
     291        f.setToolTipText(tr("Relation list filter"));
     292        f.getDocument().addDocumentListener(new DocumentListener() {
     293
     294            private void setFilter() {
     295                try {
     296                    f.setBackground(UIManager.getColor("TextField.background"));
     297                    f.setToolTipText(tr("Relation list filter"));
     298                    model.setFilter(SearchCompiler.compile(filter.getText(), false, false));
     299                } catch (SearchCompiler.ParseError ex) {
     300                    f.setBackground(new Color(255, 224, 224));
     301                    f.setToolTipText(ex.getMessage());
     302                    model.setFilter(new SearchCompiler.Always());
     303                }
     304            }
     305
     306            @Override
     307            public void insertUpdate(DocumentEvent e) {
     308                setFilter();
     309            }
     310
     311            @Override
     312            public void removeUpdate(DocumentEvent e) {
     313                setFilter();
     314            }
     315
     316            @Override
     317            public void changedUpdate(DocumentEvent e) {
     318                setFilter();
     319            }
     320        });
     321        return f;
     322    }
     323
    308324    class MouseEventHandler extends MouseAdapter {
    309325        protected void setCurrentRelationAsSelection() {
     
    312328
    313329        protected void editCurrentRelation() {
    314             new EditAction().launchEditor(getSelected());
     330            EditRelationAction.launchEditor(getSelected());
    315331        }
    316332
     
    349365        }
    350366    }
    351 
    352     /**
    353      * The edit action
    354      *
    355      */
    356     class EditAction extends AbstractAction implements ListSelectionListener{
    357         public EditAction() {
    358             putValue(SHORT_DESCRIPTION,tr( "Open an editor for the selected relation"));
    359             putValue(NAME, tr("Edit"));
    360             putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
    361             setEnabled(false);
    362         }
    363         protected Collection<RelationMember> getMembersForCurrentSelection(Relation r) {
    364             Collection<RelationMember> members = new HashSet<RelationMember>();
    365             Collection<OsmPrimitive> selection = Main.map.mapView.getEditLayer().data.getSelected();
    366             for (RelationMember member: r.getMembers()) {
    367                 if (selection.contains(member.getMember())) {
    368                     members.add(member);
    369                 }
    370             }
    371             return members;
    372         }
    373 
    374         public void launchEditor(Relation toEdit) {
    375             if (toEdit == null)
    376                 return;
    377             RelationEditor.getEditor(Main.map.mapView.getEditLayer(),toEdit, getMembersForCurrentSelection(toEdit)).setVisible(true);
    378         }
    379 
    380         public void actionPerformed(ActionEvent e) {
    381             if (!isEnabled())
    382                 return;
    383             launchEditor(getSelected());
    384         }
    385 
    386         public void valueChanged(ListSelectionEvent e) {
    387             setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length == 1);
    388         }
    389     }
    390 
     367   
    391368    /**
    392369     * The delete action
     
    498475        protected void updateEnabledState() {
    499476            setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length == 1);
    500         }
    501 
    502         public void valueChanged(ListSelectionEvent e) {
    503             updateEnabledState();
    504         }
    505     }
    506 
    507     /**
    508      * Sets the current selection to the list of relations selected in this dialog
    509      *
    510      */
    511     class SelectAction extends AbstractAction implements ListSelectionListener{
    512         boolean add;
    513         public SelectAction(boolean add) {
    514             putValue(SHORT_DESCRIPTION, add ? tr("Add the selected relations to the current selection")
    515                     : tr("Set the current selection to the list of selected relations"));
    516             putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
    517             putValue(NAME, add ? tr("Select relation (add)") : tr("Select relation"));
    518             this.add = add;
    519             updateEnabledState();
    520         }
    521 
    522         public void actionPerformed(ActionEvent e) {
    523             if (!isEnabled()) return;
    524             int [] idx = displaylist.getSelectedIndices();
    525             if (idx == null || idx.length == 0) return;
    526             ArrayList<OsmPrimitive> selection = new ArrayList<OsmPrimitive>(idx.length);
    527             for (int i: idx) {
    528                 selection.add(model.getVisibleRelation(i));
    529             }
    530             if(add) {
    531                 Main.map.mapView.getEditLayer().data.addSelected(selection);
    532             } else {
    533                 Main.map.mapView.getEditLayer().data.setSelected(selection);
    534             }
    535         }
    536 
    537         protected void updateEnabledState() {
    538             setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length > 0);
    539         }
    540 
    541         public void valueChanged(ListSelectionEvent e) {
    542             updateEnabledState();
    543         }
    544     }
    545 
    546     /**
    547      * Sets the current selection to the list of relations selected in this dialog
    548      *
    549      */
    550     class SelectMembersAction extends AbstractAction implements ListSelectionListener{
    551         boolean add;
    552         public SelectMembersAction(boolean add) {
    553             putValue(SHORT_DESCRIPTION,add ? tr("Add the members of all selected relations to current selection")
    554                     : tr("Select the members of all selected relations"));
    555             putValue(SMALL_ICON, ImageProvider.get("selectall"));
    556             putValue(NAME, add ? tr("Select members (add)") : tr("Select members"));
    557             this.add = add;
    558             updateEnabledState();
    559         }
    560 
    561         public void actionPerformed(ActionEvent e) {
    562             if (!isEnabled()) return;
    563             List<Relation> relations = model.getSelectedRelations();
    564             HashSet<OsmPrimitive> members = new HashSet<OsmPrimitive>();
    565             for(Relation r: relations) {
    566                 members.addAll(r.getMemberPrimitives());
    567             }
    568             if(add) {
    569                 Main.map.mapView.getEditLayer().data.addSelected(members);
    570             } else {
    571                 Main.map.mapView.getEditLayer().data.setSelected(members);
    572             }
    573         }
    574 
    575         protected void updateEnabledState() {
    576             setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length > 0);
    577         }
    578 
    579         public void valueChanged(ListSelectionEvent e) {
    580             updateEnabledState();
    581         }
    582     }
    583 
    584     /**
    585      * The action for downloading members of all selected relations
    586      *
    587      */
    588     class DownloadMembersAction extends AbstractAction implements ListSelectionListener{
    589 
    590         public DownloadMembersAction() {
    591             putValue(SHORT_DESCRIPTION,tr("Download all members of the selected relations"));
    592             putValue(NAME, tr("Download members"));
    593             putValue(SMALL_ICON, ImageProvider.get("dialogs", "downloadincomplete"));
    594             putValue("help", ht("/Dialog/RelationList#DownloadMembers"));
    595             updateEnabledState();
    596         }
    597 
    598         protected void updateEnabledState() {
    599             setEnabled(! model.getSelectedNonNewRelations().isEmpty());
    600         }
    601 
    602         public void valueChanged(ListSelectionEvent e) {
    603             updateEnabledState();
    604         }
    605 
    606         public void actionPerformed(ActionEvent e) {
    607             List<Relation> relations = model.getSelectedNonNewRelations();
    608             if (relations.isEmpty())
    609                 return;
    610             Main.worker.submit(new DownloadRelationTask(
    611                     model.getSelectedNonNewRelations(),
    612                     Main.map.mapView.getEditLayer())
    613                     );
    614         }
    615     }
    616 
    617     /**
    618      * Action for downloading incomplete members of selected relations
    619      *
    620      */
    621     class DownloadSelectedIncompleteMembersAction extends AbstractAction implements ListSelectionListener{
    622         public DownloadSelectedIncompleteMembersAction() {
    623             putValue(SHORT_DESCRIPTION, tr("Download incomplete members of selected relations"));
    624             putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "downloadincompleteselected"));
    625             putValue(NAME, tr("Download incomplete members"));
    626             updateEnabledState();
    627         }
    628 
    629         public Set<OsmPrimitive> buildSetOfIncompleteMembers(List<Relation> rels) {
    630             Set<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
    631             for(Relation r: rels) {
    632                 ret.addAll(r.getIncompleteMembers());
    633             }
    634             return ret;
    635         }
    636 
    637         public void actionPerformed(ActionEvent e) {
    638             if (!isEnabled())
    639                 return;
    640             List<Relation> rels = model.getSelectedRelationsWithIncompleteMembers();
    641             if (rels.isEmpty()) return;
    642             Main.worker.submit(new DownloadRelationMemberTask(
    643                     rels,
    644                     buildSetOfIncompleteMembers(rels),
    645                     Main.map.mapView.getEditLayer()
    646                     ));
    647         }
    648 
    649         protected void updateEnabledState() {
    650             setEnabled(!model.getSelectedRelationsWithIncompleteMembers().isEmpty());
    651477        }
    652478
     
    955781    }
    956782
    957     class RelationDialogPopupMenu extends ListPopupMenu {
    958 
    959         public RelationDialogPopupMenu(JList list) {
    960             super(list);
    961 
     783    class RelationDialogPopupMenu extends JPopupMenu {
     784
     785        public RelationDialogPopupMenu() {
    962786            // -- download members action
    963             add(new DownloadMembersAction());
     787            add(downloadMembersAction);
    964788
    965789            // -- download incomplete members action
    966             add(new DownloadSelectedIncompleteMembersAction());
     790            add(downloadSelectedIncompleteMembersAction);
    967791
    968792            addSeparator();
    969793
    970794            // -- select members action
    971             add(new SelectMembersAction(false));
    972             add(new SelectMembersAction(true));
     795            add(selectMemebersAction);
     796            add(addMembersToSelectionAction);
    973797
    974798            // -- select action
    975             add(new SelectAction(false));
    976             add(new SelectAction(true));
     799            add(selectRelationAction);
     800            add(addRelationToSelectionAction);
    977801
    978802            addSeparator();
  • trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r5716 r5793  
    3939import org.openstreetmap.josm.Main;
    4040import org.openstreetmap.josm.actions.AutoScaleAction;
     41import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
     42import org.openstreetmap.josm.actions.relation.EditRelationAction;
     43import org.openstreetmap.josm.actions.relation.SelectInRelationListAction;
    4144import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
    4245import org.openstreetmap.josm.data.SelectionChangedListener;
     
    4649import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    4750import org.openstreetmap.josm.data.osm.Relation;
    48 import org.openstreetmap.josm.data.osm.RelationMember;
    4951import org.openstreetmap.josm.data.osm.Way;
    5052import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
     
    6668import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
    6769import org.openstreetmap.josm.gui.SideButton;
    68 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
    69 import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
    7070import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    7171import org.openstreetmap.josm.gui.widgets.ListPopupMenu;
     
    8888    private ZoomToJOSMSelectionAction actZoomToJOSMSelection;
    8989    private ZoomToListSelection actZoomToListSelection;
    90     private SetRelationSelection actSetRelationSelection;
    91     private EditRelationSelection actEditRelationSelection;
     90    private SelectInRelationListAction actSetRelationSelection;
     91    private EditRelationAction actEditRelationSelection;
    9292    private DownloadSelectedIncompleteMembersAction actDownloadSelectedIncompleteMembers;
    9393
     
    142142
    143143        actZoomToListSelection = new ZoomToListSelection();
    144         actSetRelationSelection = new SetRelationSelection();
    145         actEditRelationSelection = new EditRelationSelection();
     144        actSetRelationSelection = new SelectInRelationListAction();
     145        actEditRelationSelection = new EditRelationAction();
    146146        actDownloadSelectedIncompleteMembers = new DownloadSelectedIncompleteMembersAction();
    147147
     148        lstPrimitives.addListSelectionListener(new ListSelectionListener() {
     149            @Override
     150            public void valueChanged(ListSelectionEvent e) {
     151                actZoomToListSelection.valueChanged(e);
     152                List<Relation> rels;
     153                rels = model.getSelectedRelationsWithIncompleteMembers();
     154                actDownloadSelectedIncompleteMembers.setRelations(rels);
     155                rels = OsmPrimitive.getFilteredList(model.getSelected(), Relation.class);
     156                actSetRelationSelection.setRelations(rels);
     157                actEditRelationSelection.setRelations(rels);
     158            }
     159        });
     160               
    148161        lstPrimitives.addMouseListener(new SelectionPopupMenuLauncher());
    149162        lstPrimitives.addMouseListener(new DblClickHandler());
     
    250263        }
    251264
     265        @Override
    252266        public void contentsChanged(ListDataEvent e) {
    253267            updateTitle();
    254268        }
    255269
     270        @Override
    256271        public void intervalAdded(ListDataEvent e) {
    257272            updateTitle();
    258273        }
    259274
     275        @Override
    260276        public void intervalRemoved(ListDataEvent e) {
    261277            updateTitle();
     
    274290        }
    275291
     292        @Override
    276293        public void actionPerformed(ActionEvent e) {
    277294            if (!isEnabled()) return;
     
    283300        }
    284301
     302        @Override
    285303        public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
    286304            updateEnabledState();
     
    329347        }
    330348
     349        @Override
    331350        public void actionPerformed(ActionEvent e) {
    332351            AutoScaleAction.autoScale("selection");
     
    337356        }
    338357
     358        @Override
    339359        public void contentsChanged(ListDataEvent e) {
    340360            updateEnabledState();
    341361        }
    342362
     363        @Override
    343364        public void intervalAdded(ListDataEvent e) {
    344365            updateEnabledState();
    345366        }
    346367
     368        @Override
    347369        public void intervalRemoved(ListDataEvent e) {
    348370            updateEnabledState();
     
    363385        }
    364386
     387        @Override
    365388        public void actionPerformed(ActionEvent e) {
    366389            BoundingXYVisitor box = new BoundingXYVisitor();
     
    378401        }
    379402
    380         public void valueChanged(ListSelectionEvent e) {
    381             updateEnabledState();
    382         }
    383     }
    384 
    385     /**
    386      * The action for setting and editing a relation in relation list dialog
    387      *
    388      */
    389     class EditRelationSelection extends SetRelationSelection {
    390         public EditRelationSelection() {
    391             putValue(NAME, tr("Call editor for relation"));
    392             putValue(SHORT_DESCRIPTION, tr("Call relation editor for selected relation"));
    393             putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
    394             updateEnabledState();
    395         }
    396 
    397         @Override
    398         public void actionPerformed(ActionEvent e) {
    399             Relation relation = (Relation)model.getSelected().toArray()[0];
    400             Collection<RelationMember> members = new HashSet<RelationMember>();
    401             Collection<OsmPrimitive> selection = model.getAllElements();
    402             for (RelationMember member: relation.getMembers()) {
    403                 if (selection.contains(member.getMember())) {
    404                     members.add(member);
    405                 }
    406             }
    407             Main.map.relationListDialog.selectRelation(relation);
    408             RelationEditor.getEditor(Main.map.mapView.getEditLayer(), relation,
    409                     members).setVisible(true);
    410         }
    411     }
    412 
    413     /**
    414      * The action for setting a relation in relation list dialog
    415      *
    416      */
    417     class SetRelationSelection extends AbstractAction implements ListSelectionListener{
    418         public SetRelationSelection() {
    419             putValue(NAME, tr("Select in relation list"));
    420             putValue(SHORT_DESCRIPTION, tr("Select relation in relation list."));
    421             putValue(SMALL_ICON, ImageProvider.get("dialogs", "selectionlist"));
    422             updateEnabledState();
    423         }
    424 
    425         public void actionPerformed(ActionEvent e) {
    426             Relation relation = (Relation)model.getSelected().toArray()[0];
    427             Main.map.relationListDialog.selectRelation(relation);
    428         }
    429 
    430         public void updateEnabledState() {
    431             Object[] sel = model.getSelected().toArray();
    432             setEnabled(sel.length == 1 && sel[0] instanceof Relation);
    433         }
    434 
     403        @Override
    435404        public void valueChanged(ListSelectionEvent e) {
    436405            updateEnabledState();
     
    663632        /* interface EditLayerChangeListener                                        */
    664633        /* ------------------------------------------------------------------------ */
     634        @Override
    665635        public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
    666636            if (newLayer == null) {
     
    676646        /* interface SelectionChangeListener                                        */
    677647        /* ------------------------------------------------------------------------ */
     648        @Override
    678649        public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
    679650            setJOSMSelection(newSelection);
     
    683654        /* interface DataSetListener                                                */
    684655        /* ------------------------------------------------------------------------ */
     656        @Override
    685657        public void dataChanged(DataChangedEvent event) {
    686658            // refresh the whole list
     
    688660        }
    689661
     662        @Override
    690663        public void nodeMoved(NodeMovedEvent event) {
    691664            // may influence the display name of primitives, update the data
     
    693666        }
    694667
     668        @Override
    695669        public void otherDatasetChange(AbstractDatasetChangedEvent event) {
    696670            // may influence the display name of primitives, update the data
     
    698672        }
    699673
     674        @Override
    700675        public void relationMembersChanged(RelationMembersChangedEvent event) {
    701676            // may influence the display name of primitives, update the data
     
    703678        }
    704679
     680        @Override
    705681        public void tagsChanged(TagsChangedEvent event) {
    706682            // may influence the display name of primitives, update the data
     
    708684        }
    709685
     686        @Override
    710687        public void wayNodesChanged(WayNodesChangedEvent event) {
    711688            // may influence the display name of primitives, update the data
     
    713690        }
    714691
     692        @Override
    715693        public void primitivesAdded(PrimitivesAddedEvent event) {/* ignored - handled by SelectionChangeListener */}
     694        @Override
    716695        public void primitivesRemoved(PrimitivesRemovedEvent event) {/* ignored - handled by SelectionChangeListener*/}
    717696    }
     
    731710        }
    732711
     712        @Override
    733713        public void actionPerformed(ActionEvent e) {
    734714            org.openstreetmap.josm.actions.search.SearchAction.searchWithoutHistory(s);
     
    811791        }
    812792
     793        @Override
    813794        public void actionPerformed(ActionEvent e) {
    814795            Main.main.getCurrentDataSet().setSelected(sel);
     
    832813                add(new SelectionMenuItem(sel));
    833814            }
    834         }
    835     }
    836 
    837     /**
    838      * Action for downloading incomplete members of selected relations
    839      *
    840      */
    841     class DownloadSelectedIncompleteMembersAction extends AbstractAction implements ListSelectionListener {
    842         public DownloadSelectedIncompleteMembersAction() {
    843             putValue(SHORT_DESCRIPTION, tr("Download incomplete members of selected relations"));
    844             putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "downloadincompleteselected"));
    845             putValue(NAME, tr("Download incomplete members"));
    846             updateEnabledState();
    847         }
    848 
    849         public Set<OsmPrimitive> buildSetOfIncompleteMembers(List<Relation> rels) {
    850             Set<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
    851             for(Relation r: rels) {
    852                 ret.addAll(r.getIncompleteMembers());
    853             }
    854             return ret;
    855         }
    856 
    857         public void actionPerformed(ActionEvent e) {
    858             if (!isEnabled())
    859                 return;
    860             List<Relation> rels = model.getSelectedRelationsWithIncompleteMembers();
    861             if (rels.isEmpty()) return;
    862             Main.worker.submit(new DownloadRelationMemberTask(
    863                     rels,
    864                     buildSetOfIncompleteMembers(rels),
    865                     Main.map.mapView.getEditLayer()
    866             ));
    867         }
    868 
    869         protected void updateEnabledState() {
    870             setEnabled(!model.getSelectedRelationsWithIncompleteMembers().isEmpty());
    871         }
    872 
    873         public void valueChanged(ListSelectionEvent e) {
    874             updateEnabledState();
    875815        }
    876816    }
     
    897837        }
    898838
     839        @Override
    899840        public int compare(OsmPrimitive a, OsmPrimitive b) {
    900841            if (a.getType().equals(b.getType()))
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    r5789 r5793  
    5353import org.openstreetmap.josm.Main;
    5454import org.openstreetmap.josm.actions.JosmAction;
     55import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
     56import org.openstreetmap.josm.actions.relation.SelectMembersAction;
     57import org.openstreetmap.josm.actions.relation.SelectRelationAction;
    5558import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
    5659import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
     
    181184    private final JosmAction[] josmActions = new JosmAction[]{addAction, editAction, deleteAction};
    182185
     186    // relation actions
     187    private final DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction = new DownloadSelectedIncompleteMembersAction();
     188    private final SelectRelationAction addRelationToSelectionAction = new SelectRelationAction(true);
     189    private final SelectMembersAction addMembersToSelectionAction = new SelectMembersAction(true);
     190    private final SelectRelationAction selectRelationAction = new SelectRelationAction(false);
     191   
    183192    @Override
    184193    public void showNotify() {
     
    379388        // setting up the membership table
    380389        membershipMenu = new JPopupMenu();
    381         membershipMenu.add(new SelectRelationAction(true));
    382         membershipMenu.add(new SelectRelationAction(false));
    383         membershipMenu.add(new SelectRelationMembersAction());
    384         membershipMenu.add(new DownloadIncompleteMembersAction());
     390        membershipMenu.add(addRelationToSelectionAction);
     391        membershipMenu.add(selectRelationAction);
     392        membershipMenu.add(addMembersToSelectionAction);
     393        membershipMenu.add(downloadSelectedIncompleteMembersAction);
    385394        membershipMenu.addSeparator();
    386395        membershipMenu.add(helpAction);
    387396
    388397        membershipData.setColumnIdentifiers(new String[]{tr("Member Of"),tr("Role"),tr("Position")});
    389         membershipTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     398        membershipTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    390399        membershipTable.addMouseListener(new PopupMenuLauncher() {
    391400            @Override
     
    393402                Point p = evt.getPoint();
    394403                int row = membershipTable.rowAtPoint(p);
    395                 if (row > -1) {
    396                     membershipTable.changeSelection(row, 0, false, false);
    397                     Relation relation = (Relation)membershipData.getValueAt(row, 0);
    398                     for (Component c : membershipMenu.getComponents()) {
    399                         if (c instanceof JMenuItem) {
    400                             Action action = ((JMenuItem) c).getAction();
    401                             if (action instanceof RelationRelated) {
    402                                 ((RelationRelated)action).setRelation(relation);
    403                             }
    404                         }
     404                membershipTable.changeSelection(row, 0, false, false);
     405                int idx[] = membershipTable.getSelectedRows();
     406                List<Relation> rels =  new ArrayList<Relation>(10);
     407                if (idx!=null) {
     408                    for (int i: idx) {
     409                        Relation r = (Relation) (membershipData.getValueAt(i, 0));
     410                        rels.add(r);
    405411                    }
    406                     membershipMenu.show(membershipTable, p.x, p.y-3);
    407                 }
     412                }
     413                selectRelationAction.setRelations(rels);
     414                addMembersToSelectionAction.setRelations(rels);
     415                addMembersToSelectionAction.setRelations(rels);
     416                downloadSelectedIncompleteMembersAction.setRelations(rels);
     417                membershipMenu.show(membershipTable, p.x, p.y-3);
    408418            }
    409419        });
     
    10441054    }
    10451055
    1046     public static interface RelationRelated {
    1047         public Relation getRelation();
    1048         public void setRelation(Relation relation);
    1049     }
    1050 
    1051     static abstract class AbstractRelationAction extends AbstractAction implements RelationRelated {
    1052         protected Relation relation;
    1053         public Relation getRelation() {
    1054             return this.relation;
    1055         }
    1056         public void setRelation(Relation relation) {
    1057             this.relation = relation;
    1058         }
    1059     }
    1060 
    1061     static class SelectRelationAction extends AbstractRelationAction {
    1062         boolean selectionmode;
    1063         public SelectRelationAction(boolean select) {
    1064             selectionmode = select;
    1065             if(select) {
    1066                 putValue(NAME, tr("Select relation"));
    1067                 putValue(SHORT_DESCRIPTION, tr("Select relation in main selection."));
    1068                 putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
    1069             } else {
    1070                 putValue(NAME, tr("Select in relation list"));
    1071                 putValue(SHORT_DESCRIPTION, tr("Select relation in relation list."));
    1072                 putValue(SMALL_ICON, ImageProvider.get("dialogs", "relationlist"));
    1073             }
    1074         }
    1075 
    1076         public void actionPerformed(ActionEvent e) {
    1077             if(selectionmode) {
    1078                 Main.map.mapView.getEditLayer().data.setSelected(relation);
    1079             } else {
    1080                 Main.map.relationListDialog.selectRelation(relation);
    1081                 Main.map.relationListDialog.unfurlDialog();
    1082             }
    1083         }
    1084     }
    1085 
    1086 
    1087     /**
    1088      * Sets the current selection to the members of selected relation
    1089      *
    1090      */
    1091     class SelectRelationMembersAction extends AbstractRelationAction {
    1092         public SelectRelationMembersAction() {
    1093             putValue(SHORT_DESCRIPTION,tr("Select the members of selected relation"));
    1094             putValue(SMALL_ICON, ImageProvider.get("selectall"));
    1095             putValue(NAME, tr("Select members"));
    1096         }
    1097 
    1098         public void actionPerformed(ActionEvent e) {
    1099             HashSet<OsmPrimitive> members = new HashSet<OsmPrimitive>();
    1100             members.addAll(relation.getMemberPrimitives());
    1101             Main.map.mapView.getEditLayer().data.setSelected(members);
    1102         }
    1103 
    1104     }
    1105 
    1106     /**
    1107      * Action for downloading incomplete members of selected relation
    1108      *
    1109      */
    1110     class DownloadIncompleteMembersAction extends AbstractRelationAction {
    1111         public DownloadIncompleteMembersAction() {
    1112             putValue(SHORT_DESCRIPTION, tr("Download incomplete members of selected relations"));
    1113             putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "downloadincompleteselected"));
    1114             putValue(NAME, tr("Download incomplete members"));
    1115         }
    1116 
    1117         public Set<OsmPrimitive> buildSetOfIncompleteMembers(Relation r) {
    1118             Set<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
    1119             ret.addAll(r.getIncompleteMembers());
    1120             return ret;
    1121         }
    1122 
    1123         public void actionPerformed(ActionEvent e) {
    1124             if (!relation.hasIncompleteMembers()) return;
    1125             ArrayList<Relation> rels = new ArrayList<Relation>();
    1126             rels.add(relation);
    1127             Main.worker.submit(new DownloadRelationMemberTask(
    1128                     rels,
    1129                     buildSetOfIncompleteMembers(relation),
    1130                     Main.map.mapView.getEditLayer()
    1131                     ));
    1132         }
    1133     }
    1134    
    11351056    class PasteValueAction extends AbstractAction {
    11361057        public PasteValueAction() {
Note: See TracChangeset for help on using the changeset viewer.