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

  1. /*
  2.  * @(#)MetalScrollButton.java    1.9 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.metal;
  22.  
  23. import java.awt.Dimension;
  24. import java.awt.Graphics;
  25. import java.awt.Color;
  26. import java.awt.Polygon;
  27.  
  28. import com.sun.java.swing.*;
  29.  
  30. import com.sun.java.swing.plaf.basic.BasicArrowButton;
  31.  
  32.  
  33. /**
  34.  * JButton object for Metal scrollbar arrows.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version 1.9 02/06/98
  44.  * @author Jeff Shapiro
  45.  */
  46. public class MetalScrollButton extends BasicArrowButton
  47. {
  48.   private static Color shadowColor;
  49.   private static Color highlightColor;
  50.   private boolean isFreeStanding = false;
  51.  
  52.   private int buttonWidth;
  53.  
  54.         public MetalScrollButton( int direction, int width, boolean freeStanding )
  55.         {
  56.             super( direction );
  57.  
  58.         shadowColor = UIManager.getColor("ScrollBar.shadow");
  59.         highlightColor = UIManager.getColor("ScrollBar.highlight");
  60.  
  61.             buttonWidth = width;
  62.         isFreeStanding = freeStanding;
  63.         }
  64.  
  65.         public void setFreeStanding( boolean freeStanding )
  66.         {
  67.         isFreeStanding = freeStanding;
  68.         }
  69.  
  70.     public void paint( Graphics g )
  71.         {
  72.         boolean isEnabled = getParent().isEnabled();
  73.  
  74.         Color arrowColor = isEnabled ? MetalLookAndFeel.getControlInfo() : MetalLookAndFeel.getControlDisabled();
  75.         boolean isPressed = getModel().isPressed();
  76.         int width = getSize().width;
  77.         int height = getSize().height;
  78.         int w = width;
  79.         int h = height;
  80.         int arrowHeight = height / 4;
  81.         int arrowWidth = height / 2;
  82.  
  83.         if ( isPressed )
  84.         {
  85.             g.setColor( MetalLookAndFeel.getControlShadow() );
  86.         }
  87.         else
  88.             {
  89.             g.setColor( getBackground() );
  90.         }
  91.  
  92.         g.fillRect( 0, 0, width, height );
  93.  
  94.         if ( getDirection() == NORTH )
  95.         {
  96.             if ( !isFreeStanding ) {
  97.             width += 2;
  98.         }
  99.  
  100.             // Draw the arrow
  101.             g.setColor( arrowColor );
  102.         g.drawLine( 7, 6, 8, 6 );
  103.         g.drawLine( 6, 7, 9, 7 );
  104.         g.drawLine( 5, 8, 10, 8 );
  105.         g.drawLine( 4, 9, 11, 9 );
  106.             
  107.         if (isEnabled) {
  108.             g.setColor( highlightColor );
  109.  
  110.             if ( !isPressed )
  111.             {
  112.             g.drawLine( 1, 1, width - 3, 1 );
  113.             g.drawLine( 1, 1, 1, height - 1 );
  114.             }
  115.  
  116.             g.drawLine( width - 1, 1, width - 1, height - 1 );
  117.  
  118.             g.setColor( shadowColor );
  119.             g.drawLine( 0, 0, width - 2, 0 );
  120.             g.drawLine( 0, 0, 0, height - 1 );
  121.             g.drawLine( width - 2, 2, width - 2, height - 1 );
  122.         } else {
  123.             MetalUtils.drawDisabledBorder(g, 0, 0, width, height+1);
  124.         }
  125.             if ( !isFreeStanding ) {
  126.             width -= 2;
  127.         }
  128.         }
  129.         else if ( getDirection() == SOUTH )
  130.         {
  131.             if ( !isFreeStanding ) {
  132.             width += 2;
  133.             height += 1;
  134.         }
  135.  
  136.             // Draw the arrow
  137.             g.setColor( arrowColor );
  138.         g.drawLine( 4, 5, 11, 5 );
  139.         g.drawLine( 5, 6, 10, 6 );
  140.         g.drawLine( 6, 7, 9, 7 );
  141.         g.drawLine( 7, 8, 8, 8 );
  142.  
  143.         if (isEnabled) {
  144.             g.setColor( highlightColor );
  145.  
  146.             if ( !isPressed )
  147.             {
  148.             g.drawLine( 1, 0, width - 3, 0 );
  149.             g.drawLine( 1, 0, 1, height - 3 );
  150.             }
  151.  
  152.             g.drawLine( 1, height - 1, width - 1, height - 1 );
  153.             g.drawLine( width - 1, 0, width - 1, height - 1 );
  154.  
  155.             g.setColor( shadowColor );
  156.             g.drawLine( 0, 0, 0, height - 2 );
  157.             g.drawLine( width - 2, 0, width - 2, height - 2 );
  158.             g.drawLine( 2, height - 2, width - 2, height - 2 );
  159.         } else {
  160.             MetalUtils.drawDisabledBorder(g, 0,-1, width, height+1);
  161.         }
  162.  
  163.             if ( !isFreeStanding ) {
  164.             width -= 2;
  165.             height -= 1;
  166.         }
  167.         }
  168.         else if ( getDirection() == EAST )
  169.         {
  170.             if ( !isFreeStanding ) {
  171.             height += 2;
  172.             width += 1;
  173.         }
  174.  
  175.             // Draw the arrow
  176.             g.setColor( arrowColor );
  177.         g.drawLine( 5, 4, 5, 11 );
  178.         g.drawLine( 6, 5, 6, 10 );
  179.         g.drawLine( 7, 6, 7, 9 );
  180.         g.drawLine( 8, 7, 8, 8 );
  181.  
  182.         if (isEnabled) {
  183.             g.setColor( highlightColor );
  184.  
  185.             if ( !isPressed )
  186.             {
  187.                 g.drawLine( 0, 1, width - 3, 1 );
  188.                 g.drawLine( 0, 1, 0, height - 3 );
  189.             }
  190.  
  191.             g.drawLine( width - 1, 1, width - 1, height - 1 );
  192.             g.drawLine( 0, height - 1, width - 1, height - 1 );
  193.  
  194.             g.setColor( shadowColor );
  195.             g.drawLine( 0, 0,width - 2, 0 );
  196.             g.drawLine( width - 2, 2, width - 2, height - 2 );
  197.             g.drawLine( 0, height - 2, width - 2, height - 2 );
  198.         } else {
  199.             MetalUtils.drawDisabledBorder(g,-1,0, width+1, height);
  200.         }
  201.             if ( !isFreeStanding ) {
  202.             height -= 2;
  203.             width -= 1;
  204.         }
  205.         }
  206.         else if ( getDirection() == WEST )
  207.         {
  208.             if ( !isFreeStanding ) {
  209.             height += 2;
  210.             width += 1;
  211.             g.translate( -1, 0 );
  212.         }
  213.  
  214.             // Draw the arrow
  215.             g.setColor( arrowColor );
  216.         g.drawLine( 6, 7, 6, 8 );
  217.         g.drawLine( 7, 6, 7, 9 );
  218.         g.drawLine( 8, 5, 8, 10 );
  219.         g.drawLine( 9, 4, 9, 11 );
  220.  
  221.         if (isEnabled) {
  222.             g.setColor( highlightColor );
  223.  
  224.  
  225.             if ( !isPressed )
  226.             {
  227.                 g.drawLine( 1, 1, width - 1, 1 );
  228.             g.drawLine( 1, 1, 1, height - 3 );
  229.             }
  230.  
  231.             g.drawLine( 1, height - 1, width - 1, height - 1 );
  232.         
  233.             g.setColor( shadowColor );
  234.             g.drawLine( 0, 0, width - 1, 0 );
  235.             g.drawLine( 0, 0, 0, height - 2 );
  236.             g.drawLine( 2, height - 2, width - 1, height - 2 );
  237.         } else {
  238.             MetalUtils.drawDisabledBorder(g,0,0, width+1, height);
  239.         }
  240.  
  241.             if ( !isFreeStanding ) {
  242.             height -= 2;
  243.             width -= 1;
  244.             g.translate( 1, 0 );
  245.         }
  246.         }
  247.         }
  248.  
  249.         public Dimension getPreferredSize()
  250.         {
  251.         if ( getDirection() == NORTH )
  252.         {
  253.             return new Dimension( buttonWidth, buttonWidth - 2 );
  254.         }
  255.         else if ( getDirection() == SOUTH )
  256.             {
  257.             return new Dimension( buttonWidth, buttonWidth - (isFreeStanding ? 1 : 2) );
  258.         }
  259.         else if ( getDirection() == EAST )
  260.             {
  261.             return new Dimension( buttonWidth - (isFreeStanding ? 1 : 2), buttonWidth );
  262.         }
  263.         else if ( getDirection() == WEST )
  264.             {
  265.             return new Dimension( buttonWidth - 2, buttonWidth );
  266.         }
  267.         else
  268.             {
  269.             return new Dimension( 0, 0 );
  270.         }
  271.         }
  272.  
  273.         public Dimension getMinimumSize()
  274.         {
  275.             return getPreferredSize();
  276.         }
  277.  
  278.         public Dimension getMaximumSize()
  279.         {
  280.             return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );
  281.         }
  282.     
  283.         public int getButtonWidth() {
  284.         return buttonWidth;
  285.     }
  286. }
  287.  
  288.