1. /*
  2. * @(#)MetalToggleButtonUI.java 1.16 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 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 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.16 11/29/01
  28. * @author Tom Santos
  29. */
  30. public class MetalToggleButtonUI extends BasicToggleButtonUI {
  31. private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
  32. protected Color focusColor;
  33. protected Color selectColor;
  34. protected Color disabledTextColor;
  35. private boolean defaults_initialized = false;
  36. // ********************************
  37. // Create PLAF
  38. // ********************************
  39. public static ComponentUI createUI(JComponent b) {
  40. return metalToggleButtonUI;
  41. }
  42. // ********************************
  43. // Install Defaults
  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. protected void uninstallDefaults(AbstractButton b) {
  55. super.uninstallDefaults(b);
  56. defaults_initialized = false;
  57. }
  58. // ********************************
  59. // Default Accessors
  60. // ********************************
  61. protected Color getSelectColor() {
  62. return selectColor;
  63. }
  64. protected Color getDisabledTextColor() {
  65. return disabledTextColor;
  66. }
  67. protected Color getFocusColor() {
  68. return focusColor;
  69. }
  70. // ********************************
  71. // Paint Methods
  72. // ********************************
  73. protected void paintButtonPressed(Graphics g, AbstractButton b) {
  74. if ( b.isContentAreaFilled() ) {
  75. Dimension size = b.getSize();
  76. g.setColor(getSelectColor());
  77. g.fillRect(0, 0, size.width, size.height);
  78. }
  79. }
  80. protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  81. AbstractButton b = (AbstractButton) c;
  82. ButtonModel model = b.getModel();
  83. FontMetrics fm = g.getFontMetrics();
  84. /* Draw the Text */
  85. if(model.isEnabled()) {
  86. /*** paint the text normally */
  87. g.setColor(b.getForeground());
  88. BasicGraphicsUtils.drawString(g,text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  89. }
  90. else {
  91. /*** paint the text disabled ***/
  92. if (model.isSelected()) {
  93. g.setColor(c.getBackground());
  94. } else {
  95. g.setColor(getDisabledTextColor());
  96. }
  97. BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  98. }
  99. }
  100. protected void paintFocus(Graphics g, AbstractButton b,
  101. Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  102. Rectangle focusRect = new Rectangle();
  103. String text = b.getText();
  104. boolean isIcon = b.getIcon() != null;
  105. // If there is text
  106. if ( text != null && !text.equals( "" ) ) {
  107. if ( !isIcon ) {
  108. focusRect.setBounds( textRect );
  109. }
  110. else {
  111. focusRect.setBounds( iconRect.union( textRect ) );
  112. }
  113. }
  114. // If there is an icon and no text
  115. else if ( isIcon ) {
  116. focusRect.setBounds( iconRect );
  117. }
  118. g.setColor(getFocusColor());
  119. g.drawRect((focusRect.x-1), (focusRect.y-1),
  120. focusRect.width+1, focusRect.height+1);
  121. }
  122. }