1. /*
  2. * @(#)MetalRadioButtonUI.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.*;
  14. import javax.swing.plaf.basic.*;
  15. import javax.swing.border.*;
  16. import javax.swing.plaf.*;
  17. import java.io.Serializable;
  18. import javax.swing.text.View;
  19. /**
  20. * RadioButtonUI implementation for MetalRadioButtonUI
  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 appropriate
  25. * for short term storage or RMI between applications running the same
  26. * version of Swing. A future release of Swing will provide support for
  27. * long term persistence.
  28. *
  29. * @version 1.17 02/02/00
  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. b.setOpaque(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 = g.getFontMetrics();
  86. Rectangle viewRect = new Rectangle(size);
  87. Rectangle iconRect = new Rectangle();
  88. Rectangle textRect = new Rectangle();
  89. Icon altIcon = b.getIcon();
  90. Icon selectedIcon = null;
  91. Icon disabledIcon = null;
  92. String text = SwingUtilities.layoutCompoundLabel(
  93. c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(),
  94. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  95. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  96. viewRect, iconRect, textRect, getDefaultTextIconGap(b)
  97. );
  98. // fill background
  99. if(c.isOpaque()) {
  100. g.setColor(b.getBackground());
  101. g.fillRect(0,0, size.width, size.height);
  102. }
  103. // Paint the radio button
  104. if(altIcon != null) {
  105. if(!model.isEnabled()) {
  106. if(model.isSelected()) {
  107. altIcon = b.getDisabledSelectedIcon();
  108. } else {
  109. altIcon = b.getDisabledIcon();
  110. }
  111. } else if(model.isPressed() && model.isArmed()) {
  112. altIcon = b.getPressedIcon();
  113. if(altIcon == null) {
  114. // Use selected icon
  115. altIcon = b.getSelectedIcon();
  116. }
  117. } else if(model.isSelected()) {
  118. if(b.isRolloverEnabled() && model.isRollover()) {
  119. altIcon = (Icon) b.getRolloverSelectedIcon();
  120. if (altIcon == null) {
  121. altIcon = (Icon) b.getSelectedIcon();
  122. }
  123. } else {
  124. altIcon = (Icon) b.getSelectedIcon();
  125. }
  126. } else if(b.isRolloverEnabled() && model.isRollover()) {
  127. altIcon = (Icon) b.getRolloverIcon();
  128. }
  129. if(altIcon == null) {
  130. altIcon = b.getIcon();
  131. }
  132. altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
  133. } else {
  134. getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y);
  135. }
  136. // Draw the Text
  137. if(text != null) {
  138. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  139. if (v != null) {
  140. v.paint(g, textRect);
  141. } else {
  142. if(model.isEnabled()) {
  143. // *** paint the text normally
  144. g.setColor(b.getForeground());
  145. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  146. textRect.x, textRect.y + fm.getAscent());
  147. } else {
  148. // *** paint the text disabled
  149. g.setColor(b.getBackground().darker());
  150. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  151. textRect.x,
  152. textRect.y + fm.getAscent());
  153. }
  154. if(b.hasFocus() && b.isFocusPainted() &&
  155. textRect.width > 0 && textRect.height > 0 ) {
  156. paintFocus(g,textRect,size);
  157. }
  158. }
  159. }
  160. }
  161. protected void paintFocus(Graphics g, Rectangle t, Dimension d){
  162. g.setColor(getFocusColor());
  163. g.drawRect(t.x,t.y-1,t.width+1,t.height+1);
  164. }
  165. }