1. /*
  2. * @(#)MetalButtonUI.java 1.37 04/04/02
  3. *
  4. * Copyright 2004 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 com.sun.java.swing.SwingUtilities2;
  9. import javax.swing.*;
  10. import javax.swing.border.*;
  11. import javax.swing.plaf.basic.*;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.beans.*;
  15. import javax.swing.plaf.*;
  16. /**
  17. * MetalButtonUI implementation
  18. * <p>
  19. * <strong>Warning:</strong>
  20. * Serialized objects of this class will not be compatible with
  21. * future Swing releases. The current serialization support is
  22. * appropriate for short term storage or RMI between applications running
  23. * the same version of Swing. As of 1.4, support for long term storage
  24. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  25. * has been added to the <code>java.beans</code> package.
  26. * Please see {@link java.beans.XMLEncoder}.
  27. *
  28. * @version 1.37 04/02/04
  29. * @author Tom Santos
  30. */
  31. public class MetalButtonUI extends BasicButtonUI {
  32. private final static MetalButtonUI metalButtonUI = new MetalButtonUI();
  33. // NOTE: These are not really needed, but at this point we can't pull
  34. // them. Their values are updated purely for historical reasons.
  35. protected Color focusColor;
  36. protected Color selectColor;
  37. protected Color disabledTextColor;
  38. // ********************************
  39. // Create PLAF
  40. // ********************************
  41. public static ComponentUI createUI(JComponent c) {
  42. return metalButtonUI;
  43. }
  44. // ********************************
  45. // Install
  46. // ********************************
  47. public void installDefaults(AbstractButton b) {
  48. super.installDefaults(b);
  49. }
  50. public void uninstallDefaults(AbstractButton b) {
  51. super.uninstallDefaults(b);
  52. }
  53. // ********************************
  54. // Create Listeners
  55. // ********************************
  56. protected BasicButtonListener createButtonListener(AbstractButton b) {
  57. return super.createButtonListener(b);
  58. }
  59. // ********************************
  60. // Default Accessors
  61. // ********************************
  62. protected Color getSelectColor() {
  63. selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  64. return selectColor;
  65. }
  66. protected Color getDisabledTextColor() {
  67. disabledTextColor = UIManager.getColor(getPropertyPrefix() +
  68. "disabledText");
  69. return disabledTextColor;
  70. }
  71. protected Color getFocusColor() {
  72. focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  73. return focusColor;
  74. }
  75. // ********************************
  76. // Paint
  77. // ********************************
  78. /**
  79. * If necessary paints the background of the component, then
  80. * invokes <code>paint</code>.
  81. *
  82. * @param g Graphics to paint to
  83. * @param c JComponent painting on
  84. * @throws NullPointerException if <code>g</code> or <code>c</code> is
  85. * null
  86. * @see javax.swing.plaf.ComponentUI#update
  87. * @see javax.swing.plaf.ComponentUI#paint
  88. * @since 1.5
  89. */
  90. public void update(Graphics g, JComponent c) {
  91. AbstractButton button = (AbstractButton)c;
  92. if ((c.getBackground() instanceof UIResource) &&
  93. button.isContentAreaFilled() && c.isEnabled()) {
  94. ButtonModel model = button.getModel();
  95. if (!MetalUtils.isToolBarButton(c)) {
  96. if (!model.isArmed() && !model.isPressed() &&
  97. MetalUtils.drawGradient(
  98. c, g, "Button.gradient", 0, 0, c.getWidth(),
  99. c.getHeight(), true)) {
  100. paint(g, c);
  101. return;
  102. }
  103. }
  104. else if (model.isRollover() && MetalUtils.drawGradient(
  105. c, g, "Button.gradient", 0, 0, c.getWidth(),
  106. c.getHeight(), true)) {
  107. paint(g, c);
  108. return;
  109. }
  110. }
  111. super.update(g, c);
  112. }
  113. protected void paintButtonPressed(Graphics g, AbstractButton b) {
  114. if ( b.isContentAreaFilled() ) {
  115. Dimension size = b.getSize();
  116. g.setColor(getSelectColor());
  117. g.fillRect(0, 0, size.width, size.height);
  118. }
  119. }
  120. protected void paintFocus(Graphics g, AbstractButton b,
  121. Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  122. Rectangle focusRect = new Rectangle();
  123. String text = b.getText();
  124. boolean isIcon = b.getIcon() != null;
  125. // If there is text
  126. if ( text != null && !text.equals( "" ) ) {
  127. if ( !isIcon ) {
  128. focusRect.setBounds( textRect );
  129. }
  130. else {
  131. focusRect.setBounds( iconRect.union( textRect ) );
  132. }
  133. }
  134. // If there is an icon and no text
  135. else if ( isIcon ) {
  136. focusRect.setBounds( iconRect );
  137. }
  138. g.setColor(getFocusColor());
  139. g.drawRect((focusRect.x-1), (focusRect.y-1),
  140. focusRect.width+1, focusRect.height+1);
  141. }
  142. protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  143. AbstractButton b = (AbstractButton) c;
  144. ButtonModel model = b.getModel();
  145. FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
  146. int mnemIndex = b.getDisplayedMnemonicIndex();
  147. /* Draw the Text */
  148. if(model.isEnabled()) {
  149. /*** paint the text normally */
  150. g.setColor(b.getForeground());
  151. }
  152. else {
  153. /*** paint the text disabled ***/
  154. g.setColor(getDisabledTextColor());
  155. }
  156. SwingUtilities2.drawStringUnderlineCharAt(c, g,text,mnemIndex,
  157. textRect.x, textRect.y + fm.getAscent());
  158. }
  159. }