1. /*
  2. * @(#)BorderFactory.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;
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import javax.swing.JComponent;
  11. import javax.swing.border.*;
  12. /**
  13. * Factory class for vending standard Border objects. Whereever
  14. * possible, this factory will hand out references to shared
  15. * Border instances.
  16. *
  17. * @version 1.16 11/29/01
  18. * @author David Kloba
  19. */
  20. public class BorderFactory
  21. {
  22. /** Don't let anyone instantiate this class */
  23. private BorderFactory() {
  24. }
  25. //// LineBorder ///////////////////////////////////////////////////////////////
  26. /**
  27. * Creates a line border withe the specified color.
  28. *
  29. * @param color a Color to use for the line
  30. * @return the Border object
  31. */
  32. public static Border createLineBorder(Color color) {
  33. return new LineBorder(color, 1);
  34. }
  35. /**
  36. * Creates a line border withe the specified color
  37. * and width. The width applies to all 4 sides of the
  38. * border. To specify widths individually for the top,
  39. * bottom, left, and right, use
  40. * {@link #createMatteBorder(int,int,int,int,Color)}.
  41. *
  42. * @param color a Color to use for the line
  43. * @param thickness an int specifying the width in pixels
  44. * @return the Border object
  45. */
  46. public static Border createLineBorder(Color color, int thickness) {
  47. return new LineBorder(color, thickness);
  48. }
  49. // public static Border createLineBorder(Color color, int thickness,
  50. // boolean drawRounded) {
  51. // return new JLineBorder(color, thickness, drawRounded);
  52. // }
  53. //// BevelBorder /////////////////////////////////////////////////////////////
  54. ///////////////////////////////////////////////////////////////////////////////
  55. static final Border sharedRaisedBevel = new BevelBorder(BevelBorder.RAISED);
  56. static final Border sharedLoweredBevel = new BevelBorder(BevelBorder.LOWERED);
  57. /**
  58. * Created a border with a raised beveled edge, using
  59. * brighter shades of the component's current background color
  60. * for highlighting, and darker shading for shadows.
  61. * (In a raised border, highlights are on top and shadows
  62. * are underneath.)
  63. *
  64. * @return the Border object
  65. */
  66. public static Border createRaisedBevelBorder() {
  67. return createSharedBevel(BevelBorder.RAISED);
  68. }
  69. /**
  70. * Created a border with a lowered beveled edge, using
  71. * brighter shades of the component's current background color
  72. * for highlighting, and darker shading for shadows.
  73. * (In a lowered border, shadows are on top and highlights
  74. * are underneath.)
  75. *
  76. * @return the Border object
  77. */
  78. public static Border createLoweredBevelBorder() {
  79. return createSharedBevel(BevelBorder.LOWERED);
  80. }
  81. /**
  82. * Create a beveled border of the specified type, using
  83. * brighter shades of the component's current background color
  84. * for highlighting, and darker shading for shadows.
  85. * (In a lowered border, shadows are on top and highlights
  86. * are underneath.).
  87. *
  88. * @param type an int specifying either BevelBorder.LOWERED
  89. * or BevelBorder.LOWERED
  90. * @return the Border object
  91. */
  92. public static Border createBevelBorder(int type) {
  93. return createSharedBevel(type);
  94. }
  95. /**
  96. * Create a beveled border of the specified type, using
  97. * the specified highlighting and shadowing. The outer
  98. * edge of the highlighted area uses a brighter shade of
  99. * the highlight color. The inner edge of the shadow area
  100. * uses a brighter shade of the shadaw color.
  101. *
  102. * @param type an int specifying either BevelBorder.LOWERED
  103. * or BevelBorder.LOWERED
  104. * @param highlight a Color object for highlights
  105. * @param shadow a Color object for shadows
  106. * @return the Border object
  107. */
  108. public static Border createBevelBorder(int type, Color highlight, Color shadow) {
  109. return new BevelBorder(type, highlight, shadow);
  110. }
  111. /**
  112. * Create a beveled border of the specified type, using
  113. * the specified colors for the inner and outer highlight
  114. * and shadow areas.
  115. *
  116. * @param type an int specifying either BevelBorder.LOWERED
  117. * or BevelBorder.LOWERED
  118. * @param highlightOuter a Color object for the outer edge of the highlight area
  119. * @param highlightInner a Color object for the inner edge of the highlight area
  120. * @param shadowOuter a Color object for the outer edge of the shadow area
  121. * @param shadowInner a Color object for the inner edge of the shadow area
  122. * @return the Border object
  123. */
  124. public static Border createBevelBorder(int type,
  125. Color highlightOuter, Color highlightInner,
  126. Color shadowOuter, Color shadowInner) {
  127. return new BevelBorder(type, highlightOuter, highlightInner,
  128. shadowOuter, shadowInner);
  129. }
  130. static Border createSharedBevel(int type) {
  131. if(type == BevelBorder.RAISED) {
  132. return sharedRaisedBevel;
  133. } else if(type == BevelBorder.LOWERED) {
  134. return sharedLoweredBevel;
  135. }
  136. return null;
  137. }
  138. //// EtchedBorder ///////////////////////////////////////////////////////////
  139. static final Border sharedEtchedBorder = new EtchedBorder();
  140. /**
  141. * Create a border with an "etched" look using
  142. * the component's current background color for
  143. * highlighting and shading.
  144. *
  145. * @return the Border object
  146. */
  147. public static Border createEtchedBorder() {
  148. return sharedEtchedBorder;
  149. }
  150. /**
  151. * Create a border with an "etched" look using
  152. * the specified highlighting and shading colors.
  153. *
  154. * @param highlight a Color object for the border highlights
  155. * @param shadow a Color object for the border shadows
  156. * @return the Border object
  157. */
  158. public static Border createEtchedBorder(Color highlight, Color shadow) {
  159. return new EtchedBorder(highlight, shadow);
  160. }
  161. //// TitledBorder ////////////////////////////////////////////////////////////
  162. /**
  163. * Create a new title border specifying the text of the title, using
  164. * the default border (etched), using the default text position (sitting on the top
  165. * line) and default justification (left) and using the default
  166. * font and text color determined by the current look and feel.
  167. *
  168. * @param title a String containing the text of the title
  169. * @return the TitledBorder object
  170. */
  171. public static TitledBorder createTitledBorder(String title) {
  172. return new TitledBorder(title);
  173. }
  174. /**
  175. * Create a new title border with an empty title specifying the
  176. * border object, using the default text position (sitting on the top
  177. * line) and default justification (left) and using the default
  178. * font, text color, and border determined by the current look and feel.
  179. * (The Motif and Windows look and feels use an etched border;
  180. * The Java look and feel use a gray border.)
  181. *
  182. * @param border the Border object to add the title to
  183. * @return the TitledBorder object
  184. */
  185. public static TitledBorder createTitledBorder(Border border) {
  186. return new TitledBorder(border);
  187. }
  188. /**
  189. * Add a title to an existing border, specifying the text of
  190. * the title, using the default positioning (sitting on the top
  191. * line) and default justification (left) and using the default
  192. * font and text color determined by the current look and feel.
  193. *
  194. * @param border the Border object to add the title to
  195. * @param title a String containing the text of the title
  196. * @return the TitledBorder object
  197. */
  198. public static TitledBorder createTitledBorder(Border border,
  199. String title) {
  200. return new TitledBorder(border, title);
  201. }
  202. /**
  203. * Add a title to an existing border, specifying the text of
  204. * the title along with its positioning, using the default
  205. * font and text color determined by the current look and feel.
  206. *
  207. * @param border the Border object to add the title to
  208. * @param title a String containing the text of the title
  209. * @param titleJustification an int specifying the left/right position
  210. * of the title -- one of TitledBorder.LEFT, TitledBorder.CENTER,
  211. * or TitledBorder.RIGHT, TitledBorder.DEFAULT_JUSTIFICATION (left).
  212. * @param titlePosition an int specifying the vertical position of
  213. * the text in relation to the border -- one of: TitledBorder.ABOVE_TOP, TitledBorder.TOP
  214. * (sitting on the top line), TitledBorder.BELOW_TOP, TitledBorder.ABOVE_BOTTOM, TitledBorder.BOTTOM
  215. * (sitting on the bottom line), TitledBorder.BELOW_BOTTOM, or
  216. * TitledBorder.DEFAULT_POSITION (top).
  217. * @return the TitledBorder object
  218. */
  219. public static TitledBorder createTitledBorder(Border border,
  220. String title,
  221. int titleJustification,
  222. int titlePosition) {
  223. return new TitledBorder(border, title, titleJustification,
  224. titlePosition);
  225. }
  226. /**
  227. * Add a title to an existing border, specifying the text of
  228. * the title along with its positioning and font, using the
  229. * default text color determined by the current look and feel.
  230. *
  231. * @param border the Border object to add the title to
  232. * @param title a String containing the text of the title
  233. * @param titleJustification an int specifying the left/right position
  234. * of the title -- one of TitledBorder.LEFT, TitledBorder.CENTER,
  235. * or TitledBorder.RIGHT, TitledBorder.DEFAULT_JUSTIFICATION (left).
  236. * @param titlePosition an int specifying the vertical position of
  237. * the text in relation to the border -- one of: TitledBorder.ABOVE_TOP, TitledBorder.TOP
  238. * (sitting on the top line), TitledBorder.BELOW_TOP, TitledBorder.ABOVE_BOTTOM, TitledBorder.BOTTOM
  239. * (sitting on the bottom line), TitledBorder.BELOW_BOTTOM, or
  240. * TitledBorder.DEFAULT_POSITION (top).
  241. * @param titleFont a Font object specifying the title font
  242. * @return the TitledBorder object
  243. */
  244. public static TitledBorder createTitledBorder(Border border,
  245. String title,
  246. int titleJustification,
  247. int titlePosition,
  248. Font titleFont) {
  249. return new TitledBorder(border, title, titleJustification,
  250. titlePosition, titleFont);
  251. }
  252. /**
  253. * Add a title to an existing border, specifying the text of
  254. * the title along with its positioning, font, and color.
  255. *
  256. * @param border the Border object to add the title to
  257. * @param title a String containing the text of the title
  258. * @param titleJustification an int specifying the left/right position
  259. * of the title -- one of TitledBorder.LEFT, TitledBorder.CENTER,
  260. * or TitledBorder.RIGHT, TitledBorder.DEFAULT_JUSTIFICATION (left).
  261. * @param titlePosition an int specifying the vertical position of
  262. * the text in relation to the border -- one of: TitledBorder.ABOVE_TOP, TitledBorder.TOP
  263. * (sitting on the top line), TitledBorder.BELOW_TOP, TitledBorder.ABOVE_BOTTOM, TitledBorder.BOTTOM
  264. * (sitting on the bottom line), TitledBorder.BELOW_BOTTOM, or
  265. * TitledBorder.DEFAULT_POSITION (top).
  266. * @param titleFont a Font object specifying the title font
  267. * @param titleColor a Color object specifying the title color
  268. * @return the TitledBorder object
  269. */
  270. public static TitledBorder createTitledBorder(Border border,
  271. String title,
  272. int titleJustification,
  273. int titlePosition,
  274. Font titleFont,
  275. Color titleColor) {
  276. return new TitledBorder(border, title, titleJustification,
  277. titlePosition, titleFont, titleColor);
  278. }
  279. //// EmptyBorder ///////////////////////////////////////////////////////////
  280. final static Border emptyBorder = new EmptyBorder(0, 0, 0, 0);
  281. /**
  282. * Creates an empty border that takes up no space. (The width
  283. * of the top, bottom, left, and right sides are all zero.)
  284. *
  285. * @return the Border object
  286. */
  287. public static Border createEmptyBorder() {
  288. return emptyBorder;
  289. }
  290. /**
  291. * Creates an empty border that takes up no space but which does
  292. * no drawing, specifying the width of the top, left, bottom, and
  293. * right sides.
  294. *
  295. * @param top an int specifying the width of the top in pixels
  296. * @param left an int specifying the width of the left side in pixels
  297. * @param bottom an int specifying the width of the right side in pixels
  298. * @param right an int specifying the width of the bottom in pixels
  299. * @return the Border object
  300. */
  301. public static Border createEmptyBorder(int top, int left,
  302. int bottom, int right) {
  303. return new EmptyBorder(top, left, bottom, right);
  304. }
  305. //// CompoundBorder ////////////////////////////////////////////////////////
  306. /**
  307. * Create a compound border with a null inside edge and a null
  308. * outside edge.
  309. *
  310. * @return the CompoundBorder object
  311. */
  312. public static CompoundBorder createCompoundBorder() {
  313. return new CompoundBorder();
  314. }
  315. /**
  316. * Create a compound border specifying the border objects to use
  317. * for the outside and inside edges.
  318. *
  319. * @param outsideBorder a Border object for the outer edge of the compound border
  320. * @param insideBorder a Border object for the inner edge of the compound border
  321. * @return the CompoundBorder object
  322. */
  323. public static CompoundBorder createCompoundBorder(Border outsideBorder,
  324. Border insideBorder) {
  325. return new CompoundBorder(outsideBorder, insideBorder);
  326. }
  327. //// MatteBorder ////////////////////////////////////////////////////////
  328. /**
  329. * Create a matte-look border using a solid color. (The difference between
  330. * this border and a line border is that you can specify the individual
  331. * border dimensions.)
  332. *
  333. * @param top an int specifying the width of the top in pixels
  334. * @param left an int specifying the width of the left side in pixels
  335. * @param bottom an int specifying the width of the right side in pixels
  336. * @param right an int specifying the width of the bottom in pixels
  337. * @param color a Color to use for the border
  338. * @return the MatteBorder object
  339. */
  340. public static MatteBorder createMatteBorder(int top, int left, int bottom, int right,
  341. Color color) {
  342. return new MatteBorder(top, left, bottom, right, color);
  343. }
  344. /**
  345. * Create a matte-look border that consists of multiple tiles of a
  346. * specified icon. Multiple copies of the icon are placed side-by-side
  347. * to fill up the border area.
  348. * <p>
  349. * Note:<br>
  350. * If the icon doesn't load, the border area is painted gray.
  351. *
  352. * @param top an int specifying the width of the top in pixels
  353. * @param left an int specifying the width of the left side in pixels
  354. * @param bottom an int specifying the width of the right side in pixels
  355. * @param right an int specifying the width of the bottom in pixels
  356. * @param tileIcon the Icon object used for the border tiles
  357. * @return the MatteBorder object
  358. */
  359. public static MatteBorder createMatteBorder(int top, int left, int bottom, int right,
  360. Icon tileIcon) {
  361. return new MatteBorder(top, left, bottom, right, tileIcon);
  362. }
  363. }