KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > PopupMenu


1 /*
2  * @(#)PopupMenu.java 1.29 04/03/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import java.awt.peer.PopupMenuPeer;
11 import javax.accessibility.*;
12
13
14 /**
15  * A class that implements a menu which can be dynamically popped up
16  * at a specified position within a component.
17  * <p>
18  * As the inheritance hierarchy implies, a <code>PopupMenu</code>
19  * can be used anywhere a <code>Menu</code> can be used.
20  * However, if you use a <code>PopupMenu</code> like a <code>Menu</code>
21  * (e.g., you add it to a <code>MenuBar</code>), then you <b>cannot</b>
22  * call <code>show</code> on that <code>PopupMenu</code>.
23  *
24  * @version 1.29 03/16/04
25  * @author Amy Fowler
26  */

27 public class PopupMenu extends Menu JavaDoc {
28
29     private static final String JavaDoc base = "popup";
30     static int nameCounter = 0;
31
32     /*
33      * JDK 1.1 serialVersionUID
34      */

35     private static final long serialVersionUID = -4620452533522760060L;
36
37     /**
38      * Creates a new popup menu with an empty name.
39      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
40      * returns true.
41      * @see java.awt.GraphicsEnvironment#isHeadless
42      */

43     public PopupMenu() throws HeadlessException JavaDoc {
44     this("");
45     }
46
47     /**
48      * Creates a new popup menu with the specified name.
49      *
50      * @param label a non-<code>null</code> string specifying
51      * the popup menu's label
52      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
53      * returns true.
54      * @see java.awt.GraphicsEnvironment#isHeadless
55      */

56     public PopupMenu(String JavaDoc label) throws HeadlessException JavaDoc {
57     super(label);
58     }
59
60     /**
61      * Constructs a name for this <code>MenuComponent</code>.
62      * Called by <code>getName</code> when the name is <code>null</code>.
63      */

64     String JavaDoc constructComponentName() {
65         synchronized (getClass()) {
66         return base + nameCounter++;
67     }
68     }
69
70     /**
71      * Creates the popup menu's peer.
72      * The peer allows us to change the appearance of the popup menu without
73      * changing any of the popup menu's functionality.
74      */

75     public void addNotify() {
76         synchronized (getTreeLock()) {
77         // If our parent is not a Component, then this PopupMenu is
78
// really just a plain, old Menu.
79
if (parent != null && !(parent instanceof Component JavaDoc)) {
80             super.addNotify();
81         }
82         else {
83             if (peer == null)
84             peer = Toolkit.getDefaultToolkit().createPopupMenu(this);
85         int nitems = getItemCount();
86         for (int i = 0 ; i < nitems ; i++) {
87             MenuItem JavaDoc mi = getItem(i);
88             mi.parent = this;
89             mi.addNotify();
90         }
91         }
92     }
93     }
94
95    /**
96      * Shows the popup menu at the x, y position relative to an origin
97      * component.
98      * The origin component must be contained within the component
99      * hierarchy of the popup menu's parent. Both the origin and the parent
100      * must be showing on the screen for this method to be valid.
101      * <p>
102      * If this <code>PopupMenu</code> is being used as a <code>Menu</code>
103      * (i.e., it has a non-<code>Component</code> parent),
104      * then you cannot call this method on the <code>PopupMenu</code>.
105      *
106      * @param origin the component which defines the coordinate space
107      * @param x the x coordinate position to popup the menu
108      * @param y the y coordinate position to popup the menu
109      * @exception NullPointerException if the parent is <code>null</code>
110      * @exception IllegalArgumentException if this <code>PopupMenu</code>
111      * has a non-<code>Component</code> parent
112      * @exception IllegalArgumentException if the origin is not in the
113      * parent's heirarchy
114      * @exception RuntimeException if the parent is not showing on screen
115      */

116     public void show(Component JavaDoc origin, int x, int y) {
117         // Use localParent for thread safety.
118
MenuContainer JavaDoc localParent = parent;
119     if (localParent == null) {
120         throw new NullPointerException JavaDoc("parent is null");
121     }
122         if (!(localParent instanceof Component JavaDoc)) {
123         throw new IllegalArgumentException JavaDoc(
124             "PopupMenus with non-Component parents cannot be shown");
125     }
126         Component JavaDoc compParent = (Component JavaDoc)localParent;
127     if (compParent != origin &&
128         compParent instanceof Container JavaDoc &&
129         !((Container JavaDoc)compParent).isAncestorOf(origin)) {
130             throw new IllegalArgumentException JavaDoc(
131             "origin not in parent's hierarchy");
132     }
133     if (compParent.getPeer() == null || !compParent.isShowing()) {
134         throw new RuntimeException JavaDoc("parent not showing on screen");
135     }
136     if (peer == null) {
137         addNotify();
138     }
139     synchronized (getTreeLock()) {
140         if (peer != null) {
141             ((PopupMenuPeer)peer).show(
142             new Event JavaDoc(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
143         }
144     }
145     }
146
147
148 /////////////////
149
// Accessibility support
150
////////////////
151

152     /**
153      * Gets the <code>AccessibleContext</code> associated with this
154      * <code>PopupMenu</code>.
155      *
156      * @return the <code>AccessibleContext</code> of this
157      * <code>PopupMenu</code>
158      */

159     public AccessibleContext getAccessibleContext() {
160         if (accessibleContext == null) {
161             accessibleContext = new AccessibleAWTPopupMenu();
162         }
163         return accessibleContext;
164     }
165
166     /**
167      * Inner class of PopupMenu used to provide default support for
168      * accessibility. This class is not meant to be used directly by
169      * application developers, but is instead meant only to be
170      * subclassed by menu component developers.
171      * <p>
172      * The class used to obtain the accessible role for this object.
173      */

174     protected class AccessibleAWTPopupMenu extends AccessibleAWTMenu
175     {
176         /*
177          * JDK 1.3 serialVersionUID
178          */

179         private static final long serialVersionUID = -4282044795947239955L;
180
181         /**
182          * Get the role of this object.
183          *
184          * @return an instance of AccessibleRole describing the role of the
185          * object
186          */

187         public AccessibleRole getAccessibleRole() {
188             return AccessibleRole.POPUP_MENU;
189         }
190
191     } // class AccessibleAWTPopupMenu
192

193 }
194
Popular Tags