001    /*
002      Copyright (C) 2001-2002 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.gui.swing;
019    
020    import java.awt.event.MouseEvent;
021    import java.util.Arrays;
022    import java.util.Collection;
023    import java.util.Iterator;
024    import org.apache.log4j.Logger;
025    import org.objectweb.jac.aspects.gui.DisplayContext;
026    import org.objectweb.jac.aspects.gui.GuiAC;
027    import org.objectweb.jac.aspects.gui.ResourceManager;
028    import org.objectweb.jac.core.rtti.ClassItem;
029    import org.objectweb.jac.core.rtti.ClassRepository;
030    import org.objectweb.jac.core.rtti.MethodItem;
031    
032    /**
033     * Gather common swing events code
034     */
035    public class SwingEvents {
036        static Logger logger = Logger.getLogger("gui.events");
037    
038        /**
039         * Displays a popup menu for an object
040         * @param context
041         * @param object the object to display a menu for
042         * @param event the mouse event that triggered the popup. The popup
043         * will be placed at the coordinates of this event.
044         * @see #showObjectsMenu(DisplayContext,Object[],MouseEvent)
045         */
046        public static void showObjectMenu(DisplayContext context, Object object, 
047                                          MouseEvent event) 
048        {
049            showObjectsMenu(context,new Object[] {object}, event);
050        }
051    
052        /**
053         * Displays a popup menu for some objects. The menus that would be
054         * displayed for each object are concatenated.
055         * @param context
056         * @param objects the objects to display a menu for
057         * @param event the mouse event that triggered the popup. The popup
058         * will be placed at the coordinates of this event.
059         * @see #showObjectMenu(DisplayContext,Object,MouseEvent) */
060        public static void showObjectsMenu(DisplayContext context, Object[] objects, 
061                                           MouseEvent event) 
062        {
063            logger.debug("showObjectsMenu for "+Arrays.asList(objects));
064            ObjectPopup dynPopup = new ObjectPopup(context);
065            for (int o=0; o<objects.length; o++) {
066                ClassItem cli = ClassRepository.get().getClass(objects[o]);
067    
068                dynPopup.addViewItem(objects[o],"View "+cli.getShortName(),
069                                     ResourceManager.getIconResource("view_icon"));
070    
071                MethodItem[] menu = GuiAC.getMenu(cli);
072                logger.debug("  menu for "+objects[o]+":"+Arrays.asList(menu));
073          
074                if (menu != null) {
075                    if (menu.length>0) 
076                        dynPopup.addSeparator();
077                    for (int i=0; i<menu.length; i++) {
078                        if (menu[i] == null) {
079                            dynPopup.addSeparator();
080                        } else {
081                            String icon = GuiAC.getIcon(menu[i]);
082                            if (icon==null) {
083                                icon = ResourceManager.getResource("blank_icon");
084                            }
085                            dynPopup.addMethodItem(objects[o],menu[i],icon);
086                        }
087                    }
088                } else {
089                    Collection meths = cli.getMethods();
090                    if (meths.size()>0) 
091                        dynPopup.addSeparator();
092                    Iterator it = meths.iterator();
093                    while (it.hasNext()) {                     
094                        MethodItem mi = ((MethodItem[])it.next())[0];
095                        // do not show the getters
096                        if (mi.isGetter() || mi.isRemover() || mi.isJacMethod()) 
097                            continue;
098                        String icon = GuiAC.getIcon(mi);
099                        if (icon==null) {
100                            icon = ResourceManager.getResource("blank_icon");
101                        }
102                        dynPopup.addMethodItem(objects[o],mi,icon);
103                    }
104                }
105                if (o+1<objects.length)
106                    dynPopup.addSeparator();
107            }
108            dynPopup.show(event.getComponent(), event.getX(), event.getY());
109        }
110    }