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