1. /*
  2. * @(#)MetalToggleButtonUI.java 1.21 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 java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.plaf.basic.BasicToggleButtonUI;
  11. import javax.swing.*;
  12. import javax.swing.border.*;
  13. import javax.swing.plaf.*;
  14. import javax.swing.*;
  15. import javax.swing.plaf.basic.BasicGraphicsUtils;
  16. import java.io.Serializable;
  17. /**
  18. * MetalToggleButton 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
  23. * appropriate for short term storage or RMI between applications running
  24. * the same version of Swing. As of 1.4, support for long term storage
  25. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  26. * has been added to the <code>java.beans</code> package.
  27. * Please see {@link java.beans.XMLEncoder}.
  28. *
  29. * @version 1.21 01/23/03
  30. * @author Tom Santos
  31. */
  32. public class MetalToggleButtonUI extends BasicToggleButtonUI {
  33. private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
  34. protected Color focusColor;
  35. protected Color selectColor;
  36. protected Color disabledTextColor;
  37. private boolean defaults_initialized = false;
  38. // ********************************
  39. // Create PLAF
  40. // ********************************
  41. public static ComponentUI createUI(JComponent b) {
  42. return metalToggleButtonUI;
  43. }
  44. // ********************************
  45. // Install Defaults
  46. // ********************************
  47. public void installDefaults(AbstractButton b) {
  48. super.installDefaults(b);
  49. if(!defaults_initialized) {
  50. focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  51. selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  52. disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
  53. defaults_initialized = true;
  54. }
  55. }
  56. protected void uninstallDefaults(AbstractButton b) {
  57. super.uninstallDefaults(b);
  58. defaults_initialized = false;
  59. }
  60. // ********************************
  61. // Default Accessors
  62. // ********************************
  63. protected Color getSelectColor() {
  64. return selectColor;
  65. }
  66. protected Color getDisabledTextColor() {
  67. return disabledTextColor;
  68. }
  69. protected Color getFocusColor() {
  70. return focusColor;
  71. }
  72. // ********************************
  73. // Paint Methods
  74. // ********************************
  75. protected void paintButtonPressed(Graphics g, AbstractButton b) {
  76. if ( b.isContentAreaFilled() ) {
  77. Dimension size = b.getSize();
  78. g.setColor(getSelectColor());
  79. g.fillRect(0, 0, size.width, size.height);
  80. }
  81. }
  82. protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  83. AbstractButton b = (AbstractButton) c;
  84. ButtonModel model = b.getModel();
  85. FontMetrics fm = g.getFontMetrics();
  86. int mnemIndex = b.getDisplayedMnemonicIndex();
  87. /* Draw the Text */
  88. if(model.isEnabled()) {
  89. /*** paint the text normally */
  90. g.setColor(b.getForeground());
  91. BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemIndex,
  92. textRect.x, textRect.y + fm.getAscent());
  93. }
  94. else {
  95. /*** paint the text disabled ***/
  96. if (model.isSelected()) {
  97. g.setColor(c.getBackground());
  98. } else {
  99. g.setColor(getDisabledTextColor());
  100. }
  101. BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemIndex,
  102. textRect.x, textRect.y + fm.getAscent());
  103. }
  104. }
  105. protected void paintFocus(Graphics g, AbstractButton b,
  106. Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  107. Rectangle focusRect = new Rectangle();
  108. String text = b.getText();
  109. boolean isIcon = b.getIcon() != null;
  110. // If there is text
  111. if ( text != null && !text.equals( "" ) ) {
  112. if ( !isIcon ) {
  113. focusRect.setBounds( textRect );
  114. }
  115. else {
  116. focusRect.setBounds( iconRect.union( textRect ) );
  117. }
  118. }
  119. // If there is an icon and no text
  120. else if ( isIcon ) {
  121. focusRect.setBounds( iconRect );
  122. }
  123. g.setColor(getFocusColor());
  124. g.drawRect((focusRect.x-1), (focusRect.y-1),
  125. focusRect.width+1, focusRect.height+1);
  126. }
  127. }