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

  1. /*
  2.  * @(#)MetalButtonUI.java    1.11 98/02/02
  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 com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.basic.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import com.sun.java.swing.plaf.*;
  29.  
  30. /**
  31.  * MetalButtonUI implementation
  32.  * <p>
  33.  * Warning: serialized objects of this class will not be compatible with
  34.  * future swing releases.  The current serialization support is appropriate
  35.  * for short term storage or RMI between Swing1.0 applications.  It will
  36.  * not be possible to load serialized Swing1.0 objects with future releases
  37.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  38.  * baseline for the serialized form of Swing objects.
  39.  *
  40.  * @version 1.11 02/02/98
  41.  * @author Tom Santos
  42.  */
  43. public class MetalButtonUI extends BasicButtonUI {
  44.  
  45.     protected Color getSelectColor()       { return UIManager.getColor("Button.pressed"); }
  46.     protected Color getDisabledTextColor() { return UIManager.getColor("Button.disabledText"); }
  47.     protected Color getFocusColor()        { return UIManager.getColor("Button.focus"); }
  48.  
  49.     private final static MetalButtonUI metalButtonUI = new MetalButtonUI(); 
  50.  
  51.  
  52.     public static ComponentUI createUI(JComponent c) {
  53.         return metalButtonUI;
  54.     }
  55.  
  56.     public void installUI(JComponent c) {
  57.         super.installUI(c);
  58.         c.setOpaque(true);
  59.     }
  60.  
  61.     protected void paintButtonPressed(Graphics g, AbstractButton b) {
  62.         if (b.isOpaque() ) {
  63.             Dimension size = b.getSize();
  64.         g.setColor( getSelectColor() );
  65.         g.fillRect( 0, 0, size.width, size.height );
  66.     }
  67.     }
  68.  
  69.     protected void paintFocus(Graphics g, Dimension size){
  70.         g.setColor(getFocusColor());
  71.     g.drawRect(2,2, size.width-5, size.height-5);    
  72.     }
  73.  
  74.  
  75.     protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  76.     AbstractButton b = (AbstractButton) c;                 
  77.     ButtonModel model = b.getModel();
  78.     FontMetrics fm = g.getFontMetrics();
  79.  
  80.     /* Draw the Text */
  81.     if(model.isEnabled()) {
  82.         /*** paint the text normally */
  83.         g.setColor(b.getForeground());
  84.         BasicGraphicsUtils.drawString(g,text, model.getMnemonic(),
  85.                       textRect.x,
  86.                       textRect.y + fm.getAscent());
  87.     }
  88.     else {
  89.         /*** paint the text disabled ***/
  90.         g.setColor(UIManager.getColor("Button.disabledText"));
  91.         BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  92.                       textRect.x, textRect.y + fm.getAscent());
  93.  
  94.     }
  95.     }
  96.  
  97.     protected BasicButtonListener createListener(JComponent c) {
  98.     return new MetalButtonListener((AbstractButton) c);
  99.     }
  100.  
  101. }
  102.  
  103. class MetalButtonListener extends BasicButtonListener
  104. {
  105.  
  106.     public MetalButtonListener(AbstractButton b) {
  107.       super(b);  
  108.     }
  109.  
  110.     public void focusGained(FocusEvent e) { 
  111.         Component c = (Component)e.getSource();
  112.     c.repaint();
  113.     }
  114. }
  115.    
  116.  
  117.