001: /*
002: * @(#)PPCWindowPeer.java 1.4 02/12/09
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.awt.pocketpc;
028:
029: import java.util.Vector;
030: import sun.awt.peer.*;
031: import java.awt.*;
032: import java.awt.event.*;
033: import sun.awt.PeerBasedToolkit;
034:
035: class PPCWindowPeer extends PPCPanelPeer implements WindowPeer {
036: private static native void initIDs();
037:
038: static {
039: initIDs();
040: }
041:
042: // PPCComponentPeer overrides
043:
044: public void dispose() {
045: allWindows.removeElement(this );
046: super .dispose();
047: }
048:
049: // WindowPeer implementation
050:
051: public native void toFront();
052:
053: public native void toBack();
054:
055: // FramePeer & DialogPeer partial shared implementation
056:
057: public void setTitle(String title) {
058: // allow a null title to pass as an empty string.
059: if (title == null) {
060: title = new String("");
061: }
062: _setTitle(title);
063: }
064:
065: native void _setTitle(String title);
066:
067: public void setResizable(boolean resizable) {
068: _setResizable(resizable);
069: ((Component) target).invalidate(); // Insets were updated.
070: }
071:
072: public native void _setResizable(boolean resizable);
073:
074: // Toolkit & peer internals
075:
076: static Vector allWindows = new Vector(); //!CQ for anchoring windows, frames, dialogs
077:
078: PPCWindowPeer(Window target) {
079: super (target);
080: }
081:
082: void initialize() {
083: super .initialize();
084:
085: updateInsets(insets_);
086: allWindows.addElement(this );
087:
088: Font f = ((Window) target).getFont();
089: if (f == null) {
090: f = new Font("Dialog", Font.PLAIN, 12);
091: ((Window) target).setFont(f);
092: setFont(f);
093: }
094: }
095:
096: native void create(PPCComponentPeer parent);
097:
098: // Synchronize the insets members (here & in helper) with actual window
099: // state.
100: native void updateInsets(Insets i);
101:
102: // Called from AwtFrame::WmActivate
103: void postFocusOnActivate() {
104: PPCToolkit.postEvent(new FocusOnActivate());
105: }
106:
107: class FocusOnActivate extends AWTEvent implements ActiveEvent {
108: public FocusOnActivate() {
109: super ((Component) PPCWindowPeer.this .target, 0);
110: }
111:
112: public void dispatch() {
113: PPCComponentPeer peer;
114:
115: peer = getFocusPeer();
116: if (peer == null) {
117: // no heavyweight had the focus, so set a default focus
118: peer = setDefaultFocus();
119: }
120:
121: if (peer != null) {
122: requestComponentFocusIfActive(peer);
123: }
124: }
125: }
126:
127: // Requests the focus be set to a given component iff the containing
128: // window is activated
129: native void requestComponentFocusIfActive(PPCComponentPeer peer);
130:
131: // Get the peer of the heavyweight component with focus
132: // in this window
133: PPCComponentPeer getFocusPeer() {
134: Component c = ((Window) target).getFocusOwner();
135: while (c != null) {
136: ComponentPeer peer = (ComponentPeer) PeerBasedToolkit
137: .getComponentPeer(c);
138: if (peer != null && peer instanceof PPCComponentPeer) {
139: return (PPCComponentPeer) peer;
140: } else {
141: // peerless sub-component, try parent.
142: c = c.getParent();
143: }
144: }
145: return null; // no child has focus
146: }
147:
148: // This method is called to set focus to a Motif-like "reasonable"
149: // default. It's only called if focus was not explicitly set by either the
150: // Java app or the user.
151: PPCComponentPeer setDefaultFocus() {
152: // Find first focus-traversable component.
153: Component c = setDefaultFocus((Container) target);
154: if (c == null) {
155: // No focus-traversable component found, so set focus the 1st leaf node
156: c = (Component) target;
157: while (c instanceof Container
158: && ((Container) c).getComponentCount() > 0
159: && PeerBasedToolkit.getComponentPeer(c) != null) {
160: Component child = getContainerElement((Container) c, 0);
161: if (child.isVisible() && child.isEnabled()) {
162: c = child;
163: } else {
164: break; // Bug fix for bug #4038896 (Oracle deadlock bug) - Jonathan Locke
165: }
166: }
167: }
168: ComponentPeer peer = PeerBasedToolkit.getComponentPeer(c);
169: if (peer instanceof sun.awt.peer.LightweightPeer)
170: return null;
171: else
172: return (PPCComponentPeer) peer;
173: }
174:
175: // This is similar to Window.activateFocus(), but won't trip over any locks
176: // held by client code.
177: private Component setDefaultFocus(Container cont) {
178: int ncomponents = cont.getComponentCount();
179: for (int i = 0; i < ncomponents; i++) {
180: Component c = getContainerElement(cont, i);
181: if (c.isVisible() && c.isEnabled()
182: && c.isFocusTraversable()) {
183: return c;
184: }
185: if (c instanceof Container && c.isVisible()
186: && c.isEnabled()) {
187: Component child = setDefaultFocus((Container) c);
188: if (child != null) {
189: return child;
190: }
191: }
192: }
193: return null;
194: }
195:
196: private Component getContainerElement(Container c, int i) {
197: return c.getComponent(i);
198: }
199: }
|