1. /*
  2. * @(#)BasicToggleButtonUI.java 1.53 00/04/06
  3. *
  4. * Copyright 1997-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.basic;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.*;
  14. import javax.swing.border.*;
  15. import javax.swing.plaf.*;
  16. import javax.swing.text.View;
  17. /**
  18. * BasicToggleButton implementation
  19. * <p>
  20. *
  21. * @version 1.53 04/06/00
  22. * @author Jeff Dinkins
  23. */
  24. public class BasicToggleButtonUI extends BasicButtonUI {
  25. private final static BasicToggleButtonUI toggleButtonUI = new BasicToggleButtonUI();
  26. private final static String propertyPrefix = "ToggleButton" + ".";
  27. // ********************************
  28. // Create PLAF
  29. // ********************************
  30. public static ComponentUI createUI(JComponent b) {
  31. return toggleButtonUI;
  32. }
  33. protected String getPropertyPrefix() {
  34. return propertyPrefix;
  35. }
  36. // ********************************
  37. // Paint Methods
  38. // ********************************
  39. public void paint(Graphics g, JComponent c) {
  40. AbstractButton b = (AbstractButton) c;
  41. ButtonModel model = b.getModel();
  42. Dimension size = b.getSize();
  43. FontMetrics fm = g.getFontMetrics();
  44. Insets i = c.getInsets();
  45. Rectangle viewRect = new Rectangle(size);
  46. viewRect.x += i.left;
  47. viewRect.y += i.top;
  48. viewRect.width -= (i.right + viewRect.x);
  49. viewRect.height -= (i.bottom + viewRect.y);
  50. Rectangle iconRect = new Rectangle();
  51. Rectangle textRect = new Rectangle();
  52. Font f = c.getFont();
  53. g.setFont(f);
  54. // layout the text and icon
  55. String text = SwingUtilities.layoutCompoundLabel(
  56. c, fm, b.getText(), b.getIcon(),
  57. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  58. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  59. viewRect, iconRect, textRect, b.getText() == null ? 0 : getDefaultTextIconGap(b)
  60. );
  61. g.setColor(b.getBackground());
  62. if (model.isArmed() && model.isPressed() || model.isSelected()) {
  63. paintButtonPressed(g,b);
  64. } else if (b.isOpaque()) {
  65. Insets insets = b.getInsets();
  66. Insets margin = b.getMargin();
  67. g.fillRect(insets.left - margin.left,
  68. insets.top - margin.top,
  69. size.width - (insets.left-margin.left) - (insets.right - margin.right),
  70. size.height - (insets.top-margin.top) - (insets.bottom - margin.bottom));
  71. }
  72. // Paint the Icon
  73. if(b.getIcon() != null) {
  74. paintIcon(g, b, iconRect);
  75. }
  76. // Draw the Text
  77. if(text != null && !text.equals("")) {
  78. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  79. if (v != null) {
  80. v.paint(g, textRect);
  81. } else {
  82. paintText(g, b, textRect, text);
  83. }
  84. }
  85. // draw the dashed focus line.
  86. if (b.isFocusPainted() && b.hasFocus()) {
  87. paintFocus(g, b, viewRect, textRect, iconRect);
  88. }
  89. }
  90. protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
  91. ButtonModel model = b.getModel();
  92. Icon icon = null;
  93. if(!model.isEnabled()) {
  94. if(model.isSelected()) {
  95. icon = (Icon) b.getDisabledSelectedIcon();
  96. } else {
  97. icon = (Icon) b.getDisabledIcon();
  98. }
  99. } else if(model.isPressed() && model.isArmed()) {
  100. icon = (Icon) b.getPressedIcon();
  101. if(icon == null) {
  102. // Use selected icon
  103. icon = (Icon) b.getSelectedIcon();
  104. }
  105. } else if(model.isSelected()) {
  106. if(b.isRolloverEnabled() && model.isRollover()) {
  107. icon = (Icon) b.getRolloverSelectedIcon();
  108. if (icon == null) {
  109. icon = (Icon) b.getSelectedIcon();
  110. }
  111. } else {
  112. icon = (Icon) b.getSelectedIcon();
  113. }
  114. } else if(b.isRolloverEnabled() && model.isRollover()) {
  115. icon = (Icon) b.getRolloverIcon();
  116. }
  117. if(icon == null) {
  118. icon = (Icon) b.getIcon();
  119. }
  120. icon.paintIcon(b, g, iconRect.x, iconRect.y);
  121. }
  122. protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text){
  123. ButtonModel model = b.getModel();
  124. FontMetrics fm = g.getFontMetrics();
  125. if(model.isEnabled()) {
  126. // *** paint the text normally
  127. g.setColor(b.getForeground());
  128. BasicGraphicsUtils.drawString(g,text, model.getMnemonic(),
  129. textRect.x,
  130. textRect.y + fm.getAscent());
  131. } else {
  132. // *** paint the text disabled
  133. g.setColor(b.getBackground().brighter());
  134. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  135. textRect.x, textRect.y + fm.getAscent());
  136. g.setColor(b.getBackground().darker());
  137. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  138. textRect.x - 1, textRect.y + fm.getAscent() - 1);
  139. }
  140. }
  141. // Basic does nothing - L&F like Motif paint the button's focus
  142. protected void paintFocus(Graphics g, AbstractButton b,
  143. Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
  144. }
  145. // Basic does nothing - L&F like Motif shade the button
  146. protected void paintButtonPressed(Graphics g, AbstractButton b){
  147. }
  148. }