1. /*
  2. * @(#)MetalButtonUI.java 1.29 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.metal;
  8. import javax.swing.*;
  9. import javax.swing.border.*;
  10. import javax.swing.plaf.basic.*;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.plaf.*;
  14. /**
  15. * MetalButtonUI implementation
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is
  20. * appropriate for short term storage or RMI between applications running
  21. * the same version of Swing. As of 1.4, support for long term storage
  22. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  23. * has been added to the <code>java.beans</code> package.
  24. * Please see {@link java.beans.XMLEncoder}.
  25. *
  26. * @version 1.29 01/23/03
  27. * @author Tom Santos
  28. */
  29. public class MetalButtonUI extends BasicButtonUI {
  30. private final static MetalButtonUI metalButtonUI = new MetalButtonUI();
  31. // NOTE: These are not really needed, but at this point we can't pull
  32. // them. Their values are updated purely for historical reasons.
  33. protected Color focusColor;
  34. protected Color selectColor;
  35. protected Color disabledTextColor;
  36. // ********************************
  37. // Create PLAF
  38. // ********************************
  39. public static ComponentUI createUI(JComponent c) {
  40. return metalButtonUI;
  41. }
  42. // ********************************
  43. // Install
  44. // ********************************
  45. public void installDefaults(AbstractButton b) {
  46. super.installDefaults(b);
  47. }
  48. public void uninstallDefaults(AbstractButton b) {
  49. super.uninstallDefaults(b);
  50. }
  51. // ********************************
  52. // Create Listeners
  53. // ********************************
  54. protected BasicButtonListener createButtonListener(AbstractButton b) {
  55. return new MetalButtonListener(b);
  56. }
  57. // ********************************
  58. // Default Accessors
  59. // ********************************
  60. protected Color getSelectColor() {
  61. selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  62. return selectColor;
  63. }
  64. protected Color getDisabledTextColor() {
  65. disabledTextColor = UIManager.getColor(getPropertyPrefix() +
  66. "disabledText");
  67. return disabledTextColor;
  68. }
  69. protected Color getFocusColor() {
  70. focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  71. return focusColor;
  72. }
  73. // ********************************
  74. // Paint
  75. // ********************************
  76. protected void paintButtonPressed(Graphics g, AbstractButton b) {
  77. if ( b.isContentAreaFilled() ) {
  78. Dimension size = b.getSize();
  79. g.setColor(getSelectColor());
  80. g.fillRect(0, 0, size.width, size.height);
  81. }
  82. }
  83. protected void paintFocus(Graphics g, AbstractButton b,
  84. Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  85. Rectangle focusRect = new Rectangle();
  86. String text = b.getText();
  87. boolean isIcon = b.getIcon() != null;
  88. // If there is text
  89. if ( text != null && !text.equals( "" ) ) {
  90. if ( !isIcon ) {
  91. focusRect.setBounds( textRect );
  92. }
  93. else {
  94. focusRect.setBounds( iconRect.union( textRect ) );
  95. }
  96. }
  97. // If there is an icon and no text
  98. else if ( isIcon ) {
  99. focusRect.setBounds( iconRect );
  100. }
  101. g.setColor(getFocusColor());
  102. g.drawRect((focusRect.x-1), (focusRect.y-1),
  103. focusRect.width+1, focusRect.height+1);
  104. }
  105. protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  106. AbstractButton b = (AbstractButton) c;
  107. ButtonModel model = b.getModel();
  108. FontMetrics fm = g.getFontMetrics();
  109. int mnemIndex = b.getDisplayedMnemonicIndex();
  110. /* Draw the Text */
  111. if(model.isEnabled()) {
  112. /*** paint the text normally */
  113. g.setColor(b.getForeground());
  114. BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemIndex,
  115. textRect.x,
  116. textRect.y + fm.getAscent());
  117. }
  118. else {
  119. /*** paint the text disabled ***/
  120. g.setColor(getDisabledTextColor());
  121. BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemIndex,
  122. textRect.x, textRect.y + fm.getAscent());
  123. }
  124. }
  125. }
  126. class MetalButtonListener extends BasicButtonListener
  127. {
  128. public MetalButtonListener(AbstractButton b) {
  129. super(b);
  130. }
  131. public void focusGained(FocusEvent e) {
  132. Component c = (Component)e.getSource();
  133. c.repaint();
  134. }
  135. public void focusLost(FocusEvent e) {
  136. AbstractButton b = (AbstractButton)e.getSource();
  137. b.getModel().setArmed(false);
  138. b.repaint();
  139. }
  140. }