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

  1. /*
  2.  * @(#)OrganicToolBarUI.java    1.6 98/02/06
  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. package com.sun.java.swing.plaf.organic;
  22.  
  23. import com.sun.java.swing.*;
  24. import java.awt.Color;
  25. import java.awt.Component;
  26. import java.awt.Container;
  27. import java.awt.Dimension;
  28. import java.awt.Frame;
  29. import java.awt.Graphics;
  30. import java.awt.Insets;
  31. import java.awt.Point;
  32. import java.awt.Rectangle;
  33. import java.awt.event.*;
  34. import java.util.*;
  35. import java.io.Serializable;
  36.  
  37. import java.beans.PropertyChangeListener;
  38. import java.beans.PropertyChangeEvent;
  39. import java.beans.PropertyVetoException;
  40.  
  41. import com.sun.java.swing.border.*;
  42. import com.sun.java.swing.plaf.*;
  43. import com.sun.java.swing.plaf.basic.*;
  44.  
  45. /**
  46.  * An Organic Look and Feel implementation of ToolBarUI.  This implementation 
  47.  * is a "combined" view/controller.
  48.  * <p>
  49.  * Warning: serialized objects of this class will not be compatible with
  50.  * future swing releases.  The current serialization support is appropriate
  51.  * for short term storage or RMI between Swing1.0 applications.  It will
  52.  * not be possible to load serialized Swing1.0 objects with future releases
  53.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  54.  * baseline for the serialized form of Swing objects.
  55.  *
  56.  * @version 1.6 02/06/98
  57.  * @author Jeff Shapiro
  58.  */
  59. public class OrganicToolBarUI extends BasicToolBarUI implements ContainerListener
  60. {
  61.     protected static Border rolloverBorder = new CompoundBorder( new OrganicRolloverButtonBorder(),
  62.                                   new ToolBarButtonMargin() );
  63.     protected Hashtable borderTable = new Hashtable();
  64.  
  65.     protected ContainerListener contListener;
  66.  
  67.     private boolean rolloverBorders = false;
  68.  
  69.     private PropertyChangeListener rolloverListener;
  70.  
  71.     protected static String IS_ROLLOVER = "JToolBar.isRollover";
  72.  
  73.     public static ComponentUI createUI(JComponent c)
  74.     {
  75.     return new OrganicToolBarUI();
  76.     }
  77.  
  78.     public void installUI( JComponent c )
  79.     {
  80.         rolloverListener = new RolloverListener();
  81.     c.addPropertyChangeListener( rolloverListener );
  82.  
  83.         super.installUI( c );
  84.  
  85.     Object rolloverProp = c.getClientProperty( IS_ROLLOVER );
  86.     if ( rolloverProp != null )
  87.     {
  88.         setRolloverBorders( ((Boolean)rolloverProp).booleanValue() );
  89.     }
  90.     }
  91.  
  92.     public void uninstallUI( JComponent c )
  93.     {
  94.         c.removePropertyChangeListener( rolloverListener );
  95.  
  96.         super.uninstallUI( c );
  97.  
  98.     setRolloverBorders( false );
  99.    }
  100.  
  101.     protected void installListeners(JComponent c) {
  102.         super.installListeners(c);
  103.         if ((contListener = createContainerListener(c)) != null) {
  104.             c.addContainerListener(contListener);
  105.         }
  106.     }
  107.  
  108.     protected void uninstallListeners(JComponent c) {
  109.         super.uninstallListeners(c);
  110.         if (contListener != null) {
  111.             c.removeContainerListener(contListener);
  112.             contListener = null;
  113.         }
  114.     }
  115.  
  116.     protected ContainerListener createContainerListener(JComponent c) {
  117.         return this;
  118.     }
  119.  
  120.     public boolean isRolloverBorders()
  121.     {
  122.         return rolloverBorders;
  123.     }
  124.  
  125.     public void setRolloverBorders( boolean rollover )
  126.     {
  127.         if ( rolloverBorders != rollover )
  128.     {
  129.         rolloverBorders = rollover;
  130.         
  131.         if ( rolloverBorders )
  132.             installRolloverBorders( toolBar );
  133.         else
  134.             removeRolloverBorders( toolBar );
  135.     }
  136.     }
  137.  
  138.     protected void installRolloverBorders ( JComponent c )
  139.     {
  140.     // Put rollover borders on buttons
  141.     Component[] components = c.getComponents();
  142.     for ( int i = 0; i < components.length; ++i ) {
  143.       if ( components[ i ] instanceof JComponent ) {
  144.           ((JComponent)components[ i ]).updateUI();
  145.           setBorderToRollover( components[ i ] );
  146.       }
  147.     }
  148.     }
  149.  
  150.     protected void removeRolloverBorders ( JComponent c )
  151.     {
  152.     // Remove the rollover borders from buttons
  153.     Component[] components = c.getComponents();
  154.     for ( int i = 0; i < components.length; ++i ) {
  155.         setBorderToNormal( components[ i ] );
  156.     }
  157.     }
  158.  
  159.     protected void setBorderToRollover( Component c ) {
  160.         if ( c instanceof JButton ) {
  161.         JButton b = (JButton)c;
  162.         if ( b.getUI() instanceof OrganicButtonUI ) {
  163.             OrganicButtonUI buttonUI = (OrganicButtonUI)b.getUI();
  164.             if ( b.getBorder() instanceof UIResource ) {
  165.             borderTable.put( b, b.getBorder() );
  166.             b.setBorder( rolloverBorder );
  167.         }
  168.         }
  169.     }
  170.     }
  171.  
  172.     protected void setBorderToNormal( Component c ) {
  173.         if ( c instanceof JButton ) {
  174.         JButton b = (JButton)c;
  175.         if ( b.getUI() instanceof OrganicButtonUI ) {
  176.             OrganicButtonUI buttonUI = (OrganicButtonUI)b.getUI();
  177.             if ( b.getBorder() == rolloverBorder ) {
  178.             b.setBorder( (Border)borderTable.remove( b ) );
  179.         }
  180.         }
  181.     }
  182.     }
  183.  
  184.     // ContainerListener overrides
  185.  
  186.     public void componentAdded( ContainerEvent e )
  187.     {
  188.         if ( rolloverBorders )
  189.     {
  190.         Component c = e.getChild();
  191.         setBorderToRollover( c );
  192.     }
  193.     }
  194.  
  195.     public void componentRemoved( ContainerEvent e )
  196.     {
  197.         if ( rolloverBorders )
  198.     {
  199.         Component c = e.getChild();
  200.         setBorderToNormal( c );
  201.     }
  202.     }
  203.  
  204.   class RolloverListener implements PropertyChangeListener, Serializable {
  205.     public void propertyChange(PropertyChangeEvent e) {
  206.     String name = e.getPropertyName();
  207.     if ( name.equals( IS_ROLLOVER ) ) {
  208.         if ( e.getNewValue() != null ) {
  209.             setRolloverBorders( ((Boolean)e.getNewValue()).booleanValue() );
  210.         }
  211.         else {
  212.         setRolloverBorders( false );
  213.         }
  214.         }
  215.     }
  216.   } // end class RolloverListener
  217.  
  218. }
  219.  
  220.  
  221. class ToolBarButtonMargin extends AbstractBorder implements Serializable {
  222.     public Insets getBorderInsets(Component c) {
  223.         return new Insets(3, 3, 3, 3);        
  224.     }
  225. }
  226.  
  227.  
  228.