1. /*
  2. * @(#)MotifScrollBarButton.java 1.18 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 com.sun.java.swing.plaf.motif;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  10. import javax.swing.plaf.*;
  11. import javax.swing.plaf.basic.BasicArrowButton;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. /**
  15. * Motif scroll bar button.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is appropriate
  20. * for short term storage or RMI between applications running the same
  21. * version of Swing. A future release of Swing will provide support for
  22. * long term persistence.
  23. */
  24. public class MotifScrollBarButton extends BasicArrowButton
  25. {
  26. private Color darkShadow = UIManager.getColor("controlShadow");
  27. private Color lightShadow = UIManager.getColor("controlLtHighlight");
  28. public MotifScrollBarButton(int direction)
  29. {
  30. super(direction);
  31. switch (direction) {
  32. case NORTH:
  33. case SOUTH:
  34. case EAST:
  35. case WEST:
  36. this.direction = direction;
  37. break;
  38. default:
  39. throw new IllegalArgumentException("invalid direction");
  40. }
  41. setRequestFocusEnabled(false);
  42. setOpaque(true);
  43. setBackground(UIManager.getColor("ScrollBar.background"));
  44. setForeground(UIManager.getColor("ScrollBar.foreground"));
  45. }
  46. public Dimension getPreferredSize() {
  47. switch (direction) {
  48. case NORTH:
  49. case SOUTH:
  50. return new Dimension(11, 12);
  51. case EAST:
  52. case WEST:
  53. default:
  54. return new Dimension(12, 11);
  55. }
  56. }
  57. public Dimension getMinimumSize() {
  58. return getPreferredSize();
  59. }
  60. public Dimension getMaximumSize() {
  61. return getPreferredSize();
  62. }
  63. public boolean isFocusTraversable() {
  64. return false;
  65. }
  66. public void paint(Graphics g)
  67. {
  68. int w = getWidth();
  69. int h = getHeight();
  70. if (isOpaque()) {
  71. g.setColor(getBackground());
  72. g.fillRect(0, 0, w, h);
  73. }
  74. boolean isPressed = getModel().isPressed();
  75. Color lead = (isPressed) ? darkShadow : lightShadow;
  76. Color trail = (isPressed) ? lightShadow : darkShadow;
  77. Color fill = getBackground();
  78. int cx = w / 2;
  79. int cy = h / 2;
  80. int s = Math.min(w, h);
  81. switch (direction) {
  82. case NORTH:
  83. g.setColor(lead);
  84. g.drawLine(cx, 0, cx, 0);
  85. for (int x = cx - 1, y = 1, dx = 1; y <= s - 2; y += 2) {
  86. g.setColor(lead);
  87. g.drawLine(x, y, x, y);
  88. if (y >= (s - 2)) {
  89. g.drawLine(x, y + 1, x, y + 1);
  90. }
  91. g.setColor(fill);
  92. g.drawLine(x + 1, y, x + dx, y);
  93. if (y < (s - 2)) {
  94. g.drawLine(x, y + 1, x + dx + 1, y + 1);
  95. }
  96. g.setColor(trail);
  97. g.drawLine(x + dx + 1, y, x + dx + 1, y);
  98. if (y >= (s - 2)) {
  99. g.drawLine(x + 1, y + 1, x + dx + 1, y + 1);
  100. }
  101. dx += 2;
  102. x -= 1;
  103. }
  104. break;
  105. case SOUTH:
  106. g.setColor(trail);
  107. g.drawLine(cx, s, cx, s);
  108. for (int x = cx - 1, y = s - 1, dx = 1; y >= 1; y -= 2) {
  109. g.setColor(lead);
  110. g.drawLine(x, y, x, y);
  111. if (y <= 2) {
  112. g.drawLine(x, y - 1, x + dx + 1, y - 1);
  113. }
  114. g.setColor(fill);
  115. g.drawLine(x + 1, y, x + dx, y);
  116. if (y > 2) {
  117. g.drawLine(x, y - 1, x + dx + 1, y - 1);
  118. }
  119. g.setColor(trail);
  120. g.drawLine(x + dx + 1, y, x + dx + 1, y);
  121. dx += 2;
  122. x -= 1;
  123. }
  124. break;
  125. case EAST:
  126. g.setColor(lead);
  127. g.drawLine(s, cy, s, cy);
  128. for (int y = cy - 1, x = s - 1, dy = 1; x >= 1; x -= 2) {
  129. g.setColor(lead);
  130. g.drawLine(x, y, x, y);
  131. if (x <= 2) {
  132. g.drawLine(x - 1, y, x - 1, y + dy + 1);
  133. }
  134. g.setColor(fill);
  135. g.drawLine(x, y + 1, x, y + dy);
  136. if (x > 2) {
  137. g.drawLine(x - 1, y, x - 1, y + dy + 1);
  138. }
  139. g.setColor(trail);
  140. g.drawLine(x, y + dy + 1, x, y + dy + 1);
  141. dy += 2;
  142. y -= 1;
  143. }
  144. break;
  145. case WEST:
  146. g.setColor(trail);
  147. g.drawLine(0, cy, 0, cy);
  148. for (int y = cy - 1, x = 1, dy = 1; x <= s - 2; x += 2) {
  149. g.setColor(lead);
  150. g.drawLine(x, y, x, y);
  151. if (x >= (s - 2)) {
  152. g.drawLine(x + 1, y, x + 1, y);
  153. }
  154. g.setColor(fill);
  155. g.drawLine(x, y + 1, x, y + dy);
  156. if (x < (s - 2)) {
  157. g.drawLine(x + 1, y, x + 1, y + dy + 1);
  158. }
  159. g.setColor(trail);
  160. g.drawLine(x, y + dy + 1, x, y + dy + 1);
  161. if (x >= (s - 2)) {
  162. g.drawLine(x + 1, y + 1, x + 1, y + dy + 1);
  163. }
  164. dy += 2;
  165. y -= 1;
  166. }
  167. break;
  168. }
  169. }
  170. }