1. /*
  2. * @(#)BorderFactory.java 1.27 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;
  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 <code>Border</code> objects. Wherever
  14. * possible, this factory will hand out references to shared
  15. * <code>Border</code> instances.
  16. * For further information and examples see
  17. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/border.html">How
  18. to Use Borders</a>,
  19. * a section in <em>The Java Tutorial</em>.
  20. *
  21. * @version 1.27 01/23/03
  22. * @author David Kloba
  23. */
  24. public class BorderFactory
  25. {
  26. /** Don't let anyone instantiate this class */
  27. private BorderFactory() {
  28. }
  29. //// LineBorder ///////////////////////////////////////////////////////////////
  30. /**
  31. * Creates a line border withe the specified color.
  32. *
  33. * @param color a <code>Color</code> to use for the line
  34. * @return the <code>Border</code> object
  35. */
  36. public static Border createLineBorder(Color color) {
  37. return new LineBorder(color, 1);
  38. }
  39. /**
  40. * Creates a line border with the specified color
  41. * and width. The width applies to all four sides of the
  42. * border. To specify widths individually for the top,
  43. * bottom, left, and right, use
  44. * {@link #createMatteBorder(int,int,int,int,Color)}.
  45. *
  46. * @param color a <code>Color</code> to use for the line
  47. * @param thickness an integer specifying the width in pixels
  48. * @return the <code>Border</code> object
  49. */
  50. public static Border createLineBorder(Color color, int thickness) {
  51. return new LineBorder(color, thickness);
  52. }
  53. // public static Border createLineBorder(Color color, int thickness,
  54. // boolean drawRounded) {
  55. // return new JLineBorder(color, thickness, drawRounded);
  56. // }
  57. //// BevelBorder /////////////////////////////////////////////////////////////
  58. ///////////////////////////////////////////////////////////////////////////////
  59. static final Border sharedRaisedBevel = new BevelBorder(BevelBorder.RAISED);
  60. static final Border sharedLoweredBevel = new BevelBorder(BevelBorder.LOWERED);
  61. /**
  62. * Creates a border with a raised beveled edge, using
  63. * brighter shades of the component's current background color
  64. * for highlighting, and darker shading for shadows.
  65. * (In a raised border, highlights are on top and shadows
  66. * are underneath.)
  67. *
  68. * @return the <code>Border</code> object
  69. */
  70. public static Border createRaisedBevelBorder() {
  71. return createSharedBevel(BevelBorder.RAISED);
  72. }
  73. /**
  74. * Creates a border with a lowered beveled edge, using
  75. * brighter shades of the component's current background color
  76. * for highlighting, and darker shading for shadows.
  77. * (In a lowered border, shadows are on top and highlights
  78. * are underneath.)
  79. *
  80. * @return the <code>Border</code> object
  81. */
  82. public static Border createLoweredBevelBorder() {
  83. return createSharedBevel(BevelBorder.LOWERED);
  84. }
  85. /**
  86. * Creates a beveled border of the specified type, using
  87. * brighter shades of the component's current background color
  88. * for highlighting, and darker shading for shadows.
  89. * (In a lowered border, shadows are on top and highlights
  90. * are underneath.)
  91. *
  92. * @param type an integer specifying either
  93. * <code>BevelBorder.LOWERED</code> or
  94. * <code>BevelBorder.RAISED</code>
  95. * @return the <code>Border</code> object
  96. */
  97. public static Border createBevelBorder(int type) {
  98. return createSharedBevel(type);
  99. }
  100. /**
  101. * Creates a beveled border of the specified type, using
  102. * the specified highlighting and shadowing. The outer
  103. * edge of the highlighted area uses a brighter shade of
  104. * the highlight color. The inner edge of the shadow area
  105. * uses a brighter shade of the shadow color.
  106. *
  107. * @param type an integer specifying either
  108. * <code>BevelBorder.LOWERED</code> or
  109. * <code>BevelBorder.RAISED</code>
  110. * @param highlight a <code>Color</code> object for highlights
  111. * @param shadow a <code>Color</code> object for shadows
  112. * @return the <code>Border</code> object
  113. */
  114. public static Border createBevelBorder(int type, Color highlight, Color shadow) {
  115. return new BevelBorder(type, highlight, shadow);
  116. }
  117. /**
  118. * Creates a beveled border of the specified type, using
  119. * the specified colors for the inner and outer highlight
  120. * and shadow areas.
  121. *
  122. * @param type an integer specifying either
  123. * <code>BevelBorder.LOWERED</code> or
  124. * <code>BevelBorder.RAISED</code>
  125. * @param highlightOuter a <code>Color</code> object for the
  126. * outer edge of the highlight area
  127. * @param highlightInner a <code>Color</code> object for the
  128. * inner edge of the highlight area
  129. * @param shadowOuter a <code>Color</code> object for the
  130. * outer edge of the shadow area
  131. * @param shadowInner a <code>Color</code> object for the
  132. * inner edge of the shadow area
  133. * @return the <code>Border</code> object
  134. */
  135. public static Border createBevelBorder(int type,
  136. Color highlightOuter, Color highlightInner,
  137. Color shadowOuter, Color shadowInner) {
  138. return new BevelBorder(type, highlightOuter, highlightInner,
  139. shadowOuter, shadowInner);
  140. }
  141. static Border createSharedBevel(int type) {
  142. if(type == BevelBorder.RAISED) {
  143. return sharedRaisedBevel;
  144. } else if(type == BevelBorder.LOWERED) {
  145. return sharedLoweredBevel;
  146. }
  147. return null;
  148. }
  149. //// EtchedBorder ///////////////////////////////////////////////////////////
  150. static final Border sharedEtchedBorder = new EtchedBorder();
  151. private static Border sharedRaisedEtchedBorder;
  152. /**
  153. * Creates a border with an "etched" look using
  154. * the component's current background color for
  155. * highlighting and shading.
  156. *
  157. * @return the <code>Border</code> object
  158. */
  159. public static Border createEtchedBorder() {
  160. return sharedEtchedBorder;
  161. }
  162. /**
  163. * Creates a border with an "etched" look using
  164. * the specified highlighting and shading colors.
  165. *
  166. * @param highlight a <code>Color</code> object for the border highlights
  167. * @param shadow a <code>Color</code> object for the border shadows
  168. * @return the <code>Border</code> object
  169. */
  170. public static Border createEtchedBorder(Color highlight, Color shadow) {
  171. return new EtchedBorder(highlight, shadow);
  172. }
  173. /**
  174. * Creates a border with an "etched" look using
  175. * the component's current background color for
  176. * highlighting and shading.
  177. *
  178. * @param type one of <code>EtchedBorder.RAISED</code>, or
  179. * <code>EtchedBorder.LOWERED</code>
  180. * @return the <code>Border</code> object
  181. * @exception IllegalArgumentException if type is not either
  182. * <code>EtchedBorder.RAISED</code> or
  183. * <code>EtchedBorder.LOWERED</code>
  184. * @since 1.3
  185. */
  186. public static Border createEtchedBorder(int type) {
  187. switch (type) {
  188. case EtchedBorder.RAISED:
  189. if (sharedRaisedEtchedBorder == null) {
  190. sharedRaisedEtchedBorder = new EtchedBorder
  191. (EtchedBorder.RAISED);
  192. }
  193. return sharedRaisedEtchedBorder;
  194. case EtchedBorder.LOWERED:
  195. return sharedEtchedBorder;
  196. default:
  197. throw new IllegalArgumentException("type must be one of EtchedBorder.RAISED or EtchedBorder.LOWERED");
  198. }
  199. }
  200. /**
  201. * Creates a border with an "etched" look using
  202. * the specified highlighting and shading colors.
  203. *
  204. * @param type one of <code>EtchedBorder.RAISED</code>, or
  205. * <code>EtchedBorder.LOWERED</code>
  206. * @param highlight a <code>Color</code> object for the border highlights
  207. * @param shadow a <code>Color</code> object for the border shadows
  208. * @return the <code>Border</code> object
  209. * @since 1.3
  210. */
  211. public static Border createEtchedBorder(int type, Color highlight,
  212. Color shadow) {
  213. return new EtchedBorder(type, highlight, shadow);
  214. }
  215. //// TitledBorder ////////////////////////////////////////////////////////////
  216. /**
  217. * Creates a new title border specifying the text of the title, using
  218. * the default border (etched), using the default text position
  219. * (sitting on the top
  220. * line) and default justification (leading) and using the default
  221. * font and text color determined by the current look and feel.
  222. *
  223. * @param title a <code>String</code> containing the text of the title
  224. * @return the <code>TitledBorder</code> object
  225. */
  226. public static TitledBorder createTitledBorder(String title) {
  227. return new TitledBorder(title);
  228. }
  229. /**
  230. * Creates a new title border with an empty title specifying the
  231. * border object, using the default text position (sitting on the top
  232. * line) and default justification (leading) and using the default
  233. * font, and text color.
  234. *
  235. * @param border the <code>Border</code> object to add the title to, if
  236. * null the <code>Border</code> is determined by the
  237. * current look and feel.
  238. * @return the <code>TitledBorder</code> object
  239. */
  240. public static TitledBorder createTitledBorder(Border border) {
  241. return new TitledBorder(border);
  242. }
  243. /**
  244. * Adds a title to an existing border, specifying the text of
  245. * the title, using the default positioning (sitting on the top
  246. * line) and default justification (leading) and using the default
  247. * font and text color determined by the current look and feel.
  248. *
  249. * @param border the <code>Border</code> object to add the title to
  250. * @param title a <code>String</code> containing the text of the title
  251. * @return the <code>TitledBorder</code> object
  252. */
  253. public static TitledBorder createTitledBorder(Border border,
  254. String title) {
  255. return new TitledBorder(border, title);
  256. }
  257. /**
  258. * Adds a title to an existing border, specifying the text of
  259. * the title along with its positioning, using the default
  260. * font and text color determined by the current look and feel.
  261. *
  262. * @param border the <code>Border</code> object to add the title to
  263. * @param title a <code>String</code> containing the text of the title
  264. * @param titleJustification an integer specifying the justification
  265. * of the title -- one of the following:
  266. *<ul>
  267. *<li><code>TitledBorder.LEFT</code>
  268. *<li><code>TitledBorder.CENTER</code>
  269. *<li><code>TitledBorder.RIGHT</code>
  270. *<li><code>TitledBorder.LEADING</code>
  271. *<li><code>TitledBorder.TRAILING<code>
  272. *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
  273. *</ul>
  274. * @param titlePosition an integer specifying the vertical position of
  275. * the text in relation to the border -- one of the following:
  276. *<ul>
  277. *<li><code> TitledBorder.ABOVE_TOP</code>
  278. *<li>TitledBorder.TOP</code> (sitting on the top line)
  279. *<li><code>TitledBorder.BELOW_TOP</code>
  280. *<li><code>TitledBorder.ABOVE_BOTTOM</code>
  281. *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
  282. *<li><code>TitledBorder.BELOW_BOTTOM</code>
  283. *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
  284. *</ul>
  285. * @return the <code>TitledBorder</code> object
  286. */
  287. public static TitledBorder createTitledBorder(Border border,
  288. String title,
  289. int titleJustification,
  290. int titlePosition) {
  291. return new TitledBorder(border, title, titleJustification,
  292. titlePosition);
  293. }
  294. /**
  295. * Adds a title to an existing border, specifying the text of
  296. * the title along with its positioning and font, using the
  297. * default text color determined by the current look and feel.
  298. *
  299. * @param border the <code>Border</code> object to add the title to
  300. * @param title a <code>String</code> containing the text of the title
  301. * @param titleJustification an integer specifying the justification
  302. * of the title -- one of the following:
  303. *<ul>
  304. *<li><code>TitledBorder.LEFT</code>
  305. *<li><code>TitledBorder.CENTER</code>
  306. *<li><code>TitledBorder.RIGHT</code>
  307. *<li><code>TitledBorder.LEADING</code>
  308. *<li><code>TitledBorder.TRAILING<code>
  309. *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
  310. *</ul>
  311. * @param titlePosition an integer specifying the vertical position of
  312. * the text in relation to the border -- one of the following:
  313. *<ul>
  314. *<li><code> TitledBorder.ABOVE_TOP</code>
  315. *<li>TitledBorder.TOP</code> (sitting on the top line)
  316. *<li><code>TitledBorder.BELOW_TOP</code>
  317. *<li><code>TitledBorder.ABOVE_BOTTOM</code>
  318. *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
  319. *<li><code>TitledBorder.BELOW_BOTTOM</code>
  320. *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
  321. *</ul>
  322. * @param titleFont a Font object specifying the title font
  323. * @return the TitledBorder object
  324. */
  325. public static TitledBorder createTitledBorder(Border border,
  326. String title,
  327. int titleJustification,
  328. int titlePosition,
  329. Font titleFont) {
  330. return new TitledBorder(border, title, titleJustification,
  331. titlePosition, titleFont);
  332. }
  333. /**
  334. * Adds a title to an existing border, specifying the text of
  335. * the title along with its positioning, font, and color.
  336. *
  337. * @param border the <code>Border</code> object to add the title to
  338. * @param title a <code>String</code> containing the text of the title
  339. * @param titleJustification an integer specifying the justification
  340. * of the title -- one of the following:
  341. *<ul>
  342. *<li><code>TitledBorder.LEFT</code>
  343. *<li><code>TitledBorder.CENTER</code>
  344. *<li><code>TitledBorder.RIGHT</code>
  345. *<li><code>TitledBorder.LEADING</code>
  346. *<li><code>TitledBorder.TRAILING<code>
  347. *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
  348. *</ul>
  349. * @param titlePosition an integer specifying the vertical position of
  350. * the text in relation to the border -- one of the following:
  351. *<ul>
  352. *<li><code> TitledBorder.ABOVE_TOP</code>
  353. *<li>TitledBorder.TOP</code> (sitting on the top line)
  354. *<li><code>TitledBorder.BELOW_TOP</code>
  355. *<li><code>TitledBorder.ABOVE_BOTTOM</code>
  356. *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
  357. *<li><code>TitledBorder.BELOW_BOTTOM</code>
  358. *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
  359. *</ul>
  360. * @param titleFont a <code>Font</code> object specifying the title font
  361. * @param titleColor a <code>Color</code> object specifying the title color
  362. * @return the <code>TitledBorder</code> object
  363. */
  364. public static TitledBorder createTitledBorder(Border border,
  365. String title,
  366. int titleJustification,
  367. int titlePosition,
  368. Font titleFont,
  369. Color titleColor) {
  370. return new TitledBorder(border, title, titleJustification,
  371. titlePosition, titleFont, titleColor);
  372. }
  373. //// EmptyBorder ///////////////////////////////////////////////////////////
  374. final static Border emptyBorder = new EmptyBorder(0, 0, 0, 0);
  375. /**
  376. * Creates an empty border that takes up no space. (The width
  377. * of the top, bottom, left, and right sides are all zero.)
  378. *
  379. * @return the <code>Border</code> object
  380. */
  381. public static Border createEmptyBorder() {
  382. return emptyBorder;
  383. }
  384. /**
  385. * Creates an empty border that takes up space but which does
  386. * no drawing, specifying the width of the top, left, bottom, and
  387. * right sides.
  388. *
  389. * @param top an integer specifying the width of the top,
  390. * in pixels
  391. * @param left an integer specifying the width of the left side,
  392. * in pixels
  393. * @param bottom an integer specifying the width of the right side,
  394. * in pixels
  395. * @param right an integer specifying the width of the bottom,
  396. * in pixels
  397. * @return the <code>Border</code> object
  398. */
  399. public static Border createEmptyBorder(int top, int left,
  400. int bottom, int right) {
  401. return new EmptyBorder(top, left, bottom, right);
  402. }
  403. //// CompoundBorder ////////////////////////////////////////////////////////
  404. /**
  405. * Creates a compound border with a <code>null</code> inside edge and a
  406. * <code>null</code> outside edge.
  407. *
  408. * @return the <code>CompoundBorder</code> object
  409. */
  410. public static CompoundBorder createCompoundBorder() {
  411. return new CompoundBorder();
  412. }
  413. /**
  414. * Creates a compound border specifying the border objects to use
  415. * for the outside and inside edges.
  416. *
  417. * @param outsideBorder a <code>Border</code> object for the outer
  418. * edge of the compound border
  419. * @param insideBorder a <code>Border</code> object for the inner
  420. * edge of the compound border
  421. * @return the <code>CompoundBorder</code> object
  422. */
  423. public static CompoundBorder createCompoundBorder(Border outsideBorder,
  424. Border insideBorder) {
  425. return new CompoundBorder(outsideBorder, insideBorder);
  426. }
  427. //// MatteBorder ////////////////////////////////////////////////////////
  428. /**
  429. * Creates a matte-look border using a solid color. (The difference between
  430. * this border and a line border is that you can specify the individual
  431. * border dimensions.)
  432. *
  433. * @param top an integer specifying the width of the top,
  434. * in pixels
  435. * @param left an integer specifying the width of the left side,
  436. * in pixels
  437. * @param bottom an integer specifying the width of the right side,
  438. * in pixels
  439. * @param right an integer specifying the width of the bottom,
  440. * in pixels
  441. * @param color a <code>Color</code> to use for the border
  442. * @return the <code>MatteBorder</code> object
  443. */
  444. public static MatteBorder createMatteBorder(int top, int left, int bottom, int right,
  445. Color color) {
  446. return new MatteBorder(top, left, bottom, right, color);
  447. }
  448. /**
  449. * Creates a matte-look border that consists of multiple tiles of a
  450. * specified icon. Multiple copies of the icon are placed side-by-side
  451. * to fill up the border area.
  452. * <p>
  453. * Note:<br>
  454. * If the icon doesn't load, the border area is painted gray.
  455. *
  456. * @param top an integer specifying the width of the top,
  457. * in pixels
  458. * @param left an integer specifying the width of the left side,
  459. * in pixels
  460. * @param bottom an integer specifying the width of the right side,
  461. * in pixels
  462. * @param right an integer specifying the width of the bottom,
  463. * in pixels
  464. * @param tileIcon the <code>Icon</code> object used for the border tiles
  465. * @return the <code>MatteBorder</code> object
  466. */
  467. public static MatteBorder createMatteBorder(int top, int left, int bottom, int right,
  468. Icon tileIcon) {
  469. return new MatteBorder(top, left, bottom, right, tileIcon);
  470. }
  471. }