1. /*
  2. * @(#)MetalScrollButton.java 1.23 03/01/23
  3. *
  4. * Copyright 2003 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. if ( !leftToRight ) {
  75. width += 1;
  76. g.translate( -1, 0 );
  77. } else {
  78. width += 2;
  79. }
  80. }
  81. // Draw the arrow
  82. g.setColor( arrowColor );
  83. int startY = ((h+1) - arrowHeight) / 2;
  84. int startX = (w / 2);
  85. // System.out.println( "startX :" + startX + " startY :"+startY);
  86. for (int line = 0; line < arrowHeight; line++) {
  87. g.drawLine( startX-line, startY+line, startX +line+1, startY+line);
  88. }
  89. /* g.drawLine( 7, 6, 8, 6 );
  90. g.drawLine( 6, 7, 9, 7 );
  91. g.drawLine( 5, 8, 10, 8 );
  92. g.drawLine( 4, 9, 11, 9 );*/
  93. if (isEnabled) {
  94. g.setColor( highlightColor );
  95. if ( !isPressed )
  96. {
  97. g.drawLine( 1, 1, width - 3, 1 );
  98. g.drawLine( 1, 1, 1, height - 1 );
  99. }
  100. g.drawLine( width - 1, 1, width - 1, height - 1 );
  101. g.setColor( shadowColor );
  102. g.drawLine( 0, 0, width - 2, 0 );
  103. g.drawLine( 0, 0, 0, height - 1 );
  104. g.drawLine( width - 2, 2, width - 2, height - 1 );
  105. } else {
  106. MetalUtils.drawDisabledBorder(g, 0, 0, width, height+1);
  107. }
  108. if ( !isFreeStanding ) {
  109. height -= 1;
  110. g.translate( 0, 1 );
  111. if ( !leftToRight ) {
  112. width -= 1;
  113. g.translate( 1, 0 );
  114. } else {
  115. width -= 2;
  116. }
  117. }
  118. }
  119. else if ( getDirection() == SOUTH )
  120. {
  121. if ( !isFreeStanding ) {
  122. height += 1;
  123. if ( !leftToRight ) {
  124. width += 1;
  125. g.translate( -1, 0 );
  126. } else {
  127. width += 2;
  128. }
  129. }
  130. // Draw the arrow
  131. g.setColor( arrowColor );
  132. int startY = (((h+1) - arrowHeight) / 2)+ arrowHeight-1;
  133. int startX = (w / 2);
  134. // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
  135. for (int line = 0; line < arrowHeight; line++) {
  136. g.drawLine( startX-line, startY-line, startX +line+1, startY-line);
  137. }
  138. /* g.drawLine( 4, 5, 11, 5 );
  139. g.drawLine( 5, 6, 10, 6 );
  140. g.drawLine( 6, 7, 9, 7 );
  141. g.drawLine( 7, 8, 8, 8 ); */
  142. if (isEnabled) {
  143. g.setColor( highlightColor );
  144. if ( !isPressed )
  145. {
  146. g.drawLine( 1, 0, width - 3, 0 );
  147. g.drawLine( 1, 0, 1, height - 3 );
  148. }
  149. g.drawLine( 1, height - 1, width - 1, height - 1 );
  150. g.drawLine( width - 1, 0, width - 1, height - 1 );
  151. g.setColor( shadowColor );
  152. g.drawLine( 0, 0, 0, height - 2 );
  153. g.drawLine( width - 2, 0, width - 2, height - 2 );
  154. g.drawLine( 2, height - 2, width - 2, height - 2 );
  155. } else {
  156. MetalUtils.drawDisabledBorder(g, 0,-1, width, height+1);
  157. }
  158. if ( !isFreeStanding ) {
  159. height -= 1;
  160. if ( !leftToRight ) {
  161. width -= 1;
  162. g.translate( 1, 0 );
  163. } else {
  164. width -= 2;
  165. }
  166. }
  167. }
  168. else if ( getDirection() == EAST )
  169. {
  170. if ( !isFreeStanding ) {
  171. height += 2;
  172. width += 1;
  173. }
  174. // Draw the arrow
  175. g.setColor( arrowColor );
  176. int startX = (((w+1) - arrowHeight) / 2) + arrowHeight-1;
  177. int startY = (h / 2);
  178. //System.out.println( "startX2 :" + startX + " startY2 :"+startY);
  179. for (int line = 0; line < arrowHeight; line++) {
  180. g.drawLine( startX-line, startY-line, startX -line, startY+line+1);
  181. }
  182. /* g.drawLine( 5, 4, 5, 11 );
  183. g.drawLine( 6, 5, 6, 10 );
  184. g.drawLine( 7, 6, 7, 9 );
  185. g.drawLine( 8, 7, 8, 8 );*/
  186. if (isEnabled) {
  187. g.setColor( highlightColor );
  188. if ( !isPressed )
  189. {
  190. g.drawLine( 0, 1, width - 3, 1 );
  191. g.drawLine( 0, 1, 0, height - 3 );
  192. }
  193. g.drawLine( width - 1, 1, width - 1, height - 1 );
  194. g.drawLine( 0, height - 1, width - 1, height - 1 );
  195. g.setColor( shadowColor );
  196. g.drawLine( 0, 0,width - 2, 0 );
  197. g.drawLine( width - 2, 2, width - 2, height - 2 );
  198. g.drawLine( 0, height - 2, width - 2, height - 2 );
  199. } else {
  200. MetalUtils.drawDisabledBorder(g,-1,0, width+1, height);
  201. }
  202. if ( !isFreeStanding ) {
  203. height -= 2;
  204. width -= 1;
  205. }
  206. }
  207. else if ( getDirection() == WEST )
  208. {
  209. if ( !isFreeStanding ) {
  210. height += 2;
  211. width += 1;
  212. g.translate( -1, 0 );
  213. }
  214. // Draw the arrow
  215. g.setColor( arrowColor );
  216. int startX = (((w+1) - arrowHeight) / 2);
  217. int startY = (h / 2);
  218. for (int line = 0; line < arrowHeight; line++) {
  219. g.drawLine( startX+line, startY-line, startX +line, startY+line+1);
  220. }
  221. /* g.drawLine( 6, 7, 6, 8 );
  222. g.drawLine( 7, 6, 7, 9 );
  223. g.drawLine( 8, 5, 8, 10 );
  224. g.drawLine( 9, 4, 9, 11 );*/
  225. if (isEnabled) {
  226. g.setColor( highlightColor );
  227. if ( !isPressed )
  228. {
  229. g.drawLine( 1, 1, width - 1, 1 );
  230. g.drawLine( 1, 1, 1, height - 3 );
  231. }
  232. g.drawLine( 1, height - 1, width - 1, height - 1 );
  233. g.setColor( shadowColor );
  234. g.drawLine( 0, 0, width - 1, 0 );
  235. g.drawLine( 0, 0, 0, height - 2 );
  236. g.drawLine( 2, height - 2, width - 1, height - 2 );
  237. } else {
  238. MetalUtils.drawDisabledBorder(g,0,0, width+1, height);
  239. }
  240. if ( !isFreeStanding ) {
  241. height -= 2;
  242. width -= 1;
  243. g.translate( 1, 0 );
  244. }
  245. }
  246. }
  247. public Dimension getPreferredSize()
  248. {
  249. if ( getDirection() == NORTH )
  250. {
  251. return new Dimension( buttonWidth, buttonWidth - 2 );
  252. }
  253. else if ( getDirection() == SOUTH )
  254. {
  255. return new Dimension( buttonWidth, buttonWidth - (isFreeStanding ? 1 : 2) );
  256. }
  257. else if ( getDirection() == EAST )
  258. {
  259. return new Dimension( buttonWidth - (isFreeStanding ? 1 : 2), buttonWidth );
  260. }
  261. else if ( getDirection() == WEST )
  262. {
  263. return new Dimension( buttonWidth - 2, buttonWidth );
  264. }
  265. else
  266. {
  267. return new Dimension( 0, 0 );
  268. }
  269. }
  270. public Dimension getMinimumSize()
  271. {
  272. return getPreferredSize();
  273. }
  274. public Dimension getMaximumSize()
  275. {
  276. return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );
  277. }
  278. public int getButtonWidth() {
  279. return buttonWidth;
  280. }
  281. }