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

  1. /*
  2.  * @(#)MacInternalFrameUI.java    1.8 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.mac;
  23.  
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import com.sun.java.swing.*;
  27. import java.beans.*;
  28. import java.util.EventListener;
  29.  
  30. import com.sun.java.swing.plaf.basic.*;
  31. import com.sun.java.swing.plaf.*;
  32.  
  33. import java.io.Serializable;
  34.  
  35. /**
  36.  * A Mac L&F implementation of InternalFrame.  
  37.  * <p>
  38.  * Warning: serialized objects of this class will not be compatible with
  39.  * future swing releases.  The current serialization support is appropriate
  40.  * for short term storage or RMI between Swing1.0 applications.  It will
  41.  * not be possible to load serialized Swing1.0 objects with future releases
  42.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  43.  * baseline for the serialized form of Swing objects.
  44.  *
  45.  * @version @(#)MacInternalFrameUI.java    1.0 11/24/97
  46.  * @author Symantec
  47.  * @author David Bustin
  48.  */
  49.  
  50. public class MacInternalFrameUI extends BasicInternalFrameUI
  51. {
  52.     Color color;
  53.     Color highlight;
  54.     Color shadow;
  55.     MacInternalFrameTitlePane titlePane;
  56.     JInternalFrame frame;
  57.         
  58.     // Window Shade support
  59.     protected boolean windowShade; // true if window shade is supported    
  60.     protected Dimension saveSize;  // used to save the dimension before collapsing frame
  61.     protected boolean resizable;   // is the frame resizable?
  62.     
  63. /////////////////////////////////////////////////////////////////////////////
  64. // ComponentUI Interface Implementation methods
  65. /////////////////////////////////////////////////////////////////////////////
  66.     public static ComponentUI createUI(JComponent w)    {
  67.         return new MacInternalFrameUI((JInternalFrame)w);
  68.     }
  69.  
  70.     public MacInternalFrameUI(JInternalFrame w)   {
  71.         super(w);
  72.         frame = w;
  73.     }
  74.             
  75.     public void installUI(JComponent c)   {
  76.         if(((JInternalFrame)c).getBorder() == null)
  77.             ((JInternalFrame)c).setBorder(
  78.             new MacInternalFrameBorder((JInternalFrame)c));
  79.         super.installUI(c);
  80.         setColors();
  81.         setWindowShade(true);
  82.         frame.setMinimumSize(new Dimension(100, 26));
  83.     }   
  84.  
  85.     public void uninstallUI(JComponent c) {
  86.         if(frame.getBorder() instanceof MacInternalFrameBorder)
  87.             frame.setBorder(null);                
  88.         super.uninstallUI(c);
  89.     }
  90.         
  91.     public JComponent createNorthPane(JInternalFrame w) {
  92.         titlePane = new MacInternalFrameTitlePane(w);
  93.         return titlePane;
  94.     }
  95.  
  96.     public Dimension getMaximumSize(JComponent x) {
  97.         return Toolkit.getDefaultToolkit().getScreenSize();
  98.     }
  99.     
  100.     /** This method is called when the frame becomes selected.
  101.       */
  102.     protected void activateFrame(JInternalFrame f) {
  103.         super.activateFrame(f);
  104.         setColors();
  105.     }
  106.     /** This method is called when the frame is no longer selected.
  107.       */
  108.     protected void deactivateFrame(JInternalFrame f) {
  109.         setColors();
  110.         super.deactivateFrame(f);
  111.     }
  112.  
  113.     void setColors() {
  114.         if (frame.isSelected()) {
  115.             color = UIManager.getColor("activeCaptionBorder");
  116.         } else {
  117.             color = UIManager.getColor("inactiveCaptionBorder");
  118.         }
  119.         highlight = color.brighter();
  120.         shadow = color.darker().darker();
  121.         titlePane.setColors(color, highlight, shadow);
  122.     }
  123.  
  124.     /** Window shade is on by default, call this method
  125.         with false to make the frame become an icon instead of 
  126.         collapsing it (Window Shade) upon iconify
  127.       */
  128.  
  129.     public void setWindowShade(boolean b) {
  130.         windowShade = b;
  131.     }
  132.  
  133.     /** This method is called when the user wants to iconify the frame.
  134.       * This action is delegated to the desktopManager unless windowShade
  135.       * is on.
  136.       */
  137.     protected void iconifyFrame(JInternalFrame f) {
  138.     if (windowShade)
  139.         {
  140.         saveSize = frame.getSize();
  141.         f.setSize(saveSize.width, 26);
  142.             resizable = f.isResizable();
  143.             f.setResizable(false);    // Disable resizing while iconfied
  144.         }
  145.     else
  146.         getDesktopManager().iconifyFrame(f);
  147.     }
  148.     /** This method is called when the user wants to deiconify the frame.
  149.       * This action is delegated to the desktopManager unless windowShade
  150.       * is on.
  151.       */
  152.     protected void deiconifyFrame(JInternalFrame f) {
  153.     if (windowShade) {
  154.             if (saveSize != null) {
  155.                 f.setSize(saveSize);
  156.             } else {
  157.                 f.setSize(f.getPreferredSize());
  158.             }
  159.             f.setResizable(resizable);
  160.         }
  161.     else
  162.             getDesktopManager().deiconifyFrame(f);
  163.     }
  164.  
  165.     protected EventListener createBorderListener(JInternalFrame w) {
  166.         return new MacBorderListener();
  167.     }
  168.  
  169.    protected DesktopManager getDesktopManager() {
  170.         return super.getDesktopManager();
  171.     }
  172.     
  173.     /////////////////////////////////////////////////////////////////////////
  174.     /// MacBorderListener Class
  175.     /////////////////////////////////////////////////////////////////////////        
  176.     protected class MacBorderListener extends MouseAdapter 
  177.         implements MouseMotionListener, SwingConstants, Serializable
  178.     {
  179.         
  180.     // _x & _y are the mousePressed location in absolute coordinate system
  181.         int _x, _y;
  182.     // __x & __y are the mousePressed location in source view's coordinate system
  183.         int __x, __y;
  184.         Rectangle startingBounds;
  185.         int resizeDir;
  186.         
  187.         protected final int RESIZE_NONE  = 0;
  188.                 
  189.         int resizeCornerSize = 16;
  190.  
  191.         public void mouseReleased(MouseEvent e) {
  192.             if (resizeDir == RESIZE_NONE)
  193.                 getDesktopManager().endDraggingFrame(frame);    
  194.             else
  195.                 getDesktopManager().endResizingFrame(frame);    
  196.                 _x = 0;
  197.                 _y = 0;
  198.                 __x = 0;
  199.                 __y = 0;
  200.                 startingBounds = null;
  201.                 resizeDir = RESIZE_NONE;
  202.             }
  203.                 
  204.         public void mousePressed(MouseEvent e) {
  205.             Point p = SwingUtilities.convertPoint((Component)e.getSource(), 
  206.                         e.getX(), e.getY(), null);
  207.             __x = e.getX();
  208.             __y = e.getY();
  209.             _x = p.x;
  210.             _y = p.y;
  211.             startingBounds = frame.getBounds();
  212.  
  213.             if (!frame.isSelected()) {
  214.                 try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
  215.             }
  216.  
  217.             if (e.getClickCount() > 1 && e.getSource() == getNorthPane()) {
  218.             if (frame.isIconifiable()) {
  219.                     if (frame.isIcon()) {
  220.                         try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
  221.                     }
  222.                     else {
  223.                         try { frame.setIcon(true); } catch (PropertyVetoException e2) { }
  224.                     }
  225.             } else if(frame.isMaximizable()) {
  226.                     if(!frame.isMaximum())
  227.                         try { frame.setMaximum(true); } catch (PropertyVetoException e2) { }
  228.                     else
  229.                         try { frame.setMaximum(false); } catch (PropertyVetoException e3) { }
  230.                 } 
  231.             }
  232.  
  233.             if (!frame.isResizable() || e.getSource() == getNorthPane()) {
  234.                 resizeDir = RESIZE_NONE;
  235.                 getDesktopManager().beginDraggingFrame(frame);
  236.                 return;
  237.             }       
  238.  
  239.             if (e.getSource() == frame) {
  240.                 Insets i = frame.getInsets();
  241.                 if(e.getX() <= i.left) {
  242.                     if(e.getY() < resizeCornerSize + i.top)
  243.                         resizeDir = NORTH_WEST;
  244.                     else if(e.getY() > frame.getHeight() 
  245.                     - resizeCornerSize - i.bottom)
  246.                         resizeDir = SOUTH_WEST;
  247.                     else                
  248.                         resizeDir = WEST;
  249.                 } else if(e.getX() >= frame.getWidth() - i.right) {
  250.                     if(e.getY() < resizeCornerSize + i.top)
  251.                         resizeDir = NORTH_EAST;
  252.                     else if(e.getY() > frame.getHeight() 
  253.                     - resizeCornerSize - i.bottom)
  254.                         resizeDir = SOUTH_EAST;
  255.                     else                
  256.                         resizeDir = EAST;
  257.                 } else if(e.getY() <= i.top) {
  258.                     if(e.getX() < resizeCornerSize + i.left)
  259.                         resizeDir = NORTH_WEST;
  260.                     else if(e.getX() > frame.getWidth() 
  261.                     - resizeCornerSize - i.right)
  262.                         resizeDir = NORTH_EAST;
  263.                     else                
  264.                         resizeDir = NORTH;
  265.                 } else if(e.getY() >= frame.getHeight() - i.bottom) {
  266.                     if(e.getX() < resizeCornerSize + i.left)
  267.                         resizeDir = SOUTH_WEST;
  268.                     else if(e.getX() > frame.getWidth() 
  269.                         - resizeCornerSize - i.right)
  270.                         resizeDir = SOUTH_EAST;
  271.                     else                
  272.                         resizeDir = SOUTH;
  273.                 } 
  274.                 getDesktopManager().beginResizingFrame(frame, resizeDir);
  275.                 return;
  276.             }
  277.         }
  278.  
  279.         public void mouseDragged(MouseEvent e) {                                        
  280.             Point p; 
  281.             int newX, newY, newW, newH;
  282.             int deltaX;
  283.             int deltaY;
  284.             Dimension min;
  285.             Dimension max;
  286.             p = SwingUtilities.convertPoint((Component)e.getSource(), 
  287.                                         e.getX(), e.getY(), null);
  288.         
  289.             // Handle a MOVE 
  290.             if(e.getSource() == getNorthPane()) {
  291.                 if (frame.isMaximum()) {
  292.                     return;  // don't allow moving of maximized frames.
  293.                 }
  294.                 Insets i = frame.getInsets();
  295.                 int pWidth, pHeight;
  296.                 Dimension s = frame.getParent().getSize();
  297.                 pWidth = s.width;
  298.                 pHeight = s.height;
  299.  
  300.                 newX = startingBounds.x - (_x - p.x);
  301.                 newY = startingBounds.y - (_y - p.y);
  302.  
  303.                 // Make sure we stay in-bounds
  304.                 if(newX + i.left <= -__x)
  305.                     newX = -__x - i.left;
  306.                 if(newY + i.top <= -__y)
  307.                     newY = -__y - i.top;
  308.                 if(newX + __x + i.right > pWidth)
  309.                     newX = pWidth - __x - i.right;
  310.                 if(newY + __y + i.bottom > pHeight)
  311.                     newY =  pHeight - __y - i.bottom;
  312.         
  313.                 getDesktopManager().dragFrame(frame, newX, newY);
  314.                 return;
  315.             }
  316.  
  317.             if(!frame.isResizable()) {
  318.                 return;
  319.             }
  320.  
  321.             min = frame.getMinimumSize();
  322.             max = frame.getMaximumSize();
  323.         
  324.             deltaX = _x - p.x;
  325.             deltaY = _y - p.y;
  326.  
  327.             newX = frame.getX();
  328.             newY = frame.getY();
  329.             newW = frame.getWidth();
  330.             newH = frame.getHeight();
  331.  
  332.             switch(resizeDir) {
  333.                 case RESIZE_NONE:
  334.                     return;
  335.                 case NORTH: 
  336.                     if(startingBounds.height + deltaY < min.height)
  337.                         deltaY = -(startingBounds.height - min.height);
  338.                     else if(startingBounds.height + deltaY > max.height)
  339.                         deltaY = (startingBounds.height - min.height);
  340.             
  341.                     newX = startingBounds.x;
  342.                     newY = startingBounds.y - deltaY;
  343.                     newW = startingBounds.width;
  344.                     newH = startingBounds.height + deltaY;
  345.                     break;
  346.                 case NORTH_EAST:     
  347.                     if(startingBounds.height + deltaY < min.height)
  348.                         deltaY = -(startingBounds.height - min.height);
  349.                     else if(startingBounds.height + deltaY > max.height)
  350.                         deltaY = (startingBounds.height - min.height);
  351.             
  352.                     if(startingBounds.width - deltaX < min.width)
  353.                         deltaX = (startingBounds.width - min.width);
  354.                     else if(startingBounds.width - deltaX > max.width)
  355.                         deltaX = -(startingBounds.width - min.width);
  356.             
  357.                     newX = startingBounds.x;
  358.                     newY = startingBounds.y - deltaY;
  359.                     newW = startingBounds.width - deltaX;
  360.                     newH = startingBounds.height + deltaY;
  361.                     break;
  362.                 case EAST:      
  363.                     if(startingBounds.width - deltaX < min.width)
  364.                         deltaX = (startingBounds.width - min.width);
  365.                     else if(startingBounds.width - deltaX > max.width)
  366.                         deltaX = -(startingBounds.width - min.width);
  367.             
  368.                     newW = startingBounds.width - deltaX;
  369.                     newH = startingBounds.height;
  370.                     break;
  371.                 case SOUTH_EAST:     
  372.                     if(startingBounds.width - deltaX < min.width)
  373.                         deltaX = (startingBounds.width - min.width);
  374.                     else if(startingBounds.width - deltaX > max.width)
  375.                         deltaX = -(startingBounds.width - min.width);
  376.             
  377.                     if(startingBounds.height - deltaY < min.height)
  378.                         deltaY = (startingBounds.height - min.height);
  379.                     else if(startingBounds.height - deltaY > max.height)
  380.                         deltaY = -(startingBounds.height - min.height);
  381.                 
  382.                     newW = startingBounds.width - deltaX;
  383.                     newH = startingBounds.height - deltaY;
  384.                     break;
  385.                 case SOUTH:      
  386.                     if(startingBounds.height - deltaY < min.height)
  387.                         deltaY = (startingBounds.height - min.height);
  388.                     else if(startingBounds.height - deltaY > max.height)
  389.                         deltaY = -(startingBounds.height - min.height);
  390.             
  391.                      newW = startingBounds.width;
  392.                     newH = startingBounds.height - deltaY;
  393.                     break;
  394.                 case SOUTH_WEST:
  395.                     if(startingBounds.height - deltaY < min.height)
  396.                         deltaY = (startingBounds.height - min.height);
  397.                     else if(startingBounds.height - deltaY > max.height)
  398.                         deltaY = -(startingBounds.height - min.height);
  399.             
  400.                     if(startingBounds.width + deltaX < min.width)
  401.                         deltaX = -(startingBounds.width - min.width);
  402.                     else if(startingBounds.width + deltaX > max.width)
  403.                         deltaX = (startingBounds.width - min.width);
  404.             
  405.                     newX = startingBounds.x - deltaX;
  406.                     newY = startingBounds.y;
  407.                     newW = startingBounds.width + deltaX;
  408.                     newH = startingBounds.height - deltaY;
  409.                     break;
  410.                 case WEST:      
  411.                     if(startingBounds.width + deltaX < min.width)
  412.                         deltaX = -(startingBounds.width - min.width);
  413.                     else if(startingBounds.width + deltaX > max.width)
  414.                         deltaX = (startingBounds.width - min.width);
  415.             
  416.                     newX = startingBounds.x - deltaX;
  417.                     newY = startingBounds.y;
  418.                     newW = startingBounds.width + deltaX;
  419.                     newH = startingBounds.height;
  420.                     break;
  421.                 case NORTH_WEST:     
  422.                     if(startingBounds.width + deltaX < min.width)
  423.                         deltaX = -(startingBounds.width - min.width);
  424.                     else if(startingBounds.width + deltaX > max.width)
  425.                         deltaX = (startingBounds.width - min.width);
  426.             
  427.                     if(startingBounds.height + deltaY < min.height)
  428.                         deltaY = -(startingBounds.height - min.height);
  429.                     else if(startingBounds.height + deltaY > max.height)
  430.                         deltaY = (startingBounds.height - min.height);
  431.             
  432.                     newX = startingBounds.x - deltaX;
  433.                     newY = startingBounds.y - deltaY;
  434.                     newW = startingBounds.width + deltaX;
  435.                     newH = startingBounds.height + deltaY;
  436.                     break;
  437.                 default:
  438.                     return;
  439.             }
  440.  
  441.             getDesktopManager().resizeFrame(frame, newX, newY, newW, newH);
  442.         }
  443.  
  444.         public void mouseMoved(MouseEvent e) {
  445.  
  446.             if(!frame.isResizable())
  447.             return;
  448.         
  449.             if(e.getSource() == frame) {
  450.                 Insets i = frame.getInsets();
  451.                 if(e.getX() <= i.left) {
  452.                     if(e.getY() < resizeCornerSize + i.top)
  453.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
  454.                     else if(e.getY() > frame.getHeight() - resizeCornerSize - i.bottom)
  455.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
  456.                     else                
  457.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
  458.                 } else if(e.getX() >= frame.getWidth() - i.right) {
  459.                     if(e.getY() < resizeCornerSize + i.top)
  460.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
  461.                     else if(e.getY() > frame.getHeight() - resizeCornerSize - i.bottom)
  462.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
  463.                     else                
  464.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
  465.                 } else if(e.getY() <= i.top) {
  466.                     if(e.getX() < resizeCornerSize + i.left)
  467.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
  468.                     else if(e.getX() > frame.getWidth() - resizeCornerSize - i.right)
  469.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
  470.                     else                
  471.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
  472.                 } else if(e.getY() >= frame.getHeight() - i.bottom) {
  473.                     if(e.getX() < resizeCornerSize + i.left)
  474.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
  475.                     else if(e.getX() > frame.getWidth() - resizeCornerSize - i.right)
  476.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
  477.                     else                
  478.                         frame.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
  479.                 }
  480.             return;
  481.             }
  482.  
  483.             frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));    
  484.         }         
  485.  
  486.         public void mouseExited(MouseEvent e) {
  487.             mouseReleased(e);
  488.             frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));    
  489.         }
  490.     };    /// End BorderListener Class
  491.  
  492. }
  493.