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