home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / Frame.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  12.1 KB  |  444 lines

  1. /*
  2.  * @(#)Frame.java    1.72 98/03/18
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.FramePeer;
  17. import java.awt.event.*;
  18. import java.util.Vector;
  19. import java.io.ObjectOutputStream;
  20. import java.io.ObjectInputStream;
  21. import java.io.IOException;
  22. import sun.awt.AppContext;
  23.  
  24.  
  25. /**
  26.  * A Frame is a top-level window with a title and a border.
  27.  * The default layout for a frame is BorderLayout.
  28.  *
  29.  * Frames are capable of generating the following types of window events:
  30.  * WindowOpened, WindowClosing, WindowClosed, WindowIconified,
  31.  * WindowDeiconified, WindowActivated, WindowDeactivated.
  32.  *
  33.  * @version     1.72, 03/18/98
  34.  * @author     Sami Shaio
  35.  * @see WindowEvent
  36.  * @see Window#addWindowListener
  37.  * @since       JDK1.0
  38.  */
  39. public class Frame extends Window implements MenuContainer {
  40.  
  41.     /* Note: These are being obsoleted;  programs should use the Cursor class
  42.      * variables going forward. See Cursor and Component.setCursor.
  43.      */
  44.  
  45.    /**
  46.     * @deprecated   replaced by <code>Cursor.DEFAULT_CURSOR</code>.
  47.     */
  48.     public static final int    DEFAULT_CURSOR           = Cursor.DEFAULT_CURSOR;
  49.  
  50.  
  51.    /**
  52.     * @deprecated   replaced by <code>Cursor.CROSSHAIR_CURSOR</code>.
  53.     */
  54.     public static final int    CROSSHAIR_CURSOR         = Cursor.CROSSHAIR_CURSOR;
  55.  
  56.    /**
  57.     * @deprecated   replaced by <code>Cursor.TEXT_CURSOR</code>.
  58.     */
  59.     public static final int    TEXT_CURSOR              = Cursor.TEXT_CURSOR;
  60.  
  61.    /**
  62.     * @deprecated   replaced by <code>Cursor.WAIT_CURSOR</code>.
  63.     */
  64.     public static final int    WAIT_CURSOR             = Cursor.WAIT_CURSOR;
  65.  
  66.    /**
  67.     * @deprecated   replaced by <code>Cursor.SW_RESIZE_CURSOR</code>.
  68.     */
  69.     public static final int    SW_RESIZE_CURSOR         = Cursor.SW_RESIZE_CURSOR;
  70.  
  71.    /**
  72.     * @deprecated   replaced by <code>Cursor.SE_RESIZE_CURSOR</code>.
  73.     */
  74.     public static final int    SE_RESIZE_CURSOR         = Cursor.SE_RESIZE_CURSOR;
  75.  
  76.    /**
  77.     * @deprecated   replaced by <code>Cursor.NW_RESIZE_CURSOR</code>.
  78.     */
  79.     public static final int    NW_RESIZE_CURSOR        = Cursor.NW_RESIZE_CURSOR;
  80.  
  81.    /**
  82.     * @deprecated   replaced by <code>Cursor.NE_RESIZE_CURSOR</code>.
  83.     */
  84.     public static final int    NE_RESIZE_CURSOR         = Cursor.NE_RESIZE_CURSOR;
  85.  
  86.    /**
  87.     * @deprecated   replaced by <code>Cursor.N_RESIZE_CURSOR</code>.
  88.     */
  89.     public static final int    N_RESIZE_CURSOR         = Cursor.N_RESIZE_CURSOR;
  90.  
  91.    /**
  92.     * @deprecated   replaced by <code>Cursor.S_RESIZE_CURSOR</code>.
  93.     */
  94.     public static final int    S_RESIZE_CURSOR         = Cursor.S_RESIZE_CURSOR;
  95.  
  96.    /**
  97.     * @deprecated   replaced by <code>Cursor.W_RESIZE_CURSOR</code>.
  98.     */
  99.     public static final int    W_RESIZE_CURSOR             = Cursor.W_RESIZE_CURSOR;
  100.  
  101.    /**
  102.     * @deprecated   replaced by <code>Cursor.E_RESIZE_CURSOR</code>.
  103.     */
  104.     public static final int    E_RESIZE_CURSOR            = Cursor.E_RESIZE_CURSOR;
  105.  
  106.    /**
  107.     * @deprecated   replaced by <code>Cursor.HAND_CURSOR</code>.
  108.     */
  109.     public static final int    HAND_CURSOR            = Cursor.HAND_CURSOR;
  110.  
  111.    /**
  112.     * @deprecated   replaced by <code>Cursor.MOVE_CURSOR</code>.
  113.     */
  114.     public static final int    MOVE_CURSOR            = Cursor.MOVE_CURSOR;    
  115.  
  116.     String     title = "Untitled";
  117.     Image      icon;
  118.     MenuBar    menuBar;
  119.     boolean    resizable = true;
  120.     boolean    mbManagement = false;    /* used only by the Motif impl.    */
  121.  
  122.     /* 
  123.      * The Windows owned by the Frame.
  124.      * Note: in 1.2 this has been superceded by Window.ownedWindowList
  125.      */
  126.     Vector ownedWindows;
  127.  
  128.     private static final String base = "frame";
  129.     private static int nameCounter = 0;
  130.  
  131.     /*
  132.      * JDK 1.1 serialVersionUID 
  133.      */
  134.      private static final long serialVersionUID = 2673458971256075116L;
  135.  
  136.     /**
  137.      * Constructs a new instance of <code>Frame</code> that is 
  138.      * initially invisible.
  139.      * @see Component#setSize
  140.      * @see Component#setVisible
  141.      */
  142.     public Frame() {
  143.         this("");
  144.     }
  145.  
  146.     /**
  147.      * Constructs a new, initially invisible <code>Frame</code> object 
  148.      * with the specified title.
  149.      * @param title the title for the frame
  150.      * @see java.awt.Component#setSize
  151.      * @see java.awt.Component#setVisible
  152.      */
  153.     public Frame(String title) {
  154.     this.name = base + nameCounter++;
  155.     this.title = title;
  156.     visible = false;
  157.     setLayout(new BorderLayout());
  158.         addToFrameList();
  159.     }
  160.  
  161.     /**
  162.      * Creates the Frame's peer.  The peer allows us to change the look
  163.      * of the Frame without changing its functionality.
  164.      */
  165.     public void addNotify() {
  166.     peer = getToolkit().createFrame(this);
  167.         MenuBar menuBar = this.menuBar;
  168.     if (menuBar != null) {
  169.         menuBar.addNotify();
  170.         ((FramePeer)peer).setMenuBar(menuBar);
  171.     }
  172.     super.addNotify();
  173.     }
  174.  
  175.     /**
  176.      * Gets the title of the frame.
  177.      * @return    the title of this frame, or <code>null</code> 
  178.      *                if this frame doesn't have a title.
  179.      * @see       java.awt.Frame#setTitle
  180.      */
  181.     public String getTitle() {
  182.     return title;
  183.     }
  184.  
  185.     /**
  186.      * Sets the title for this frame to the specified title.
  187.      * @param    title    the specified title of this frame.
  188.      * @see      java.awt.Frame#getTitle
  189.      */
  190.     public synchronized void setTitle(String title) {
  191.     this.title = title;
  192.         FramePeer peer = (FramePeer)this.peer;
  193.     if (peer != null) {
  194.         peer.setTitle(title);
  195.     }
  196.     }
  197.  
  198.     /**
  199.      * Gets the icon image for this frame.
  200.      * @return    the icon image for this frame, or <code>null</code> 
  201.      *                    if this frame doesn't have an icon image.
  202.      * @see       java.awt.Frame#setIconImage
  203.      */
  204.     public Image getIconImage() {
  205.     return icon;
  206.     }
  207.  
  208.     /**
  209.      * Sets the image to display when this frame is iconized. 
  210.      * Not all platforms support the concept of iconizing a window.
  211.      * @param     image the icon image to be displayed
  212.      * @see       java.awt.Frame#getIconImage
  213.      */
  214.     public synchronized void setIconImage(Image image) {
  215.     this.icon = image;
  216.         FramePeer peer = (FramePeer)this.peer;
  217.     if (peer != null) {
  218.         peer.setIconImage(image);
  219.     }
  220.     }
  221.  
  222.     /**
  223.      * Gets the menu bar for this frame.
  224.      * @return    the menu bar for this frame, or <code>null</code> 
  225.      *                   if this frame doesn't have a menu bar.
  226.      * @see       java.awt.Frame#setMenuBar
  227.      */
  228.     public MenuBar getMenuBar() {
  229.     return menuBar;
  230.     }
  231.  
  232.     /**
  233.      * Sets the menu bar for this frame to the specified menu bar.
  234.      * @param     mb the menu bar being set
  235.      * @see       java.awt.Frame#getMenuBar
  236.      */
  237.     public synchronized void setMenuBar(MenuBar mb) {
  238.     if (menuBar == mb) {
  239.         return;
  240.     }
  241.     if ((mb != null) && (mb.parent != null)) {
  242.         mb.parent.remove(mb);
  243.     }
  244.     if (menuBar != null) {
  245.         remove(menuBar);
  246.     }
  247.     menuBar = mb;
  248.     if (menuBar != null) {
  249.         menuBar.parent = this;
  250.  
  251.         FramePeer peer = (FramePeer)this.peer;
  252.         if (peer != null) {
  253.         mbManagement = true;
  254.         menuBar.addNotify();
  255.         peer.setMenuBar(menuBar);
  256.         }
  257.     }
  258.         invalidate();
  259.     }
  260.  
  261.     /**
  262.      * Indicates whether this frame is resizable.  
  263.      * By default, all frames are initially resizable. 
  264.      * @return    <code>true</code> if the user can resize this frame; 
  265.      *                        <code>false</code> otherwise.
  266.      * @see       java.awt.Frame#setResizable
  267.      */
  268.     public boolean isResizable() {
  269.     return resizable;
  270.     }
  271.  
  272.     /**
  273.      * Sets the resizable flag, which determines whether 
  274.      * this frame is resizable. 
  275.      * By default, all frames are initially resizable. 
  276.      * @param    resizable   <code>true</code> if this frame is resizable; 
  277.      *                       <code>false</code> otherwise.
  278.      * @see      java.awt.Frame#isResizable
  279.      */
  280.     public synchronized void setResizable(boolean resizable) {
  281.     this.resizable = resizable;
  282.         FramePeer peer = (FramePeer)this.peer;
  283.     if (peer != null) {
  284.         peer.setResizable(resizable);
  285.     }
  286.     }
  287.  
  288.     /**
  289.      * Removes the specified menu bar from this frame.
  290.      * @param    m   the menu component to remove.
  291.      */
  292.     public synchronized void remove(MenuComponent m) {
  293.     if (m == menuBar) {
  294.         FramePeer peer = (FramePeer)this.peer;
  295.         if (peer != null) {
  296.         mbManagement = true;
  297.         menuBar.removeNotify();
  298.         peer.setMenuBar(null);
  299.         }
  300.         menuBar.parent = null;
  301.         menuBar = null;
  302.     } else {
  303.             super.remove(m);
  304.         }
  305.     }
  306.  
  307.     /**
  308.      * Disposes of the Frame. This method must
  309.      * be called to release the resources that
  310.      * are used for the frame.  All components
  311.      * contained by the frame and all windows
  312.      * owned by the frame will also be destroyed.
  313.      */
  314.     public synchronized void dispose() {
  315.     if (menuBar != null) {
  316.         remove(menuBar);
  317.         menuBar = null;
  318.     }
  319.         removeFromFrameList();
  320.     super.dispose();
  321.     }
  322.  
  323.     void postProcessKeyEvent(KeyEvent e) {
  324.         if (menuBar != null && menuBar.handleShortcut(e)) {
  325.             e.consume();
  326.             return;
  327.         }
  328.         super.postProcessKeyEvent(e);
  329.     }
  330.  
  331.     /**
  332.      * Returns the parameter String of this Frame.
  333.      */
  334.     protected String paramString() {
  335.     String str = super.paramString();
  336.     if (resizable) {
  337.         str += ",resizable";
  338.     }
  339.     if (title != null) {
  340.         str += ",title=" + title;
  341.     }
  342.     return str;
  343.     }
  344.  
  345.     /**
  346.      * @deprecated As of JDK version 1.1,
  347.      * replaced by <code>Component.setCursor(Cursor)</code>.
  348.      */
  349.     public synchronized void setCursor(int cursorType) {
  350.     if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
  351.         throw new IllegalArgumentException("illegal cursor type");
  352.     }
  353.     setCursor(Cursor.getPredefinedCursor(cursorType));
  354.     }
  355.  
  356.     /**
  357.      * @deprecated As of JDK version 1.1,
  358.      * replaced by <code>Component.getCursor()</code>.
  359.      */
  360.     public int getCursorType() {
  361.     return (getCursor().getType());
  362.     }
  363.  
  364.     /**
  365.      * Returns an array containing all Frames created by the application.
  366.      * If called from an applet, the array will only include the Frames
  367.      * created by those applets which share a SecurityContext.
  368.      * @see   java.lang.SecurityManager#getSecurityContext
  369.      * @since JDK1.2
  370.      */
  371.     public static Frame[] getFrames() {
  372.         synchronized (Frame.class) {
  373.             Frame frameCopy[];
  374.             Vector frameList = 
  375.                 (Vector)AppContext.getAppContext().get(Frame.class);
  376.             if (frameList != null) {
  377.             frameCopy = new Frame[frameList.size()];
  378.             frameList.copyInto(frameCopy);
  379.             } else {
  380.                 frameCopy = new Frame[0];
  381.             }
  382.             return frameCopy;
  383.         }
  384.     }
  385.  
  386.     void addToFrameList() {
  387.         synchronized (Frame.class) {
  388.             Vector frameList = 
  389.                 (Vector)AppContext.getAppContext().get(Frame.class);
  390.             if (frameList == null) {
  391.                 frameList = new Vector();
  392.                 AppContext.getAppContext().put(Frame.class, frameList);
  393.             }
  394.             frameList.addElement(this);
  395.         }
  396.     }
  397.  
  398.     void removeFromFrameList() {
  399.         synchronized (Frame.class) {
  400.             Vector frameList = 
  401.                 (Vector)AppContext.getAppContext().get(Frame.class);
  402.             if (frameList != null) {
  403.                 frameList.removeElement(this);
  404.             }
  405.         }
  406.     }
  407.  
  408.     /* Serialization support.  If there's a MenuBar we restore
  409.      * its (transient) parent field here.  Likewise for top level 
  410.      * windows that are "owned" by this frame.
  411.      */
  412.  
  413.     private int frameSerializedDataVersion = 1;
  414.  
  415.  
  416.     private void writeObject(ObjectOutputStream s)
  417.       throws IOException 
  418.     {
  419.       s.defaultWriteObject();
  420.     }
  421.  
  422.  
  423.     private void readObject(ObjectInputStream s)
  424.       throws ClassNotFoundException, IOException 
  425.     {
  426.       s.defaultReadObject();
  427.  
  428.       if (menuBar != null)
  429.     menuBar.parent = this;
  430.  
  431.       // Ensure 1.1 serialized Frames can read & hook-up
  432.       // owned windows properly
  433.       //
  434.       if (ownedWindows != null) {
  435.           ownedWindowList = ownedWindows;
  436.           connectOwnedWindows();
  437.           ownedWindows = null;  
  438.       }
  439.  
  440.       addToFrameList();
  441.     }
  442. }
  443.  
  444.