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