home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / WindowsDesktopManager.java < prev    next >
Text File  |  1998-02-26  |  5KB  |  154 lines

  1. /*
  2.  * @(#)WindowsDesktopManager.java    1.5 98/02/05
  3.  *
  4.  * Copyright (c) 1997 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.  */
  20.  
  21.  
  22. package com.sun.java.swing.plaf.windows;
  23.  
  24. import com.sun.java.swing.DefaultDesktopManager;
  25. import com.sun.java.swing.JInternalFrame;
  26. import com.sun.java.swing.JLayeredPane;
  27. import java.awt.Component;
  28. import java.awt.Container;
  29. import java.awt.Dimension;
  30. import java.beans.PropertyVetoException;
  31. import java.util.Vector;
  32.  
  33. /**
  34.  * This class implements a DesktopManager which more closely follows 
  35.  * the MDI model than the DefaultDesktopManager.  Unlike the
  36.  * DefaultDesktopManager policy, MDI requires that the selected
  37.  * and activated child frames are the same, and that that frame
  38.  * always be the top-most window.
  39.  * <p>
  40.  * The maximized state is managed by the DesktopManager with MDI,
  41.  * instead of just being a property of the individual child frame.
  42.  * This means that if the currently selected window is maximized
  43.  * and another window is selected, that new window will be maximized.
  44.  *
  45.  * @see com.sun.java.swing.DefaultDesktopManager
  46.  * @version 1.5 02/05/98
  47.  * @author Thomas Ball
  48.  */
  49. public class WindowsDesktopManager extends DefaultDesktopManager 
  50.         implements java.io.Serializable {
  51.  
  52.     /* The frame which is currently selected/activated.
  53.      * We store this value to enforce MDI's single-selection model.
  54.      */
  55.     JInternalFrame currentFrame;
  56.  
  57.     /* The list of frames, sorted by order of creation.
  58.      * This list is necessary because by default the order of
  59.      * child frames in the JDesktopPane changes during frame
  60.      * activation (the activated frame is moved to index 0).
  61.      * We preserve the creation order so that "next" and "previous"
  62.      * frame actions make sense.
  63.      */
  64.     Vector childFrames = new Vector(1);
  65.  
  66.     public void closeFrame(JInternalFrame f) {
  67.         activateNextFrame();
  68.         childFrames.removeElement(f);
  69.         super.closeFrame(f);
  70.     }
  71.  
  72.     public void activateFrame(JInternalFrame f) {
  73.         try {
  74.             super.activateFrame(f);
  75.  
  76.             // If this is the first activation, add to child list.
  77.             if (childFrames.indexOf(f) == -1) {
  78.                 childFrames.addElement(f);
  79.             }
  80.  
  81.             if (currentFrame != null && f != currentFrame) {
  82.                 // If the current frame is maximized, transfer that 
  83.                 // attribute to the frame being activated.
  84.                 if (currentFrame.isMaximum()) {
  85.                     currentFrame.setMaximum(false);
  86.                     f.setMaximum(true);
  87.                 }
  88.                 if (currentFrame.isSelected()) {
  89.                     currentFrame.setSelected(false);
  90.                 }
  91.             }
  92.  
  93.             if (!f.isSelected()) {
  94.                 f.setSelected(true);
  95.             }
  96.             currentFrame = f;
  97.         } catch (PropertyVetoException e) {}
  98.     }
  99.  
  100.     private void switchFrame(boolean next) {
  101.         if (currentFrame == null) {
  102.             return;
  103.         }
  104.  
  105.         int count = childFrames.size();
  106.         if (count <= 1) {
  107.             // No other child frames.
  108.             return;
  109.         }
  110.  
  111.         int currentIndex = childFrames.indexOf(currentFrame);
  112.         if (currentIndex == -1) {
  113.             // should never happen...
  114.             return;
  115.         }
  116.  
  117.         int nextIndex;
  118.         if (next) {
  119.             nextIndex = currentIndex + 1;
  120.             if (nextIndex == count) {
  121.                 nextIndex = 0;
  122.             }
  123.         } else {
  124.             nextIndex = currentIndex - 1;
  125.             if (nextIndex == -1) {
  126.                 nextIndex = count - 1;
  127.             }
  128.         }
  129.         JInternalFrame f = (JInternalFrame)childFrames.elementAt(nextIndex);
  130.         activateFrame(f);
  131.         currentFrame = f;
  132.     }
  133.     
  134.     /**
  135.      * Activate the next child JInternalFrame, as determined by
  136.      * the frames' Z-order.  If there is only one child frame, it
  137.      * remains activated.  If there are no child frames, nothing 
  138.      * happens.
  139.      */
  140.     public void activateNextFrame() {
  141.         switchFrame(true);
  142.     }
  143.  
  144.     /**
  145.      * Activate the previous child JInternalFrame, as determined by
  146.      * the frames' Z-order.  If there is only one child frame, it
  147.      * remains activated.  If there are no child frames, nothing 
  148.      * happens.
  149.      */
  150.     public void activatePreviousFrame() {
  151.         switchFrame(false);
  152.     }
  153. }
  154.