1. /*
  2. * @(#)MetalToggleButtonUI.java 1.28 04/04/02
  3. *
  4. * Copyright 2004 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 com.sun.java.swing.SwingUtilities2;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.lang.ref.*;
  12. import java.util.*;
  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 java.io.Serializable;
  19. /**
  20. * MetalToggleButton implementation
  21. * <p>
  22. * <strong>Warning:</strong>
  23. * Serialized objects of this class will not be compatible with
  24. * future Swing releases. The current serialization support is
  25. * appropriate for short term storage or RMI between applications running
  26. * the same version of Swing. As of 1.4, support for long term storage
  27. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  28. * has been added to the <code>java.beans</code> package.
  29. * Please see {@link java.beans.XMLEncoder}.
  30. *
  31. * @version 1.28 04/02/04
  32. * @author Tom Santos
  33. */
  34. public class MetalToggleButtonUI extends BasicToggleButtonUI {
  35. private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
  36. protected Color focusColor;
  37. protected Color selectColor;
  38. protected Color disabledTextColor;
  39. private boolean defaults_initialized = false;
  40. // ********************************
  41. // Create PLAF
  42. // ********************************
  43. public static ComponentUI createUI(JComponent b) {
  44. return metalToggleButtonUI;
  45. }
  46. // ********************************
  47. // Install Defaults
  48. // ********************************
  49. public void installDefaults(AbstractButton b) {
  50. super.installDefaults(b);
  51. if(!defaults_initialized) {
  52. focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  53. selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  54. disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
  55. defaults_initialized = true;
  56. }
  57. }
  58. protected void uninstallDefaults(AbstractButton b) {
  59. super.uninstallDefaults(b);
  60. defaults_initialized = false;
  61. }
  62. // ********************************
  63. // Default Accessors
  64. // ********************************
  65. protected Color getSelectColor() {
  66. return selectColor;
  67. }
  68. protected Color getDisabledTextColor() {
  69. return disabledTextColor;
  70. }
  71. protected Color getFocusColor() {
  72. return focusColor;
  73. }
  74. // ********************************
  75. // Paint Methods
  76. // ********************************
  77. /**
  78. * If necessary paints the background of the component, then invokes
  79. * <code>paint</code>.
  80. *
  81. * @param g Graphics to paint to
  82. * @param c JComponent painting on
  83. * @throws NullPointerException if <code>g</code> or <code>c</code> is
  84. * null
  85. * @see javax.swing.plaf.ComponentUI#update
  86. * @see javax.swing.plaf.ComponentUI#paint
  87. * @since 1.5
  88. */
  89. public void update(Graphics g, JComponent c) {
  90. AbstractButton button = (AbstractButton)c;
  91. if ((c.getBackground() instanceof UIResource) &&
  92. button.isContentAreaFilled() && c.isEnabled()) {
  93. ButtonModel model = button.getModel();
  94. if (!MetalUtils.isToolBarButton(c)) {
  95. if (!model.isArmed() && !model.isPressed() &&
  96. MetalUtils.drawGradient(
  97. c, g, "ToggleButton.gradient", 0, 0, c.getWidth(),
  98. c.getHeight(), true)) {
  99. paint(g, c);
  100. return;
  101. }
  102. }
  103. else if ((model.isRollover() || model.isSelected()) &&
  104. MetalUtils.drawGradient(c, g, "ToggleButton.gradient",
  105. 0, 0, c.getWidth(), c.getHeight(), true)) {
  106. paint(g, c);
  107. return;
  108. }
  109. }
  110. super.update(g, c);
  111. }
  112. protected void paintButtonPressed(Graphics g, AbstractButton b) {
  113. if ( b.isContentAreaFilled() ) {
  114. g.setColor(getSelectColor());
  115. g.fillRect(0, 0, b.getWidth(), b.getHeight());
  116. }
  117. }
  118. protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  119. AbstractButton b = (AbstractButton) c;
  120. ButtonModel model = b.getModel();
  121. FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
  122. int mnemIndex = b.getDisplayedMnemonicIndex();
  123. /* Draw the Text */
  124. if(model.isEnabled()) {
  125. /*** paint the text normally */
  126. g.setColor(b.getForeground());
  127. }
  128. else {
  129. /*** paint the text disabled ***/
  130. if (model.isSelected()) {
  131. g.setColor(c.getBackground());
  132. } else {
  133. g.setColor(getDisabledTextColor());
  134. }
  135. }
  136. SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex,
  137. textRect.x, textRect.y + fm.getAscent());
  138. }
  139. protected void paintFocus(Graphics g, AbstractButton b,
  140. Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  141. Rectangle focusRect = new Rectangle();
  142. String text = b.getText();
  143. boolean isIcon = b.getIcon() != null;
  144. // If there is text
  145. if ( text != null && !text.equals( "" ) ) {
  146. if ( !isIcon ) {
  147. focusRect.setBounds( textRect );
  148. }
  149. else {
  150. focusRect.setBounds( iconRect.union( textRect ) );
  151. }
  152. }
  153. // If there is an icon and no text
  154. else if ( isIcon ) {
  155. focusRect.setBounds( iconRect );
  156. }
  157. g.setColor(getFocusColor());
  158. g.drawRect((focusRect.x-1), (focusRect.y-1),
  159. focusRect.width+1, focusRect.height+1);
  160. }
  161. /**
  162. * Paints the appropriate icon of the button <code>b</code> in the
  163. * space <code>iconRect</code>.
  164. *
  165. * @param g Graphics to paint to
  166. * @param b Button to render for
  167. * @param iconRect space to render in
  168. * @throws NullPointerException if any of the arguments are null.
  169. * @since 1.5
  170. */
  171. protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
  172. super.paintIcon(g, b, iconRect);
  173. }
  174. }