1. /*
  2. * @(#)MetalButtonUI.java 1.21 01/11/29
  3. *
  4. * Copyright 2002 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 appropriate
  20. * for short term storage or RMI between applications running the same
  21. * version of Swing. A future release of Swing will provide support for
  22. * long term persistence.
  23. *
  24. * @version 1.21 11/29/01
  25. * @author Tom Santos
  26. */
  27. public class MetalButtonUI extends BasicButtonUI {
  28. private final static MetalButtonUI metalButtonUI = new MetalButtonUI();
  29. private boolean defaults_initialized = false;
  30. protected Color focusColor;
  31. protected Color selectColor;
  32. protected Color disabledTextColor;
  33. // ********************************
  34. // Create PLAF
  35. // ********************************
  36. public static ComponentUI createUI(JComponent c) {
  37. return metalButtonUI;
  38. }
  39. // ********************************
  40. // Install
  41. // ********************************
  42. public void installDefaults(AbstractButton b) {
  43. super.installDefaults(b);
  44. if(!defaults_initialized) {
  45. focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  46. selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  47. disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
  48. defaults_initialized = true;
  49. }
  50. }
  51. public void uninstallDefaults(AbstractButton b) {
  52. super.uninstallDefaults(b);
  53. defaults_initialized = false;
  54. }
  55. // ********************************
  56. // Create Listeners
  57. // ********************************
  58. protected BasicButtonListener createButtonListener(AbstractButton b) {
  59. return new MetalButtonListener(b);
  60. }
  61. // ********************************
  62. // Default Accessors
  63. // ********************************
  64. protected Color getSelectColor() {
  65. return selectColor;
  66. }
  67. protected Color getDisabledTextColor() {
  68. return disabledTextColor;
  69. }
  70. protected Color getFocusColor() {
  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. /* Draw the Text */
  110. if(model.isEnabled()) {
  111. /*** paint the text normally */
  112. g.setColor(b.getForeground());
  113. BasicGraphicsUtils.drawString(g,text, model.getMnemonic(),
  114. textRect.x,
  115. textRect.y + fm.getAscent());
  116. }
  117. else {
  118. /*** paint the text disabled ***/
  119. g.setColor(getDisabledTextColor());
  120. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  121. textRect.x, textRect.y + fm.getAscent());
  122. }
  123. }
  124. }
  125. class MetalButtonListener extends BasicButtonListener
  126. {
  127. public MetalButtonListener(AbstractButton b) {
  128. super(b);
  129. }
  130. public void focusGained(FocusEvent e) {
  131. Component c = (Component)e.getSource();
  132. c.repaint();
  133. }
  134. }