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: *
020: * @version $Revision$
021: */package java.awt;
022:
023: import org.apache.harmony.awt.gl.MultiRectArea;
024: import org.apache.harmony.awt.wtk.NativeWindow;
025:
026: /**
027: * Non-component window for displaying menus and drop-down lists
028: */
029: abstract class PopupBox {
030:
031: NativeWindow nativeWindow;
032:
033: final Dimension size = new Dimension();
034: final Point location = new Point();
035: private Window owner;
036: private PopupBox parent;
037: private PopupBox activeChild;
038: private boolean visible;
039: private ModalContext modalContext;
040:
041: final Toolkit toolkit = Toolkit.getDefaultToolkit();
042:
043: PopupBox() {
044: }
045:
046: void addNotify() {
047: if (nativeWindow == null) {
048: nativeWindow = toolkit.createPopupNativeWindow(this );
049: }
050: }
051:
052: void removeNotify() {
053: if (nativeWindow != null) {
054: toolkit.removePopupNativeWindow(nativeWindow);
055: NativeWindow temp = nativeWindow;
056: nativeWindow = null;
057: temp.dispose();
058: }
059: }
060:
061: Dimension getSize() {
062: return new Dimension(size);
063: }
064:
065: int getHeight() {
066: return getSize().height;
067: }
068:
069: Point getLocation() {
070: return new Point(location);
071: }
072:
073: Point getScreenLocation() {
074: return new Point(location);
075: }
076:
077: boolean contains(Point p) {
078: Rectangle r = new Rectangle(getScreenLocation(), getSize());
079: return r.contains(p);
080: }
081:
082: Window getOwner() {
083: return owner;
084: }
085:
086: PopupBox getParent() {
087: return parent;
088: }
089:
090: void setParent(PopupBox p) {
091: parent = p;
092: }
093:
094: final PopupBox getActiveChild() {
095: return activeChild;
096: }
097:
098: final NativeWindow getNativeWindow() {
099: return nativeWindow;
100: }
101:
102: final boolean isVisible() {
103: return visible;
104: }
105:
106: boolean isMenu() {
107: return false;
108: }
109:
110: final void setModal(boolean modal) {
111: if (modal) {
112: if (modalContext == null) {
113: modalContext = new ModalContext();
114: }
115: } else {
116: if (modalContext != null) {
117: if (modalContext.isModalLoopRunning()) {
118: modalContext.endModalLoop();
119: }
120: modalContext = null;
121: }
122: }
123: }
124:
125: final boolean isModal() {
126: return modalContext != null;
127: }
128:
129: boolean isMenuBar() {
130: return false;
131: }
132:
133: Rectangle calculateBounds() {
134: return new Rectangle(location, size);
135: }
136:
137: void paint(MultiRectArea clipArea) {
138: if (nativeWindow != null) {
139: Graphics gr = getGraphics(clipArea);
140: if (gr != null) {
141: paint(gr);
142: }
143: gr.dispose();
144: }
145: }
146:
147: abstract void paint(Graphics gr);
148:
149: /**
150: * Mouse events handler
151: * @param eventId - one of the MouseEvent.MOUSE_* constants
152: * @param where - mouse location
153: * @param mouseButton - mouse button that was pressed or released
154: * @param when - event time
155: * @param modifiers - input event modifiers
156: */
157: abstract void onMouseEvent(int eventId, Point where,
158: int mouseButton, long when, int modifiers, int wheelRotation);
159:
160: /**
161: * Keyboard events handler
162: * @param eventId - one of the KeyEvent.KEY_* constants
163: * @param vKey - the key code
164: * @param when - event time
165: * @param modifiers - input event modifiers
166: */
167: abstract void onKeyEvent(int eventId, int vKey, long when,
168: int modifiers);
169:
170: /**
171: * Determine if the pop-up box should be hidden if the mouse button was
172: * pressed at the <code>start</code> position
173: * and released at the <code>end</code> position
174: * @param start - the location where mouse button was pressed
175: * @param end - the location where mouse button was released
176: * @return true if the pop-up box should be hidden; false otherwise
177: */
178: boolean closeOnUngrab(Point start, Point end) {
179: return true;
180: }
181:
182: /**
183: * Pass the keyboard event to the topmost active pop-up box
184: * @param eventId - one of the KeyEvent.KEY_* constants
185: * @param vKey - the key code
186: * @param when - event time
187: * @param modifiers - input event modifiers
188: */
189: final void dispatchKeyEvent(int eventId, int vKey, long when,
190: int modifiers) {
191: if (activeChild != null) {
192: activeChild
193: .dispatchKeyEvent(eventId, vKey, when, modifiers);
194: } else {
195: onKeyEvent(eventId, vKey, when, modifiers);
196: }
197: }
198:
199: void setDefaultCursor() {
200: Cursor.getDefaultCursor().getNativeCursor().setCursor(
201: nativeWindow.getId());
202: }
203:
204: private void show(int x, int y, int w, int h) {
205: visible = true;
206: addNotify();
207:
208: location.setLocation(x, y);
209: size.setSize(w, h);
210: nativeWindow.setBounds(x, y, w, h, 0);
211: nativeWindow.setVisible(true);
212: setDefaultCursor();
213: }
214:
215: void show(Frame owner) {
216: this .owner = owner;
217:
218: Rectangle bounds = calculateBounds();
219: show(bounds.x, bounds.y, bounds.width, bounds.height);
220: }
221:
222: void show(Point location, Dimension size, Window owner) {
223: this .owner = owner;
224: if (parent != null) {
225: parent.activeChild = this ;
226: }
227: show(location.x, location.y, size.width, size.height);
228:
229: toolkit.dispatcher.popupDispatcher.activate(this );
230: if (modalContext != null) {
231: modalContext.runModalLoop();
232: }
233: }
234:
235: void hide() {
236: if (!visible) {
237: return;
238: }
239: visible = false;
240:
241: if (activeChild != null) {
242: activeChild.hide();
243: }
244:
245: if (nativeWindow != null) {
246: nativeWindow.setVisible(false);
247: }
248:
249: if (parent != null) {
250: parent.activeChild = null;
251: }
252:
253: if (modalContext != null) {
254: modalContext.endModalLoop();
255: }
256:
257: toolkit.dispatcher.popupDispatcher.deactivate(this );
258: }
259:
260: Graphics getGraphics(MultiRectArea clip) {
261: Dimension size = getSize();
262: Graphics gr = toolkit.getGraphicsFactory().getGraphics2D(
263: nativeWindow, 0, 0, size.width, size.height);
264: if (gr != null && clip != null) {
265: gr.setClip(clip);
266: }
267: return gr;
268: }
269:
270: }
|