home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Frame.java < prev    next >
Text File  |  1996-05-03  |  6KB  |  246 lines

  1. /*
  2.  * @(#)Frame.java    1.39 96/02/29 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.FramePeer;
  22.  
  23. /**
  24.  * A Frame is a top-level window with a title.
  25.  * The default layout for a frame is BorderLayout.
  26.  *
  27.  * @version     1.39, 29 Feb 1996
  28.  * @author     Sami Shaio
  29.  */
  30. public class Frame extends Window implements MenuContainer {
  31.     public static final int    DEFAULT_CURSOR           = 0;
  32.     public static final int    CROSSHAIR_CURSOR         = 1;
  33.     public static final int    TEXT_CURSOR              = 2;
  34.     public static final int    WAIT_CURSOR             = 3;
  35.     public static final int    SW_RESIZE_CURSOR         = 4;
  36.     public static final int    SE_RESIZE_CURSOR         = 5;
  37.     public static final int    NW_RESIZE_CURSOR        = 6;
  38.     public static final int    NE_RESIZE_CURSOR         = 7;
  39.     public static final int    N_RESIZE_CURSOR         = 8;
  40.     public static final int    S_RESIZE_CURSOR         = 9;
  41.     public static final int    W_RESIZE_CURSOR             = 10;
  42.     public static final int    E_RESIZE_CURSOR            = 11;
  43.     public static final int    HAND_CURSOR            = 12;
  44.     public static final int    MOVE_CURSOR            = 13;    
  45.     
  46.     String     title = "Untitled";
  47.     Image      icon;
  48.     MenuBar    menuBar;
  49.     boolean    resizable = true;
  50.     Image    cursorImage;
  51.     int        cursorType = DEFAULT_CURSOR;
  52.     Color    cursorFg;
  53.     Color    cursorBg;
  54.     
  55.     /**
  56.      * Constructs a new Frame that is initially invisible.
  57.      * @see Component#resize
  58.      * @see Component#show
  59.      */
  60.     public Frame() {
  61.     visible = false;
  62.     setLayout(new BorderLayout());
  63.     }
  64.     
  65.     /**
  66.      * Constructs a new, initially invisible Frame with the specified 
  67.      * title.
  68.      * @param title te specified title 
  69.      * @see Component#resize
  70.      * @see Component#show
  71.      */
  72.     public Frame(String title) {
  73.     this();
  74.     this.title = title;
  75.     }
  76.     
  77.     /**
  78.      * Creates the Frame's peer.  The peer allows us to change the look
  79.      * of the Frame without changing its functionality.
  80.      */
  81.     public synchronized void addNotify() {
  82.     peer = getToolkit().createFrame(this);
  83.     if (menuBar != null) {
  84.         menuBar.addNotify();
  85.         ((FramePeer)peer).setMenuBar(menuBar);
  86.     }
  87.     super.addNotify();
  88.     }
  89.     
  90.     /**
  91.      * Gets the title of the Frame.
  92.      * @see #setTitle
  93.      */
  94.     public String getTitle() {
  95.     return title;
  96.     }
  97.     
  98.     /**
  99.      * Sets the title for this Frame to the specified title.
  100.      * @param title the specified title of this Frame
  101.      * @see #getTitle
  102.      */
  103.     public void setTitle(String title) {
  104.     this.title = title;
  105.     FramePeer peer = (FramePeer)this.peer;
  106.     if (peer != null) {
  107.         peer.setTitle(title);
  108.     }
  109.     }
  110.     
  111.     /**
  112.      * Returns the icon image for this Frame.
  113.      */
  114.     public Image getIconImage() {
  115.     return icon;
  116.     }
  117.     
  118.     /**
  119.      * Sets the image to display when this Frame is iconized. Note that
  120.      * not all platforms support the concept of iconizing a window.
  121.      * @param image the icon image to be displayed
  122.      */
  123.     public void setIconImage(Image image) {
  124.     this.icon = image;
  125.     FramePeer peer = (FramePeer)this.peer;
  126.     if (peer != null) {
  127.         peer.setIconImage(image);
  128.     }
  129.     }
  130.     
  131.     /**
  132.      * Gets the menu bar for this Frame.
  133.      */
  134.     public MenuBar getMenuBar() {
  135.     return menuBar;
  136.     }
  137.     
  138.     /**
  139.      * Sets the menubar for this Frame to the specified menubar.
  140.      * @param mb the menubar being set
  141.      */
  142.     public synchronized void setMenuBar(MenuBar mb) {
  143.     if (menuBar == mb) {
  144.         return;
  145.     }
  146.     if ((mb != null) && (mb.parent != null)) {
  147.         mb.parent.remove(mb);
  148.     }
  149.     if (menuBar != null) {
  150.         remove(menuBar);
  151.     }
  152.     menuBar = mb;
  153.     if (menuBar != null) {
  154.         menuBar.parent = this;
  155.     
  156.          FramePeer peer = (FramePeer)this.peer;
  157.         if (peer != null) {
  158.             menuBar.addNotify();
  159.             peer.setMenuBar(menuBar);
  160.         }
  161.     }
  162.     }
  163.     
  164.     /**
  165.      * Removes the specified menu bar from this Frame.
  166.      */
  167.     public synchronized void remove(MenuComponent m) {
  168.     if (m == menuBar) {
  169.         FramePeer peer = (FramePeer)this.peer;
  170.         if (peer != null) {
  171.         menuBar.removeNotify();
  172.         menuBar.parent = null;
  173.         peer.setMenuBar(null);
  174.         }
  175.         menuBar = null;
  176.     }
  177.     }
  178.     
  179.     /**
  180.      * Disposes of the Frame. This method must
  181.      * be called to release the resources that
  182.      * are used for the frame.
  183.      */
  184.     public synchronized void dispose() {
  185.     if (menuBar != null) {
  186.         remove(menuBar);
  187.             menuBar = null;
  188.     }
  189.         super.dispose();
  190.     }
  191.     
  192.     /**
  193.      * Returns true if the user can resize the Frame.
  194.      */
  195.     public boolean isResizable() {
  196.     return resizable;
  197.     }
  198.     
  199.     /**
  200.      * Sets the resizable flag.
  201.      * @param resizable true if resizable; false otherwise.
  202.      */
  203.     public void setResizable(boolean resizable) {
  204.     this.resizable = resizable;
  205.     FramePeer peer = (FramePeer)this.peer;
  206.     if (peer != null) {
  207.         peer.setResizable(resizable);
  208.     }
  209.     }
  210.     
  211.     /**
  212.      * Set the cursor image to a predefined cursor.
  213.      * @param cursorType one of the cursor constants defined above.
  214.      */
  215.     public void setCursor(int cursorType) {
  216.     if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
  217.         throw new IllegalArgumentException("illegal cursor type");
  218.     }
  219.     this.cursorType = cursorType;
  220.     if (peer != null) {
  221.         ((FramePeer)peer).setCursor(cursorType);
  222.     }
  223.     }
  224.  
  225.     /**
  226.      * Return the cursor type
  227.      */
  228.     public int getCursorType() {
  229.     return cursorType;
  230.     }
  231.     
  232.     /**
  233.      * Returns the parameter String of this Frame.
  234.      */
  235.     protected String paramString() {
  236.     String str = super.paramString();
  237.     if (resizable) {
  238.         str += ",resizable";
  239.     }
  240.     if (title != null) {
  241.         str += ",title=" + title;
  242.     }
  243.     return str;
  244.     }
  245. }
  246.