0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package java.awt;
0019:
0020: import java.awt.event.WindowEvent;
0021: import java.awt.event.WindowFocusListener;
0022: import java.awt.event.WindowListener;
0023: import java.awt.event.WindowStateListener;
0024: import java.awt.im.InputContext;
0025: import java.awt.image.BufferStrategy;
0026: import java.beans.PropertyChangeListener;
0027: import java.io.IOException;
0028: import java.io.ObjectInputStream;
0029: import java.security.AccessController;
0030: import java.security.PrivilegedAction;
0031: import java.util.ArrayList;
0032: import java.util.EventListener;
0033: import java.util.Iterator;
0034: import java.util.Locale;
0035: import java.util.ResourceBundle;
0036: import java.util.Set;
0037: import javax.accessibility.Accessible;
0038: import javax.accessibility.AccessibleContext;
0039: import javax.accessibility.AccessibleRole;
0040: import javax.accessibility.AccessibleState;
0041: import javax.accessibility.AccessibleStateSet;
0042: import org.apache.harmony.awt.AWTPermissionCollection;
0043: import org.apache.harmony.awt.FieldsAccessor;
0044: import org.apache.harmony.awt.gl.MultiRectArea;
0045: import org.apache.harmony.awt.im.InputMethodContext;
0046: import org.apache.harmony.awt.internal.nls.Messages;
0047: import org.apache.harmony.awt.wtk.NativeWindow;
0048:
0049: public class Window extends Container implements Accessible {
0050: private static final long serialVersionUID = 4497834738069338734L;
0051:
0052: private final AWTListenerList<WindowFocusListener> windowFocusListeners = new AWTListenerList<WindowFocusListener>(
0053: this );
0054:
0055: private final AWTListenerList<WindowListener> windowListeners = new AWTListenerList<WindowListener>(
0056: this );
0057:
0058: private final AWTListenerList<WindowStateListener> windowStateListeners = new AWTListenerList<WindowStateListener>(
0059: this );
0060:
0061: private final ArrayList<Window> ownedWindows = new ArrayList<Window>();
0062:
0063: private transient Component focusOwner;
0064:
0065: private boolean focusableWindowState = true;// By default, all Windows have
0066: // a focusable Window state of
0067: // true
0068:
0069: private Insets nativeInsets = new Insets(0, 0, 0, 0);
0070:
0071: /** Security warning for non-secure windows */
0072: private final String warningString;
0073:
0074: // Properties of Frame and Dialog
0075: private String title;
0076:
0077: private boolean resizable;
0078:
0079: private boolean undecorated;
0080:
0081: private boolean alwaysOnTop;
0082:
0083: boolean locationByPlatform;
0084:
0085: /** The window is popup menu or tooltip (for internal use) */
0086: private boolean popup;
0087:
0088: /**
0089: * Focus proxy native window actually has native focus when this
0090: * Window(Frame) is active, but some other (owned) Window is focused
0091: */
0092: private transient NativeWindow focusProxy;
0093:
0094: /**
0095: * Component which has requested focus last
0096: */
0097: private transient Component requestedFocus;
0098:
0099: private final transient GraphicsConfiguration graphicsConfiguration;
0100:
0101: private boolean opened;
0102:
0103: private boolean disposed;
0104:
0105: boolean painted;
0106:
0107: private transient InputContext inputContext;
0108:
0109: protected class AccessibleAWTWindow extends AccessibleAWTContainer {
0110: private static final long serialVersionUID = 4215068635060671780L;
0111:
0112: @Override
0113: public AccessibleStateSet getAccessibleStateSet() {
0114: toolkit.lockAWT();
0115: try {
0116: AccessibleStateSet set = super .getAccessibleStateSet();
0117: if (isFocused()) {
0118: set.add(AccessibleState.ACTIVE);
0119: }
0120: if (isResizable()) {
0121: set.add(AccessibleState.RESIZABLE);
0122: }
0123: return set;
0124: } finally {
0125: toolkit.unlockAWT();
0126: }
0127: }
0128:
0129: @Override
0130: public AccessibleRole getAccessibleRole() {
0131: toolkit.lockAWT();
0132: try {
0133: return AccessibleRole.WINDOW;
0134: } finally {
0135: toolkit.unlockAWT();
0136: }
0137: }
0138: }
0139:
0140: public Window(Window owner) {
0141: this (owner, null);
0142: toolkit.lockAWT();
0143: try {
0144: } finally {
0145: toolkit.unlockAWT();
0146: }
0147: }
0148:
0149: private void addWindow(Window window) {
0150: ownedWindows.add(window);
0151: }
0152:
0153: public Window(Window owner, GraphicsConfiguration gc) {
0154: toolkit.lockAWT();
0155: try {
0156: if (!(this instanceof Frame)
0157: && !(this instanceof EmbeddedWindow)) {
0158: if (owner == null) {
0159: // awt.125=null owner window
0160: throw new IllegalArgumentException(Messages
0161: .getString("awt.125")); //$NON-NLS-1$
0162: }
0163: owner.addWindow(this );
0164: }
0165: parent = owner; // window's parent is the same as owner(by spec)
0166: graphicsConfiguration = getGraphicsConfiguration(gc);
0167: warningString = getWarningStringImpl();
0168: super .setLayout(new BorderLayout());
0169: if (owner == null) {
0170: setBackground(getDefaultBackground());
0171: setForeground(getDefaultForeground());
0172: }
0173: visible = false;
0174: focusCycleRoot = true; // FIXME
0175: // Top-levels initialize their focus traversal policies
0176: // using the context default policy.
0177: // The context default policy is established by
0178: // using KeyboardFocusManager.setDefaultFocusTraversalPolicy().
0179: setFocusTraversalPolicy(KeyboardFocusManager
0180: .getCurrentKeyboardFocusManager()
0181: .getDefaultFocusTraversalPolicy());
0182: redrawManager = new RedrawManager(this );
0183: cursor = Cursor.getDefaultCursor(); // for Window cursor is always
0184: // set(non-null)
0185: } finally {
0186: toolkit.unlockAWT();
0187: }
0188: }
0189:
0190: NativeWindow getFocusProxy() {
0191: return focusProxy;
0192: }
0193:
0194: public Window(Frame owner) {
0195: this ((Window) owner);
0196: toolkit.lockAWT();
0197: try {
0198: } finally {
0199: toolkit.unlockAWT();
0200: }
0201: }
0202:
0203: @Override
0204: protected void finalize() throws Throwable {
0205: // do nothing
0206: }
0207:
0208: @Override
0209: public void addNotify() {
0210: toolkit.lockAWT();
0211: try {
0212: super .addNotify();
0213: focusProxy = toolkit.createFocusProxyNativeWindow(this );
0214: } finally {
0215: toolkit.unlockAWT();
0216: }
0217: }
0218:
0219: @Override
0220: public void removeNotify() {
0221: toolkit.lockAWT();
0222: try {
0223: disposeFocusProxy();
0224: super .removeNotify();
0225: } finally {
0226: toolkit.unlockAWT();
0227: }
0228: }
0229:
0230: @Override
0231: public AccessibleContext getAccessibleContext() {
0232: toolkit.lockAWT();
0233: try {
0234: return super .getAccessibleContext();
0235: } finally {
0236: toolkit.unlockAWT();
0237: }
0238: }
0239:
0240: @Override
0241: public Toolkit getToolkit() {
0242: return toolkit;
0243: }
0244:
0245: @Override
0246: public void setCursor(Cursor cursor) {
0247: toolkit.lockAWT();
0248: try {
0249: // for Window cursor is always set(non-null)
0250: super .setCursor(cursor != null ? cursor : Cursor
0251: .getDefaultCursor());
0252: } finally {
0253: toolkit.unlockAWT();
0254: }
0255: }
0256:
0257: @Override
0258: public void addPropertyChangeListener(
0259: PropertyChangeListener listener) {
0260: toolkit.lockAWT();
0261: try {
0262: super .addPropertyChangeListener(listener);
0263: } finally {
0264: toolkit.unlockAWT();
0265: }
0266: }
0267:
0268: @Override
0269: public void addPropertyChangeListener(String propertyName,
0270: PropertyChangeListener listener) {
0271: toolkit.lockAWT();
0272: try {
0273: super .addPropertyChangeListener(propertyName, listener);
0274: } finally {
0275: toolkit.unlockAWT();
0276: }
0277: }
0278:
0279: public void createBufferStrategy(int a0)
0280: throws org.apache.harmony.luni.util.NotImplementedException {
0281: toolkit.lockAWT();
0282: try {
0283: } finally {
0284: toolkit.unlockAWT();
0285: }
0286: if (true) {
0287: throw new RuntimeException("Method is not implemented"); //$NON-NLS-1$
0288: }
0289: return;
0290: }
0291:
0292: public void createBufferStrategy(int a0, BufferCapabilities a1)
0293: throws AWTException,
0294: org.apache.harmony.luni.util.NotImplementedException {
0295: toolkit.lockAWT();
0296: try {
0297: } finally {
0298: toolkit.unlockAWT();
0299: }
0300: if (true) {
0301: throw new RuntimeException("Method is not implemented"); // TODO: //$NON-NLS-1$
0302: // implement
0303: }
0304: return;
0305: }
0306:
0307: public void dispose() {
0308: toolkit.lockAWT();
0309: try {
0310: if (!disposed) {
0311: prepare4HierarchyChange();
0312: hide();
0313: disposeOwnedWindows();
0314: mapToDisplay(false);
0315: disposed = true;
0316: opened = false;
0317: disposeInputContext();
0318: finishHierarchyChange(this , parent, 0);
0319: postEvent(new WindowEvent(this ,
0320: WindowEvent.WINDOW_CLOSED));
0321: }
0322: } finally {
0323: toolkit.unlockAWT();
0324: }
0325: }
0326:
0327: private void disposeInputContext() {
0328: // only default windows input contexts are disposed
0329: // custom input contexts returned by
0330: // overridden getInputContext() are not!
0331: if (inputContext != null) {
0332: inputContext.dispose();
0333: }
0334: }
0335:
0336: /**
0337: * Remove focus proxy native window from map which is stored in Toolkit
0338: */
0339: private void disposeFocusProxy() {
0340: if (focusProxy != null) {
0341: toolkit.removeFocusProxyNativeWindow(focusProxy);
0342: focusProxy = null;
0343: }
0344: }
0345:
0346: /**
0347: * dispose all owned windows explicitly to remove them from Toolkit's map
0348: */
0349: private void disposeOwnedWindows() {
0350: for (int i = 0; i < ownedWindows.size(); i++) {
0351: Window win = ownedWindows.get(i);
0352: if (win != null) {
0353: win.dispose();
0354: }
0355: }
0356: }
0357:
0358: public BufferStrategy getBufferStrategy()
0359: throws org.apache.harmony.luni.util.NotImplementedException {
0360: toolkit.lockAWT();
0361: try {
0362: } finally {
0363: toolkit.unlockAWT();
0364: }
0365: if (true) {
0366: throw new RuntimeException("Method is not implemented"); // TODO: //$NON-NLS-1$
0367: // implement
0368: }
0369: return null;
0370: }
0371:
0372: @Override
0373: public final Container getFocusCycleRootAncestor() {
0374: toolkit.lockAWT();
0375: try {
0376: // Always returns null because Windows have no ancestors
0377: return null;
0378: } finally {
0379: toolkit.unlockAWT();
0380: }
0381: }
0382:
0383: public Component getFocusOwner() {
0384: toolkit.lockAWT();
0385: try {
0386: return isFocused() ? focusOwner : null;
0387: } finally {
0388: toolkit.unlockAWT();
0389: }
0390: }
0391:
0392: @Override
0393: public Set<AWTKeyStroke> getFocusTraversalKeys(int id) {
0394: // why override?
0395: toolkit.lockAWT();
0396: try {
0397: return super .getFocusTraversalKeys(id);
0398: } finally {
0399: toolkit.unlockAWT();
0400: }
0401: }
0402:
0403: @Override
0404: public GraphicsConfiguration getGraphicsConfiguration() {
0405: toolkit.lockAWT();
0406: try {
0407: if (graphicsConfiguration != null) {
0408: return graphicsConfiguration;
0409: } else if (parent != null) {
0410: return parent.getGraphicsConfiguration();
0411: } else {
0412: return GraphicsEnvironment
0413: .getLocalGraphicsEnvironment()
0414: .getDefaultScreenDevice()
0415: .getDefaultConfiguration();
0416: }
0417: } finally {
0418: toolkit.unlockAWT();
0419: }
0420: }
0421:
0422: @Override
0423: public InputContext getInputContext() {
0424: toolkit.lockAWT();
0425: try {
0426: if (inputContext == null) {
0427: inputContext = InputContext.getInstance();
0428: }
0429: return inputContext;
0430: } finally {
0431: toolkit.unlockAWT();
0432: }
0433: }
0434:
0435: @Override
0436: public Locale getLocale() {
0437: toolkit.lockAWT();
0438: try {
0439: return super .getLocale();
0440: } finally {
0441: toolkit.unlockAWT();
0442: }
0443: }
0444:
0445: /**
0446: * @deprecated
0447: */
0448: @Deprecated
0449: @SuppressWarnings("deprecation")
0450: @Override
0451: public void hide() {
0452: toolkit.lockAWT();
0453: try {
0454: super .hide();
0455: painted = false;
0456: // hide all owned windows explicitly:
0457: for (int i = 0; i < ownedWindows.size(); i++) {
0458: Window w = ownedWindows.get(i);
0459: if (w != null) {
0460: w.hide();
0461: }
0462: }
0463: notifyInputMethod(null);
0464: } finally {
0465: toolkit.unlockAWT();
0466: }
0467: }
0468:
0469: @Override
0470: public final boolean isFocusCycleRoot() {
0471: toolkit.lockAWT();
0472: try {
0473: // Every Window is, by default, a "focus cycle root".
0474: return true;
0475: } finally {
0476: toolkit.unlockAWT();
0477: }
0478: }
0479:
0480: public final boolean isFocusableWindow() {
0481: toolkit.lockAWT();
0482: try {
0483: return getFocusableWindowState()
0484: && (isActivateable() || getFrameDialogOwner()
0485: .isShowing()
0486: && focusTraversalCycleNotEmpty());
0487: } finally {
0488: toolkit.unlockAWT();
0489: }
0490: }
0491:
0492: final boolean isActivateable() {
0493: return (this instanceof Frame) || (this instanceof Dialog)
0494: || (this instanceof EmbeddedWindow);
0495: }
0496:
0497: private boolean focusTraversalCycleNotEmpty() {
0498: return getFocusTraversalPolicy().getFirstComponent(this ) != null;
0499: }
0500:
0501: /**
0502: * Gets the nearest ancestor "activateable" window which is typically Frame
0503: * or Dialog
0504: */
0505: Window getFrameDialogOwner() {
0506: for (Window o = this ;; o = (Window) o.parent) {
0507: if ((o == null) || o.isActivateable()) {
0508: return o;
0509: }
0510: }
0511: }
0512:
0513: @Override
0514: public boolean isShowing() {
0515: toolkit.lockAWT();
0516: try {
0517: return (isVisible() && isDisplayable());
0518: } finally {
0519: toolkit.unlockAWT();
0520: }
0521: }
0522:
0523: /**
0524: * @deprecated
0525: */
0526: @SuppressWarnings("deprecation")
0527: @Deprecated
0528: @Override
0529: public boolean postEvent(Event evt) {
0530: toolkit.lockAWT();
0531: try {
0532: // do not propagate event to parent(owner) window:
0533: return handleEvent(evt);
0534: } finally {
0535: toolkit.unlockAWT();
0536: }
0537: }
0538:
0539: /**
0540: * @deprecated
0541: */
0542: @Deprecated
0543: @SuppressWarnings("deprecation")
0544: @Override
0545: public void show() {
0546: toolkit.lockAWT();
0547: try {
0548: if (opened) {
0549: if (isVisible()) {
0550: toFront();
0551: return;
0552: }
0553: } else {
0554: disposed = false;
0555: }
0556:
0557: if (getFont() == null) {
0558: setFont(Font.DEFAULT_FONT);
0559: }
0560:
0561: super .show();
0562: toFront();
0563: if (!opened) {
0564: opened = true;
0565: postEvent(new WindowEvent(this ,
0566: WindowEvent.WINDOW_OPENED));
0567: }
0568: } finally {
0569: toolkit.unlockAWT();
0570: }
0571: }
0572:
0573: public Component getMostRecentFocusOwner() {
0574: toolkit.lockAWT();
0575: try {
0576: // if the Window has never been focused, focus should be set to the
0577: // Window's initial Component to focus
0578: return (focusOwner != null) && (focusOwner != this ) ? focusOwner
0579: : (isFocusableWindow() ? getFocusTraversalPolicy()
0580: .getInitialComponent(this ) : null);
0581: } finally {
0582: toolkit.unlockAWT();
0583: }
0584: }
0585:
0586: void setFocusOwner(Component owner) {
0587: focusOwner = owner;
0588: }
0589:
0590: @Override
0591: public final void setFocusCycleRoot(boolean value) {
0592: toolkit.lockAWT();
0593: try {
0594: // Does nothing because Windows must always be roots of a focus
0595: // traversal cycle.
0596: // The passed-in value is ignored.
0597: } finally {
0598: toolkit.unlockAWT();
0599: }
0600: }
0601:
0602: public void toFront() {
0603: toolkit.lockAWT();
0604: try {
0605: NativeWindow win = getNativeWindow();
0606: if (win != null) {
0607: win.toFront();
0608: }
0609: } finally {
0610: toolkit.unlockAWT();
0611: }
0612: }
0613:
0614: /**
0615: * @deprecated
0616: */
0617: @SuppressWarnings("deprecation")
0618: @Deprecated
0619: public void applyResourceBundle(ResourceBundle rb) {
0620: toolkit.lockAWT();
0621: try {
0622: applyComponentOrientation(ComponentOrientation
0623: .getOrientation(rb));
0624: } finally {
0625: toolkit.unlockAWT();
0626: }
0627: }
0628:
0629: /**
0630: * @deprecated
0631: */
0632: @Deprecated
0633: public void applyResourceBundle(String rbName) {
0634: toolkit.lockAWT();
0635: try {
0636: applyResourceBundle(ResourceBundle.getBundle(rbName));
0637: } finally {
0638: toolkit.unlockAWT();
0639: }
0640: }
0641:
0642: public boolean getFocusableWindowState() {
0643: toolkit.lockAWT();
0644: try {
0645: return focusableWindowState;
0646: } finally {
0647: toolkit.unlockAWT();
0648: }
0649: }
0650:
0651: public Window[] getOwnedWindows() {
0652: toolkit.lockAWT();
0653: try {
0654: return ownedWindows.toArray(new Window[0]);
0655: } finally {
0656: toolkit.unlockAWT();
0657: }
0658: }
0659:
0660: public Window getOwner() {
0661: toolkit.lockAWT();
0662: try {
0663: return (Window) parent;
0664: } finally {
0665: toolkit.unlockAWT();
0666: }
0667: }
0668:
0669: public final String getWarningString() {
0670: return warningString;
0671: }
0672:
0673: private final String getWarningStringImpl() {
0674: SecurityManager sm = System.getSecurityManager();
0675: if (sm == null) {
0676: return null;
0677: }
0678: if (sm.checkTopLevelWindow(this )) {
0679: return null;
0680: }
0681: PrivilegedAction<String> action = new PrivilegedAction<String>() {
0682: public String run() {
0683: return System.getProperty(
0684: "awt.appletWarning", "Warning: Java window"); //$NON-NLS-1$ //$NON-NLS-2$
0685: }
0686: };
0687: return AccessController.doPrivileged(action);
0688: }
0689:
0690: public boolean isActive() {
0691: toolkit.lockAWT();
0692: try {
0693: return KeyboardFocusManager
0694: .getCurrentKeyboardFocusManager().getActiveWindow() == this ;
0695: } finally {
0696: toolkit.unlockAWT();
0697: }
0698: }
0699:
0700: public boolean isFocused() {
0701: toolkit.lockAWT();
0702: try {
0703: return KeyboardFocusManager
0704: .getCurrentKeyboardFocusManager()
0705: .getFocusedWindow() == this ;
0706: } finally {
0707: toolkit.unlockAWT();
0708: }
0709: }
0710:
0711: public void pack() {
0712: toolkit.lockAWT();
0713: try {
0714: if (getFont() == null) {
0715: setFont(Font.DEFAULT_FONT);
0716: }
0717:
0718: if ((parent != null) && !parent.isDisplayable()) {
0719: parent.mapToDisplay(true);
0720: }
0721: if (!isDisplayable()) {
0722: mapToDisplay(true);
0723: }
0724: setSize(getPreferredSize());
0725: validate();
0726: getNativeWindow().setPacked(true);
0727: } finally {
0728: toolkit.unlockAWT();
0729: }
0730: }
0731:
0732: public final boolean isAlwaysOnTop() {
0733: toolkit.lockAWT();
0734: try {
0735: return alwaysOnTop;
0736: } finally {
0737: toolkit.unlockAWT();
0738: }
0739: }
0740:
0741: public final void setAlwaysOnTop(boolean alwaysOnTop)
0742: throws SecurityException {
0743: boolean wasAlwaysOnTop;
0744: toolkit.lockAWT();
0745: try {
0746: if (this .alwaysOnTop == alwaysOnTop) {
0747: return;
0748: }
0749: SecurityManager sm = System.getSecurityManager();
0750: if (sm != null) {
0751: sm
0752: .checkPermission(AWTPermissionCollection.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
0753: }
0754: wasAlwaysOnTop = this .alwaysOnTop;
0755: this .alwaysOnTop = alwaysOnTop;
0756: NativeWindow win = getNativeWindow();
0757: if (win != null) {
0758: win.setAlwaysOnTop(alwaysOnTop);
0759: }
0760: } finally {
0761: toolkit.unlockAWT();
0762: }
0763: firePropertyChange("alwaysOnTop", wasAlwaysOnTop, alwaysOnTop); //$NON-NLS-1$
0764: }
0765:
0766: /**
0767: * Called by AWT in response to native event "insets changed" on this Window
0768: *
0769: * @param insets new native insets
0770: */
0771: void setNativeInsets(Insets insets) {
0772: if (this .nativeInsets.equals(insets)) {
0773: return;
0774: }
0775: nativeInsets = (Insets) insets.clone();
0776: validateMenuBar();
0777: invalidate();
0778: validate();
0779: }
0780:
0781: void validateMenuBar() {
0782: // do nothing, override in Frame to do useful work
0783: }
0784:
0785: @Override
0786: Insets getNativeInsets() {
0787: return (Insets) nativeInsets.clone();
0788: }
0789:
0790: @Override
0791: void setBounds(int x, int y, int w, int h, int bMask,
0792: boolean updateBehavior) {
0793: boolean resized = ((w != this .w) || (h != this .h));
0794: super .setBounds(x, y, w, h, bMask, updateBehavior);
0795: if (visible && resized && !updateBehavior) {
0796: validate();
0797: }
0798: }
0799:
0800: @Override
0801: public void setBounds(int x, int y, int width, int height) {
0802: locationByPlatform = false;
0803: super .setBounds(x, y, width, height);
0804: }
0805:
0806: public void setLocationByPlatform(boolean byPlatform) {
0807: toolkit.lockAWT();
0808: try {
0809: if (byPlatform && visible && behaviour.isDisplayable()) {
0810: // awt.126=Window is showing
0811: throw new IllegalComponentStateException(Messages
0812: .getString("awt.126")); //$NON-NLS-1$
0813: }
0814: locationByPlatform = byPlatform;
0815: } finally {
0816: toolkit.unlockAWT();
0817: }
0818: }
0819:
0820: public boolean isLocationByPlatform() {
0821: toolkit.lockAWT();
0822: try {
0823: if (visible && behaviour.isDisplayable()) {
0824: return false;
0825: }
0826: return locationByPlatform;
0827: } finally {
0828: toolkit.unlockAWT();
0829: }
0830: }
0831:
0832: public void setFocusableWindowState(boolean state) {
0833: boolean oldState;
0834: toolkit.lockAWT();
0835: try {
0836: oldState = focusableWindowState;
0837: focusableWindowState = state;
0838: // call cb here to make window natively non-focusable
0839: NativeWindow win = getNativeWindow();
0840: if (win != null) {
0841: win.setFocusable(state);
0842: }
0843: if (!state) {
0844: moveFocusToOwner();
0845: }
0846: } finally {
0847: toolkit.unlockAWT();
0848: }
0849: firePropertyChange(
0850: "focusableWindowState", oldState, focusableWindowState); //$NON-NLS-1$
0851: }
0852:
0853: /**
0854: * If this is a focused window then attempt to focus the most recently
0855: * focused Component of this Window's owner or clear global focus owner if
0856: * attempt fails
0857: */
0858: private void moveFocusToOwner() {
0859: if (isFocused()) {
0860: Component compToFocus = null;
0861: for (Window wnd = getOwner(); wnd != null
0862: && compToFocus == null; wnd = wnd.getOwner()) {
0863: compToFocus = wnd.getMostRecentFocusOwner();
0864: if (compToFocus != null
0865: && !compToFocus.requestFocusImpl(false, true,
0866: false)) {
0867: compToFocus = null;
0868: }
0869: }
0870: if (compToFocus == null) {
0871: KeyboardFocusManager.getCurrentKeyboardFocusManager()
0872: .clearGlobalFocusOwner();
0873: }
0874: }
0875: }
0876:
0877: public void setLocationRelativeTo(Component c) {
0878: toolkit.lockAWT();
0879: try {
0880: Rectangle screenRect = getGraphicsConfiguration()
0881: .getBounds();
0882: int minX = screenRect.x;
0883: int minY = screenRect.y;
0884: int maxX = minX + screenRect.width - 1;
0885: int maxY = minY + screenRect.height;
0886: int centerX = (minX + maxX) / 2;
0887: int centerY = (minY + maxY) / 2;
0888: int x = centerX;
0889: int y = centerY;
0890: // if comp is null or not showing, then set location
0891: // relative to "component" of 1-pixel size located
0892: // at the center of the screen
0893: Point loc = new Point(centerX, centerY);
0894: int compX = loc.x;
0895: Dimension compSize = new Dimension();
0896: if ((c != null) && c.isShowing()) {
0897: loc = c.getLocationOnScreen();
0898: compX = loc.x;
0899: compSize = c.getSize();
0900: }
0901: // first get center coords:
0902: loc.translate(compSize.width / 2, compSize.height / 2);
0903: // now get upper-left corner coords:
0904: int w = getWidth(), h = getHeight();
0905: loc.translate(-w / 2, -h / 2);
0906: // check if screenRect contains new window
0907: // bounds rectangle and if not - change location
0908: // of window to fit into screenRect
0909: x = Math.max(loc.x, minX);
0910: y = Math.max(loc.y, minY);
0911: int right = x + w, bottom = y + h;
0912: if (right > maxX) {
0913: x -= right - maxX;
0914: }
0915: if (bottom > maxY) {
0916: y -= bottom - maxY;
0917: // If the bottom of the component is offscreen,
0918: // the window is placed to the side of the Component
0919: // that is closest to the center of the screen.
0920: int compRight = compX + compSize.width;
0921: int distRight = Math.abs(compRight - centerX);
0922: int distLeft = Math.abs(centerX - compX);
0923: x = ((distRight < distLeft) ? compRight : (compX - w));
0924: x = Math.max(x, minX);
0925: right = x + w;
0926: if (right > maxX) {
0927: x -= right - maxX;
0928: }
0929: }
0930: setLocation(x, y);
0931: } finally {
0932: toolkit.unlockAWT();
0933: }
0934: }
0935:
0936: public void toBack() {
0937: toolkit.lockAWT();
0938: try {
0939: NativeWindow win = getNativeWindow();
0940: if (win != null) {
0941: win.toBack();
0942: }
0943: // TODO?: reset the focused Window(this or any of owners) to the
0944: // top-most Window in the VM
0945: } finally {
0946: toolkit.unlockAWT();
0947: }
0948: }
0949:
0950: boolean isResizable() {
0951: return resizable;
0952: }
0953:
0954: void setResizable(boolean resizable) {
0955: if (this .resizable == resizable) {
0956: return;
0957: }
0958: this .resizable = resizable;
0959: NativeWindow win = getNativeWindow();
0960: if (win != null && !undecorated && !popup) {
0961: win.setResizable(resizable);
0962: }
0963: }
0964:
0965: boolean isUndecorated() {
0966: return undecorated;
0967: }
0968:
0969: void setUndecorated(boolean undecorated) {
0970: if (this .undecorated == undecorated) {
0971: return;
0972: }
0973: if (isDisplayable()) {
0974: // awt.127=Cannot change the decorations while the window is visible
0975: throw new IllegalComponentStateException(Messages
0976: .getString("awt.127")); //$NON-NLS-1$
0977: }
0978: this .undecorated = undecorated;
0979: }
0980:
0981: boolean isPopup() {
0982: return popup;
0983: }
0984:
0985: void setPopup(boolean popup) {
0986: if (isDisplayable()) {
0987: // awt.127=Cannot change the decorations while the window is visible
0988: throw new IllegalComponentStateException(Messages
0989: .getString("awt.127")); //$NON-NLS-1$
0990: }
0991: this .popup = popup;
0992: }
0993:
0994: String getTitle() {
0995: return title;
0996: }
0997:
0998: /**
0999: * Set title for Frame or Dialog<br>
1000: * It does lockAWT() properly so there's no need to synchronize the calls of
1001: * this method
1002: *
1003: * @param title - value to set
1004: */
1005: void setTitle(String title) {
1006: String oldTitle = this .title;
1007: toolkit.lockAWT();
1008: try {
1009: this .title = (title == null) ? "" : title; //$NON-NLS-1$
1010: NativeWindow win = getNativeWindow();
1011: if (win != null) {
1012: win.setTitle(title);
1013: }
1014: } finally {
1015: toolkit.unlockAWT();
1016: }
1017: firePropertyChange("title", oldTitle, title); //$NON-NLS-1$
1018: }
1019:
1020: @Override
1021: RedrawManager getRedrawManager() {
1022: return redrawManager;
1023: }
1024:
1025: void redrawAll() {
1026: if (redrawManager.redrawAll()) {
1027: painted = true;
1028: }
1029: }
1030:
1031: void setRequestedFocus(Component component) {
1032: requestedFocus = component;
1033: }
1034:
1035: Component getRequestedFocus() {
1036: return requestedFocus;
1037: }
1038:
1039: @Override
1040: AccessibleContext createAccessibleContext() {
1041: return new AccessibleAWTWindow();
1042: }
1043:
1044: /**
1045: * Gets the default Cursor if Window is disabled even if Cursor is
1046: * explicitly set
1047: *
1048: * @return actual Cursor to be displayed
1049: * @see Component.getRealCursor()
1050: */
1051: @Override
1052: Cursor getRealCursor() {
1053: return isEnabled() ? getCursor() : Cursor.getDefaultCursor();
1054: }
1055:
1056: @Override
1057: String autoName() {
1058: int number = toolkit.autoNumber.nextWindow++;
1059: return "window" + Integer.toString(number); //$NON-NLS-1$
1060: }
1061:
1062: public void addWindowFocusListener(WindowFocusListener l) {
1063: windowFocusListeners.addUserListener(l);
1064: }
1065:
1066: public void addWindowListener(WindowListener l) {
1067: windowListeners.addUserListener(l);
1068: }
1069:
1070: public void addWindowStateListener(WindowStateListener l) {
1071: windowStateListeners.addUserListener(l);
1072: }
1073:
1074: public WindowFocusListener[] getWindowFocusListeners() {
1075: return windowFocusListeners
1076: .getUserListeners(new WindowFocusListener[0]);
1077: }
1078:
1079: public WindowListener[] getWindowListeners() {
1080: return windowListeners.getUserListeners(new WindowListener[0]);
1081: }
1082:
1083: public WindowStateListener[] getWindowStateListeners() {
1084: return windowStateListeners
1085: .getUserListeners(new WindowStateListener[0]);
1086: }
1087:
1088: public void removeWindowFocusListener(WindowFocusListener l) {
1089: windowFocusListeners.removeUserListener(l);
1090: }
1091:
1092: public void removeWindowListener(WindowListener l) {
1093: windowListeners.removeUserListener(l);
1094: }
1095:
1096: public void removeWindowStateListener(WindowStateListener l) {
1097: windowStateListeners.removeUserListener(l);
1098: }
1099:
1100: @SuppressWarnings("unchecked")
1101: @Override
1102: public <T extends EventListener> T[] getListeners(
1103: Class<T> listenerType) {
1104: if (WindowFocusListener.class.isAssignableFrom(listenerType)) {
1105: return (T[]) getWindowFocusListeners();
1106: } else if (WindowStateListener.class
1107: .isAssignableFrom(listenerType)) {
1108: return (T[]) getWindowStateListeners();
1109: } else if (WindowListener.class.isAssignableFrom(listenerType)) {
1110: return (T[]) getWindowListeners();
1111: } else {
1112: return super .getListeners(listenerType);
1113: }
1114: }
1115:
1116: @Override
1117: protected void processEvent(AWTEvent e) {
1118: long eventMask = toolkit.eventTypeLookup.getEventMask(e);
1119: if (eventMask == AWTEvent.WINDOW_EVENT_MASK) {
1120: processWindowEvent((WindowEvent) e);
1121: } else if (eventMask == AWTEvent.WINDOW_STATE_EVENT_MASK) {
1122: processWindowStateEvent((WindowEvent) e);
1123: } else if (eventMask == AWTEvent.WINDOW_FOCUS_EVENT_MASK) {
1124: processWindowFocusEvent((WindowEvent) e);
1125: } else {
1126: super .processEvent(e);
1127: }
1128: }
1129:
1130: protected void processWindowEvent(WindowEvent e) {
1131: for (Iterator<?> i = windowListeners.getUserIterator(); i
1132: .hasNext();) {
1133: WindowListener listener = (WindowListener) i.next();
1134: switch (e.getID()) {
1135: case WindowEvent.WINDOW_ACTIVATED:
1136: listener.windowActivated(e);
1137: break;
1138: case WindowEvent.WINDOW_CLOSED:
1139: listener.windowClosed(e);
1140: break;
1141: case WindowEvent.WINDOW_CLOSING:
1142: listener.windowClosing(e);
1143: break;
1144: case WindowEvent.WINDOW_DEACTIVATED:
1145: listener.windowDeactivated(e);
1146: break;
1147: case WindowEvent.WINDOW_DEICONIFIED:
1148: listener.windowDeiconified(e);
1149: break;
1150: case WindowEvent.WINDOW_ICONIFIED:
1151: listener.windowIconified(e);
1152: break;
1153: case WindowEvent.WINDOW_OPENED:
1154: listener.windowOpened(e);
1155: break;
1156: }
1157: }
1158: }
1159:
1160: protected void processWindowFocusEvent(WindowEvent e) {
1161: for (Iterator<?> i = windowFocusListeners.getUserIterator(); i
1162: .hasNext();) {
1163: WindowFocusListener listener = (WindowFocusListener) i
1164: .next();
1165: switch (e.getID()) {
1166: case WindowEvent.WINDOW_GAINED_FOCUS:
1167: listener.windowGainedFocus(e);
1168: break;
1169: case WindowEvent.WINDOW_LOST_FOCUS:
1170: listener.windowLostFocus(e);
1171: break;
1172: }
1173: }
1174: }
1175:
1176: protected void processWindowStateEvent(WindowEvent e) {
1177: for (Iterator<?> i = windowStateListeners.getUserIterator(); i
1178: .hasNext();) {
1179: WindowStateListener listener = (WindowStateListener) i
1180: .next();
1181: switch (e.getID()) {
1182: case WindowEvent.WINDOW_STATE_CHANGED:
1183: listener.windowStateChanged(e);
1184: break;
1185: }
1186: }
1187: }
1188:
1189: @Override
1190: void moveFocusOnHide() {
1191: // let native system move focus itself
1192: // if native focused window is the same as
1193: // java focused window
1194: if (!isActivateable()) {
1195: super .moveFocusOnHide();
1196: }
1197: }
1198:
1199: @Override
1200: ComponentBehavior createBehavior() {
1201: return new HWBehavior(this );
1202: }
1203:
1204: private GraphicsConfiguration getGraphicsConfiguration(
1205: GraphicsConfiguration gc) {
1206: if (gc == null) {
1207: Toolkit.checkHeadless();
1208: GraphicsEnvironment ge = GraphicsEnvironment
1209: .getLocalGraphicsEnvironment();
1210: gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
1211: } else if (GraphicsEnvironment.isHeadless()) {
1212: // awt.128=Graphics environment is headless
1213: throw new IllegalArgumentException(Messages
1214: .getString("awt.128")); //$NON-NLS-1$
1215: }
1216: if (gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN) {
1217: // awt.129=Not a screen device
1218: throw new IllegalArgumentException(Messages
1219: .getString("awt.129")); //$NON-NLS-1$
1220: }
1221: return gc;
1222: }
1223:
1224: @Override
1225: Color getDefaultBackground() {
1226: return SystemColor.window;
1227: }
1228:
1229: @Override
1230: Color getDefaultForeground() {
1231: return SystemColor.windowText;
1232: }
1233:
1234: @Override
1235: boolean isPrepainter() {
1236: return true;
1237: }
1238:
1239: @Override
1240: void prepaint(Graphics g) {
1241: Color back = getBackground();
1242: if (back == null) {
1243: back = getDefaultBackground();
1244: }
1245: g.setColor(back);
1246: Insets ins = getNativeInsets();
1247: g.fillRect(ins.left, ins.top, w - ins.right - ins.left, h
1248: - ins.bottom - ins.top);
1249: }
1250:
1251: /**
1252: * Called immediately after native window has been created. Updates native
1253: * window state & properties to make them correspond to Java Window
1254: * state/properties
1255: */
1256: @Override
1257: void nativeWindowCreated(NativeWindow win) {
1258: win.setFocusable(getFocusableWindowState());
1259: nativeInsets = win.getInsets();
1260: win.setAlwaysOnTop(isAlwaysOnTop());
1261: win.setIconImage(getIconImage());
1262: }
1263:
1264: /**
1265: * Returns icon image of the owner frame. This method is overridden as
1266: * public in the class Frame
1267: */
1268: Image getIconImage() {
1269: toolkit.lockAWT();
1270: try {
1271: for (Container c = parent; c != null; c = c.parent) {
1272: if (c instanceof Frame) {
1273: return ((Frame) c).getIconImage();
1274: }
1275: }
1276: return null;
1277: } finally {
1278: toolkit.unlockAWT();
1279: }
1280: }
1281:
1282: @Override
1283: MultiRectArea getObscuredRegion(Rectangle part) {
1284: if (!visible || behaviour.getNativeWindow() == null) {
1285: return null;
1286: }
1287: Insets ins = getNativeInsets();
1288: Rectangle r = new Rectangle(ins.left, ins.top, w - ins.left
1289: - ins.right, h - ins.top - ins.bottom);
1290: if (part != null) {
1291: r = r.intersection(part);
1292: }
1293: if (r.isEmpty()) {
1294: return null;
1295: }
1296: return behaviour.getNativeWindow().getObscuredRegion(r);
1297: }
1298:
1299: private void readObject(ObjectInputStream stream)
1300: throws IOException, ClassNotFoundException {
1301: stream.defaultReadObject();
1302: FieldsAccessor accessor = new FieldsAccessor(Window.class, this );
1303: accessor
1304: .set(
1305: "graphicsConfiguration", getGraphicsConfiguration(null)); //$NON-NLS-1$
1306: visible = false;
1307: redrawManager = new RedrawManager(this );
1308: }
1309:
1310: @Override
1311: void notifyInputMethod(Rectangle bounds) {
1312: InputContext ic = getInputContext();
1313: if (ic instanceof InputMethodContext) {
1314: ((InputMethodContext) ic).notifyClientWindowChange(bounds);
1315: }
1316: }
1317: }
|