1. /*
  2. * @(#)MetalRadioButtonUI.java 1.29 03/12/19
  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 javax.swing.*;
  12. import javax.swing.plaf.basic.*;
  13. import javax.swing.border.*;
  14. import javax.swing.plaf.*;
  15. import java.io.Serializable;
  16. import javax.swing.text.View;
  17. /**
  18. * RadioButtonUI implementation for MetalRadioButtonUI
  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.29 12/19/03
  30. * @author Michael C. Albers (Metal modifications)
  31. * @author Jeff Dinkins (original BasicRadioButtonCode)
  32. */
  33. public class MetalRadioButtonUI extends BasicRadioButtonUI {
  34. private static final MetalRadioButtonUI metalRadioButtonUI = new MetalRadioButtonUI();
  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 c) {
  43. return metalRadioButtonUI;
  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. LookAndFeel.installProperty(b, "opaque", Boolean.TRUE);
  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. public synchronized void paint(Graphics g, JComponent c) {
  78. AbstractButton b = (AbstractButton) c;
  79. ButtonModel model = b.getModel();
  80. Dimension size = c.getSize();
  81. int w = size.width;
  82. int h = size.height;
  83. Font f = c.getFont();
  84. g.setFont(f);
  85. FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
  86. Rectangle viewRect = new Rectangle(size);
  87. Rectangle iconRect = new Rectangle();
  88. Rectangle textRect = new Rectangle();
  89. Insets i = c.getInsets();
  90. viewRect.x += i.left;
  91. viewRect.y += i.top;
  92. viewRect.width -= (i.right + viewRect.x);
  93. viewRect.height -= (i.bottom + viewRect.y);
  94. Icon altIcon = b.getIcon();
  95. Icon selectedIcon = null;
  96. Icon disabledIcon = null;
  97. String text = SwingUtilities.layoutCompoundLabel(
  98. c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(),
  99. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  100. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  101. viewRect, iconRect, textRect, b.getIconTextGap());
  102. // fill background
  103. if(c.isOpaque()) {
  104. g.setColor(b.getBackground());
  105. g.fillRect(0,0, size.width, size.height);
  106. }
  107. // Paint the radio button
  108. if(altIcon != null) {
  109. if(!model.isEnabled()) {
  110. if(model.isSelected()) {
  111. altIcon = b.getDisabledSelectedIcon();
  112. } else {
  113. altIcon = b.getDisabledIcon();
  114. }
  115. } else if(model.isPressed() && model.isArmed()) {
  116. altIcon = b.getPressedIcon();
  117. if(altIcon == null) {
  118. // Use selected icon
  119. altIcon = b.getSelectedIcon();
  120. }
  121. } else if(model.isSelected()) {
  122. if(b.isRolloverEnabled() && model.isRollover()) {
  123. altIcon = (Icon) b.getRolloverSelectedIcon();
  124. if (altIcon == null) {
  125. altIcon = (Icon) b.getSelectedIcon();
  126. }
  127. } else {
  128. altIcon = (Icon) b.getSelectedIcon();
  129. }
  130. } else if(b.isRolloverEnabled() && model.isRollover()) {
  131. altIcon = (Icon) b.getRolloverIcon();
  132. }
  133. if(altIcon == null) {
  134. altIcon = b.getIcon();
  135. }
  136. altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
  137. } else {
  138. getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y);
  139. }
  140. // Draw the Text
  141. if(text != null) {
  142. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  143. if (v != null) {
  144. v.paint(g, textRect);
  145. } else {
  146. int mnemIndex = b.getDisplayedMnemonicIndex();
  147. if(model.isEnabled()) {
  148. // *** paint the text normally
  149. g.setColor(b.getForeground());
  150. } else {
  151. // *** paint the text disabled
  152. g.setColor(getDisabledTextColor());
  153. }
  154. SwingUtilities2.drawStringUnderlineCharAt(c,g,text,
  155. mnemIndex, textRect.x, textRect.y + fm.getAscent());
  156. }
  157. if(b.hasFocus() && b.isFocusPainted() &&
  158. textRect.width > 0 && textRect.height > 0 ) {
  159. paintFocus(g,textRect,size);
  160. }
  161. }
  162. }
  163. protected void paintFocus(Graphics g, Rectangle t, Dimension d){
  164. g.setColor(getFocusColor());
  165. g.drawRect(t.x-1, t.y-1, t.width+1, t.height+1);
  166. }
  167. }