001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.lang.reflect.InvocationTargetException;
023:
024: import javax.accessibility.AccessibleContext;
025: import javax.accessibility.AccessibleRole;
026:
027: import org.apache.harmony.awt.internal.nls.Messages;
028:
029: public class PopupMenu extends Menu {
030:
031: private static final long serialVersionUID = -4620452533522760060L;
032:
033: protected class AccessibleAWTPopupMenu extends AccessibleAWTMenu {
034:
035: private static final long serialVersionUID = -4282044795947239955L;
036:
037: @Override
038: public AccessibleRole getAccessibleRole() {
039: return AccessibleRole.POPUP_MENU;
040: }
041:
042: }
043:
044: public PopupMenu() throws HeadlessException {
045: this (""); //$NON-NLS-1$
046: toolkit.lockAWT();
047: try {
048: } finally {
049: toolkit.unlockAWT();
050: }
051: }
052:
053: public PopupMenu(String label) throws HeadlessException {
054: super (label);
055: toolkit.lockAWT();
056: try {
057: } finally {
058: toolkit.unlockAWT();
059: }
060: }
061:
062: @Override
063: public void addNotify() {
064: toolkit.lockAWT();
065: try {
066: super .addNotify();
067: } finally {
068: toolkit.unlockAWT();
069: }
070: }
071:
072: @Override
073: public AccessibleContext getAccessibleContext() {
074: toolkit.lockAWT();
075: try {
076: return super .getAccessibleContext();
077: } finally {
078: toolkit.unlockAWT();
079: }
080: }
081:
082: public void show(final Component origin, final int x, final int y) {
083: toolkit.lockAWT();
084: try {
085: MenuContainer parent = getParent();
086: if (parent == null) {
087: // awt.71=Parent is null
088: throw new NullPointerException(Messages
089: .getString("awt.71")); //$NON-NLS-1$
090: }
091: if (!(parent instanceof Component)) {
092: // awt.11F=parent is not a component
093: throw new IllegalArgumentException(Messages
094: .getString("awt.11F")); //$NON-NLS-1$
095: }
096: if (parent != origin
097: && !((parent instanceof Container) && ((Container) parent)
098: .isAncestorOf(origin))) {
099: // awt.120=origin is not a descendant of parent
100: throw new IllegalArgumentException(Messages
101: .getString("awt.120")); //$NON-NLS-1$
102: }
103: if (!((Component) parent).isShowing()) {
104: // awt.121=parent must be showing on the screen
105: throw new RuntimeException(Messages
106: .getString("awt.121")); //$NON-NLS-1$
107: }
108:
109: if (EventQueue.isDispatchThread()) {
110: showImpl(origin, x, y);
111: } else {
112: try {
113: toolkit.unsafeInvokeAndWait(new Runnable() {
114: public void run() {
115: showImpl(origin, x, y);
116: }
117: });
118: } catch (InterruptedException e) {
119: e.printStackTrace();
120: } catch (InvocationTargetException e) {
121: e.printStackTrace();
122: }
123: }
124:
125: } finally {
126: toolkit.unlockAWT();
127: }
128: }
129:
130: /**
131: * Actually show the popup menu.
132: * This method must be called on event dispatch thread
133: *
134: * @param origin - the component to show menu at
135: * @param x - x coordinate relative to origin
136: * @param y - y coordinate relative to origin
137: */
138: //
139: private void showImpl(Component origin, int x, int y) {
140: Point originLocation = origin.getLocationOnScreen();
141: super .show(originLocation.x + x, originLocation.y + y, true);
142: }
143:
144: @Override
145: void hide() {
146: if (!isVisible()) {
147: return;
148: }
149:
150: super .hide();
151: }
152:
153: @Override
154: boolean hasDefaultFont() {
155: return true;
156: }
157:
158: @Override
159: AccessibleContext createAccessibleContext() {
160: return new AccessibleAWTPopupMenu();
161: }
162:
163: }
|