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 Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.event.WindowEvent;
023: import java.lang.ref.WeakReference;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026:
027: import javax.accessibility.AccessibleContext;
028: import javax.accessibility.AccessibleRole;
029: import javax.accessibility.AccessibleStateSet;
030:
031: import org.apache.harmony.awt.gl.MultiRectArea;
032: import org.apache.harmony.awt.wtk.NativeWindow;
033:
034: public class Frame extends Window implements MenuContainer {
035: private static final long serialVersionUID = 2673458971256075116L;
036:
037: @Deprecated
038: public static final int DEFAULT_CURSOR = 0;
039:
040: @Deprecated
041: public static final int CROSSHAIR_CURSOR = 1;
042:
043: @Deprecated
044: public static final int TEXT_CURSOR = 2;
045:
046: @Deprecated
047: public static final int WAIT_CURSOR = 3;
048:
049: @Deprecated
050: public static final int SW_RESIZE_CURSOR = 4;
051:
052: @Deprecated
053: public static final int SE_RESIZE_CURSOR = 5;
054:
055: @Deprecated
056: public static final int NW_RESIZE_CURSOR = 6;
057:
058: @Deprecated
059: public static final int NE_RESIZE_CURSOR = 7;
060:
061: @Deprecated
062: public static final int N_RESIZE_CURSOR = 8;
063:
064: @Deprecated
065: public static final int S_RESIZE_CURSOR = 9;
066:
067: @Deprecated
068: public static final int W_RESIZE_CURSOR = 10;
069:
070: @Deprecated
071: public static final int E_RESIZE_CURSOR = 11;
072:
073: @Deprecated
074: public static final int HAND_CURSOR = 12;
075:
076: @Deprecated
077: public static final int MOVE_CURSOR = 13;
078:
079: public static final int NORMAL = 0;
080:
081: public static final int ICONIFIED = 1;
082:
083: public static final int MAXIMIZED_HORIZ = 2;
084:
085: public static final int MAXIMIZED_VERT = 4;
086:
087: public static final int MAXIMIZED_BOTH = 6;
088:
089: private int state = NORMAL;
090: private MenuBar menuBar;
091:
092: private Image iconImage;
093:
094: private Rectangle maximizedBounds;
095:
096: protected class AccessibleAWTFrame extends AccessibleAWTWindow {
097: private static final long serialVersionUID = -6172960752956030250L;
098:
099: @Override
100: public AccessibleRole getAccessibleRole() {
101: toolkit.lockAWT();
102: try {
103: return AccessibleRole.FRAME;
104: } finally {
105: toolkit.unlockAWT();
106: }
107: }
108:
109: @Override
110: public AccessibleStateSet getAccessibleStateSet() {
111: // do nothing(Window does everything already)
112: return super .getAccessibleStateSet();
113: }
114: }
115:
116: public Frame(String title, GraphicsConfiguration gc) {
117: super (null, gc);
118: toolkit.lockAWT();
119: try {
120: setTitle(title);
121: setResizable(true);
122: toolkit.allFrames.add(this );
123: } finally {
124: toolkit.unlockAWT();
125: }
126: }
127:
128: public Frame(String title) throws HeadlessException {
129: this (title, null);
130: toolkit.lockAWT();
131: try {
132: } finally {
133: toolkit.unlockAWT();
134: }
135: }
136:
137: public Frame() throws HeadlessException {
138: this (""); //$NON-NLS-1$
139: toolkit.lockAWT();
140: try {
141: } finally {
142: toolkit.unlockAWT();
143: }
144: }
145:
146: public Frame(GraphicsConfiguration gc) {
147: this ("", gc); //$NON-NLS-1$
148: toolkit.lockAWT();
149: try {
150: } finally {
151: toolkit.unlockAWT();
152: }
153: }
154:
155: @Override
156: protected void finalize() throws Throwable {
157: // do nothing
158: }
159:
160: @Override
161: public void remove(MenuComponent popup) {
162: toolkit.lockAWT();
163: try {
164: // TODO: implement
165: // temporary solve
166: super .remove(popup);
167: } finally {
168: toolkit.unlockAWT();
169: }
170: }
171:
172: public int getState() {
173: toolkit.lockAWT();
174: try {
175: return getExtendedState();
176: } finally {
177: toolkit.unlockAWT();
178: }
179: }
180:
181: @Override
182: public void addNotify() {
183: toolkit.lockAWT();
184: try {
185: super .addNotify();
186: if (menuBar != null) {
187: menuBar.addNotify();
188: }
189: } finally {
190: toolkit.unlockAWT();
191: }
192: }
193:
194: @Override
195: public AccessibleContext getAccessibleContext() {
196: toolkit.lockAWT();
197: try {
198: return super .getAccessibleContext();
199: } finally {
200: toolkit.unlockAWT();
201: }
202: }
203:
204: @Override
205: protected String paramString() {
206: toolkit.lockAWT();
207: try {
208: return (super .paramString() + ",title=" + getTitle() + //$NON-NLS-1$
209: (isResizable() ? ",resizable" : "") + //$NON-NLS-1$ //$NON-NLS-2$
210: "," + getStateString()); //$NON-NLS-1$
211: } finally {
212: toolkit.unlockAWT();
213: }
214: }
215:
216: private String getStateString() {
217: String str = ""; //$NON-NLS-1$
218: if ((state & NORMAL) != 0) {
219: str = "normal"; //$NON-NLS-1$
220: }
221: if ((state & ICONIFIED) != 0) {
222: str = "iconified"; //$NON-NLS-1$
223: }
224: if ((state & MAXIMIZED_VERT) != 0) {
225: str = "maximized_vert"; //$NON-NLS-1$
226: }
227: if ((state & MAXIMIZED_HORIZ) != 0) {
228: str = "maximized_horiz"; //$NON-NLS-1$
229: }
230: if ((state & MAXIMIZED_BOTH) != 0) {
231: str = "maximized"; //$NON-NLS-1$
232: }
233:
234: return str;
235: }
236:
237: @Override
238: public void removeNotify() {
239: toolkit.lockAWT();
240: try {
241: if (menuBar != null) {
242: menuBar.removeNotify();
243: }
244: super .removeNotify();
245: } finally {
246: toolkit.unlockAWT();
247: }
248: }
249:
250: /**
251: * @deprecated
252: */
253: @Deprecated
254: public void setCursor(int cursorType) {
255: toolkit.lockAWT();
256: try {
257: setCursor(Cursor.getPredefinedCursor(cursorType));
258: } finally {
259: toolkit.unlockAWT();
260: }
261: }
262:
263: /**
264: * @deprecated
265: */
266: @Deprecated
267: public int getCursorType() {
268: toolkit.lockAWT();
269: try {
270: return getCursor().getType();
271: } finally {
272: toolkit.unlockAWT();
273: }
274: }
275:
276: public static Frame[] getFrames() {
277: Toolkit.staticLockAWT();
278: try {
279: return Toolkit.getDefaultToolkit().allFrames.getFrames();
280: } finally {
281: Toolkit.staticUnlockAWT();
282: }
283: }
284:
285: /**
286: * Returns icon image of this frame.
287: */
288: @Override
289: public Image getIconImage() {
290: toolkit.lockAWT();
291: try {
292: return iconImage;
293: } finally {
294: toolkit.unlockAWT();
295: }
296: }
297:
298: public Rectangle getMaximizedBounds() {
299: toolkit.lockAWT();
300: try {
301: return maximizedBounds;
302: } finally {
303: toolkit.unlockAWT();
304: }
305: }
306:
307: public MenuBar getMenuBar() {
308: toolkit.lockAWT();
309: try {
310: return menuBar;
311: } finally {
312: toolkit.unlockAWT();
313: }
314: }
315:
316: @Override
317: public String getTitle() {
318: toolkit.lockAWT();
319: try {
320: return super .getTitle();
321: } finally {
322: toolkit.unlockAWT();
323: }
324: }
325:
326: @Override
327: public boolean isResizable() {
328: toolkit.lockAWT();
329: try {
330: return super .isResizable();
331: } finally {
332: toolkit.unlockAWT();
333: }
334: }
335:
336: @Override
337: public boolean isUndecorated() {
338: toolkit.lockAWT();
339: try {
340: return super .isUndecorated();
341: } finally {
342: toolkit.unlockAWT();
343: }
344: }
345:
346: public int getExtendedState() {
347: toolkit.lockAWT();
348: try {
349: return state;
350: } finally {
351: toolkit.unlockAWT();
352: }
353: }
354:
355: public void setExtendedState(int state) {
356: toolkit.lockAWT();
357: try {
358: int oldState = this .state;
359: int newState = NORMAL;
360:
361: if (((state & ICONIFIED) > 0)
362: && toolkit.isFrameStateSupported(ICONIFIED)) {
363: newState |= ICONIFIED;
364: }
365: if (((state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH)
366: && toolkit.isFrameStateSupported(MAXIMIZED_BOTH)) {
367: newState |= MAXIMIZED_BOTH;
368: } else {
369: if (((state & MAXIMIZED_VERT) > 0)
370: && toolkit
371: .isFrameStateSupported(MAXIMIZED_VERT)) {
372: newState |= MAXIMIZED_VERT;
373: }
374: if (((state & MAXIMIZED_HORIZ) > 0)
375: && toolkit
376: .isFrameStateSupported(MAXIMIZED_HORIZ)) {
377: newState |= MAXIMIZED_HORIZ;
378: }
379: }
380:
381: if (newState != oldState) {
382: NativeWindow window = behaviour.getNativeWindow();
383:
384: this .state = newState;
385: if (window != null) {
386: window.setState(state);
387: }
388: postStateEvents(oldState);
389: }
390: } finally {
391: toolkit.unlockAWT();
392: }
393: }
394:
395: void updateExtendedState(int state) {
396: int oldState = this .state;
397:
398: if (state != oldState) {
399: this .state = state;
400: postStateEvents(oldState);
401: }
402: }
403:
404: private void postStateEvents(int oldState) {
405: int newIconified = (state & ICONIFIED);
406: int oldIconified = (oldState & ICONIFIED);
407:
408: if (newIconified != oldIconified) {
409: int eventID = (newIconified != 0 ? WindowEvent.WINDOW_ICONIFIED
410: : WindowEvent.WINDOW_DEICONIFIED);
411:
412: postEvent(new WindowEvent(this , eventID, 0, 0));
413: }
414: postEvent(new WindowEvent(this ,
415: WindowEvent.WINDOW_STATE_CHANGED, oldState, state));
416: }
417:
418: public void setIconImage(Image image) {
419: toolkit.lockAWT();
420: try {
421: iconImage = prepareIconImage(image);
422: NativeWindow win = getNativeWindow();
423: if (win != null) {
424: win.setIconImage(iconImage);
425: }
426: } finally {
427: toolkit.unlockAWT();
428: }
429: }
430:
431: private Image prepareIconImage(Image image) {
432: if (image == null) {
433: return null;
434: }
435:
436: MediaTracker mt = new MediaTracker(this );
437: mt.addImage(image, 0);
438: try {
439: if (mt.waitForID(0, 2000)) {
440: return image;
441: }
442: } catch (InterruptedException e) {
443: }
444: return null;
445: }
446:
447: public void setMaximizedBounds(Rectangle bounds) {
448: toolkit.lockAWT();
449: try {
450: maximizedBounds = bounds;
451: NativeWindow win = getNativeWindow();
452: if (win != null) {
453: win.setMaximizedBounds(bounds);
454: }
455: } finally {
456: toolkit.unlockAWT();
457: }
458: }
459:
460: public void setMenuBar(MenuBar menu) {
461: toolkit.lockAWT();
462: try {
463: if (menuBar != null) {
464: if (isDisplayable()) {
465: menuBar.removeNotify();
466: }
467: }
468: menuBar = menu;
469: if (menuBar != null) {
470: menuBar.setParent(this );
471: if (isDisplayable()) {
472: menuBar.addNotify();
473: }
474: }
475: } finally {
476: toolkit.unlockAWT();
477: }
478: }
479:
480: @Override
481: public void setResizable(boolean resizable) {
482: toolkit.lockAWT();
483: try {
484: super .setResizable(resizable);
485: } finally {
486: toolkit.unlockAWT();
487: }
488: }
489:
490: public void setState(int state) {
491: toolkit.lockAWT();
492: try {
493: setExtendedState(state);
494: } finally {
495: toolkit.unlockAWT();
496: }
497: }
498:
499: @Override
500: public void setTitle(String title) {
501: super .setTitle(title);
502: }
503:
504: @Override
505: public void setUndecorated(boolean undecorated) {
506: toolkit.lockAWT();
507: try {
508: super .setUndecorated(undecorated);
509: } finally {
510: toolkit.unlockAWT();
511: }
512: }
513:
514: @Override
515: public Insets getInsets() {
516: toolkit.lockAWT();
517: try {
518: Insets insets = super .getInsets();
519: if (menuBar != null) {
520: insets.top += menuBar.getHeight();
521: }
522: return insets;
523: } finally {
524: toolkit.unlockAWT();
525: }
526: }
527:
528: @Override
529: void setBounds(int x, int y, int w, int h, int bMask,
530: boolean updateBehavior) {
531: boolean widthChanged = (this .w != w);
532: super .setBounds(x, y, w, h, bMask, updateBehavior);
533: if ((menuBar != null) && widthChanged) {
534: menuBar.validate();
535: }
536: }
537:
538: @Override
539: void nativeWindowCreated(NativeWindow win) {
540: super .nativeWindowCreated(win);
541:
542: win.setMaximizedBounds(getMaximizedBounds());
543: }
544:
545: @Override
546: String autoName() {
547: int number = toolkit.autoNumber.nextFrame++;
548: return ("frame" + Integer.toString(number)); //$NON-NLS-1$
549: }
550:
551: @Override
552: AccessibleContext createAccessibleContext() {
553: return new AccessibleAWTFrame();
554: }
555:
556: void paintMenuBar(MultiRectArea clipArea) {
557: if (menuBar == null) {
558: return;
559: }
560: Point menuPos = menuBar.getLocation();
561: Rectangle bounds = new Rectangle(menuPos.x, menuPos.y, menuBar
562: .getWidth(), menuBar.getHeight());
563: MultiRectArea menuClip = new MultiRectArea(clipArea);
564: if (menuClip.getRectCount() > 0) {
565: clipArea.substract(bounds);
566: menuClip.translate(-menuPos.x, -menuPos.y);
567: Graphics g = menuBar.getGraphics(menuClip);
568: if (g != null) {
569: menuBar.paint(g);
570: }
571: g.dispose();
572: }
573: }
574:
575: @Override
576: void validateMenuBar() {
577: if (menuBar != null) {
578: menuBar.validate();
579: }
580: }
581:
582: static final class AllFrames {
583: private final ArrayList<WeakReference<Frame>> frames = new ArrayList<WeakReference<Frame>>();
584:
585: void add(Frame f) {
586: frames.add(new WeakReference<Frame>(f));
587: }
588:
589: Frame[] getFrames() {
590: ArrayList<Frame> aliveFrames = new ArrayList<Frame>();
591:
592: for (Iterator<WeakReference<Frame>> it = frames.iterator(); it
593: .hasNext();) {
594: WeakReference<?> ref = it.next();
595: Frame f = (Frame) ref.get();
596: if (f != null) {
597: aliveFrames.add(f);
598: }
599: }
600:
601: return aliveFrames.toArray(new Frame[0]);
602: }
603: }
604: }
|