home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 May / PCW596.iso / wtest / clico / sunsoft / java / solaris.lzh / SOLARIS.TAZ / SOLARIS / hotjava / classsrc / awt / Frame.java < prev    next >
Encoding:
Java Source  |  1995-05-15  |  7.8 KB  |  291 lines

  1. /*
  2.  * @(#)Frame.java    1.39 95/03/20 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994 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 awt;
  20.  
  21. import awt.*;
  22. import java.util.*;
  23.  
  24. /**
  25.  * A Frame is a top-level window that can contain other windows.
  26.  *
  27.  * @version 1.39 20 Mar 1995
  28.  * @author Sami Shaio
  29.  */
  30. public class Frame extends Container {
  31.     static final Vector    allFrames = new Vector();
  32.     MenuBar    menuBar;
  33.     boolean placed = false;
  34.     public Font defaultFont = null;
  35.     public Color background;
  36.     private static Frame defaultParent;    // this is a very unfortunate hack
  37.  
  38.     /* Cache of the value returned from WServer.frameHasStatusBar(). */
  39.     boolean hasNativeStatusBar;    
  40.  
  41.     /* If !hasNativeStatusBar, the frame creates a status bar 
  42.        from a label.  Used only when !hasNativeStatusBar. */
  43.     Label statusBar;
  44.  
  45.     /* Current status message */
  46.     public String statusMessage;
  47.  
  48.     /**
  49.      * Constructs a Frame.
  50.      * @param ws is the WServer to create this Frame.
  51.      * @param hasTitleBar is true if this Frame should have a title region.
  52.      * @param isModal specifies whether the Frame is modal or not. If
  53.      * it is modal, then Frame.map will block until the frame is unmapped.
  54.      * @param parentFrame if not null then this Frame will be attached
  55.      * to the given Frame and serve the function of a dialog window.
  56.      * @param w is the width of this Frame.
  57.      * @param h is the height of this Frame.
  58.      * @param bg is the background color of this Frame.
  59.      */
  60.     public Frame(WServer ws,
  61.          boolean hasTitleBar,
  62.          boolean isModal,
  63.          Frame parentFrame,
  64.          int w,
  65.          int h,
  66.          Color bg) {
  67.     super(null, null);
  68.     if (defaultParent == null)
  69.         defaultParent = this;
  70.     wServer = ws;
  71.     background = bg;
  72.     wServer.frameCreate(this, hasTitleBar, isModal, parentFrame, w, h, bg);
  73.     setLayout(BorderLayout.defaultLayout);
  74.     addInsets(0, 0, 0, 0);
  75.         hasNativeStatusBar = wServer.frameHasStatusBar(this);
  76.     allFrames.addElement(this);
  77.     }
  78.  
  79.     /**
  80.      * Creates a modeless Frame.
  81.      */
  82.     public Frame(WServer ws,
  83.          boolean hasTitleBar,
  84.          Frame parentFrame,
  85.          int w,
  86.          int h,
  87.          Color bg) {
  88.     this(ws, hasTitleBar, false, parentFrame, w, h, bg);
  89.     }
  90.  
  91.     /**
  92.      * Constructs a Frame.  Unlike the previous constructor, this does not
  93.      * have a "parent" parameter.  It uses a default parent parameter that
  94.      * is the first Frame ever created.  In general, this is a <i>bad</i>
  95.      * constructor to call, but unfortunatly there are occasionally situations
  96.      * where Frames need to be created, but finding an appropriate parent is
  97.      * impossible.
  98.      * @param hasTitleBar is true if this Frame should have a title region.
  99.      * @param w is the width of this Frame.
  100.      * @param h is the height of this Frame.
  101.      * @param bg is the background color of this Frame.
  102.      */
  103.     public Frame(boolean hasTitleBar,
  104.          int w,
  105.          int h,
  106.          Color bg) {
  107.     this(defaultParent.wServer, hasTitleBar, defaultParent, w, h, bg);
  108. //    if (defaultParent == null)
  109. //        throw new IllegalArgumentException("No default parent frame");
  110.     }
  111.  
  112.  
  113.     /**
  114.      * Sets the default font to use for all gui elements contained in
  115.      * this Frame.
  116.      */
  117.     public synchronized void setDefaultFont(Font f) {
  118.     defaultFont = f;
  119.     wServer.frameSetDefaultFont(this, defaultFont);
  120.     }
  121.  
  122.     /**
  123.      * Called to inform the Frame that its size has changed and it
  124.      * should layout its children.
  125.      */
  126.     public synchronized void resize() {
  127.     tidy = false;
  128.     if (mapped) {
  129.         layout();
  130.         tidy = true;
  131.     }
  132.     }
  133.  
  134.     /**
  135.      * Returns the preferred dimension of this frame.
  136.      */
  137.     public Dimension getPreferredSize() {
  138.     return (theLayout != null) ?
  139.         theLayout.getPreferredSize(this) :
  140.         super.getPreferredSize();
  141.     }
  142.  
  143.     /**
  144.      * Returns the minimum dimension to hold this Frame.
  145.      */
  146.     public Dimension minDimension() {
  147.     Dimension    m;
  148.  
  149.     if (theLayout != null) {
  150.         m = theLayout.minDimension(this);
  151.         return m;
  152.     } else {
  153.         return super.minDimension();
  154.     }
  155.     }
  156.  
  157.     /** Shows this Frame.*/
  158.     public void map() {
  159.     super.map();
  160.     wServer.frameShow(this);
  161.     if (!placed) {
  162.         resize();
  163.         wServer.sync();
  164.         placed = true;
  165.     }
  166.     }
  167.  
  168.     /** Hides this Frame. */
  169.     public void unMap() {
  170.     wServer.frameHide(this);
  171.     super.unMap();
  172.     }
  173.  
  174.     /** Sets the title of this frame. */
  175.     public void setTitle(String title) {
  176.     wServer.frameSetTitle(this, title);
  177.     }
  178.  
  179.     /** Disposes of this Frame. */
  180.     public void dispose() {
  181.     int size = allFrames.size();
  182.     wServer.frameDispose(this);
  183.     for (int i = 0; i < size; i++) {
  184.         if (allFrames.elementAt(i) == this) {
  185.         allFrames.removeElementAt(i);
  186.         break;
  187.         }
  188.     }
  189.     }
  190.  
  191.     /** Override this method to take some action when the user decides
  192.      * to destroy this Frame.
  193.      */
  194.     public void handleQuit() {
  195.     System.exit(0);
  196.     }
  197.  
  198.  
  199.     /** Moves this Frame. UNIMPLEMENTED */
  200.     public void   move(int newX, int newY) {
  201.     }
  202.  
  203.     /**
  204.      * Set the minimum size of the window
  205.      */
  206.     public void setMinSize(int pWidth, int pHeight) {
  207.     wServer.frameSetMinSize(this, pWidth, pHeight);
  208.     }
  209.  
  210.     /** Sets the minimum size that this frame can be resized to and
  211.      * still show all its children.
  212.      */
  213.     public void setMinSize() {
  214.     Dimension d = minDimension();
  215.     setMinSize(d.width, d.height);
  216.     if (!mapped) {
  217.         resize(d.width, d.height);
  218.     }
  219.     }
  220.  
  221.     /**
  222.      * Reshapes this Frame to the given dimensions.
  223.      */
  224.     public void reshape(int x, int y, int w, int h) {
  225.     super.reshape(x, y, w, h);
  226.     if (mapped) {
  227.         unMap();
  228.         wServer.frameReshape(this, x, y, w, h);
  229.         layout();
  230.         map();
  231.     } else {
  232.         wServer.frameReshape(this, x, y, w, h);
  233.         layout();
  234.     }
  235.     }
  236.  
  237.     /*<not public yet>
  238.      * Shows or hides the status bar. 
  239.      * @param show is true if the status bar should be displayed.
  240.      */
  241.     public void showStatusBar(boolean show) {
  242.         if (hasNativeStatusBar) {
  243.             wServer.frameShowStatusBar(this, show);
  244.         } else {
  245.             if (show) {
  246.         statusBar.map();
  247.             } else {
  248.         statusBar.unMap();
  249.             }
  250.         }
  251.     }
  252.  
  253.     /*<not public yet>
  254.      * Returns whether the status bar is visible or not.
  255.      */
  256.     public boolean showingStatusBar() {
  257.         if (hasNativeStatusBar) {
  258.             return wServer.frameShowingStatusBar(this);
  259.         } else {
  260.             return statusBar.mapped;
  261.         }
  262.     }
  263.  
  264.     /*<not public yet>
  265.      * Displays the specified message in the status bar.
  266.      * @param message is the message to display in the status bar.
  267.      */
  268.     public void setStatusMessage(String message) {
  269.         statusMessage = message;
  270.         if (hasNativeStatusBar) {
  271.         wServer.frameSetStatusMessage(this, message);
  272.         } else {
  273.             statusBar.setText(message);
  274.         }
  275.     }
  276.  
  277.     /**
  278.      * Set the image to display when this Frame is iconized. Note that
  279.      * not all platforms support the concept of iconizing a window.
  280.      */
  281.     public void setIconImage(Image image) {
  282.     wServer.frameSetIconImage(this, image);
  283.     }
  284.  
  285.    /* temporary. remove once status bar code is moved here and
  286.       hotjava.java is updated appropriately. */
  287.     public boolean hasStatusBar() {
  288.     return wServer.frameHasStatusBar(this);
  289.     }
  290. }
  291.