home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Frame.java < prev    next >
Text File  |  1998-01-23  |  12KB  |  452 lines

  1. /*
  2.  * @(#)Frame.java    1.71 97/11/13 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.71, 11/13/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.     boolean     mbManagement = false;   /* used only by the Motif impl. */
  128.  
  129.     /* 
  130.      * The Windows owned by the Frame.
  131.      */
  132.     Vector ownedWindows;
  133.  
  134.     private static final String base = "frame";
  135.     private static int nameCounter = 0;
  136.  
  137.     /*
  138.      * JDK 1.1 serialVersionUID 
  139.      */
  140.      private static final long serialVersionUID = 2673458971256075116L;
  141.  
  142.     /**
  143.      * Constructs a new instance of <code>Frame</code> that is 
  144.      * initially invisible.
  145.      * @see Component#setSize
  146.      * @see Component#setVisible
  147.      * @since JDK1.0
  148.      */
  149.     public Frame() {
  150.         this("");
  151.     }
  152.  
  153.     /**
  154.      * Constructs a new, initially invisible <code>Frame</code> object 
  155.      * with the specified title.
  156.      * @param title the title for the frame
  157.      * @see java.awt.Component#setSize
  158.      * @see java.awt.Component#setVisible
  159.      * @since JDK1.0
  160.      */
  161.     public Frame(String title) {
  162.     this.name = base + nameCounter++;
  163.     this.title = title;
  164.     visible = false;
  165.     setLayout(new BorderLayout());
  166.     }
  167.  
  168.     /** 
  169.      * Adds the specified window to the list of windows owned by
  170.      * the frame.
  171.      * @param window the window to be added
  172.      */
  173.     Window addOwnedWindow(Window window) {
  174.         if (window != null) {
  175.         if (ownedWindows == null) {
  176.             ownedWindows = new Vector();
  177.         }
  178.         ownedWindows.addElement(window);
  179.     }
  180.     return window;
  181.     }
  182.  
  183.     /** 
  184.      * Removes the specified window from the list of windows owned by
  185.      * the frame.
  186.      * @param window the window to be added
  187.      */
  188.     void removeOwnedWindow(Window window) {
  189.         if (window != null) {
  190.         if (ownedWindows != null) {
  191.           ownedWindows.removeElement(window);
  192.         }
  193.     }
  194.     }
  195.  
  196.     /**
  197.      * Creates the Frame's peer.  The peer allows us to change the look
  198.      * of the Frame without changing its functionality.
  199.      * @since JDK1.0
  200.      */
  201.     public void addNotify() {
  202.     peer = getToolkit().createFrame(this);
  203.         MenuBar menuBar = this.menuBar;
  204.     if (menuBar != null) {
  205.         menuBar.addNotify();
  206.         ((FramePeer)peer).setMenuBar(menuBar);
  207.     }
  208.     super.addNotify();
  209.     }
  210.  
  211.     /**
  212.      * Gets the title of the frame.
  213.      * @return    the title of this frame, or <code>null</code> 
  214.      *                if this frame doesn't have a title.
  215.      * @see       java.awt.Frame#setTitle
  216.      * @since     JDK1.0
  217.      */
  218.     public String getTitle() {
  219.     return title;
  220.     }
  221.  
  222.     /**
  223.      * Sets the title for this frame to the specified title.
  224.      * @param    title    the specified title of this frame.
  225.      * @see      java.awt.Frame#getTitle
  226.      * @since    JDK1.0
  227.      */
  228.     public synchronized void setTitle(String title) {
  229.     this.title = title;
  230.         FramePeer peer = (FramePeer)this.peer;
  231.     if (peer != null) {
  232.         peer.setTitle(title);
  233.     }
  234.     }
  235.  
  236.     /**
  237.      * Gets the icon image for this frame.
  238.      * @return    the icon image for this frame, or <code>null</code> 
  239.      *                    if this frame doesn't have an icon image.
  240.      * @see       java.awt.Frame#setIconImage
  241.      * @since     JDK1.0
  242.      */
  243.     public Image getIconImage() {
  244.     return icon;
  245.     }
  246.  
  247.     /**
  248.      * Sets the image to display when this frame is iconized. 
  249.      * Not all platforms support the concept of iconizing a window.
  250.      * @param     image the icon image to be displayed
  251.      * @see       java.awt.Frame#getIconImage
  252.      * @since     JDK1.0
  253.      */
  254.     public synchronized void setIconImage(Image image) {
  255.     this.icon = image;
  256.         FramePeer peer = (FramePeer)this.peer;
  257.     if (peer != null) {
  258.         peer.setIconImage(image);
  259.     }
  260.     }
  261.  
  262.     /**
  263.      * Gets the menu bar for this frame.
  264.      * @return    the menu bar for this frame, or <code>null</code> 
  265.      *                   if this frame doesn't have a menu bar.
  266.      * @see       java.awt.Frame#setMenuBar
  267.      * @since     JDK1.0
  268.      */
  269.     public MenuBar getMenuBar() {
  270.     return menuBar;
  271.     }
  272.  
  273.     /**
  274.      * Sets the menu bar for this frame to the specified menu bar.
  275.      * @param     mb the menu bar being set
  276.      * @see       java.awt.Frame#getMenuBar
  277.      * @since     JDK1.0
  278.      */
  279.     public synchronized void setMenuBar(MenuBar mb) {
  280.     if (menuBar == mb) {
  281.         return;
  282.     }
  283.     if ((mb != null) && (mb.parent != null)) {
  284.         mb.parent.remove(mb);
  285.     }
  286.     if (menuBar != null) {
  287.         remove(menuBar);
  288.     }
  289.     menuBar = mb;
  290.     if (menuBar != null) {
  291.         menuBar.parent = this;
  292.  
  293.         FramePeer peer = (FramePeer)this.peer;
  294.         if (peer != null) {
  295.         mbManagement = true;
  296.         menuBar.addNotify();
  297.         peer.setMenuBar(menuBar);
  298.         }
  299.     }
  300.         invalidate();
  301.     }
  302.  
  303.     /**
  304.      * Indicates whether this frame is resizable.  
  305.      * By default, all frames are initially resizable. 
  306.      * @return    <code>true</code> if the user can resize this frame; 
  307.      *                        <code>false</code> otherwise.
  308.      * @see       java.awt.Frame#setResizable
  309.      * @since     JDK1.0
  310.      */
  311.     public boolean isResizable() {
  312.     return resizable;
  313.     }
  314.  
  315.     /**
  316.      * Sets the resizable flag, which determines whether 
  317.      * this frame is resizable. 
  318.      * By default, all frames are initially resizable. 
  319.      * @param    resizable   <code>true</code> if this frame is resizable; 
  320.      *                       <code>false</code> otherwise.
  321.      * @see      java.awt.Frame#isResizable
  322.      * @since    JDK1.0
  323.      */
  324.     public synchronized void setResizable(boolean resizable) {
  325.     this.resizable = resizable;
  326.         FramePeer peer = (FramePeer)this.peer;
  327.     if (peer != null) {
  328.         peer.setResizable(resizable);
  329.     }
  330.     }
  331.  
  332.     /**
  333.      * Removes the specified menu bar from this frame.
  334.      * @param    m   the menu component to remove.
  335.      * @since    JDK1.0
  336.      */
  337.     public synchronized void remove(MenuComponent m) {
  338.     if (m == menuBar) {
  339.         FramePeer peer = (FramePeer)this.peer;
  340.         if (peer != null) {
  341.         mbManagement = true;
  342.         menuBar.removeNotify();
  343.         menuBar.parent = null;
  344.         peer.setMenuBar(null);
  345.         }
  346.         menuBar = null;
  347.     } else {
  348.             super.remove(m);
  349.         }
  350.     }
  351.  
  352.     /**
  353.      * Disposes of the Frame. This method must
  354.      * be called to release the resources that
  355.      * are used for the frame.  All components
  356.      * contained by the frame and all windows
  357.      * owned by the frame will also be destroyed.
  358.      * @since JDK1.0
  359.      */
  360.     public void dispose() {     // synch removed.
  361.       synchronized (Component.LOCK) {
  362.     if (ownedWindows != null) {
  363.       int ownedWindowCount = ownedWindows.size();
  364.       Window ownedWindowCopy[] = new Window[ownedWindowCount];
  365.       ownedWindows.copyInto(ownedWindowCopy);
  366.       for (int i = 0; i < ownedWindowCount ; i++) {
  367.         ownedWindowCopy[i].dispose();
  368.       }
  369.     }
  370.     if (menuBar != null) {
  371.         remove(menuBar);
  372.         menuBar = null;
  373.     }
  374.       }
  375.       super.dispose();
  376.     }
  377.  
  378.     void postProcessKeyEvent(KeyEvent e) {
  379.         if (menuBar != null && menuBar.handleShortcut(e)) {
  380.             e.consume();
  381.             return;
  382.         }
  383.         super.postProcessKeyEvent(e);
  384.     }
  385.  
  386.     /**
  387.      * Returns the parameter String of this Frame.
  388.      */
  389.     protected String paramString() {
  390.     String str = super.paramString();
  391.     if (resizable) {
  392.         str += ",resizable";
  393.     }
  394.     if (title != null) {
  395.         str += ",title=" + title;
  396.     }
  397.     return str;
  398.     }
  399.  
  400.     /**
  401.      * @deprecated As of JDK version 1.1,
  402.      * replaced by <code>Component.setCursor(Cursor)</code>.
  403.      */
  404.     public synchronized void setCursor(int cursorType) {
  405.     if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
  406.         throw new IllegalArgumentException("illegal cursor type");
  407.     }
  408.     setCursor(Cursor.getPredefinedCursor(cursorType));
  409.     }
  410.  
  411.     /**
  412.      * @deprecated As of JDK version 1.1,
  413.      * replaced by <code>Component.getCursor()</code>.
  414.      */
  415.     public int getCursorType() {
  416.     return (getCursor().getType());
  417.     }
  418.  
  419.  
  420.     /* Serialization support.  If there's a MenuBar we restore
  421.      * its (transient) parent field here.  Likewise for top level 
  422.      * windows that are "owned" by this frame.
  423.      */
  424.  
  425.     private int frameSerializedDataVersion = 1;
  426.  
  427.  
  428.     private void writeObject(ObjectOutputStream s)
  429.       throws IOException 
  430.     {
  431.       s.defaultWriteObject();
  432.     }
  433.  
  434.  
  435.     private void readObject(ObjectInputStream s)
  436.       throws ClassNotFoundException, IOException 
  437.     {
  438.       s.defaultReadObject();
  439.  
  440.       if (menuBar != null)
  441.     menuBar.parent = this;
  442.  
  443.       if (ownedWindows != null) {
  444.     for(int i = 0; i < ownedWindows.size(); i++) {
  445.       Window child = (Window)(ownedWindows.elementAt(i));
  446.       child.parent = this;
  447.     }
  448.       }
  449.     }
  450. }
  451.  
  452.