1. /*
  2. * @(#)MetalRadioButtonUI.java 1.14 01/11/29
  3. *
  4. * Copyright 2002 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. /**
  16. * RadioButtonUI implementation for MetalRadioButtonUI
  17. * <p>
  18. * <strong>Warning:</strong>
  19. * Serialized objects of this class will not be compatible with
  20. * future Swing releases. The current serialization support is appropriate
  21. * for short term storage or RMI between applications running the same
  22. * version of Swing. A future release of Swing will provide support for
  23. * long term persistence.
  24. *
  25. * @version 1.14 11/29/01
  26. * @author Michael C. Albers (Metal modifications)
  27. * @author Jeff Dinkins (original BasicRadioButtonCode)
  28. */
  29. public class MetalRadioButtonUI extends BasicRadioButtonUI {
  30. private static final MetalRadioButtonUI metalRadioButtonUI = new MetalRadioButtonUI();
  31. protected Color focusColor;
  32. protected Color selectColor;
  33. protected Color disabledTextColor;
  34. private boolean defaults_initialized = false;
  35. // ********************************
  36. // Create PlAF
  37. // ********************************
  38. public static ComponentUI createUI(JComponent c) {
  39. return metalRadioButtonUI;
  40. }
  41. // ********************************
  42. // Install Defaults
  43. // ********************************
  44. public void installDefaults(AbstractButton b) {
  45. super.installDefaults(b);
  46. if(!defaults_initialized) {
  47. focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
  48. selectColor = UIManager.getColor(getPropertyPrefix() + "select");
  49. disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
  50. defaults_initialized = true;
  51. }
  52. b.setOpaque(true);
  53. }
  54. protected void uninstallDefaults(AbstractButton b) {
  55. super.uninstallDefaults(b);
  56. defaults_initialized = false;
  57. }
  58. // ********************************
  59. // Default Accessors
  60. // ********************************
  61. protected Color getSelectColor() {
  62. return selectColor;
  63. }
  64. protected Color getDisabledTextColor() {
  65. return disabledTextColor;
  66. }
  67. protected Color getFocusColor() {
  68. return focusColor;
  69. }
  70. // ********************************
  71. // Paint Methods
  72. // ********************************
  73. public synchronized void paint(Graphics g, JComponent c) {
  74. AbstractButton b = (AbstractButton) c;
  75. ButtonModel model = b.getModel();
  76. Dimension size = c.getSize();
  77. int w = size.width;
  78. int h = size.height;
  79. Font f = c.getFont();
  80. g.setFont(f);
  81. FontMetrics fm = g.getFontMetrics();
  82. Rectangle viewRect = new Rectangle(size);
  83. Rectangle iconRect = new Rectangle();
  84. Rectangle textRect = new Rectangle();
  85. Icon altIcon = b.getIcon();
  86. Icon selectedIcon = null;
  87. Icon disabledIcon = null;
  88. String text = SwingUtilities.layoutCompoundLabel(
  89. c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(),
  90. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  91. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  92. viewRect, iconRect, textRect, getDefaultTextIconGap(b)
  93. );
  94. // fill background
  95. if(c.isOpaque()) {
  96. g.setColor(b.getBackground());
  97. g.fillRect(0,0, size.width, size.height);
  98. }
  99. // Paint the radio button
  100. if(altIcon != null) {
  101. if(!model.isEnabled()) {
  102. altIcon = b.getDisabledIcon();
  103. } else if(model.isPressed() && model.isArmed()) {
  104. altIcon = b.getPressedIcon();
  105. if(altIcon == null) {
  106. // Use selected icon
  107. altIcon = b.getSelectedIcon();
  108. }
  109. } else if(model.isSelected()) {
  110. if(b.isRolloverEnabled() && model.isRollover()) {
  111. altIcon = (Icon) b.getRolloverSelectedIcon();
  112. if (altIcon == null) {
  113. altIcon = (Icon) b.getSelectedIcon();
  114. }
  115. }
  116. else {
  117. altIcon = (Icon) b.getSelectedIcon();
  118. }
  119. } else if(b.isRolloverEnabled() && model.isRollover()) {
  120. altIcon = (Icon) b.getRolloverIcon();
  121. }
  122. if(altIcon == null) {
  123. altIcon = b.getIcon();
  124. }
  125. altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
  126. } else {
  127. getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y);
  128. }
  129. // Draw the Text
  130. if(text != null) {
  131. if(model.isEnabled()) {
  132. // *** paint the text normally
  133. g.setColor(b.getForeground());
  134. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  135. textRect.x, textRect.y + fm.getAscent());
  136. } else {
  137. // *** paint the text disabled
  138. g.setColor(b.getBackground().darker());
  139. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  140. textRect.x,
  141. textRect.y + fm.getAscent());
  142. }
  143. if(b.hasFocus() && b.isFocusPainted() &&
  144. textRect.width > 0 && textRect.height > 0 ) {
  145. paintFocus(g,textRect,size);
  146. }
  147. }
  148. }
  149. protected void paintFocus(Graphics g, Rectangle t, Dimension d){
  150. g.setColor(getFocusColor());
  151. g.drawRect(t.x,t.y-1,t.width+1,t.height+1);
  152. }
  153. }