Changeset 5821 in josm for trunk/src


Ignore:
Timestamp:
2013-04-02T00:55:56+02:00 (12 years ago)
Author:
Don-vip
Message:

see #7846 - Large code refactorization in management of popup menus to simplify interactions with plugins (needed at least for imagery-xml-bounds and tag2link plugins)

Location:
trunk/src/org/openstreetmap/josm
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/relation/AbstractRelationAction.java

    r5799 r5821  
    77import javax.swing.AbstractAction;
    88
     9import org.openstreetmap.josm.actions.OsmPrimitiveAction;
     10import org.openstreetmap.josm.data.osm.OsmPrimitive;
    911import org.openstreetmap.josm.data.osm.Relation;
     12import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    1013
    1114/**
     
    1417 * @since 5793
    1518 */
    16 public abstract class AbstractRelationAction extends AbstractAction {
     19public abstract class AbstractRelationAction extends AbstractAction implements OsmPrimitiveAction {
    1720    protected Collection<Relation> relations = Collections.<Relation>emptySet();
    1821
    19     /**
    20      * Specifies the working set of relations.
    21      * @param relations The new working set of relations. Can be null or empty
     22    protected static final Collection<Relation> getRelations(Collection<? extends OsmPrimitive> primitives) {
     23        if (primitives == null || primitives.isEmpty()) {
     24            return Collections.<Relation>emptySet();
     25        } else {
     26            return new SubclassFilteredCollection<OsmPrimitive, Relation>(
     27                    primitives, OsmPrimitive.relationPredicate);
     28        }
     29    }
     30   
     31    /* (non-Javadoc)
     32     * @see org.openstreetmap.josm.actions.relation.RelationAction#setPrimitives
    2233     */
    23     public void setRelations(Collection<Relation> relations) {
    24         if (relations==null) {
    25             this.relations = Collections.<Relation>emptySet();
    26         } else {
    27             this.relations = relations;
    28         }
     34    @Override
     35    public void setPrimitives(Collection<? extends OsmPrimitive> primitives) {
     36        this.relations = getRelations(primitives);
    2937        updateEnabledState();
    3038    }
  • trunk/src/org/openstreetmap/josm/actions/relation/DownloadMembersAction.java

    r5799 r5821  
    66
    77import java.awt.event.ActionEvent;
     8import java.util.Collection;
    89
    910import org.openstreetmap.josm.Main;
     11import org.openstreetmap.josm.data.osm.OsmPrimitive;
     12import org.openstreetmap.josm.data.osm.Relation;
    1013import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
    1114import org.openstreetmap.josm.tools.ImageProvider;
     15import org.openstreetmap.josm.tools.Predicate;
     16import org.openstreetmap.josm.tools.Utils;
    1217
    1318/**
    1419 * The action for downloading members of relations
    15 
     20 * @since 5793
    1621 */
    1722public class DownloadMembersAction extends AbstractRelationAction {
     
    3237        Main.worker.submit(new DownloadRelationTask(relations, Main.map.mapView.getEditLayer()));
    3338    }
     39
     40    @Override
     41    public void setPrimitives(Collection<? extends OsmPrimitive> primitives) {
     42        // selected non-new relations
     43        this.relations = Utils.filter(getRelations(primitives), new Predicate<Relation>(){
     44            @Override public boolean evaluate(Relation r) {
     45                return !r.isNew();
     46            }});
     47        updateEnabledState();
     48    }
    3449}
  • trunk/src/org/openstreetmap/josm/actions/relation/DownloadSelectedIncompleteMembersAction.java

    r5799 r5821  
    1414import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
    1515import org.openstreetmap.josm.tools.ImageProvider;
     16import org.openstreetmap.josm.tools.Predicate;
     17import org.openstreetmap.josm.tools.Utils;
    1618
    1719/**
     
    5153                Main.map.mapView.getEditLayer()));
    5254    }
     55
     56    @Override
     57    public void setPrimitives(Collection<? extends OsmPrimitive> primitives) {
     58        // selected relations with incomplete members
     59        this.relations = Utils.filter(getRelations(primitives), new Predicate<Relation>(){
     60            @Override public boolean evaluate(Relation r) {
     61                return !r.isNew() && r.hasIncompleteMembers();
     62            }});
     63        updateEnabledState();
     64    }
    5365}
  • trunk/src/org/openstreetmap/josm/actions/relation/DuplicateRelationAction.java

    r5799 r5821  
    1717 */
    1818public class DuplicateRelationAction extends AbstractRelationAction {
     19   
     20    /**
     21     * Constructs a new {@code DuplicateRelationAction}.
     22     */
    1923    public DuplicateRelationAction() {
    2024        putValue(SHORT_DESCRIPTION, tr("Create a copy of this relation and open it in another editor window"));
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java

    r5799 r5821  
    1616import java.util.Collections;
    1717import java.util.HashSet;
    18 import java.util.Iterator;
    1918import java.util.List;
    2019import java.util.Set;
     
    2221import javax.swing.AbstractAction;
    2322import javax.swing.AbstractListModel;
    24 import javax.swing.Action;
    2523import javax.swing.DefaultListSelectionModel;
    2624import javax.swing.JComponent;
    2725import javax.swing.JList;
    28 import javax.swing.JMenuItem;
    2926import javax.swing.JPanel;
    3027import javax.swing.JPopupMenu;
     
    3936import javax.swing.event.ListSelectionEvent;
    4037import javax.swing.event.ListSelectionListener;
    41 import javax.swing.event.PopupMenuListener;
    4238
    4339import org.openstreetmap.josm.Main;
     
    6965import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    7066import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
     67import org.openstreetmap.josm.gui.PopupMenuHandler;
    7168import org.openstreetmap.josm.gui.SideButton;
    7269import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
     
    9592    private final NewAction newAction;
    9693   
    97     /** the popup menu */
    98     private final RelationDialogPopupMenu popupMenu;
     94    /** the popup menu and its handler */
     95    private final JPopupMenu popupMenu = new JPopupMenu();
     96    private final PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
    9997
    10098    private final JTextField filter;
     
    109107    private final DownloadMembersAction downloadMembersAction = new DownloadMembersAction();
    110108    private final DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction = new DownloadSelectedIncompleteMembersAction();
    111     private final SelectMembersAction selectMemebersAction = new SelectMembersAction(false);
     109    private final SelectMembersAction selectMembersAction = new SelectMembersAction(false);
    112110    private final SelectMembersAction addMembersToSelectionAction = new SelectMembersAction(true);
    113111    private final SelectRelationAction selectRelationAction = new SelectRelationAction(false);
    114112    private final SelectRelationAction addRelationToSelectionAction = new SelectRelationAction(true);
    115     /** add all selected primitives to the given realtions */
     113    /** add all selected primitives to the given relations */
    116114    private final AddSelectionToRelations addSelectionToRelations = new AddSelectionToRelations();
    117115   
     
    154152            }
    155153        });
     154
     155        // Setup popup menu handler
     156        setupPopupMenuHandler();
    156157       
    157158        JPanel pane = new JPanel(new BorderLayout());
     
    175176        InputMapUtils.addEnterAction(displaylist, selectRelationAction);
    176177
    177         popupMenu = new RelationDialogPopupMenu();
    178 
    179178        // Edit relation on Ctrl-Enter
    180179        displaylist.getActionMap().put("edit", editAction);
     
    186185    // inform all actions about list of relations they need
    187186    private void updateActionsRelationLists() {
    188         List<Relation> rels;
    189         rels = model.getSelectedNonNewRelations();
    190         downloadMembersAction.setRelations(rels);
    191 
    192         rels = model.getSelectedRelationsWithIncompleteMembers();
    193         downloadSelectedIncompleteMembersAction.setRelations(rels);
    194 
    195         rels = model.getSelectedRelations();
    196         editAction.setRelations(rels);
    197         deleteRelationsAction.setRelations(rels);
    198         addSelectionToRelations.setRelations(rels);
    199         selectMemebersAction.setRelations(rels);
    200         addMembersToSelectionAction.setRelations(rels);
    201         selectRelationAction.setRelations(rels);
    202         addRelationToSelectionAction.setRelations(rels);
    203         duplicateAction.setRelations(rels);
     187        popupMenuHandler.setPrimitives(model.getSelectedRelations());
    204188    }
    205189   
     
    509493            }
    510494        }
    511 
    512         /**
    513          * Replies the list of selected relations with incomplete members
    514          *
    515          * @return the list of selected relations with incomplete members
    516          */
    517         public List<Relation> getSelectedRelationsWithIncompleteMembers() {
    518             List<Relation> ret = getSelectedNonNewRelations();
    519             Iterator<Relation> it = ret.iterator();
    520             while(it.hasNext()) {
    521                 Relation r = it.next();
    522                 if (!r.hasIncompleteMembers()) {
    523                     it.remove();
    524                 }
    525             }
    526             return ret;
    527         }
    528495       
    529496        private void updateFilteredRelations() {
     
    569536
    570537        /**
    571          * Replies the list of selected, non-new relations. Empty list,
    572          * if there are no selected, non-new relations.
    573          *
    574          * @return the list of selected, non-new relations.
    575          */
    576         public List<Relation> getSelectedNonNewRelations() {
    577             ArrayList<Relation> ret = new ArrayList<Relation>();
    578             for (int i=0; i<getSize();i++) {
    579                 if (!selectionModel.isSelectedIndex(i)) {
    580                     continue;
    581                 }
    582                 if (getVisibleRelation(i).isNew()) {
    583                     continue;
    584                 }
    585                 ret.add(getVisibleRelation(i));
    586             }
    587             return ret;
    588         }
    589 
    590         /**
    591538         * Replies the list of selected relations. Empty list,
    592539         * if there are no selected relations.
     
    656603    }
    657604
    658     class RelationDialogPopupMenu extends JPopupMenu {
    659 
    660         public RelationDialogPopupMenu() {
    661             // -- download members action
    662             add(downloadMembersAction);
    663 
    664             // -- download incomplete members action
    665             add(downloadSelectedIncompleteMembersAction);
    666 
    667             addSeparator();
    668 
    669             // -- select members action
    670             add(selectMemebersAction);
    671             add(addMembersToSelectionAction);
    672 
    673             // -- select action
    674             add(selectRelationAction);
    675             add(addRelationToSelectionAction);
    676 
    677             addSeparator();
    678 
    679             add(addSelectionToRelations);
    680         }
    681     }
    682 
     605    private final void setupPopupMenuHandler() {
     606       
     607        // -- download members action
     608        popupMenuHandler.addAction(downloadMembersAction);
     609
     610        // -- download incomplete members action
     611        popupMenuHandler.addAction(downloadSelectedIncompleteMembersAction);
     612
     613        popupMenuHandler.addSeparator();
     614
     615        // -- select members action
     616        popupMenuHandler.addAction(selectMembersAction);
     617        popupMenuHandler.addAction(addMembersToSelectionAction);
     618
     619        // -- select action
     620        popupMenuHandler.addAction(selectRelationAction);
     621        popupMenuHandler.addAction(addRelationToSelectionAction);
     622
     623        popupMenuHandler.addSeparator();
     624
     625        popupMenuHandler.addAction(addSelectionToRelations);
     626    }
     627   
    683628    /* ---------------------------------------------------------------------------------- */
    684     /* Methods that can be called from plugins                                                                    */
     629    /* Methods that can be called from plugins                                            */
    685630    /* ---------------------------------------------------------------------------------- */
    686631
    687     public void addPopupMenuSeparator() {
    688         popupMenu.addSeparator();
    689     }
    690 
    691     public JMenuItem addPopupMenuAction(Action a) {
    692         return popupMenu.add(a);
    693     }
    694 
    695     public void addPopupMenuListener(PopupMenuListener l) {
    696         popupMenu.addPopupMenuListener(l);
    697     }
    698 
    699     public void removePopupMenuListener(PopupMenuListener l) {
    700         popupMenu.addPopupMenuListener(l);
     632    /**
     633     * Replies the popup menu handler.
     634     * @return The popup menu handler
     635     */
     636    public PopupMenuHandler getPopupMenuHandler() {
     637        return popupMenuHandler;
    701638    }
    702639
  • trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r5806 r5821  
    2424import javax.swing.AbstractAction;
    2525import javax.swing.AbstractListModel;
    26 import javax.swing.Action;
    2726import javax.swing.DefaultListSelectionModel;
    2827import javax.swing.JList;
     
    3534import javax.swing.event.ListSelectionEvent;
    3635import javax.swing.event.ListSelectionListener;
    37 import javax.swing.event.PopupMenuListener;
    3836
    3937import org.openstreetmap.josm.Main;
     
    6765import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener;
    6866import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
     67import org.openstreetmap.josm.gui.PopupMenuHandler;
    6968import org.openstreetmap.josm.gui.SideButton;
    7069import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    9291    private DownloadSelectedIncompleteMembersAction actDownloadSelectedIncompleteMembers;
    9392
    94     private SelectionPopup popupMenu;
     93    /** the popup menu and its handler */
     94    private final ListPopupMenu popupMenu;
     95    private final PopupMenuHandler popupMenuHandler;
    9596
    9697    /**
     
    148149        actDownloadSelectedIncompleteMembers = new DownloadSelectedIncompleteMembersAction();
    149150
     151        popupMenu = new ListPopupMenu(lstPrimitives);
     152        popupMenuHandler = setupPopupMenuHandler();
     153
    150154        lstPrimitives.addListSelectionListener(new ListSelectionListener() {
    151155            @Override
    152156            public void valueChanged(ListSelectionEvent e) {
    153157                actZoomToListSelection.valueChanged(e);
    154                 List<Relation> rels;
    155                 rels = model.getSelectedRelationsWithIncompleteMembers();
    156                 actDownloadSelectedIncompleteMembers.setRelations(rels);
    157                 rels = OsmPrimitive.getFilteredList(model.getSelected(), Relation.class);
    158                 actSetRelationSelection.setRelations(rels);
    159                 actEditRelationSelection.setRelations(rels);
     158                popupMenuHandler.setPrimitives(model.getSelected());
    160159            }
    161160        });
     
    164163        lstPrimitives.addMouseListener(new DblClickHandler());
    165164
    166         popupMenu = new SelectionPopup(lstPrimitives);
    167165        InputMapUtils.addEnterAction(lstPrimitives, actZoomToListSelection);
    168166    }
     
    221219    }
    222220
    223     /**
    224      * The popup menu for the selection list
    225      */
    226     class SelectionPopup extends ListPopupMenu {
    227         public SelectionPopup(JList list) {
    228             super(list);
    229             add(actZoomToJOSMSelection);
    230             add(actZoomToListSelection);
    231             addSeparator();
    232             add(actSetRelationSelection);
    233             add(actEditRelationSelection);
    234             addSeparator();
    235             add(actDownloadSelectedIncompleteMembers);
    236         }
    237     }
    238 
    239     public void addPopupMenuSeparator() {
    240         popupMenu.addSeparator();
    241     }
    242 
    243     public JMenuItem addPopupMenuAction(Action a) {
    244         return popupMenu.add(a);
    245     }
    246 
    247     public void addPopupMenuListener(PopupMenuListener l) {
    248         popupMenu.addPopupMenuListener(l);
    249     }
    250 
    251     public void removePopupMenuListener(PopupMenuListener l) {
    252         popupMenu.addPopupMenuListener(l);
     221    private final PopupMenuHandler setupPopupMenuHandler() {
     222        PopupMenuHandler handler = new PopupMenuHandler(popupMenu);
     223        handler.addAction(actZoomToJOSMSelection);
     224        handler.addAction(actZoomToListSelection);
     225        handler.addSeparator();
     226        handler.addAction(actSetRelationSelection);
     227        handler.addAction(actEditRelationSelection);
     228        handler.addSeparator();
     229        handler.addAction(actDownloadSelectedIncompleteMembers);
     230        return handler;
     231    }
     232
     233    /**
     234     * Replies the popup menu handler.
     235     * @return The popup menu handler
     236     */
     237    public PopupMenuHandler getPopupMenuHandler() {
     238        return popupMenuHandler;
    253239    }
    254240
     
    596582            }
    597583            setSelected(sel);
    598         }
    599 
    600         /**
    601          * Replies the list of selected relations with incomplete members
    602          *
    603          * @return the list of selected relations with incomplete members
    604          */
    605         public List<Relation> getSelectedRelationsWithIncompleteMembers() {
    606             List<Relation> ret = new LinkedList<Relation>();
    607             for(int i=0; i<getSize(); i++) {
    608                 if (!selectionModel.isSelectedIndex(i)) {
    609                     continue;
    610                 }
    611                 OsmPrimitive p = selection.get(i);
    612                 if (! (p instanceof Relation)) {
    613                     continue;
    614                 }
    615                 if (p.isNew()) {
    616                     continue;
    617                 }
    618                 Relation r = (Relation)p;
    619                 if (r.hasIncompleteMembers()) {
    620                     ret.add(r);
    621                 }
    622             }
    623             return ret;
    624584        }
    625585
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    r5800 r5821  
    3232
    3333import javax.swing.AbstractAction;
    34 import javax.swing.Action;
    3534import javax.swing.JComponent;
    3635import javax.swing.JLabel;
    37 import javax.swing.JMenuItem;
    3836import javax.swing.JPanel;
    3937import javax.swing.JPopupMenu;
     
    4442import javax.swing.event.ListSelectionEvent;
    4543import javax.swing.event.ListSelectionListener;
    46 import javax.swing.event.PopupMenuListener;
    4744import javax.swing.table.DefaultTableCellRenderer;
    4845import javax.swing.table.DefaultTableModel;
     
    7774import org.openstreetmap.josm.gui.MapFrame;
    7875import org.openstreetmap.josm.gui.MapView;
     76import org.openstreetmap.josm.gui.PopupMenuHandler;
    7977import org.openstreetmap.josm.gui.SideButton;
    8078import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
     
    111109 */
    112110public class PropertiesDialog extends ToggleDialog implements SelectionChangedListener, MapView.EditLayerChangeListener, DataSetListenerAdapter.Listener {
    113     // hook for roadsigns plugin to display a small
    114     // button in the upper right corner of this dialog
     111
     112    /**
     113     * hook for roadsigns plugin to display a small button in the upper right corner of this dialog
     114     */
    115115    public static final JPanel pluginHook = new JPanel();
    116116
     
    134134    private final JTable membershipTable = new JTable(membershipData);
    135135
    136     private JPopupMenu propertyMenu;
    137     private JPopupMenu membershipMenu;
     136    // Popup menus
     137    private final JPopupMenu propertyMenu = new JPopupMenu();
     138    private final JPopupMenu membershipMenu = new JPopupMenu();
     139
     140    // Popup menu handlers
     141    private final PopupMenuHandler propertyMenuHandler = new PopupMenuHandler(propertyMenu);
     142    private final PopupMenuHandler membershipMenuHandler = new PopupMenuHandler(membershipMenu);
    138143
    139144    private final Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>();
     
    202207    /**
    203208     * Create a new PropertiesDialog
     209     * @param mapFrame The parent map fram
    204210     */
    205211    public PropertiesDialog(MapFrame mapFrame) {
     
    370376    private void setupMembershipMenu() {
    371377        // setting up the membership table
    372         membershipMenu = new JPopupMenu();
    373         membershipMenu.add(addRelationToSelectionAction);
    374         membershipMenu.add(selectRelationAction);
    375         membershipMenu.add(addMembersToSelectionAction);
    376         membershipMenu.add(downloadSelectedIncompleteMembersAction);
     378        membershipMenuHandler.addAction(addRelationToSelectionAction);
     379        membershipMenuHandler.addAction(selectRelationAction);
     380        membershipMenuHandler.addAction(addMembersToSelectionAction);
     381        membershipMenuHandler.addAction(downloadSelectedIncompleteMembersAction);
    377382        membershipMenu.addSeparator();
    378383        membershipMenu.add(helpAction);
     
    389394                    idx = new int[]{row};
    390395                }
    391                 List<Relation> rels =  new ArrayList<Relation>(10);
     396                List<Relation> rels = new ArrayList<Relation>();
    392397                for (int i: idx) {
    393398                    Relation r = (Relation) (membershipData.getValueAt(i, 0));
    394399                    rels.add(r);
    395400                }
    396                 selectRelationAction.setRelations(rels);
    397                 addRelationToSelectionAction.setRelations(rels);
    398                 addMembersToSelectionAction.setRelations(rels);
    399                 downloadSelectedIncompleteMembersAction.setRelations(rels);
     401                membershipMenuHandler.setPrimitives(rels);
    400402                membershipMenu.show(membershipTable, p.x, p.y-3);
    401403            }
     
    407409     */
    408410    private void setupPropertiesMenu() {
    409         propertyMenu = new JPopupMenu();
    410411        propertyMenu.add(pasteValueAction);
    411412        propertyMenu.add(copyValueAction);
     
    695696
    696697    // <editor-fold defaultstate="collapsed" desc="Methods that are called by plugins to extend fuctionalty ">
    697     public void addPropertyPopupMenuSeparator() {
    698         propertyMenu.addSeparator();
    699     }
    700 
    701     public JMenuItem addPropertyPopupMenuAction(Action a) {
    702         return propertyMenu.add(a);
    703     }
    704 
    705     public void addPropertyPopupMenuListener(PopupMenuListener l) {
    706         propertyMenu.addPopupMenuListener(l);
    707     }
    708 
    709     public void removePropertyPopupMenuListener(PopupMenuListener l) {
    710         propertyMenu.addPopupMenuListener(l);
     698   
     699    /**
     700     * Replies the property popup menu handler.
     701     * @return The property popup menu handler
     702     */
     703    public PopupMenuHandler getPropertyPopupMenuHandler() {
     704        return propertyMenuHandler;
    711705    }
    712706
     
    721715    }
    722716
    723     public void addMembershipPopupMenuSeparator() {
    724         membershipMenu.addSeparator();
    725     }
    726 
    727     public JMenuItem addMembershipPopupMenuAction(Action a) {
    728         return membershipMenu.add(a);
    729     }
    730 
    731     public void addMembershipPopupMenuListener(PopupMenuListener l) {
    732         membershipMenu.addPopupMenuListener(l);
    733     }
    734 
    735     public void removeMembershipPopupMenuListener(PopupMenuListener l) {
    736         membershipMenu.addPopupMenuListener(l);
     717    /**
     718     * Replies the membership popup menu handler.
     719     * @return The membership popup menu handler
     720     */
     721    public PopupMenuHandler getMembershipPopupMenuHandler() {
     722        return membershipMenuHandler;
    737723    }
    738724
Note: See TracChangeset for help on using the changeset viewer.