KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > SwingEvents


1 /*
2   Copyright (C) 2001-2002 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.swing;
19
20 import java.awt.event.MouseEvent JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.aspects.gui.DisplayContext;
26 import org.objectweb.jac.aspects.gui.GuiAC;
27 import org.objectweb.jac.aspects.gui.ResourceManager;
28 import org.objectweb.jac.core.rtti.ClassItem;
29 import org.objectweb.jac.core.rtti.ClassRepository;
30 import org.objectweb.jac.core.rtti.MethodItem;
31
32 /**
33  * Gather common swing events code
34  */

35 public class SwingEvents {
36     static Logger logger = Logger.getLogger("gui.events");
37
38     /**
39      * Displays a popup menu for an object
40      * @param context
41      * @param object the object to display a menu for
42      * @param event the mouse event that triggered the popup. The popup
43      * will be placed at the coordinates of this event.
44      * @see #showObjectsMenu(DisplayContext,Object[],MouseEvent)
45      */

46     public static void showObjectMenu(DisplayContext context, Object JavaDoc object,
47                                       MouseEvent JavaDoc event)
48     {
49         showObjectsMenu(context,new Object JavaDoc[] {object}, event);
50     }
51
52     /**
53      * Displays a popup menu for some objects. The menus that would be
54      * displayed for each object are concatenated.
55      * @param context
56      * @param objects the objects to display a menu for
57      * @param event the mouse event that triggered the popup. The popup
58      * will be placed at the coordinates of this event.
59      * @see #showObjectMenu(DisplayContext,Object,MouseEvent) */

60     public static void showObjectsMenu(DisplayContext context, Object JavaDoc[] objects,
61                                        MouseEvent JavaDoc event)
62     {
63         logger.debug("showObjectsMenu for "+Arrays.asList(objects));
64         ObjectPopup dynPopup = new ObjectPopup(context);
65         for (int o=0; o<objects.length; o++) {
66             ClassItem cli = ClassRepository.get().getClass(objects[o]);
67
68             dynPopup.addViewItem(objects[o],"View "+cli.getShortName(),
69                                  ResourceManager.getIconResource("view_icon"));
70
71             MethodItem[] menu = GuiAC.getMenu(cli);
72             logger.debug(" menu for "+objects[o]+":"+Arrays.asList(menu));
73       
74             if (menu != null) {
75                 if (menu.length>0)
76                     dynPopup.addSeparator();
77                 for (int i=0; i<menu.length; i++) {
78                     if (menu[i] == null) {
79                         dynPopup.addSeparator();
80                     } else {
81                         String JavaDoc icon = GuiAC.getIcon(menu[i]);
82                         if (icon==null) {
83                             icon = ResourceManager.getResource("blank_icon");
84                         }
85                         dynPopup.addMethodItem(objects[o],menu[i],icon);
86                     }
87                 }
88             } else {
89                 Collection JavaDoc meths = cli.getMethods();
90                 if (meths.size()>0)
91                     dynPopup.addSeparator();
92                 Iterator JavaDoc it = meths.iterator();
93                 while (it.hasNext()) {
94                     MethodItem mi = ((MethodItem[])it.next())[0];
95                     // do not show the getters
96
if (mi.isGetter() || mi.isRemover() || mi.isJacMethod())
97                         continue;
98                     String JavaDoc icon = GuiAC.getIcon(mi);
99                     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 }
111
Popular Tags