home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 12.1 KB | 444 lines |
- /*
- * @(#)Frame.java 1.72 98/03/18
- *
- * Copyright 1995-1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
- package java.awt;
-
- import java.awt.peer.FramePeer;
- import java.awt.event.*;
- import java.util.Vector;
- import java.io.ObjectOutputStream;
- import java.io.ObjectInputStream;
- import java.io.IOException;
- import sun.awt.AppContext;
-
-
- /**
- * A Frame is a top-level window with a title and a border.
- * The default layout for a frame is BorderLayout.
- *
- * Frames are capable of generating the following types of window events:
- * WindowOpened, WindowClosing, WindowClosed, WindowIconified,
- * WindowDeiconified, WindowActivated, WindowDeactivated.
- *
- * @version 1.72, 03/18/98
- * @author Sami Shaio
- * @see WindowEvent
- * @see Window#addWindowListener
- * @since JDK1.0
- */
- public class Frame extends Window implements MenuContainer {
-
- /* Note: These are being obsoleted; programs should use the Cursor class
- * variables going forward. See Cursor and Component.setCursor.
- */
-
- /**
- * @deprecated replaced by <code>Cursor.DEFAULT_CURSOR</code>.
- */
- public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;
-
-
- /**
- * @deprecated replaced by <code>Cursor.CROSSHAIR_CURSOR</code>.
- */
- public static final int CROSSHAIR_CURSOR = Cursor.CROSSHAIR_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.TEXT_CURSOR</code>.
- */
- public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.WAIT_CURSOR</code>.
- */
- public static final int WAIT_CURSOR = Cursor.WAIT_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.SW_RESIZE_CURSOR</code>.
- */
- public static final int SW_RESIZE_CURSOR = Cursor.SW_RESIZE_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.SE_RESIZE_CURSOR</code>.
- */
- public static final int SE_RESIZE_CURSOR = Cursor.SE_RESIZE_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.NW_RESIZE_CURSOR</code>.
- */
- public static final int NW_RESIZE_CURSOR = Cursor.NW_RESIZE_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.NE_RESIZE_CURSOR</code>.
- */
- public static final int NE_RESIZE_CURSOR = Cursor.NE_RESIZE_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.N_RESIZE_CURSOR</code>.
- */
- public static final int N_RESIZE_CURSOR = Cursor.N_RESIZE_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.S_RESIZE_CURSOR</code>.
- */
- public static final int S_RESIZE_CURSOR = Cursor.S_RESIZE_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.W_RESIZE_CURSOR</code>.
- */
- public static final int W_RESIZE_CURSOR = Cursor.W_RESIZE_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.E_RESIZE_CURSOR</code>.
- */
- public static final int E_RESIZE_CURSOR = Cursor.E_RESIZE_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.HAND_CURSOR</code>.
- */
- public static final int HAND_CURSOR = Cursor.HAND_CURSOR;
-
- /**
- * @deprecated replaced by <code>Cursor.MOVE_CURSOR</code>.
- */
- public static final int MOVE_CURSOR = Cursor.MOVE_CURSOR;
-
- String title = "Untitled";
- Image icon;
- MenuBar menuBar;
- boolean resizable = true;
- boolean mbManagement = false; /* used only by the Motif impl. */
-
- /*
- * The Windows owned by the Frame.
- * Note: in 1.2 this has been superceded by Window.ownedWindowList
- */
- Vector ownedWindows;
-
- private static final String base = "frame";
- private static int nameCounter = 0;
-
- /*
- * JDK 1.1 serialVersionUID
- */
- private static final long serialVersionUID = 2673458971256075116L;
-
- /**
- * Constructs a new instance of <code>Frame</code> that is
- * initially invisible.
- * @see Component#setSize
- * @see Component#setVisible
- */
- public Frame() {
- this("");
- }
-
- /**
- * Constructs a new, initially invisible <code>Frame</code> object
- * with the specified title.
- * @param title the title for the frame
- * @see java.awt.Component#setSize
- * @see java.awt.Component#setVisible
- */
- public Frame(String title) {
- this.name = base + nameCounter++;
- this.title = title;
- visible = false;
- setLayout(new BorderLayout());
- addToFrameList();
- }
-
- /**
- * Creates the Frame's peer. The peer allows us to change the look
- * of the Frame without changing its functionality.
- */
- public void addNotify() {
- peer = getToolkit().createFrame(this);
- MenuBar menuBar = this.menuBar;
- if (menuBar != null) {
- menuBar.addNotify();
- ((FramePeer)peer).setMenuBar(menuBar);
- }
- super.addNotify();
- }
-
- /**
- * Gets the title of the frame.
- * @return the title of this frame, or <code>null</code>
- * if this frame doesn't have a title.
- * @see java.awt.Frame#setTitle
- */
- public String getTitle() {
- return title;
- }
-
- /**
- * Sets the title for this frame to the specified title.
- * @param title the specified title of this frame.
- * @see java.awt.Frame#getTitle
- */
- public synchronized void setTitle(String title) {
- this.title = title;
- FramePeer peer = (FramePeer)this.peer;
- if (peer != null) {
- peer.setTitle(title);
- }
- }
-
- /**
- * Gets the icon image for this frame.
- * @return the icon image for this frame, or <code>null</code>
- * if this frame doesn't have an icon image.
- * @see java.awt.Frame#setIconImage
- */
- public Image getIconImage() {
- return icon;
- }
-
- /**
- * Sets the image to display when this frame is iconized.
- * Not all platforms support the concept of iconizing a window.
- * @param image the icon image to be displayed
- * @see java.awt.Frame#getIconImage
- */
- public synchronized void setIconImage(Image image) {
- this.icon = image;
- FramePeer peer = (FramePeer)this.peer;
- if (peer != null) {
- peer.setIconImage(image);
- }
- }
-
- /**
- * Gets the menu bar for this frame.
- * @return the menu bar for this frame, or <code>null</code>
- * if this frame doesn't have a menu bar.
- * @see java.awt.Frame#setMenuBar
- */
- public MenuBar getMenuBar() {
- return menuBar;
- }
-
- /**
- * Sets the menu bar for this frame to the specified menu bar.
- * @param mb the menu bar being set
- * @see java.awt.Frame#getMenuBar
- */
- public synchronized void setMenuBar(MenuBar mb) {
- if (menuBar == mb) {
- return;
- }
- if ((mb != null) && (mb.parent != null)) {
- mb.parent.remove(mb);
- }
- if (menuBar != null) {
- remove(menuBar);
- }
- menuBar = mb;
- if (menuBar != null) {
- menuBar.parent = this;
-
- FramePeer peer = (FramePeer)this.peer;
- if (peer != null) {
- mbManagement = true;
- menuBar.addNotify();
- peer.setMenuBar(menuBar);
- }
- }
- invalidate();
- }
-
- /**
- * Indicates whether this frame is resizable.
- * By default, all frames are initially resizable.
- * @return <code>true</code> if the user can resize this frame;
- * <code>false</code> otherwise.
- * @see java.awt.Frame#setResizable
- */
- public boolean isResizable() {
- return resizable;
- }
-
- /**
- * Sets the resizable flag, which determines whether
- * this frame is resizable.
- * By default, all frames are initially resizable.
- * @param resizable <code>true</code> if this frame is resizable;
- * <code>false</code> otherwise.
- * @see java.awt.Frame#isResizable
- */
- public synchronized void setResizable(boolean resizable) {
- this.resizable = resizable;
- FramePeer peer = (FramePeer)this.peer;
- if (peer != null) {
- peer.setResizable(resizable);
- }
- }
-
- /**
- * Removes the specified menu bar from this frame.
- * @param m the menu component to remove.
- */
- public synchronized void remove(MenuComponent m) {
- if (m == menuBar) {
- FramePeer peer = (FramePeer)this.peer;
- if (peer != null) {
- mbManagement = true;
- menuBar.removeNotify();
- peer.setMenuBar(null);
- }
- menuBar.parent = null;
- menuBar = null;
- } else {
- super.remove(m);
- }
- }
-
- /**
- * Disposes of the Frame. This method must
- * be called to release the resources that
- * are used for the frame. All components
- * contained by the frame and all windows
- * owned by the frame will also be destroyed.
- */
- public synchronized void dispose() {
- if (menuBar != null) {
- remove(menuBar);
- menuBar = null;
- }
- removeFromFrameList();
- super.dispose();
- }
-
- void postProcessKeyEvent(KeyEvent e) {
- if (menuBar != null && menuBar.handleShortcut(e)) {
- e.consume();
- return;
- }
- super.postProcessKeyEvent(e);
- }
-
- /**
- * Returns the parameter String of this Frame.
- */
- protected String paramString() {
- String str = super.paramString();
- if (resizable) {
- str += ",resizable";
- }
- if (title != null) {
- str += ",title=" + title;
- }
- return str;
- }
-
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>Component.setCursor(Cursor)</code>.
- */
- public synchronized void setCursor(int cursorType) {
- if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
- throw new IllegalArgumentException("illegal cursor type");
- }
- setCursor(Cursor.getPredefinedCursor(cursorType));
- }
-
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>Component.getCursor()</code>.
- */
- public int getCursorType() {
- return (getCursor().getType());
- }
-
- /**
- * Returns an array containing all Frames created by the application.
- * If called from an applet, the array will only include the Frames
- * created by those applets which share a SecurityContext.
- * @see java.lang.SecurityManager#getSecurityContext
- * @since JDK1.2
- */
- public static Frame[] getFrames() {
- synchronized (Frame.class) {
- Frame frameCopy[];
- Vector frameList =
- (Vector)AppContext.getAppContext().get(Frame.class);
- if (frameList != null) {
- frameCopy = new Frame[frameList.size()];
- frameList.copyInto(frameCopy);
- } else {
- frameCopy = new Frame[0];
- }
- return frameCopy;
- }
- }
-
- void addToFrameList() {
- synchronized (Frame.class) {
- Vector frameList =
- (Vector)AppContext.getAppContext().get(Frame.class);
- if (frameList == null) {
- frameList = new Vector();
- AppContext.getAppContext().put(Frame.class, frameList);
- }
- frameList.addElement(this);
- }
- }
-
- void removeFromFrameList() {
- synchronized (Frame.class) {
- Vector frameList =
- (Vector)AppContext.getAppContext().get(Frame.class);
- if (frameList != null) {
- frameList.removeElement(this);
- }
- }
- }
-
- /* Serialization support. If there's a MenuBar we restore
- * its (transient) parent field here. Likewise for top level
- * windows that are "owned" by this frame.
- */
-
- private int frameSerializedDataVersion = 1;
-
-
- private void writeObject(ObjectOutputStream s)
- throws IOException
- {
- s.defaultWriteObject();
- }
-
-
- private void readObject(ObjectInputStream s)
- throws ClassNotFoundException, IOException
- {
- s.defaultReadObject();
-
- if (menuBar != null)
- menuBar.parent = this;
-
- // Ensure 1.1 serialized Frames can read & hook-up
- // owned windows properly
- //
- if (ownedWindows != null) {
- ownedWindowList = ownedWindows;
- connectOwnedWindows();
- ownedWindows = null;
- }
-
- addToFrameList();
- }
- }
-
-