1. /*
  2. * @(#)MetalBorders.java 1.30 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 javax.swing.*;
  9. import javax.swing.border.*;
  10. import javax.swing.plaf.*;
  11. import javax.swing.plaf.basic.BasicBorders;
  12. import javax.swing.text.JTextComponent;
  13. import java.awt.Component;
  14. import java.awt.Insets;
  15. import java.awt.Dimension;
  16. import java.awt.Rectangle;
  17. import java.awt.Color;
  18. import java.awt.Dialog;
  19. import java.awt.Frame;
  20. import java.awt.Graphics;
  21. import java.awt.Window;
  22. import java.io.Serializable;
  23. /**
  24. * Factory object that can vend Borders appropriate for the metal L & F.
  25. * @author Steve Wilson
  26. * @version 1.30 01/23/03
  27. */
  28. public class MetalBorders {
  29. public static class Flush3DBorder extends AbstractBorder implements UIResource{
  30. private static final Insets insets = new Insets(2, 2, 2, 2);
  31. public void paintBorder(Component c, Graphics g, int x, int y,
  32. int w, int h) {
  33. if (c.isEnabled()) {
  34. MetalUtils.drawFlush3DBorder(g, x, y, w, h);
  35. } else {
  36. MetalUtils.drawDisabledBorder(g, x, y, w, h);
  37. }
  38. }
  39. public Insets getBorderInsets(Component c) {
  40. return insets;
  41. }
  42. public Insets getBorderInsets(Component c, Insets newInsets) {
  43. newInsets.top = insets.top;
  44. newInsets.left = insets.left;
  45. newInsets.bottom = insets.bottom;
  46. newInsets.right = insets.right;
  47. return newInsets;
  48. }
  49. }
  50. public static class ButtonBorder extends AbstractBorder implements UIResource {
  51. protected static Insets borderInsets = new Insets( 3, 3, 3, 3 );
  52. public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  53. AbstractButton button = (AbstractButton)c;
  54. ButtonModel model = button.getModel();
  55. if ( model.isEnabled() ) {
  56. boolean isPressed = model.isPressed() && model.isArmed();
  57. boolean isDefault = (button instanceof JButton && ((JButton)button).isDefaultButton());
  58. if (isPressed && isDefault) {
  59. MetalUtils.drawDefaultButtonPressedBorder(g, x, y, w, h);
  60. } else if (isPressed) {
  61. MetalUtils.drawPressed3DBorder( g, x, y, w, h );
  62. } else if (isDefault) {
  63. MetalUtils.drawDefaultButtonBorder( g, x, y, w, h, false);
  64. } else {
  65. MetalUtils.drawButtonBorder( g, x, y, w, h, false);
  66. }
  67. } else { // disabled state
  68. MetalUtils.drawDisabledBorder( g, x, y, w-1, h-1 );
  69. }
  70. }
  71. public Insets getBorderInsets( Component c ) {
  72. return borderInsets;
  73. }
  74. public Insets getBorderInsets(Component c, Insets newInsets) {
  75. newInsets.top = borderInsets.top;
  76. newInsets.left = borderInsets.left;
  77. newInsets.bottom = borderInsets.bottom;
  78. newInsets.right = borderInsets.right;
  79. return newInsets;
  80. }
  81. }
  82. public static class InternalFrameBorder extends AbstractBorder implements UIResource {
  83. private static final Insets insets = new Insets(5, 5, 5, 5);
  84. private static final int corner = 14;
  85. public void paintBorder(Component c, Graphics g, int x, int y,
  86. int w, int h) {
  87. Color background;
  88. Color highlight;
  89. Color shadow;
  90. if (c instanceof JInternalFrame && ((JInternalFrame)c).isSelected()) {
  91. background = MetalLookAndFeel.getPrimaryControlDarkShadow();
  92. highlight = MetalLookAndFeel.getPrimaryControlShadow();
  93. shadow = MetalLookAndFeel.getPrimaryControlInfo();
  94. } else {
  95. background = MetalLookAndFeel.getControlDarkShadow();
  96. highlight = MetalLookAndFeel.getControlShadow();
  97. shadow = MetalLookAndFeel.getControlInfo();
  98. }
  99. g.setColor(background);
  100. // Draw outermost lines
  101. g.drawLine( 1, 0, w-2, 0);
  102. g.drawLine( 0, 1, 0, h-2);
  103. g.drawLine( w-1, 1, w-1, h-2);
  104. g.drawLine( 1, h-1, w-2, h-1);
  105. // Draw the bulk of the border
  106. for (int i = 1; i < 5; i++) {
  107. g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);
  108. }
  109. if (c instanceof JInternalFrame &&
  110. ((JInternalFrame)c).isResizable()) {
  111. g.setColor(highlight);
  112. // Draw the Long highlight lines
  113. g.drawLine( corner+1, 3, w-corner, 3);
  114. g.drawLine( 3, corner+1, 3, h-corner);
  115. g.drawLine( w-2, corner+1, w-2, h-corner);
  116. g.drawLine( corner+1, h-2, w-corner, h-2);
  117. g.setColor(shadow);
  118. // Draw the Long shadow lines
  119. g.drawLine( corner, 2, w-corner-1, 2);
  120. g.drawLine( 2, corner, 2, h-corner-1);
  121. g.drawLine( w-3, corner, w-3, h-corner-1);
  122. g.drawLine( corner, h-3, w-corner-1, h-3);
  123. }
  124. }
  125. public Insets getBorderInsets(Component c) {
  126. return insets;
  127. }
  128. public Insets getBorderInsets(Component c, Insets newInsets) {
  129. newInsets.top = insets.top;
  130. newInsets.left = insets.left;
  131. newInsets.bottom = insets.bottom;
  132. newInsets.right = insets.right;
  133. return newInsets;
  134. }
  135. }
  136. /**
  137. * Border for a Frame.
  138. * @since 1.4
  139. */
  140. static class FrameBorder extends AbstractBorder implements UIResource {
  141. private static final Insets insets = new Insets(5, 5, 5, 5);
  142. private static final int corner = 14;
  143. public void paintBorder(Component c, Graphics g, int x, int y,
  144. int w, int h) {
  145. Color background;
  146. Color highlight;
  147. Color shadow;
  148. Window window = SwingUtilities.getWindowAncestor(c);
  149. if (window != null && window.isActive()) {
  150. background = MetalLookAndFeel.getPrimaryControlDarkShadow();
  151. highlight = MetalLookAndFeel.getPrimaryControlShadow();
  152. shadow = MetalLookAndFeel.getPrimaryControlInfo();
  153. } else {
  154. background = MetalLookAndFeel.getControlDarkShadow();
  155. highlight = MetalLookAndFeel.getControlShadow();
  156. shadow = MetalLookAndFeel.getControlInfo();
  157. }
  158. g.setColor(background);
  159. // Draw outermost lines
  160. g.drawLine( x+1, y+0, x+w-2, y+0);
  161. g.drawLine( x+0, y+1, x+0, y +h-2);
  162. g.drawLine( x+w-1, y+1, x+w-1, y+h-2);
  163. g.drawLine( x+1, y+h-1, x+w-2, y+h-1);
  164. // Draw the bulk of the border
  165. for (int i = 1; i < 5; i++) {
  166. g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);
  167. }
  168. if ((window instanceof Frame) && ((Frame) window).isResizable()) {
  169. g.setColor(highlight);
  170. // Draw the Long highlight lines
  171. g.drawLine( corner+1, 3, w-corner, 3);
  172. g.drawLine( 3, corner+1, 3, h-corner);
  173. g.drawLine( w-2, corner+1, w-2, h-corner);
  174. g.drawLine( corner+1, h-2, w-corner, h-2);
  175. g.setColor(shadow);
  176. // Draw the Long shadow lines
  177. g.drawLine( corner, 2, w-corner-1, 2);
  178. g.drawLine( 2, corner, 2, h-corner-1);
  179. g.drawLine( w-3, corner, w-3, h-corner-1);
  180. g.drawLine( corner, h-3, w-corner-1, h-3);
  181. }
  182. }
  183. public Insets getBorderInsets(Component c) {
  184. return insets;
  185. }
  186. public Insets getBorderInsets(Component c, Insets newInsets)
  187. {
  188. newInsets.top = insets.top;
  189. newInsets.left = insets.left;
  190. newInsets.bottom = insets.bottom;
  191. newInsets.right = insets.right;
  192. return newInsets;
  193. }
  194. }
  195. /**
  196. * Border for a Frame.
  197. * @since 1.4
  198. */
  199. static class DialogBorder extends AbstractBorder implements UIResource
  200. {
  201. private static final Insets insets = new Insets(5, 5, 5, 5);
  202. private static final int corner = 14;
  203. protected Color getActiveBackground()
  204. {
  205. return MetalLookAndFeel.getPrimaryControlDarkShadow();
  206. }
  207. protected Color getActiveHighlight()
  208. {
  209. return MetalLookAndFeel.getPrimaryControlShadow();
  210. }
  211. protected Color getActiveShadow()
  212. {
  213. return MetalLookAndFeel.getPrimaryControlInfo();
  214. }
  215. protected Color getInactiveBackground()
  216. {
  217. return MetalLookAndFeel.getControlDarkShadow();
  218. }
  219. protected Color getInactiveHighlight()
  220. {
  221. return MetalLookAndFeel.getControlShadow();
  222. }
  223. protected Color getInactiveShadow()
  224. {
  225. return MetalLookAndFeel.getControlInfo();
  226. }
  227. public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)
  228. {
  229. Color background;
  230. Color highlight;
  231. Color shadow;
  232. Window window = SwingUtilities.getWindowAncestor(c);
  233. if (window != null && window.isActive()) {
  234. background = getActiveBackground();
  235. highlight = getActiveHighlight();
  236. shadow = getActiveShadow();
  237. } else {
  238. background = getInactiveBackground();
  239. highlight = getInactiveHighlight();
  240. shadow = getInactiveShadow();
  241. }
  242. g.setColor(background);
  243. // Draw outermost lines
  244. g.drawLine( x + 1, y + 0, x + w-2, y + 0);
  245. g.drawLine( x + 0, y + 1, x + 0, y + h - 2);
  246. g.drawLine( x + w - 1, y + 1, x + w - 1, y + h - 2);
  247. g.drawLine( x + 1, y + h - 1, x + w - 2, y + h - 1);
  248. // Draw the bulk of the border
  249. for (int i = 1; i < 5; i++) {
  250. g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);
  251. }
  252. if ((window instanceof Dialog) && ((Dialog) window).isResizable()) {
  253. g.setColor(highlight);
  254. // Draw the Long highlight lines
  255. g.drawLine( corner+1, 3, w-corner, 3);
  256. g.drawLine( 3, corner+1, 3, h-corner);
  257. g.drawLine( w-2, corner+1, w-2, h-corner);
  258. g.drawLine( corner+1, h-2, w-corner, h-2);
  259. g.setColor(shadow);
  260. // Draw the Long shadow lines
  261. g.drawLine( corner, 2, w-corner-1, 2);
  262. g.drawLine( 2, corner, 2, h-corner-1);
  263. g.drawLine( w-3, corner, w-3, h-corner-1);
  264. g.drawLine( corner, h-3, w-corner-1, h-3);
  265. }
  266. }
  267. public Insets getBorderInsets(Component c) {
  268. return insets;
  269. }
  270. public Insets getBorderInsets(Component c, Insets newInsets)
  271. {
  272. newInsets.top = insets.top;
  273. newInsets.left = insets.left;
  274. newInsets.bottom = insets.bottom;
  275. newInsets.right = insets.right;
  276. return newInsets;
  277. }
  278. }
  279. /**
  280. * Border for an Error Dialog.
  281. * @since 1.4
  282. */
  283. static class ErrorDialogBorder extends DialogBorder implements UIResource
  284. {
  285. protected Color getActiveBackground() {
  286. return UIManager.getColor("OptionPane.errorDialog.border.background");
  287. }
  288. }
  289. /**
  290. * Border for a QuestionDialog. Also used for a JFileChooser and a
  291. * JColorChooser..
  292. * @since 1.4
  293. */
  294. static class QuestionDialogBorder extends DialogBorder implements UIResource
  295. {
  296. protected Color getActiveBackground() {
  297. return UIManager.getColor("OptionPane.questionDialog.border.background");
  298. }
  299. }
  300. /**
  301. * Border for a Warning Dialog.
  302. * @since 1.4
  303. */
  304. static class WarningDialogBorder extends DialogBorder implements UIResource
  305. {
  306. protected Color getActiveBackground() {
  307. return UIManager.getColor("OptionPane.warningDialog.border.background");
  308. }
  309. }
  310. /**
  311. * Border for a Palette.
  312. * @since 1.3
  313. */
  314. public static class PaletteBorder extends AbstractBorder implements UIResource {
  315. private static final Insets insets = new Insets(1, 1, 1, 1);
  316. int titleHeight = 0;
  317. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  318. g.translate(x,y);
  319. g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  320. g.drawLine(0, 1, 0, h-2);
  321. g.drawLine(1, h-1, w-2, h-1);
  322. g.drawLine(w-1, 1, w-1, h-2);
  323. g.drawLine( 1, 0, w-2, 0);
  324. g.drawRect(1,1, w-3, h-3);
  325. g.translate(-x,-y);
  326. }
  327. public Insets getBorderInsets(Component c) {
  328. return insets;
  329. }
  330. public Insets getBorderInsets(Component c, Insets newInsets) {
  331. newInsets.top = insets.top;
  332. newInsets.left = insets.left;
  333. newInsets.bottom = insets.bottom;
  334. newInsets.right = insets.right;
  335. return newInsets;
  336. }
  337. }
  338. public static class OptionDialogBorder extends AbstractBorder implements UIResource {
  339. private static final Insets insets = new Insets(3, 3, 3, 3);
  340. int titleHeight = 0;
  341. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  342. g.translate(x,y);
  343. int messageType = JOptionPane.PLAIN_MESSAGE;
  344. if (c instanceof JInternalFrame) {
  345. Object obj = ((JInternalFrame) c).getClientProperty(
  346. "JInternalFrame.messageType");
  347. if (obj != null && (obj instanceof Integer)) {
  348. messageType = ((Integer) obj).intValue();
  349. }
  350. }
  351. Color borderColor;
  352. switch (messageType) {
  353. case(JOptionPane.ERROR_MESSAGE):
  354. borderColor = UIManager.getColor(
  355. "OptionPane.errorDialog.border.background");
  356. break;
  357. case(JOptionPane.QUESTION_MESSAGE):
  358. borderColor = UIManager.getColor(
  359. "OptionPane.questionDialog.border.background");
  360. break;
  361. case(JOptionPane.WARNING_MESSAGE):
  362. borderColor = UIManager.getColor(
  363. "OptionPane.warningDialog.border.background");
  364. break;
  365. case(JOptionPane.INFORMATION_MESSAGE):
  366. case(JOptionPane.PLAIN_MESSAGE):
  367. default:
  368. borderColor = MetalLookAndFeel.getPrimaryControlDarkShadow();
  369. break;
  370. }
  371. g.setColor(borderColor);
  372. // Draw outermost lines
  373. g.drawLine( 1, 0, w-2, 0);
  374. g.drawLine( 0, 1, 0, h-2);
  375. g.drawLine( w-1, 1, w-1, h-2);
  376. g.drawLine( 1, h-1, w-2, h-1);
  377. // Draw the bulk of the border
  378. for (int i = 1; i < 3; i++) {
  379. g.drawRect(i, i, w-(i*2)-1, h-(i*2)-1);
  380. }
  381. g.translate(-x,-y);
  382. }
  383. public Insets getBorderInsets(Component c) {
  384. return insets;
  385. }
  386. public Insets getBorderInsets(Component c, Insets newInsets) {
  387. newInsets.top = insets.top;
  388. newInsets.left = insets.left;
  389. newInsets.bottom = insets.bottom;
  390. newInsets.right = insets.right;
  391. return newInsets;
  392. }
  393. }
  394. public static class MenuBarBorder extends AbstractBorder implements UIResource {
  395. protected static Insets borderInsets = new Insets( 1, 0, 1, 0 );
  396. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  397. g.translate( x, y );
  398. g.setColor( MetalLookAndFeel.getControlShadow() );
  399. g.drawLine( 0, h-1, w, h-1 );
  400. g.translate( -x, -y );
  401. }
  402. public Insets getBorderInsets( Component c ) {
  403. return borderInsets;
  404. }
  405. public Insets getBorderInsets(Component c, Insets newInsets) {
  406. newInsets.top = borderInsets.top;
  407. newInsets.left = borderInsets.left;
  408. newInsets.bottom = borderInsets.bottom;
  409. newInsets.right = borderInsets.right;
  410. return newInsets;
  411. }
  412. }
  413. public static class MenuItemBorder extends AbstractBorder implements UIResource {
  414. protected static Insets borderInsets = new Insets( 2, 2, 2, 2 );
  415. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  416. JMenuItem b = (JMenuItem) c;
  417. ButtonModel model = b.getModel();
  418. g.translate( x, y );
  419. if ( c.getParent() instanceof JMenuBar ) {
  420. if ( model.isArmed() || model.isSelected() ) {
  421. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  422. g.drawLine( 0, 0, w - 2, 0 );
  423. g.drawLine( 0, 0, 0, h - 1 );
  424. g.drawLine( w - 2, 2, w - 2, h - 1 );
  425. g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  426. g.drawLine( w - 1, 1, w - 1, h - 1 );
  427. g.setColor( MetalLookAndFeel.getMenuBackground() );
  428. g.drawLine( w - 1, 0, w - 1, 0 );
  429. }
  430. } else {
  431. if ( model.isArmed() || ( c instanceof JMenu && model.isSelected() ) ) {
  432. g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  433. g.drawLine( 0, 0, w - 1, 0 );
  434. g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  435. g.drawLine( 0, h - 1, w - 1, h - 1 );
  436. } else {
  437. g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  438. g.drawLine( 0, 0, 0, h - 1 );
  439. }
  440. }
  441. g.translate( -x, -y );
  442. }
  443. public Insets getBorderInsets( Component c ) {
  444. return borderInsets;
  445. }
  446. public Insets getBorderInsets(Component c, Insets newInsets) {
  447. newInsets.top = borderInsets.top;
  448. newInsets.left = borderInsets.left;
  449. newInsets.bottom = borderInsets.bottom;
  450. newInsets.right = borderInsets.right;
  451. return newInsets;
  452. }
  453. }
  454. public static class PopupMenuBorder extends AbstractBorder implements UIResource {
  455. protected static Insets borderInsets = new Insets( 3, 1, 2, 1 );
  456. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  457. g.translate( x, y );
  458. g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  459. g.drawRect( 0, 0, w - 1, h - 1 );
  460. g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  461. g.drawLine( 1, 1, w - 2, 1 );
  462. g.drawLine( 1, 2, 1, 2 );
  463. g.drawLine( 1, h - 2, 1, h - 2 );
  464. g.translate( -x, -y );
  465. }
  466. public Insets getBorderInsets( Component c ) {
  467. return borderInsets;
  468. }
  469. public Insets getBorderInsets(Component c, Insets newInsets) {
  470. newInsets.top = borderInsets.top;
  471. newInsets.left = borderInsets.left;
  472. newInsets.bottom = borderInsets.bottom;
  473. newInsets.right = borderInsets.right;
  474. return newInsets;
  475. }
  476. }
  477. public static class RolloverButtonBorder extends ButtonBorder {
  478. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  479. AbstractButton b = (AbstractButton) c;
  480. ButtonModel model = b.getModel();
  481. if ( model.isRollover() && !( model.isPressed() && !model.isArmed() ) ) {
  482. super.paintBorder( c, g, x, y, w, h );
  483. }
  484. }
  485. }
  486. /**
  487. * A border which is like a Margin border but it will only honor the margin
  488. * if the margin has been explicitly set by the developer.
  489. *
  490. * Note: This is identical to the package private class
  491. * BasicBorders.RolloverMarginBorder and should probably be consolidated.
  492. */
  493. static class RolloverMarginBorder extends EmptyBorder {
  494. public RolloverMarginBorder() {
  495. super(3,3,3,3); // hardcoded margin for JLF requirements.
  496. }
  497. public Insets getBorderInsets(Component c) {
  498. return getBorderInsets(c, new Insets(0,0,0,0));
  499. }
  500. public Insets getBorderInsets(Component c, Insets insets) {
  501. Insets margin = null;
  502. if (c instanceof AbstractButton) {
  503. margin = ((AbstractButton)c).getMargin();
  504. }
  505. if (margin == null || margin instanceof UIResource) {
  506. // default margin so replace
  507. insets.left = left;
  508. insets.top = top;
  509. insets.right = right;
  510. insets.bottom = bottom;
  511. } else {
  512. // Margin which has been explicitly set by the user.
  513. insets.left = margin.left;
  514. insets.top = margin.top;
  515. insets.right = margin.right;
  516. insets.bottom = margin.bottom;
  517. }
  518. return insets;
  519. }
  520. }
  521. public static class ToolBarBorder extends AbstractBorder implements UIResource, SwingConstants
  522. {
  523. protected MetalBumps bumps = new MetalBumps( 10, 10,
  524. MetalLookAndFeel.getControlHighlight(),
  525. MetalLookAndFeel.getControlDarkShadow(),
  526. MetalLookAndFeel.getMenuBackground() );
  527. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h )
  528. {
  529. g.translate( x, y );
  530. if ( ((JToolBar) c).isFloatable() )
  531. {
  532. if ( ((JToolBar) c).getOrientation() == HORIZONTAL )
  533. {
  534. bumps.setBumpArea( 10, c.getSize().height - 4 );
  535. if( MetalUtils.isLeftToRight(c) ) {
  536. bumps.paintIcon( c, g, 2, 2 );
  537. } else {
  538. bumps.paintIcon( c, g, c.getBounds().width-12, 2 );
  539. }
  540. }
  541. else // vertical
  542. {
  543. bumps.setBumpArea( c.getSize().width - 4, 10 );
  544. bumps.paintIcon( c, g, 2, 2 );
  545. }
  546. }
  547. g.translate( -x, -y );
  548. }
  549. public Insets getBorderInsets( Component c ) {
  550. return getBorderInsets(c, new Insets(0,0,0,0));
  551. }
  552. public Insets getBorderInsets(Component c, Insets newInsets) {
  553. newInsets.top = newInsets.left = newInsets.bottom = newInsets.right = 2;
  554. if ( ((JToolBar) c).isFloatable() ) {
  555. if ( ((JToolBar) c).getOrientation() == HORIZONTAL ) {
  556. if (c.getComponentOrientation().isLeftToRight()) {
  557. newInsets.left = 16;
  558. } else {
  559. newInsets.right = 16;
  560. }
  561. } else {// vertical
  562. newInsets.top = 16;
  563. }
  564. }
  565. Insets margin = ((JToolBar) c).getMargin();
  566. if ( margin != null ) {
  567. newInsets.left += margin.left;
  568. newInsets.top += margin.top;
  569. newInsets.right += margin.right;
  570. newInsets.bottom += margin.bottom;
  571. }
  572. return newInsets;
  573. }
  574. }
  575. private static Border buttonBorder;
  576. /**
  577. * Returns a border instance for a JButton
  578. * @since 1.3
  579. */
  580. public static Border getButtonBorder() {
  581. if (buttonBorder == null) {
  582. buttonBorder = new BorderUIResource.CompoundBorderUIResource(
  583. new MetalBorders.ButtonBorder(),
  584. new BasicBorders.MarginBorder());
  585. }
  586. return buttonBorder;
  587. }
  588. private static Border textBorder;
  589. /**
  590. * Returns a border instance for a text component
  591. * @since 1.3
  592. */
  593. public static Border getTextBorder() {
  594. if (textBorder == null) {
  595. textBorder = new BorderUIResource.CompoundBorderUIResource(
  596. new MetalBorders.Flush3DBorder(),
  597. new BasicBorders.MarginBorder());
  598. }
  599. return textBorder;
  600. }
  601. private static Border textFieldBorder;
  602. /**
  603. * Returns a border instance for a JTextField
  604. * @since 1.3
  605. */
  606. public static Border getTextFieldBorder() {
  607. if (textFieldBorder == null) {
  608. textFieldBorder = new BorderUIResource.CompoundBorderUIResource(
  609. new MetalBorders.TextFieldBorder(),
  610. new BasicBorders.MarginBorder());
  611. }
  612. return textFieldBorder;
  613. }
  614. public static class TextFieldBorder extends Flush3DBorder {
  615. public void paintBorder(Component c, Graphics g, int x, int y,
  616. int w, int h) {
  617. if (!(c instanceof JTextComponent)) {
  618. // special case for non-text components (bug ID 4144840)
  619. if (c.isEnabled()) {
  620. MetalUtils.drawFlush3DBorder(g, x, y, w, h);
  621. } else {
  622. MetalUtils.drawDisabledBorder(g, x, y, w, h);
  623. }
  624. return;
  625. }
  626. if (c.isEnabled() && ((JTextComponent)c).isEditable()) {
  627. MetalUtils.drawFlush3DBorder(g, x, y, w, h);
  628. } else {
  629. MetalUtils.drawDisabledBorder(g, x, y, w, h);
  630. }
  631. }
  632. }
  633. public static class ScrollPaneBorder extends AbstractBorder implements UIResource {
  634. private static final Insets insets = new Insets(1, 1, 2, 2);
  635. public void paintBorder(Component c, Graphics g, int x, int y,
  636. int w, int h) {
  637. JScrollPane scroll = (JScrollPane)c;
  638. JComponent colHeader = scroll.getColumnHeader();
  639. int colHeaderHeight = 0;
  640. if (colHeader != null)
  641. colHeaderHeight = colHeader.getHeight();
  642. JComponent rowHeader = scroll.getRowHeader();
  643. int rowHeaderWidth = 0;
  644. if (rowHeader != null)
  645. rowHeaderWidth = rowHeader.getWidth();
  646. g.translate( x, y);
  647. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  648. g.drawRect( 0, 0, w-2, h-2 );
  649. g.setColor( MetalLookAndFeel.getControlHighlight() );
  650. g.drawLine( w-1, 1, w-1, h-1);
  651. g.drawLine( 1, h-1, w-1, h-1);
  652. g.setColor( MetalLookAndFeel.getControl() );
  653. g.drawLine( w-2, 2+colHeaderHeight, w-2, 2+colHeaderHeight );
  654. g.drawLine( 1+rowHeaderWidth, h-2, 1+rowHeaderWidth, h-2 );
  655. g.translate( -x, -y);
  656. }
  657. public Insets getBorderInsets(Component c) {
  658. return insets;
  659. }
  660. }
  661. private static Border toggleButtonBorder;
  662. /**
  663. * Returns a border instance for a JToggleButton
  664. * @since 1.3
  665. */
  666. public static Border getToggleButtonBorder() {
  667. if (toggleButtonBorder == null) {
  668. toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource(
  669. new MetalBorders.ToggleButtonBorder(),
  670. new BasicBorders.MarginBorder());
  671. }
  672. return toggleButtonBorder;
  673. }
  674. /**
  675. * @since 1.3
  676. */
  677. public static class ToggleButtonBorder extends ButtonBorder {
  678. public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  679. AbstractButton button = (AbstractButton)c;
  680. ButtonModel model = button.getModel();
  681. if (! c.isEnabled() ) {
  682. MetalUtils.drawDisabledBorder( g, x, y, w-1, h-1 );
  683. } else {
  684. if ( model.isPressed() && model.isArmed() ) {
  685. MetalUtils.drawPressed3DBorder( g, x, y, w, h );
  686. } else if ( model.isSelected() ) {
  687. MetalUtils.drawDark3DBorder( g, x, y, w, h );
  688. } else {
  689. MetalUtils.drawFlush3DBorder( g, x, y, w, h );
  690. }
  691. }
  692. }
  693. }
  694. /**
  695. * Border for a Table Header
  696. * @since 1.3
  697. */
  698. public static class TableHeaderBorder extends javax.swing.border.AbstractBorder {
  699. protected Insets editorBorderInsets = new Insets( 2, 2, 2, 0 );
  700. public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  701. g.translate( x, y );
  702. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  703. g.drawLine( w-1, 0, w-1, h-1 );
  704. g.drawLine( 1, h-1, w-1, h-1 );
  705. g.setColor( MetalLookAndFeel.getControlHighlight() );
  706. g.drawLine( 0, 0, w-2, 0 );
  707. g.drawLine( 0, 0, 0, h-2 );
  708. g.translate( -x, -y );
  709. }
  710. public Insets getBorderInsets( Component c ) {
  711. return editorBorderInsets;
  712. }
  713. }
  714. /**
  715. * Returns a border instance for a Desktop Icon
  716. * @since 1.3
  717. */
  718. public static Border getDesktopIconBorder() {
  719. return new BorderUIResource.CompoundBorderUIResource(
  720. new LineBorder(MetalLookAndFeel.getControlDarkShadow(), 1),
  721. new MatteBorder (2,2,1,2, MetalLookAndFeel.getControl()));
  722. }
  723. }