1. /*
  2. * @(#)MetalButtonUI.java 1.22 00/02/02
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.plaf.metal;
  11. import javax.swing.*;
  12. import javax.swing.border.*;
  13. import javax.swing.plaf.basic.*;
  14. import java.awt.*;
  15. import java.awt.event.*;
  16. import javax.swing.plaf.*;
  17. /**
  18. * MetalButtonUI implementation
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. *
  27. * @version 1.22 02/02/00
  28. * @author Tom Santos
  29. */
  30. public class MetalButtonUI extends BasicButtonUI {
  31. private final static MetalButtonUI metalButtonUI = new MetalButtonUI();
  32. private boolean defaults_initialized = false;
  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. if(!defaults_initialized) {
  48. focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  49. selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  50. disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
  51. defaults_initialized = true;
  52. }
  53. }
  54. public void uninstallDefaults(AbstractButton b) {
  55. super.uninstallDefaults(b);
  56. defaults_initialized = false;
  57. }
  58. // ********************************
  59. // Create Listeners
  60. // ********************************
  61. protected BasicButtonListener createButtonListener(AbstractButton b) {
  62. return new MetalButtonListener(b);
  63. }
  64. // ********************************
  65. // Default Accessors
  66. // ********************************
  67. protected Color getSelectColor() {
  68. return selectColor;
  69. }
  70. protected Color getDisabledTextColor() {
  71. return disabledTextColor;
  72. }
  73. protected Color getFocusColor() {
  74. return focusColor;
  75. }
  76. // ********************************
  77. // Paint
  78. // ********************************
  79. protected void paintButtonPressed(Graphics g, AbstractButton b) {
  80. if ( b.isContentAreaFilled() ) {
  81. Dimension size = b.getSize();
  82. g.setColor(getSelectColor());
  83. g.fillRect(0, 0, size.width, size.height);
  84. }
  85. }
  86. protected void paintFocus(Graphics g, AbstractButton b,
  87. Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  88. Rectangle focusRect = new Rectangle();
  89. String text = b.getText();
  90. boolean isIcon = b.getIcon() != null;
  91. // If there is text
  92. if ( text != null && !text.equals( "" ) ) {
  93. if ( !isIcon ) {
  94. focusRect.setBounds( textRect );
  95. }
  96. else {
  97. focusRect.setBounds( iconRect.union( textRect ) );
  98. }
  99. }
  100. // If there is an icon and no text
  101. else if ( isIcon ) {
  102. focusRect.setBounds( iconRect );
  103. }
  104. g.setColor(getFocusColor());
  105. g.drawRect((focusRect.x-1), (focusRect.y-1),
  106. focusRect.width+1, focusRect.height+1);
  107. }
  108. protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  109. AbstractButton b = (AbstractButton) c;
  110. ButtonModel model = b.getModel();
  111. FontMetrics fm = g.getFontMetrics();
  112. /* Draw the Text */
  113. if(model.isEnabled()) {
  114. /*** paint the text normally */
  115. g.setColor(b.getForeground());
  116. BasicGraphicsUtils.drawString(g,text, model.getMnemonic(),
  117. textRect.x,
  118. textRect.y + fm.getAscent());
  119. }
  120. else {
  121. /*** paint the text disabled ***/
  122. g.setColor(getDisabledTextColor());
  123. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  124. textRect.x, textRect.y + fm.getAscent());
  125. }
  126. }
  127. }
  128. class MetalButtonListener extends BasicButtonListener
  129. {
  130. public MetalButtonListener(AbstractButton b) {
  131. super(b);
  132. }
  133. public void focusGained(FocusEvent e) {
  134. Component c = (Component)e.getSource();
  135. c.repaint();
  136. }
  137. }