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

  1. /*
  2.  * @(#)Window.java    1.24 96/05/02 Arthur van Hoff
  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.WindowPeer;
  22.  
  23. /**
  24.  * A Window is a top-level window with no borders and no
  25.  * menubar. It could be used to implement a pop-up menu.
  26.  * The default layout for a window is BorderLayout.
  27.  *
  28.  * @version     1.24, 02 May 1996
  29.  * @author     Sami Shaio
  30.  * @author     Arthur van Hoff
  31.  */
  32. public class Window extends Container {
  33.     String      warningString;
  34.  
  35.     private FocusManager   focusMgr;
  36.  
  37.     Window() {
  38.     SecurityManager sm = System.getSecurityManager();
  39.     if ((sm != null) && !sm.checkTopLevelWindow(this)) {
  40.         warningString = System.getProperty("awt.appletWarning",
  41.                            "Warning: Applet Window");
  42.     }
  43.     focusMgr = new FocusManager(this);
  44.     }
  45.  
  46.     /**
  47.      * Constructs a new Window initialized to an invisible state. It
  48.      * behaves as a modal dialog in that it will block input to other
  49.      * windows when shown.
  50.      *
  51.      * @param parent the owner of the dialog
  52.      * @see Component#resize
  53.      * @see #show
  54.      */
  55.     public Window(Frame parent) {
  56. /*
  57.     SecurityManager sm = System.getSecurityManager();
  58.  
  59.     if ((sm != null) && !sm.checkTopLevelWindow(this)) {
  60.         warningString = System.getProperty("awt.appletWarning",
  61.                            "Warning: Applet Window");
  62.     }
  63. */
  64.     this();
  65.     this.parent = parent;
  66.     visible = false;
  67.     setLayout(new BorderLayout());
  68.     }
  69.  
  70.     /**
  71.      * Creates the Window's peer.  The peer allows us to modify the appearance of the
  72.      * Window without changing its functionality.
  73.      */
  74.     public synchronized void addNotify() {
  75.     if (peer == null) {
  76.         peer = getToolkit().createWindow(this);
  77.     }
  78.     super.addNotify();
  79.     }
  80.  
  81.     /**
  82.      * Packs the components of the Window.
  83.      */
  84.     public synchronized void pack() {
  85.         if (parent != null && parent.getPeer() == null) {
  86.             parent.addNotify();
  87.         }
  88.     if (peer == null) {
  89.         addNotify();
  90.     }
  91.     resize(preferredSize());
  92.     validate();
  93.     }
  94.  
  95.     /**
  96.      * Shows the Window. This will bring the window to the
  97.      * front if the window is already visible.
  98.      * @see Component#hide
  99.      */
  100.     public void show() {
  101.     synchronized(this) {
  102.             if (parent != null && parent.getPeer() == null) {
  103.                 parent.addNotify();
  104.             }
  105.         if (peer == null) {
  106.         addNotify();
  107.         }
  108.         validate();        
  109.     }
  110.     if (visible) {
  111.         toFront();
  112.     } else {
  113.         synchronized(this) {
  114.             visible = true;
  115.         }
  116.         peer.show();
  117.     }
  118.     }
  119.  
  120.     /**
  121.      * Disposes of the Window. This method must
  122.      * be called to release the resources that
  123.      * are used for the window.
  124.      */
  125.     public synchronized void dispose() {
  126.     hide();
  127.     removeNotify();
  128.     }
  129.  
  130.     /**
  131.      * Brings the frame to the front of the Window.
  132.      */
  133.     public void toFront() {
  134.     WindowPeer peer = (WindowPeer)this.peer;
  135.     if (peer != null) {
  136.         peer.toFront();
  137.     }
  138.     }
  139.  
  140.     /**
  141.      * Sends the frame to the back of the Window.
  142.      */
  143.     public void toBack() {
  144.     WindowPeer peer = (WindowPeer)this.peer;
  145.     if (peer != null) {
  146.         peer.toBack();
  147.     }
  148.     }
  149.  
  150.     /**
  151.      * Returns the toolkit of this frame.
  152.      * @see Toolkit
  153.      */
  154.     public Toolkit getToolkit() {
  155.     return Toolkit.getDefaultToolkit();
  156.     }
  157.  
  158.     /**
  159.      * Gets the warning string for this window. This is
  160.      * a string that will be displayed somewhere in the
  161.      * visible area of windows that are not secure.
  162.      */
  163.     public final String getWarningString() {
  164.     return warningString;
  165.     }
  166.  
  167.     /* Handle TAB and Shift-TAB events. */
  168.     boolean handleTabEvent(Event e) {
  169.         if (e.id != Event.KEY_PRESS && e.id != Event.KEY_RELEASE) {
  170.             return false;
  171.         }
  172.         if (e.key != '\t' || (e.target instanceof TextArea)) {
  173.             return false;
  174.         }
  175.     if ((e.modifiers & ~Event.SHIFT_MASK) > 0) {
  176.         return false;
  177.     }
  178.     if (e.id == Event.KEY_RELEASE) {
  179.         return true;
  180.     }
  181.     if (e.shiftDown()) {
  182.         return focusMgr.focusPrevious();
  183.     } else {
  184.         return focusMgr.focusNext();
  185.     }
  186.      }
  187.  
  188.     void setFocusOwner(Component c) {
  189.     focusMgr.setFocusOwner(c);
  190.     }
  191.  
  192.     void nextFocus(Component base) {
  193.     focusMgr.focusNext(base);
  194.     }
  195. }
  196.  
  197. class FocusManager {
  198.      Container focusRoot;
  199.      Component focusOwner;
  200.  
  201.      FocusManager(Container cont) {
  202.     focusRoot = cont;
  203.      }
  204.      
  205.      synchronized void setFocusOwner(Component c) {
  206.     focusOwner = c;
  207.      }
  208.     
  209.      boolean focusNext() {
  210.     return focusNext(focusOwner);
  211.      }
  212.  
  213.      synchronized boolean focusNext(Component base) {
  214.     int i;    
  215.     Component target = base;
  216.     if (target == null || target.parent == null) {
  217.         return false;
  218.     }   
  219.     do {
  220.         boolean found = false;
  221.         Container p = target.parent;
  222.         Component c;
  223.         for (i = 0; i < p.ncomponents; i++) {
  224.             c = p.component[i];
  225.             if (found) {
  226.             if (c instanceof Container) {
  227.                 if (focusForward((Container)c)) {
  228.                 return true;
  229.                 }
  230.             } else {
  231.                 if (assignFocus(c)) {
  232.                 return true;
  233.                 }
  234.             }            
  235.             } else if (c == target) {
  236.             found = true;    
  237.             }
  238.         } 
  239.         target = p;
  240.     } while (target != focusRoot);
  241.  
  242.         return false;        
  243.     }
  244.  
  245.     boolean focusPrevious() {
  246.     return focusPrevious(focusOwner);
  247.     }
  248.     
  249.     synchronized boolean focusPrevious(Component base) {
  250.     int i;
  251.     Component target = base;
  252.     if (target == null || target.parent == null) {
  253.         return false;
  254.     }       
  255.     do {
  256.         boolean found = false;
  257.         Container p = target.parent;
  258.         Component c;
  259.         for (i = p.ncomponents-1; i >= 0; i--) {
  260.             c = p.component[i];
  261.             if (found) {
  262.             if (c instanceof Container) {
  263.                 if (focusBackward((Container)c)) {
  264.                 return true;
  265.                 }
  266.             } else {
  267.                 if (assignFocus(c)) {
  268.                 return true;
  269.                 }
  270.             }            
  271.             } else if (c == target) {
  272.             found = true;    
  273.             }
  274.         } 
  275.         target = p;
  276.     } while (target != focusRoot);
  277.  
  278.         return false;        
  279.     }
  280.  
  281.     boolean assignFocus(Component c) {
  282.         if (c.isVisible() && c.tabbable() && c.isEnabled() ) {
  283.             c.requestFocus();
  284.             return true;
  285.         }
  286.         return false;
  287.     }
  288.  
  289.     boolean focusForward(Container cont) {
  290.     int i;
  291.         for(i = 0; i < cont.ncomponents; i++) {
  292.            if (cont.component[i] instanceof Container) {
  293.                 if (focusForward((Container)cont.component[i]) == true) {
  294.                     return true;
  295.                 }
  296.             } else {
  297.                 if (assignFocus(cont.component[i]) == true) {
  298.                     return true;
  299.                 }
  300.             }    
  301.         }
  302.         return false;
  303.     }
  304.  
  305.     boolean focusBackward(Container cont) {
  306.     int i;
  307.         for(i = cont.ncomponents-1; i >= 0; i--) {
  308.            if (cont.component[i] instanceof Container) {
  309.                 if (focusBackward((Container)cont.component[i]) == true) {
  310.                     return true;
  311.                 }
  312.             } else {
  313.                 if (assignFocus(cont.component[i]) == true) {
  314.                     return true;
  315.                 }
  316.             }    
  317.         }
  318.         return false;
  319.     }
  320. }
  321.