1. /*
  2. * @(#)BasicToggleButtonUI.java 1.56 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.basic;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.border.*;
  12. import javax.swing.plaf.*;
  13. import javax.swing.text.View;
  14. /**
  15. * BasicToggleButton implementation
  16. * <p>
  17. *
  18. * @version 1.56 01/23/03
  19. * @author Jeff Dinkins
  20. */
  21. public class BasicToggleButtonUI extends BasicButtonUI {
  22. private final static BasicToggleButtonUI toggleButtonUI = new BasicToggleButtonUI();
  23. private final static String propertyPrefix = "ToggleButton" + ".";
  24. // ********************************
  25. // Create PLAF
  26. // ********************************
  27. public static ComponentUI createUI(JComponent b) {
  28. return toggleButtonUI;
  29. }
  30. protected String getPropertyPrefix() {
  31. return propertyPrefix;
  32. }
  33. // ********************************
  34. // Paint Methods
  35. // ********************************
  36. public void paint(Graphics g, JComponent c) {
  37. AbstractButton b = (AbstractButton) c;
  38. ButtonModel model = b.getModel();
  39. Dimension size = b.getSize();
  40. FontMetrics fm = g.getFontMetrics();
  41. Insets i = c.getInsets();
  42. Rectangle viewRect = new Rectangle(size);
  43. viewRect.x += i.left;
  44. viewRect.y += i.top;
  45. viewRect.width -= (i.right + viewRect.x);
  46. viewRect.height -= (i.bottom + viewRect.y);
  47. Rectangle iconRect = new Rectangle();
  48. Rectangle textRect = new Rectangle();
  49. Font f = c.getFont();
  50. g.setFont(f);
  51. // layout the text and icon
  52. String text = SwingUtilities.layoutCompoundLabel(
  53. c, fm, b.getText(), b.getIcon(),
  54. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  55. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  56. viewRect, iconRect, textRect,
  57. b.getText() == null ? 0 : b.getIconTextGap());
  58. g.setColor(b.getBackground());
  59. if (model.isArmed() && model.isPressed() || model.isSelected()) {
  60. paintButtonPressed(g,b);
  61. } else if (b.isOpaque()) {
  62. Insets insets = b.getInsets();
  63. Insets margin = b.getMargin();
  64. g.fillRect(insets.left - margin.left,
  65. insets.top - margin.top,
  66. size.width - (insets.left-margin.left) - (insets.right - margin.right),
  67. size.height - (insets.top-margin.top) - (insets.bottom - margin.bottom));
  68. }
  69. // Paint the Icon
  70. if(b.getIcon() != null) {
  71. paintIcon(g, b, iconRect);
  72. }
  73. // Draw the Text
  74. if(text != null && !text.equals("")) {
  75. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  76. if (v != null) {
  77. v.paint(g, textRect);
  78. } else {
  79. paintText(g, b, textRect, text);
  80. }
  81. }
  82. // draw the dashed focus line.
  83. if (b.isFocusPainted() && b.hasFocus()) {
  84. paintFocus(g, b, viewRect, textRect, iconRect);
  85. }
  86. }
  87. protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
  88. ButtonModel model = b.getModel();
  89. Icon icon = null;
  90. if(!model.isEnabled()) {
  91. if(model.isSelected()) {
  92. icon = (Icon) b.getDisabledSelectedIcon();
  93. } else {
  94. icon = (Icon) b.getDisabledIcon();
  95. }
  96. } else if(model.isPressed() && model.isArmed()) {
  97. icon = (Icon) b.getPressedIcon();
  98. if(icon == null) {
  99. // Use selected icon
  100. icon = (Icon) b.getSelectedIcon();
  101. }
  102. } else if(model.isSelected()) {
  103. if(b.isRolloverEnabled() && model.isRollover()) {
  104. icon = (Icon) b.getRolloverSelectedIcon();
  105. if (icon == null) {
  106. icon = (Icon) b.getSelectedIcon();
  107. }
  108. } else {
  109. icon = (Icon) b.getSelectedIcon();
  110. }
  111. } else if(b.isRolloverEnabled() && model.isRollover()) {
  112. icon = (Icon) b.getRolloverIcon();
  113. }
  114. if(icon == null) {
  115. icon = (Icon) b.getIcon();
  116. }
  117. icon.paintIcon(b, g, iconRect.x, iconRect.y);
  118. }
  119. /**
  120. * Overriden so that the text will not be rendered as shifted for
  121. * Toggle buttons and subclasses.
  122. */
  123. protected int getTextShiftOffset() {
  124. return 0;
  125. }
  126. }