1. /*
  2. * @(#)BasicArrowButton.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 javax.swing.plaf.basic;
  8. import java.awt.Dimension;
  9. import java.awt.Graphics;
  10. import java.awt.Color;
  11. import javax.swing.*;
  12. /**
  13. * JButton object that draws a scaled Arrow in one of the cardinal directions.
  14. * <p>
  15. * <strong>Warning:</strong>
  16. * Serialized objects of this class will not be compatible with
  17. * future Swing releases. The current serialization support is appropriate
  18. * for short term storage or RMI between applications running the same
  19. * version of Swing. A future release of Swing will provide support for
  20. * long term persistence.
  21. *
  22. * @version 1.18 11/29/01
  23. * @author David Kloba
  24. */
  25. public class BasicArrowButton extends JButton implements SwingConstants
  26. {
  27. protected int direction;
  28. public BasicArrowButton(int direction) {
  29. super();
  30. setRequestFocusEnabled(false);
  31. setDirection(direction);
  32. setBackground(UIManager.getColor("control"));
  33. }
  34. public int getDirection() { return direction; }
  35. public void setDirection(int dir) { direction = dir; }
  36. public void paint(Graphics g) {
  37. Color origColor;
  38. boolean isPressed, isEnabled;
  39. int w, h, size;
  40. w = getSize().width;
  41. h = getSize().height;
  42. origColor = g.getColor();
  43. isPressed = getModel().isPressed();
  44. isEnabled = isEnabled();
  45. g.setColor(getBackground());
  46. g.fillRect(1, 1, w-2, h-2);
  47. /// Draw the proper Border
  48. if (isPressed) {
  49. g.setColor(UIManager.getColor("controlShadow"));
  50. g.drawRect(0, 0, w-1, h-1);
  51. } else {
  52. // Using the background color set above
  53. g.drawLine(0, 0, 0, h-1);
  54. g.drawLine(1, 0, w-2, 0);
  55. g.setColor(UIManager.getColor("controlLtHighlight")); // inner 3D border
  56. g.drawLine(1, 1, 1, h-3);
  57. g.drawLine(2, 1, w-3, 1);
  58. g.setColor(UIManager.getColor("controlShadow")); // inner 3D border
  59. g.drawLine(1, h-2, w-2, h-2);
  60. g.drawLine(w-2, 1, w-2, h-3);
  61. g.setColor(UIManager.getColor("controlDkShadow")); // black drop shadow __|
  62. g.drawLine(0, h-1, w-1, h-1);
  63. g.drawLine(w-1, h-1, w-1, 0);
  64. }
  65. // If there's no room to draw arrow, bail
  66. if(h < 5 || w < 5) {
  67. g.setColor(origColor);
  68. return;
  69. }
  70. if (isPressed) {
  71. g.translate(1, 1);
  72. }
  73. // Draw the arrow
  74. size = Math.min((h - 4) / 3, (w - 4) / 3);
  75. size = Math.max(size, 2);
  76. paintTriangle(g, (w - size) / 2, (h - size) / 2,
  77. size, direction, isEnabled);
  78. // Reset the Graphics back to it's original settings
  79. if (isPressed) {
  80. g.translate(-1, -1);
  81. }
  82. g.setColor(origColor);
  83. }
  84. public Dimension getPreferredSize() {
  85. return new Dimension(16, 16);
  86. }
  87. public Dimension getMinimumSize() {
  88. return new Dimension(5, 5);
  89. }
  90. public Dimension getMaximumSize() {
  91. return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  92. }
  93. public boolean isFocusTraversable() {
  94. return false;
  95. }
  96. public void paintTriangle(Graphics g, int x, int y, int size,
  97. int direction, boolean isEnabled) {
  98. Color oldColor = g.getColor();
  99. int mid, i, j;
  100. j = 0;
  101. size = Math.max(size, 2);
  102. mid = size / 2;
  103. g.translate(x, y);
  104. if(isEnabled)
  105. g.setColor(UIManager.getColor("controlDkShadow"));
  106. else
  107. g.setColor(UIManager.getColor("controlShadow"));
  108. switch(direction) {
  109. case NORTH:
  110. for(i = 0; i < size; i++) {
  111. g.drawLine(mid-i, i, mid+i, i);
  112. }
  113. if(!isEnabled) {
  114. g.setColor(UIManager.getColor("controlLtHighlight"));
  115. g.drawLine(mid-i+2, i, mid+i, i);
  116. }
  117. break;
  118. case SOUTH:
  119. if(!isEnabled) {
  120. g.translate(1, 1);
  121. g.setColor(UIManager.getColor("controlLtHighlight"));
  122. for(i = size-1; i >= 0; i--) {
  123. g.drawLine(mid-i, j, mid+i, j);
  124. j++;
  125. }
  126. g.translate(-1, -1);
  127. g.setColor(UIManager.getColor("controlShadow"));
  128. }
  129. j = 0;
  130. for(i = size-1; i >= 0; i--) {
  131. g.drawLine(mid-i, j, mid+i, j);
  132. j++;
  133. }
  134. break;
  135. case WEST:
  136. for(i = 0; i < size; i++) {
  137. g.drawLine(i, mid-i, i, mid+i);
  138. }
  139. if(!isEnabled) {
  140. g.setColor(UIManager.getColor("controlLtHighlight"));
  141. g.drawLine(i, mid-i+2, i, mid+i);
  142. }
  143. break;
  144. case EAST:
  145. if(!isEnabled) {
  146. g.translate(1, 1);
  147. g.setColor(UIManager.getColor("controlLtHighlight"));
  148. for(i = size-1; i >= 0; i--) {
  149. g.drawLine(j, mid-i, j, mid+i);
  150. j++;
  151. }
  152. g.translate(-1, -1);
  153. g.setColor(UIManager.getColor("controlShadow"));
  154. }
  155. j = 0;
  156. for(i = size-1; i >= 0; i--) {
  157. g.drawLine(j, mid-i, j, mid+i);
  158. j++;
  159. }
  160. break;
  161. }
  162. g.translate(-x, -y);
  163. g.setColor(oldColor);
  164. }
  165. }