1. /*
  2. * @(#)WindowsToggleButtonUI.java 1.26 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 com.sun.java.swing.plaf.windows;
  8. import javax.swing.plaf.basic.*;
  9. import javax.swing.border.*;
  10. import javax.swing.plaf.*;
  11. import javax.swing.*;
  12. import java.awt.*;
  13. /**
  14. * A Windows toggle button.
  15. * <p>
  16. * <strong>Warning:</strong>
  17. * Serialized objects of this class will not be compatible with
  18. * future Swing releases. The current serialization support is appropriate
  19. * for short term storage or RMI between applications running the same
  20. * version of Swing. A future release of Swing will provide support for
  21. * long term persistence.
  22. *
  23. * @version 1.26 01/23/03
  24. * @author Jeff Dinkins
  25. */
  26. public class WindowsToggleButtonUI extends BasicToggleButtonUI
  27. {
  28. protected static int dashedRectGapX;
  29. protected static int dashedRectGapY;
  30. protected static int dashedRectGapWidth;
  31. protected static int dashedRectGapHeight;
  32. protected Color focusColor;
  33. private final static WindowsToggleButtonUI windowsToggleButtonUI = new WindowsToggleButtonUI();
  34. private boolean defaults_initialized = false;
  35. public static ComponentUI createUI(JComponent b) {
  36. return windowsToggleButtonUI;
  37. }
  38. // ********************************
  39. // Defaults
  40. // ********************************
  41. protected void installDefaults(AbstractButton b) {
  42. super.installDefaults(b);
  43. if(!defaults_initialized) {
  44. String pp = getPropertyPrefix();
  45. dashedRectGapX = ((Integer)UIManager.get("Button.dashedRectGapX")).intValue();
  46. dashedRectGapY = ((Integer)UIManager.get("Button.dashedRectGapY")).intValue();
  47. dashedRectGapWidth = ((Integer)UIManager.get("Button.dashedRectGapWidth")).intValue();
  48. dashedRectGapHeight = ((Integer)UIManager.get("Button.dashedRectGapHeight")).intValue();
  49. focusColor = UIManager.getColor(pp + "focus");
  50. defaults_initialized = true;
  51. }
  52. XPStyle xp = XPStyle.getXP();
  53. if (xp != null) {
  54. b.setBorder(xp.getBorder("button.pushbutton"));
  55. b.setOpaque(false);
  56. b.setRolloverEnabled(true);
  57. }
  58. }
  59. protected void uninstallDefaults(AbstractButton b) {
  60. super.uninstallDefaults(b);
  61. defaults_initialized = false;
  62. }
  63. protected Color getFocusColor() {
  64. return focusColor;
  65. }
  66. // ********************************
  67. // Paint Methods
  68. // ********************************
  69. protected void paintButtonPressed(Graphics g, AbstractButton b) {
  70. if (XPStyle.getXP() == null &&
  71. b.isContentAreaFilled() &&
  72. !(b.getBorder() instanceof UIResource)) {
  73. // This is a special case in which the toggle button in the
  74. // Rollover JToolBar will render the button in a pressed state
  75. Color oldColor = g.getColor();
  76. int w = b.getWidth();
  77. int h = b.getHeight();
  78. UIDefaults table = UIManager.getLookAndFeelDefaults();
  79. Color shade = table.getColor("ToggleButton.shadow");
  80. Component p = b.getParent();
  81. if (p != null && p.getBackground().equals(shade)) {
  82. shade = table.getColor("ToggleButton.darkShadow");
  83. }
  84. g.setColor(shade);
  85. g.drawRect(0, 0, w-1, h-1);
  86. g.setColor(table.getColor("ToggleButton.highlight"));
  87. g.drawLine(w-1, 0, w-1, h-1);
  88. g.drawLine(0, h-1, w-1, h-1);
  89. g.setColor(oldColor);
  90. }
  91. }
  92. public void paint(Graphics g, JComponent c) {
  93. if (XPStyle.getXP() != null) {
  94. WindowsButtonUI.paintXPButtonBackground(g, c);
  95. }
  96. super.paint(g, c);
  97. }
  98. /**
  99. * Overridden method to render the text without the mnemonic
  100. */
  101. protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
  102. WindowsGraphicsUtils.paintText(g, b, textRect, text, getTextShiftOffset());
  103. }
  104. protected void paintFocus(Graphics g, AbstractButton b,
  105. Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
  106. if (b.getParent() instanceof JToolBar) {
  107. // Windows doesn't draw the focus rect for buttons in a toolbar.
  108. return;
  109. }
  110. g.setColor(getFocusColor());
  111. BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
  112. b.getWidth() - dashedRectGapWidth,
  113. b.getHeight() - dashedRectGapHeight);
  114. }
  115. // ********************************
  116. // Layout Methods
  117. // ********************************
  118. public Dimension getPreferredSize(JComponent c) {
  119. Dimension d = super.getPreferredSize(c);
  120. /* Ensure that the width and height of the button is odd,
  121. * to allow for the focus line if focus is painted
  122. */
  123. AbstractButton b = (AbstractButton)c;
  124. if (b.isFocusPainted()) {
  125. if(d.width % 2 == 0) { d.width += 1; }
  126. if(d.height % 2 == 0) { d.height += 1; }
  127. }
  128. return d;
  129. }
  130. }