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