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

  1. /*
  2.  * @(#)DefaultDesktopManager.java    1.10 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;
  23.  
  24. import java.awt.Rectangle;
  25. import java.awt.Dimension;
  26. import java.awt.Insets;
  27. import java.awt.LayoutManager;
  28. import java.awt.Component;
  29. import java.awt.Container;
  30. import java.awt.Graphics;
  31. import java.awt.Dimension;
  32. import java.beans.PropertyVetoException;
  33. import java.beans.PropertyChangeEvent;
  34. import com.sun.java.swing.border.Border;
  35. import java.awt.event.ComponentListener;
  36. import java.awt.event.ComponentAdapter;
  37. import java.awt.event.ComponentEvent;
  38.  
  39. /** This is an implementaion of the DesktopManager. It currently implements a
  40.   * the basic behaviors for managing JInternanFrames in an arbitrary parent.
  41.   * JInternalFrames that are not children of a JDesktop will use this component
  42.   * to handle their desktop-like actions.
  43.   * @see JDesktopPane
  44.   * @see JInternalFrame
  45.   * @version 1.10 02/05/98
  46.   * @author David Kloba
  47.   */
  48. public class DefaultDesktopManager implements DesktopManager {
  49.     final static String PREVIOUS_BOUNDS_PROPERTY = "previousBounds";
  50.     final static String HAS_BEEN_ICONIFIED_PROPERTY = "wasIconOnce";
  51.  
  52.     /** Normally this method will not be called. If it is, it
  53.       * try to determine the appropriate parent from the desktopIcon of the frame.
  54.       * Will remove the desktopIcon from it's parent if it successfully adds the frame.
  55.       */
  56.     public void openFrame(JInternalFrame f) {
  57.         if(f.getDesktopIcon().getParent() != null) {
  58.             f.getDesktopIcon().getParent().add(f);
  59.             removeIconFor(f);
  60.         }
  61.     }
  62.  
  63.     /** Removes the frame, and if necessary the desktopIcon, from it's parent. */
  64.     public void closeFrame(JInternalFrame f) {
  65.         if(f.getParent() != null) {
  66.             Container c = f.getParent();
  67.             c.remove(f);
  68.             c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
  69.         }
  70.         removeIconFor(f);
  71.         if(getPreviousBounds(f) != null)
  72.             setPreviousBounds(f, null);
  73.         if(wasIcon(f))
  74.             setWasIcon(f, null);
  75.     }
  76.  
  77.     /** Resizes the frame to fill it's parents bounds. */
  78.     public void maximizeFrame(JInternalFrame f) {
  79.  
  80.         Rectangle p;
  81.         if(!f.isIcon()) {
  82.         setPreviousBounds(f, f.getBounds());
  83.             p = f.getParent().getBounds();
  84.         } else {
  85.             Container c = f.getDesktopIcon().getParent();
  86.             if(c == null)
  87.                 return;
  88.             p = c.getBounds();
  89.             try { f.setIcon(false); } catch (PropertyVetoException e2) { }
  90.         }
  91.         setBoundsForFrame(f, 0, 0, p.width, p.height);
  92.         try { f.setSelected(true); } catch (PropertyVetoException e2) { }
  93.  
  94.         removeIconFor(f);
  95.     }
  96.  
  97.     /** Restores the frame back to it's size and position prior to a maximizeFrame()
  98.       * call.
  99.       */
  100.     public void minimizeFrame(JInternalFrame f) {
  101.         if(getPreviousBounds(f) != null) {
  102.             Rectangle r = getPreviousBounds(f);
  103.             setPreviousBounds(f, null);
  104.             try { f.setSelected(true); } catch (PropertyVetoException e2) { }
  105.             if(f.isIcon())
  106.                 try { f.setIcon(false); } catch (PropertyVetoException e2) { }
  107.             setBoundsForFrame(f, r.x, r.y, r.width, r.height);
  108.         }
  109.         removeIconFor(f);
  110.     }
  111.  
  112.     /** Removes the frame from it's parent and adds it's desktopIcon to the parent. */
  113.     public void iconifyFrame(JInternalFrame f) {
  114.         JInternalFrame.JDesktopIcon desktopIcon;
  115.         Container c;
  116.  
  117.         desktopIcon = f.getDesktopIcon();
  118.         if(!wasIcon(f)) {
  119.             Rectangle r = getBoundsForIconOf(f);
  120.             desktopIcon.setBounds(r.x, r.y, r.width, r.height);
  121.             setWasIcon(f, Boolean.TRUE);
  122.         }
  123.  
  124.         c = f.getParent();
  125.         c.remove(f);
  126.         c.add(desktopIcon);
  127.         c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
  128.         try { f.setSelected(false); } catch (PropertyVetoException e2) { }
  129.     }
  130.  
  131.     /** Removes the desktopIcon from it's parent and adds it's frame to the parent. */
  132.     public void deiconifyFrame(JInternalFrame f) {
  133.         JInternalFrame.JDesktopIcon desktopIcon;
  134.         Dimension size;
  135.  
  136.         desktopIcon = f.getDesktopIcon();
  137.         if(desktopIcon.getParent() != null) {
  138.             desktopIcon.getParent().add(f);
  139.             removeIconFor(f);
  140.             try { f.setSelected(true); } catch (PropertyVetoException e2) { }
  141.         }
  142.     }
  143.  
  144.     /** Finds all the other peers of type JInternalFrames and set's their
  145.       * IS_SELECTED_PROPERTY to false. Also moves <b>f</b> to the front.
  146.       */
  147.     public void activateFrame(JInternalFrame f) {
  148.         Container p = f.getParent();
  149.         Component[] c;
  150.  
  151.         if(p == null) {
  152.             // If the frame is not in parent, it's icon maybe, check it
  153.             p = f.getDesktopIcon().getParent();
  154.             if(p == null)
  155.                 return;
  156.         }
  157.  
  158.         if(p instanceof JLayeredPane) {
  159.             c = ((JLayeredPane)p).getComponentsInLayer(
  160.                                 ((JLayeredPane)p).getLayer(f));
  161.         } else {
  162.             c = p.getComponents();
  163.         }
  164.         int i, count;
  165.         count = c.length;
  166.         for(i = 0; i < count; i++) {
  167.             if(c[i] != f && c[i] instanceof JInternalFrame) {
  168.                 JInternalFrame w = (JInternalFrame)c[i];
  169.                 if(w.isSelected())        {
  170.                    try { w.setSelected(false); } catch (PropertyVetoException e2) {}
  171.                 }
  172.             } else if(c[i] instanceof JInternalFrame.JDesktopIcon && c[i] != f.getDesktopIcon()) {
  173.                 JInternalFrame w = ((JInternalFrame.JDesktopIcon)c[i]).getInternalFrame();
  174.                 if(w.isSelected())        {
  175.                    try { w.setSelected(false); } catch (PropertyVetoException e2) {}
  176.                 }
  177.             }
  178.         }
  179.         f.moveToFront();
  180.     }
  181.  
  182.     public void deactivateFrame(JInternalFrame f) {
  183.     }
  184.  
  185.     public void beginDraggingFrame(JComponent f) {}
  186.  
  187.     /** Calls setBoundsForFrame() with the new values. */
  188.     public void dragFrame(JComponent f, int newX, int newY) {
  189.         setBoundsForFrame(f, newX, newY, f.getWidth(), f.getHeight());
  190.     }
  191.  
  192.     public void endDraggingFrame(JComponent f) {}
  193.  
  194.     public void beginResizingFrame(JComponent f, int direction) {}
  195.  
  196.     /** Calls setBoundsForFrame() with the new values. */
  197.     public void resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
  198.         setBoundsForFrame(f, newX, newY, newWidth, newHeight);
  199.     }
  200.  
  201.     public void endResizingFrame(JComponent f) {}
  202.  
  203.     // PENDING(klobad) this should be optimized
  204.     /** This moves the JComponent and repaints the damaged areas. */
  205.     public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
  206.         boolean didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight);
  207.         f.setBounds(newX, newY, newWidth, newHeight);
  208.         if(didResize) {
  209.             f.validate();
  210.         }
  211.     }
  212.  
  213.     /** Convience method to remove the desktopIcon of <b>f</b> is necessary. */
  214.     protected void removeIconFor(JInternalFrame f) {
  215.         JInternalFrame.JDesktopIcon di = f.getDesktopIcon();
  216.         Container c = di.getParent();
  217.         if(c != null) {
  218.             c.remove(di);
  219.             c.repaint(di.getX(), di.getY(), di.getWidth(), di.getHeight());
  220.         }
  221.     }
  222.  
  223.     /** The iconifyFrame() code calls this to determine the proper bounds
  224.       * for the desktopIcon.
  225.       */
  226.     protected Rectangle getBoundsForIconOf(JInternalFrame f) {
  227.         Rectangle p = f.getParent().getBounds();
  228.         Dimension prefSize = f.getDesktopIcon().getPreferredSize();
  229.         int x2, y2, w2, h2;
  230.  
  231.         w2 = prefSize.width;
  232.         h2 = prefSize.height;
  233.         x2 = 0;
  234.         y2 = p.height - h2;
  235.  
  236.         if(f.getParent() == null && f.getDesktopIcon().getParent() == null) {
  237.             return new Rectangle(x2, y2, w2, h2);
  238.         }
  239.  
  240.         Container lp = (Container)f.getParent();
  241.         if(lp == null)
  242.             lp = f.getDesktopIcon().getParent();
  243.  
  244.         int pos = lp.getComponentCount();
  245.         Component c[] = lp.getComponents();
  246.         Rectangle b;
  247.         int i, x3 = 0, y3 = p.height;
  248.         JInternalFrame next;
  249.         for(i = pos - 1; i >=0; i--)    {
  250.             next = null;
  251.             if(lp.getComponent(i) instanceof JInternalFrame)
  252.                 next = (JInternalFrame)lp.getComponent(i);
  253.             else if(lp.getComponent(i) instanceof JInternalFrame.JDesktopIcon)
  254.                 next = ((JInternalFrame.JDesktopIcon)lp.getComponent(i)).getInternalFrame();
  255.             if(next != null) {
  256.                 if(next != f && wasIcon(next)) {
  257.                     b = next.getDesktopIcon().getBounds();
  258.                     if(b.y < y3) {
  259.                         y3 = b.y;
  260.                         x3 = b.x + b.width;
  261.                     } else if(b.y == y3) {
  262.                         if(b.x + b.width > x3)
  263.                             x3 = b.x + b.width;
  264.                     }
  265.                 }
  266.             }
  267.         }
  268.  
  269.         if(y3 != p.height)
  270.             y2 = y3;
  271.         if(x3 + w2 > p.width) {
  272.             y2 -= h2;
  273.             x2 = 0;
  274.         } else {
  275.             x2 = x3;
  276.         }
  277.  
  278.         return new Rectangle(x2, y2, w2, h2);
  279.     }
  280.  
  281.     /** Stores the bounds of the component just before a maximize call. */
  282.     protected void setPreviousBounds(JInternalFrame f, Rectangle r)     {
  283.     if (r != null) {
  284.         f.putClientProperty(PREVIOUS_BOUNDS_PROPERTY, r);
  285.     }
  286.     }
  287.  
  288.     protected Rectangle getPreviousBounds(JInternalFrame f)     {
  289.         return (Rectangle)f.getClientProperty(PREVIOUS_BOUNDS_PROPERTY);
  290.     }
  291.  
  292.     /** Sets that the component has been iconized and the bounds of the
  293.       * desktopIcon are valid.
  294.       */
  295.     protected void setWasIcon(JInternalFrame f, Boolean value)  {
  296.     if (value != null) {
  297.         f.putClientProperty(HAS_BEEN_ICONIFIED_PROPERTY, value);
  298.     }
  299.     }
  300.  
  301.     protected boolean wasIcon(JInternalFrame f) {
  302.         return (f.getClientProperty(HAS_BEEN_ICONIFIED_PROPERTY) == Boolean.TRUE);
  303.     }
  304. }
  305.  
  306.