1. /*
  2. * @(#)MetalToggleButtonUI.java 1.17 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 java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.plaf.basic.BasicToggleButtonUI;
  14. import javax.swing.*;
  15. import javax.swing.border.*;
  16. import javax.swing.plaf.*;
  17. import javax.swing.*;
  18. import javax.swing.plaf.basic.BasicGraphicsUtils;
  19. import java.io.Serializable;
  20. /**
  21. * MetalToggleButton implementation
  22. * <p>
  23. * <strong>Warning:</strong>
  24. * Serialized objects of this class will not be compatible with
  25. * future Swing releases. The current serialization support is appropriate
  26. * for short term storage or RMI between applications running the same
  27. * version of Swing. A future release of Swing will provide support for
  28. * long term persistence.
  29. *
  30. * @version 1.17 02/02/00
  31. * @author Tom Santos
  32. */
  33. public class MetalToggleButtonUI extends BasicToggleButtonUI {
  34. private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
  35. protected Color focusColor;
  36. protected Color selectColor;
  37. protected Color disabledTextColor;
  38. private boolean defaults_initialized = false;
  39. // ********************************
  40. // Create PLAF
  41. // ********************************
  42. public static ComponentUI createUI(JComponent b) {
  43. return metalToggleButtonUI;
  44. }
  45. // ********************************
  46. // Install Defaults
  47. // ********************************
  48. public void installDefaults(AbstractButton b) {
  49. super.installDefaults(b);
  50. if(!defaults_initialized) {
  51. focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  52. selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  53. disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
  54. defaults_initialized = true;
  55. }
  56. }
  57. protected void uninstallDefaults(AbstractButton b) {
  58. super.uninstallDefaults(b);
  59. defaults_initialized = false;
  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 Methods
  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 paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  84. AbstractButton b = (AbstractButton) c;
  85. ButtonModel model = b.getModel();
  86. FontMetrics fm = g.getFontMetrics();
  87. /* Draw the Text */
  88. if(model.isEnabled()) {
  89. /*** paint the text normally */
  90. g.setColor(b.getForeground());
  91. BasicGraphicsUtils.drawString(g,text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  92. }
  93. else {
  94. /*** paint the text disabled ***/
  95. if (model.isSelected()) {
  96. g.setColor(c.getBackground());
  97. } else {
  98. g.setColor(getDisabledTextColor());
  99. }
  100. BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  101. }
  102. }
  103. protected void paintFocus(Graphics g, AbstractButton b,
  104. Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  105. Rectangle focusRect = new Rectangle();
  106. String text = b.getText();
  107. boolean isIcon = b.getIcon() != null;
  108. // If there is text
  109. if ( text != null && !text.equals( "" ) ) {
  110. if ( !isIcon ) {
  111. focusRect.setBounds( textRect );
  112. }
  113. else {
  114. focusRect.setBounds( iconRect.union( textRect ) );
  115. }
  116. }
  117. // If there is an icon and no text
  118. else if ( isIcon ) {
  119. focusRect.setBounds( iconRect );
  120. }
  121. g.setColor(getFocusColor());
  122. g.drawRect((focusRect.x-1), (focusRect.y-1),
  123. focusRect.width+1, focusRect.height+1);
  124. }
  125. }