home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / java / awt / Window.java < prev   
Encoding:
Java Source  |  1997-01-27  |  7.9 KB  |  323 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, 05/02/96
  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.  
  111.  
  112.     if (visible) {
  113.         toFront();
  114.     } else {
  115.         synchronized(this) {
  116.             visible = true;
  117.         }
  118.         peer.show();
  119.     }
  120.     }
  121.  
  122.     /**
  123.      * Disposes of the Window. This method must
  124.      * be called to release the resources that
  125.      * are used for the window.
  126.      */
  127.     public synchronized void dispose() {
  128.     hide();
  129.     removeNotify();
  130.     }
  131.  
  132.     /**
  133.      * Brings the frame to the front of the Window.
  134.      */
  135.     public void toFront() {
  136.     WindowPeer peer = (WindowPeer)this.peer;
  137.     if (peer != null) {
  138.         peer.toFront();
  139.     }
  140.     }
  141.  
  142.     /**
  143.      * Sends the frame to the back of the Window.
  144.      */
  145.     public void toBack() {
  146.     WindowPeer peer = (WindowPeer)this.peer;
  147.     if (peer != null) {
  148.         peer.toBack();
  149.     }
  150.     }
  151.  
  152.     /**
  153.      * Returns the toolkit of this frame.
  154.      * @see Toolkit
  155.      */
  156.     public Toolkit getToolkit() {
  157.     return Toolkit.getDefaultToolkit();
  158.     }
  159.  
  160.     /**
  161.      * Gets the warning string for this window. This is
  162.      * a string that will be displayed somewhere in the
  163.      * visible area of windows that are not secure.
  164.      */
  165.     public final String getWarningString() {
  166.     return warningString;
  167.     }
  168.  
  169.     /* Handle TAB and Shift-TAB events. */
  170.     boolean handleTabEvent(Event e) {
  171.         if (e.id != Event.KEY_PRESS && e.id != Event.KEY_RELEASE) {
  172.             return false;
  173.         }
  174.         if (e.key != '\t' || (e.target instanceof TextArea)) {
  175.             return false;
  176.         }
  177.     if ((e.modifiers & ~Event.SHIFT_MASK) > 0) {
  178.         return false;
  179.     }
  180.     if (e.id == Event.KEY_RELEASE) {
  181.         return true;
  182.     }
  183.     if (e.shiftDown()) {
  184.         return focusMgr.focusPrevious();
  185.     } else {
  186.         return focusMgr.focusNext();
  187.     }
  188.      }
  189.  
  190.     void setFocusOwner(Component c) {
  191.     focusMgr.setFocusOwner(c);
  192.     }
  193.  
  194.     void nextFocus(Component base) {
  195.     focusMgr.focusNext(base);
  196.     }
  197. }
  198.  
  199. class FocusManager {
  200.      Container focusRoot;
  201.      Component focusOwner;
  202.  
  203.      FocusManager(Container cont) {
  204.     focusRoot = cont;
  205.      }
  206.      
  207.      synchronized void setFocusOwner(Component c) {
  208.     focusOwner = c;
  209.      }
  210.     
  211.      boolean focusNext() {
  212.     return focusNext(focusOwner);
  213.      }
  214.  
  215.      synchronized boolean focusNext(Component base) {
  216.     int i;    
  217.     Component target = base;
  218.     if (target == null || target.parent == null) {
  219.         return false;
  220.     }   
  221.     do {
  222.         boolean found = false;
  223.         Container p = target.parent;
  224.         Component c;
  225.         for (i = 0; i < p.ncomponents; i++) {
  226.             c = p.component[i];
  227.             if (found) {
  228.             if (c instanceof Container) {
  229.                 if (focusForward((Container)c)) {
  230.                 return true;
  231.                 }
  232.             } else {
  233.                 if (assignFocus(c)) {
  234.                 return true;
  235.                 }
  236.             }            
  237.             } else if (c == target) {
  238.             found = true;    
  239.             }
  240.         } 
  241.         target = p;
  242.     } while (target != focusRoot);
  243.  
  244.         return false;        
  245.     }
  246.  
  247.     boolean focusPrevious() {
  248.     return focusPrevious(focusOwner);
  249.     }
  250.     
  251.     synchronized boolean focusPrevious(Component base) {
  252.     int i;
  253.     Component target = base;
  254.     if (target == null || target.parent == null) {
  255.         return false;
  256.     }       
  257.     do {
  258.         boolean found = false;
  259.         Container p = target.parent;
  260.         Component c;
  261.         for (i = p.ncomponents-1; i >= 0; i--) {
  262.             c = p.component[i];
  263.             if (found) {
  264.             if (c instanceof Container) {
  265.                 if (focusBackward((Container)c)) {
  266.                 return true;
  267.                 }
  268.             } else {
  269.                 if (assignFocus(c)) {
  270.                 return true;
  271.                 }
  272.             }            
  273.             } else if (c == target) {
  274.             found = true;    
  275.             }
  276.         } 
  277.         target = p;
  278.     } while (target != focusRoot);
  279.  
  280.         return false;        
  281.     }
  282.  
  283.     boolean assignFocus(Component c) {
  284.         if (c.isVisible() && c.tabbable() && c.isEnabled() ) {
  285.             c.requestFocus();
  286.             return true;
  287.         }
  288.         return false;
  289.     }
  290.  
  291.     boolean focusForward(Container cont) {
  292.     int i;
  293.         for(i = 0; i < cont.ncomponents; i++) {
  294.            if (cont.component[i] instanceof Container) {
  295.                 if (focusForward((Container)cont.component[i]) == true) {
  296.                     return true;
  297.                 }
  298.             } else {
  299.                 if (assignFocus(cont.component[i]) == true) {
  300.                     return true;
  301.                 }
  302.             }    
  303.         }
  304.         return false;
  305.     }
  306.  
  307.     boolean focusBackward(Container cont) {
  308.     int i;
  309.         for(i = cont.ncomponents-1; i >= 0; i--) {
  310.            if (cont.component[i] instanceof Container) {
  311.                 if (focusBackward((Container)cont.component[i]) == true) {
  312.                     return true;
  313.                 }
  314.             } else {
  315.                 if (assignFocus(cont.component[i]) == true) {
  316.                     return true;
  317.                 }
  318.             }    
  319.         }
  320.         return false;
  321.     }
  322. }
  323.