home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / Frame.java < prev    next >
Text File  |  1997-10-27  |  9KB  |  356 lines

  1. /*
  2.  * @(#)Frame.java    1.65 97/05/21 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.
  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.  * @see WindowEvent
  40.  * @see Window#addWindowListener
  41.  *
  42.  * @version     1.65, 05/21/97
  43.  * @author     Sami Shaio
  44.  */
  45. public class Frame extends Window implements MenuContainer {
  46.  
  47.     /* Note: These are being obsoleted;  programs should use the Cursor class
  48.      * variables going forward. See Cursor and Component.setCursor.
  49.      */
  50.     public static final int    DEFAULT_CURSOR           = Cursor.DEFAULT_CURSOR;
  51.     public static final int    CROSSHAIR_CURSOR         = Cursor.CROSSHAIR_CURSOR;
  52.     public static final int    TEXT_CURSOR              = Cursor.TEXT_CURSOR;
  53.     public static final int    WAIT_CURSOR             = Cursor.WAIT_CURSOR;
  54.     public static final int    SW_RESIZE_CURSOR         = Cursor.SW_RESIZE_CURSOR;
  55.     public static final int    SE_RESIZE_CURSOR         = Cursor.SE_RESIZE_CURSOR;
  56.     public static final int    NW_RESIZE_CURSOR        = Cursor.NW_RESIZE_CURSOR;
  57.     public static final int    NE_RESIZE_CURSOR         = Cursor.NE_RESIZE_CURSOR;
  58.     public static final int    N_RESIZE_CURSOR         = Cursor.N_RESIZE_CURSOR;
  59.     public static final int    S_RESIZE_CURSOR         = Cursor.S_RESIZE_CURSOR;
  60.     public static final int    W_RESIZE_CURSOR             = Cursor.W_RESIZE_CURSOR;
  61.     public static final int    E_RESIZE_CURSOR            = Cursor.E_RESIZE_CURSOR;
  62.     public static final int    HAND_CURSOR            = Cursor.HAND_CURSOR;
  63.     public static final int    MOVE_CURSOR            = Cursor.MOVE_CURSOR;    
  64.  
  65.     String     title = "Untitled";
  66.     Image      icon;
  67.     MenuBar    menuBar;
  68.     boolean    resizable = true;
  69.  
  70.     /* 
  71.      * The Windows owned by the Frame.
  72.      */
  73.     Vector ownedWindows;
  74.  
  75.     private static final String base = "frame";
  76.     private static int nameCounter = 0;
  77.  
  78.     /*
  79.      * JDK 1.1 serialVersionUID 
  80.      */
  81.      private static final long serialVersionUID = 2673458971256075116L;
  82.  
  83.     /**
  84.      * Constructs a new Frame that is initially invisible.
  85.      * @see Component#setSize
  86.      * @see Component#setVisible
  87.      */
  88.     public Frame() {
  89.         this("");
  90.     }
  91.  
  92.     /**
  93.      * Constructs a new, initially invisible Frame with the specified 
  94.      * title.
  95.      * @param title the title for the frame
  96.      * @see Component#setSize
  97.      * @see Component#setVisible
  98.      */
  99.     public Frame(String title) {
  100.     this.name = base + nameCounter++;
  101.     this.title = title;
  102.     visible = false;
  103.     setLayout(new BorderLayout());
  104.     }
  105.  
  106.     /** 
  107.      * Adds the specified window to the list of windows owned by
  108.      * the frame.
  109.      * @param window the window to be added
  110.      */
  111.     Window addOwnedWindow(Window window) {
  112.         if (window != null) {
  113.         if (ownedWindows == null) {
  114.             ownedWindows = new Vector();
  115.         }
  116.         ownedWindows.addElement(window);
  117.     }
  118.     return window;
  119.     }
  120.  
  121.     /** 
  122.      * Removes the specified window from the list of windows owned by
  123.      * the frame.
  124.      * @param window the window to be added
  125.      */
  126.     void removeOwnedWindow(Window window) {
  127.         if (window != null) {
  128.         if (ownedWindows != null) {
  129.           ownedWindows.removeElement(window);
  130.         }
  131.     }
  132.     }
  133.  
  134.     /**
  135.      * Creates the Frame's peer.  The peer allows us to change the look
  136.      * of the Frame without changing its functionality.
  137.      */
  138.     public void addNotify() {
  139.     peer = getToolkit().createFrame(this);
  140.         MenuBar menuBar = this.menuBar;
  141.     if (menuBar != null) {
  142.         menuBar.addNotify();
  143.         ((FramePeer)peer).setMenuBar(menuBar);
  144.     }
  145.     super.addNotify();
  146.     }
  147.  
  148.     /**
  149.      * Gets the title of the Frame.
  150.      * @see #setTitle
  151.      */
  152.     public String getTitle() {
  153.     return title;
  154.     }
  155.  
  156.     /**
  157.      * Sets the title for this Frame to the specified title.
  158.      * @param title the specified title of this Frame
  159.      * @see #getTitle
  160.      */
  161.     public synchronized void setTitle(String title) {
  162.     this.title = title;
  163.         FramePeer peer = (FramePeer)this.peer;
  164.     if (peer != null) {
  165.         peer.setTitle(title);
  166.     }
  167.     }
  168.  
  169.     /**
  170.      * Returns the icon image for this Frame.
  171.      */
  172.     public Image getIconImage() {
  173.     return icon;
  174.     }
  175.  
  176.     /**
  177.      * Sets the image to display when this Frame is iconized. Note that
  178.      * not all platforms support the concept of iconizing a window.
  179.      * @param image the icon image to be displayed
  180.      */
  181.     public synchronized void setIconImage(Image image) {
  182.     this.icon = image;
  183.         FramePeer peer = (FramePeer)this.peer;
  184.     if (peer != null) {
  185.         peer.setIconImage(image);
  186.     }
  187.     }
  188.  
  189.     /**
  190.      * Gets the menu bar for this Frame.
  191.      */
  192.     public MenuBar getMenuBar() {
  193.     return menuBar;
  194.     }
  195.  
  196.     /**
  197.      * Sets the menubar for this Frame to the specified menubar.
  198.      * @param mb the menubar being set
  199.      */
  200.     public synchronized void setMenuBar(MenuBar mb) {
  201.     if (menuBar == mb) {
  202.         return;
  203.     }
  204.     if ((mb != null) && (mb.parent != null)) {
  205.         mb.parent.remove(mb);
  206.     }
  207.     if (menuBar != null) {
  208.         remove(menuBar);
  209.     }
  210.     menuBar = mb;
  211.     if (menuBar != null) {
  212.         menuBar.parent = this;
  213.  
  214.         FramePeer peer = (FramePeer)this.peer;
  215.         if (peer != null) {
  216.         menuBar.addNotify();
  217.         peer.setMenuBar(menuBar);
  218.         }
  219.     }
  220.         invalidate();
  221.     }
  222.  
  223.     /**
  224.      * Returns true if the user can resize the Frame.
  225.      */
  226.     public boolean isResizable() {
  227.     return resizable;
  228.     }
  229.  
  230.     /**
  231.      * Sets the resizable flag.
  232.      * @param resizable true if resizable; false otherwise.
  233.      */
  234.     public synchronized void setResizable(boolean resizable) {
  235.     this.resizable = resizable;
  236.         FramePeer peer = (FramePeer)this.peer;
  237.     if (peer != null) {
  238.         peer.setResizable(resizable);
  239.     }
  240.     }
  241.  
  242.     /**
  243.      * Removes the specified menu bar from this Frame.
  244.      */
  245.     public synchronized void remove(MenuComponent m) {
  246.     if (m == menuBar) {
  247.         FramePeer peer = (FramePeer)this.peer;
  248.         if (peer != null) {
  249.         menuBar.removeNotify();
  250.         menuBar.parent = null;
  251.         peer.setMenuBar(null);
  252.         }
  253.         menuBar = null;
  254.     } else {
  255.             super.remove(m);
  256.         }
  257.     }
  258.  
  259.     /**
  260.      * Disposes of the Frame. This method must
  261.      * be called to release the resources that
  262.      * are used for the frame.  All components
  263.      * contained by the frame and all windows
  264.      * owned by the frame will also be destroyed.
  265.      */
  266.     public synchronized void dispose() {
  267.     if (ownedWindows != null) {
  268.       int ownedWindowCount = ownedWindows.size();
  269.       Window ownedWindowCopy[] = new Window[ownedWindowCount];
  270.       ownedWindows.copyInto(ownedWindowCopy);
  271.       for (int i = 0; i < ownedWindowCount ; i++) {
  272.         ownedWindowCopy[i].dispose();
  273.       }
  274.     }
  275.     if (menuBar != null) {
  276.         remove(menuBar);
  277.         menuBar = null;
  278.     }
  279.     super.dispose();
  280.     }
  281.  
  282.     void postProcessKeyEvent(KeyEvent e) {
  283.         if (menuBar != null && menuBar.handleShortcut(e)) {
  284.             e.consume();
  285.             return;
  286.         }
  287.         super.postProcessKeyEvent(e);
  288.     }
  289.  
  290.     /**
  291.      * Returns the parameter String of this Frame.
  292.      */
  293.     protected String paramString() {
  294.     String str = super.paramString();
  295.     if (resizable) {
  296.         str += ",resizable";
  297.     }
  298.     if (title != null) {
  299.         str += ",title=" + title;
  300.     }
  301.     return str;
  302.     }
  303.  
  304.     /**
  305.      * @deprecated As of JDK version 1.1,
  306.      * replaced by Component.setCursor(Cursor).
  307.      */
  308.     public synchronized void setCursor(int cursorType) {
  309.     if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
  310.         throw new IllegalArgumentException("illegal cursor type");
  311.     }
  312.     setCursor(Cursor.getPredefinedCursor(cursorType));
  313.     }
  314.  
  315.     /**
  316.      * @deprecated As of JDK version 1.1,
  317.      * replaced by Component.getCursor().
  318.      */
  319.     public int getCursorType() {
  320.     return (getCursor().getType());
  321.     }
  322.  
  323.  
  324.     /* Serialization support.  If there's a MenuBar we restore
  325.      * its (transient) parent field here.  Likewise for top level 
  326.      * windows that are "owned" by this frame.
  327.      */
  328.  
  329.     private int frameSerializedDataVersion = 1;
  330.  
  331.  
  332.     private void writeObject(ObjectOutputStream s)
  333.       throws IOException 
  334.     {
  335.       s.defaultWriteObject();
  336.     }
  337.  
  338.  
  339.     private void readObject(ObjectInputStream s)
  340.       throws ClassNotFoundException, IOException 
  341.     {
  342.       s.defaultReadObject();
  343.  
  344.       if (menuBar != null)
  345.     menuBar.parent = this;
  346.  
  347.       if (ownedWindows != null) {
  348.     for(int i = 0; i < ownedWindows.size(); i++) {
  349.       Window child = (Window)(ownedWindows.elementAt(i));
  350.       child.parent = this;
  351.     }
  352.       }
  353.     }
  354. }
  355.  
  356.