1. /*
  2. * @(#)BasicToggleButtonUI.java 1.58 03/12/19
  3. *
  4. * Copyright 2004 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.58 12/19/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. }
  62. // Paint the Icon
  63. if(b.getIcon() != null) {
  64. paintIcon(g, b, iconRect);
  65. }
  66. // Draw the Text
  67. if(text != null && !text.equals("")) {
  68. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  69. if (v != null) {
  70. v.paint(g, textRect);
  71. } else {
  72. paintText(g, b, textRect, text);
  73. }
  74. }
  75. // draw the dashed focus line.
  76. if (b.isFocusPainted() && b.hasFocus()) {
  77. paintFocus(g, b, viewRect, textRect, iconRect);
  78. }
  79. }
  80. protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
  81. ButtonModel model = b.getModel();
  82. Icon icon = null;
  83. if(!model.isEnabled()) {
  84. if(model.isSelected()) {
  85. icon = (Icon) b.getDisabledSelectedIcon();
  86. } else {
  87. icon = (Icon) b.getDisabledIcon();
  88. }
  89. } else if(model.isPressed() && model.isArmed()) {
  90. icon = (Icon) b.getPressedIcon();
  91. if(icon == null) {
  92. // Use selected icon
  93. icon = (Icon) b.getSelectedIcon();
  94. }
  95. } else if(model.isSelected()) {
  96. if(b.isRolloverEnabled() && model.isRollover()) {
  97. icon = (Icon) b.getRolloverSelectedIcon();
  98. if (icon == null) {
  99. icon = (Icon) b.getSelectedIcon();
  100. }
  101. } else {
  102. icon = (Icon) b.getSelectedIcon();
  103. }
  104. } else if(b.isRolloverEnabled() && model.isRollover()) {
  105. icon = (Icon) b.getRolloverIcon();
  106. }
  107. if(icon == null) {
  108. icon = (Icon) b.getIcon();
  109. }
  110. icon.paintIcon(b, g, iconRect.x, iconRect.y);
  111. }
  112. /**
  113. * Overriden so that the text will not be rendered as shifted for
  114. * Toggle buttons and subclasses.
  115. */
  116. protected int getTextShiftOffset() {
  117. return 0;
  118. }
  119. }