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