home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / Frame.java < prev    next >
Text File  |  1997-08-30  |  11KB  |  447 lines

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