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