1. /*
  2. * @(#)MetalRadioButtonUI.java 1.25 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.*;
  11. import javax.swing.plaf.basic.*;
  12. import javax.swing.border.*;
  13. import javax.swing.plaf.*;
  14. import java.io.Serializable;
  15. import javax.swing.text.View;
  16. /**
  17. * RadioButtonUI implementation for MetalRadioButtonUI
  18. * <p>
  19. * <strong>Warning:</strong>
  20. * Serialized objects of this class will not be compatible with
  21. * future Swing releases. The current serialization support is
  22. * appropriate for short term storage or RMI between applications running
  23. * the same version of Swing. As of 1.4, support for long term storage
  24. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  25. * has been added to the <code>java.beans</code> package.
  26. * Please see {@link java.beans.XMLEncoder}.
  27. *
  28. * @version 1.25 01/23/03
  29. * @author Michael C. Albers (Metal modifications)
  30. * @author Jeff Dinkins (original BasicRadioButtonCode)
  31. */
  32. public class MetalRadioButtonUI extends BasicRadioButtonUI {
  33. private static final MetalRadioButtonUI metalRadioButtonUI = new MetalRadioButtonUI();
  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 c) {
  42. return metalRadioButtonUI;
  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. b.setOpaque(true);
  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. public synchronized void paint(Graphics g, JComponent c) {
  77. AbstractButton b = (AbstractButton) c;
  78. ButtonModel model = b.getModel();
  79. Dimension size = c.getSize();
  80. int w = size.width;
  81. int h = size.height;
  82. Font f = c.getFont();
  83. g.setFont(f);
  84. FontMetrics fm = g.getFontMetrics();
  85. Rectangle viewRect = new Rectangle(size);
  86. Rectangle iconRect = new Rectangle();
  87. Rectangle textRect = new Rectangle();
  88. Insets i = c.getInsets();
  89. viewRect.x += i.left;
  90. viewRect.y += i.top;
  91. viewRect.width -= (i.right + viewRect.x);
  92. viewRect.height -= (i.bottom + viewRect.y);
  93. Icon altIcon = b.getIcon();
  94. Icon selectedIcon = null;
  95. Icon disabledIcon = null;
  96. String text = SwingUtilities.layoutCompoundLabel(
  97. c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(),
  98. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  99. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  100. viewRect, iconRect, textRect, b.getIconTextGap());
  101. // fill background
  102. if(c.isOpaque()) {
  103. g.setColor(b.getBackground());
  104. g.fillRect(0,0, size.width, size.height);
  105. }
  106. // Paint the radio button
  107. if(altIcon != null) {
  108. if(!model.isEnabled()) {
  109. if(model.isSelected()) {
  110. altIcon = b.getDisabledSelectedIcon();
  111. } else {
  112. altIcon = b.getDisabledIcon();
  113. }
  114. } else if(model.isPressed() && model.isArmed()) {
  115. altIcon = b.getPressedIcon();
  116. if(altIcon == null) {
  117. // Use selected icon
  118. altIcon = b.getSelectedIcon();
  119. }
  120. } else if(model.isSelected()) {
  121. if(b.isRolloverEnabled() && model.isRollover()) {
  122. altIcon = (Icon) b.getRolloverSelectedIcon();
  123. if (altIcon == null) {
  124. altIcon = (Icon) b.getSelectedIcon();
  125. }
  126. } else {
  127. altIcon = (Icon) b.getSelectedIcon();
  128. }
  129. } else if(b.isRolloverEnabled() && model.isRollover()) {
  130. altIcon = (Icon) b.getRolloverIcon();
  131. }
  132. if(altIcon == null) {
  133. altIcon = b.getIcon();
  134. }
  135. altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
  136. } else {
  137. getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y);
  138. }
  139. // Draw the Text
  140. if(text != null) {
  141. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  142. if (v != null) {
  143. v.paint(g, textRect);
  144. } else {
  145. int mnemIndex = b.getDisplayedMnemonicIndex();
  146. if(model.isEnabled()) {
  147. // *** paint the text normally
  148. g.setColor(b.getForeground());
  149. BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,
  150. mnemIndex, textRect.x, textRect.y + fm.getAscent());
  151. } else {
  152. // *** paint the text disabled
  153. g.setColor(getDisabledTextColor());
  154. BasicGraphicsUtils.drawStringUnderlineCharAt(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. }
  164. protected void paintFocus(Graphics g, Rectangle t, Dimension d){
  165. g.setColor(getFocusColor());
  166. g.drawRect(t.x-1, t.y-1, t.width+1, t.height+1);
  167. }
  168. }