1. /*
  2. * @(#)MetalScrollButton.java 1.16 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.metal;
  8. import java.awt.Dimension;
  9. import java.awt.Graphics;
  10. import java.awt.Color;
  11. import java.awt.Polygon;
  12. import javax.swing.*;
  13. import javax.swing.plaf.basic.BasicArrowButton;
  14. /**
  15. * JButton object for Metal scrollbar arrows.
  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. * @version 1.16 11/29/01
  25. * @author Tom Santos
  26. * @author Steve Wilson
  27. */
  28. public class MetalScrollButton extends BasicArrowButton
  29. {
  30. private static Color shadowColor;
  31. private static Color highlightColor;
  32. private boolean isFreeStanding = false;
  33. private int buttonWidth;
  34. public MetalScrollButton( int direction, int width, boolean freeStanding )
  35. {
  36. super( direction );
  37. shadowColor = UIManager.getColor("ScrollBar.darkShadow");
  38. highlightColor = UIManager.getColor("ScrollBar.highlight");
  39. buttonWidth = width;
  40. isFreeStanding = freeStanding;
  41. }
  42. public void setFreeStanding( boolean freeStanding )
  43. {
  44. isFreeStanding = freeStanding;
  45. }
  46. public void paint( Graphics g )
  47. {
  48. boolean isEnabled = getParent().isEnabled();
  49. Color arrowColor = isEnabled ? MetalLookAndFeel.getControlInfo() : MetalLookAndFeel.getControlDisabled();
  50. boolean isPressed = getModel().isPressed();
  51. int width = getWidth();
  52. int height = getHeight();
  53. int w = width;
  54. int h = height;
  55. int arrowHeight = (height+1) / 4;
  56. int arrowWidth = (height+1) / 2;
  57. if ( isPressed )
  58. {
  59. g.setColor( MetalLookAndFeel.getControlShadow() );
  60. }
  61. else
  62. {
  63. g.setColor( getBackground() );
  64. }
  65. g.fillRect( 0, 0, width, height );
  66. if ( getDirection() == NORTH )
  67. {
  68. if ( !isFreeStanding ) {
  69. width += 2;
  70. }
  71. // Draw the arrow
  72. g.setColor( arrowColor );
  73. int startY = ((h+1) - arrowHeight) / 2;
  74. int startX = (w / 2);
  75. // System.out.println( "startX :" + startX + " startY :"+startY);
  76. for (int line = 0; line < arrowHeight; line++) {
  77. g.drawLine( startX-line, startY+line, startX +line+1, startY+line);
  78. }
  79. /* g.drawLine( 7, 6, 8, 6 );
  80. g.drawLine( 6, 7, 9, 7 );
  81. g.drawLine( 5, 8, 10, 8 );
  82. g.drawLine( 4, 9, 11, 9 );*/
  83. if (isEnabled) {
  84. g.setColor( highlightColor );
  85. if ( !isPressed )
  86. {
  87. g.drawLine( 1, 1, width - 3, 1 );
  88. g.drawLine( 1, 1, 1, height - 1 );
  89. }
  90. g.drawLine( width - 1, 1, width - 1, height - 1 );
  91. g.setColor( shadowColor );
  92. g.drawLine( 0, 0, width - 2, 0 );
  93. g.drawLine( 0, 0, 0, height - 1 );
  94. g.drawLine( width - 2, 2, width - 2, height - 1 );
  95. } else {
  96. MetalUtils.drawDisabledBorder(g, 0, 0, width, height+1);
  97. }
  98. if ( !isFreeStanding ) {
  99. width -= 2;
  100. }
  101. }
  102. else if ( getDirection() == SOUTH )
  103. {
  104. if ( !isFreeStanding ) {
  105. width += 2;
  106. height += 1;
  107. }
  108. // Draw the arrow
  109. g.setColor( arrowColor );
  110. int startY = (((h+1) - arrowHeight) / 2)+ arrowHeight-1;
  111. int startX = (w / 2);
  112. // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
  113. for (int line = 0; line < arrowHeight; line++) {
  114. g.drawLine( startX-line, startY-line, startX +line+1, startY-line);
  115. }
  116. /* g.drawLine( 4, 5, 11, 5 );
  117. g.drawLine( 5, 6, 10, 6 );
  118. g.drawLine( 6, 7, 9, 7 );
  119. g.drawLine( 7, 8, 8, 8 ); */
  120. if (isEnabled) {
  121. g.setColor( highlightColor );
  122. if ( !isPressed )
  123. {
  124. g.drawLine( 1, 0, width - 3, 0 );
  125. g.drawLine( 1, 0, 1, height - 3 );
  126. }
  127. g.drawLine( 1, height - 1, width - 1, height - 1 );
  128. g.drawLine( width - 1, 0, width - 1, height - 1 );
  129. g.setColor( shadowColor );
  130. g.drawLine( 0, 0, 0, height - 2 );
  131. g.drawLine( width - 2, 0, width - 2, height - 2 );
  132. g.drawLine( 2, height - 2, width - 2, height - 2 );
  133. } else {
  134. MetalUtils.drawDisabledBorder(g, 0,-1, width, height+1);
  135. }
  136. if ( !isFreeStanding ) {
  137. width -= 2;
  138. height -= 1;
  139. }
  140. }
  141. else if ( getDirection() == EAST )
  142. {
  143. if ( !isFreeStanding ) {
  144. height += 2;
  145. width += 1;
  146. }
  147. // Draw the arrow
  148. g.setColor( arrowColor );
  149. int startX = (((w+1) - arrowHeight) / 2) + arrowHeight-1;
  150. int startY = (h / 2);
  151. //System.out.println( "startX2 :" + startX + " startY2 :"+startY);
  152. for (int line = 0; line < arrowHeight; line++) {
  153. g.drawLine( startX-line, startY-line, startX -line, startY+line+1);
  154. }
  155. /* g.drawLine( 5, 4, 5, 11 );
  156. g.drawLine( 6, 5, 6, 10 );
  157. g.drawLine( 7, 6, 7, 9 );
  158. g.drawLine( 8, 7, 8, 8 );*/
  159. if (isEnabled) {
  160. g.setColor( highlightColor );
  161. if ( !isPressed )
  162. {
  163. g.drawLine( 0, 1, width - 3, 1 );
  164. g.drawLine( 0, 1, 0, height - 3 );
  165. }
  166. g.drawLine( width - 1, 1, width - 1, height - 1 );
  167. g.drawLine( 0, height - 1, width - 1, height - 1 );
  168. g.setColor( shadowColor );
  169. g.drawLine( 0, 0,width - 2, 0 );
  170. g.drawLine( width - 2, 2, width - 2, height - 2 );
  171. g.drawLine( 0, height - 2, width - 2, height - 2 );
  172. } else {
  173. MetalUtils.drawDisabledBorder(g,-1,0, width+1, height);
  174. }
  175. if ( !isFreeStanding ) {
  176. height -= 2;
  177. width -= 1;
  178. }
  179. }
  180. else if ( getDirection() == WEST )
  181. {
  182. if ( !isFreeStanding ) {
  183. height += 2;
  184. width += 1;
  185. g.translate( -1, 0 );
  186. }
  187. // Draw the arrow
  188. g.setColor( arrowColor );
  189. int startX = (((w+1) - arrowHeight) / 2);
  190. int startY = (h / 2);
  191. for (int line = 0; line < arrowHeight; line++) {
  192. g.drawLine( startX+line, startY-line, startX +line, startY+line+1);
  193. }
  194. /* g.drawLine( 6, 7, 6, 8 );
  195. g.drawLine( 7, 6, 7, 9 );
  196. g.drawLine( 8, 5, 8, 10 );
  197. g.drawLine( 9, 4, 9, 11 );*/
  198. if (isEnabled) {
  199. g.setColor( highlightColor );
  200. if ( !isPressed )
  201. {
  202. g.drawLine( 1, 1, width - 1, 1 );
  203. g.drawLine( 1, 1, 1, height - 3 );
  204. }
  205. g.drawLine( 1, height - 1, width - 1, height - 1 );
  206. g.setColor( shadowColor );
  207. g.drawLine( 0, 0, width - 1, 0 );
  208. g.drawLine( 0, 0, 0, height - 2 );
  209. g.drawLine( 2, height - 2, width - 1, height - 2 );
  210. } else {
  211. MetalUtils.drawDisabledBorder(g,0,0, width+1, height);
  212. }
  213. if ( !isFreeStanding ) {
  214. height -= 2;
  215. width -= 1;
  216. g.translate( 1, 0 );
  217. }
  218. }
  219. }
  220. public Dimension getPreferredSize()
  221. {
  222. if ( getDirection() == NORTH )
  223. {
  224. return new Dimension( buttonWidth, buttonWidth - 2 );
  225. }
  226. else if ( getDirection() == SOUTH )
  227. {
  228. return new Dimension( buttonWidth, buttonWidth - (isFreeStanding ? 1 : 2) );
  229. }
  230. else if ( getDirection() == EAST )
  231. {
  232. return new Dimension( buttonWidth - (isFreeStanding ? 1 : 2), buttonWidth );
  233. }
  234. else if ( getDirection() == WEST )
  235. {
  236. return new Dimension( buttonWidth - 2, buttonWidth );
  237. }
  238. else
  239. {
  240. return new Dimension( 0, 0 );
  241. }
  242. }
  243. public Dimension getMinimumSize()
  244. {
  245. return getPreferredSize();
  246. }
  247. public Dimension getMaximumSize()
  248. {
  249. return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );
  250. }
  251. public int getButtonWidth() {
  252. return buttonWidth;
  253. }
  254. }