1. /*
  2. * @(#)BasicBorders.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.plaf.basic;
  8. import javax.swing.*;
  9. import javax.swing.border.*;
  10. import javax.swing.plaf.*;
  11. import javax.swing.text.JTextComponent;
  12. import java.awt.Component;
  13. import java.awt.Insets;
  14. import java.awt.Dimension;
  15. import java.awt.Rectangle;
  16. import java.awt.Color;
  17. import java.awt.Graphics;
  18. import java.io.Serializable;
  19. /**
  20. * Factory object that can vend Borders appropriate for the basic L & F.
  21. * @version 1.16 11/29/01
  22. * @author Georges Saab
  23. * @author Amy Fowler
  24. */
  25. public class BasicBorders {
  26. public static class ButtonBorder extends AbstractBorder implements UIResource {
  27. protected Color shadow;
  28. protected Color darkShadow;
  29. protected Color highlight;
  30. protected Color lightHighlight;
  31. public ButtonBorder(Color shadow, Color darkShadow,
  32. Color highlight, Color lightHighlight) {
  33. this.shadow = shadow;
  34. this.darkShadow = darkShadow;
  35. this.highlight = highlight;
  36. this.lightHighlight = lightHighlight;
  37. }
  38. public void paintBorder(Component c, Graphics g, int x, int y,
  39. int width, int height) {
  40. boolean isPressed = false;
  41. boolean isDefault = false;
  42. if (c instanceof AbstractButton) {
  43. AbstractButton b = (AbstractButton)c;
  44. ButtonModel model = b.getModel();
  45. isPressed = model.isPressed() && model.isArmed();
  46. if (c instanceof JButton) {
  47. isDefault = ((JButton)c).isDefaultButton();
  48. }
  49. }
  50. BasicGraphicsUtils.drawBezel(g, x, y, width, height,
  51. isPressed, isDefault, shadow,
  52. darkShadow, highlight, lightHighlight);
  53. }
  54. public Insets getBorderInsets(Component c) {
  55. // leave room for default visual
  56. return new Insets(3,3,3,3);
  57. }
  58. }
  59. public static class ToggleButtonBorder extends ButtonBorder {
  60. public ToggleButtonBorder(Color shadow, Color darkShadow,
  61. Color highlight, Color lightHighlight) {
  62. super(shadow, darkShadow, highlight, lightHighlight);
  63. }
  64. public void paintBorder(Component c, Graphics g, int x, int y,
  65. int width, int height) {
  66. BasicGraphicsUtils.drawBezel(g, x, y, width, height,
  67. false, false,
  68. shadow, darkShadow,
  69. highlight, lightHighlight);
  70. }
  71. public Insets getBorderInsets(Component c) {
  72. return new Insets(2, 2, 2, 2);
  73. }
  74. }
  75. public static class RadioButtonBorder extends ButtonBorder {
  76. public RadioButtonBorder(Color shadow, Color darkShadow,
  77. Color highlight, Color lightHighlight) {
  78. super(shadow, darkShadow, highlight, lightHighlight);
  79. }
  80. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  81. if (c instanceof AbstractButton) {
  82. AbstractButton b = (AbstractButton)c;
  83. ButtonModel model = b.getModel();
  84. if (model.isArmed() && model.isPressed() || model.isSelected()) {
  85. BasicGraphicsUtils.drawLoweredBezel(g, x, y, width, height,
  86. shadow, darkShadow,
  87. highlight, lightHighlight);
  88. } else {
  89. BasicGraphicsUtils.drawBezel(g, x, y, width, height,
  90. false, b.isFocusPainted() && b.hasFocus(),
  91. shadow, darkShadow,
  92. highlight, lightHighlight);
  93. }
  94. } else {
  95. BasicGraphicsUtils.drawBezel(g, x, y, width, height, false, false,
  96. shadow, darkShadow, highlight, lightHighlight);
  97. }
  98. }
  99. public Insets getBorderInsets(Component c) {
  100. return new Insets(2, 2, 2, 2);
  101. }
  102. }
  103. public static class MenuBarBorder extends AbstractBorder implements UIResource {
  104. private Color shadow;
  105. private Color highlight;
  106. public MenuBarBorder(Color shadow, Color highlight) {
  107. this.shadow = shadow;
  108. this.highlight = highlight;
  109. }
  110. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  111. BasicGraphicsUtils.drawGroove(g, 0, height-2,
  112. width, height,
  113. shadow, highlight);
  114. }
  115. public Insets getBorderInsets(Component c) {
  116. return new Insets(0, 0, 2, 0);
  117. }
  118. }
  119. public static class MarginBorder extends AbstractBorder implements UIResource {
  120. public Insets getBorderInsets(Component c) {
  121. Insets margin = null;
  122. //
  123. // Ideally we'd have an interface defined for classes which
  124. // support margins (to avoid this hackery), but we've
  125. // decided against it for simplicity
  126. //
  127. if (c instanceof AbstractButton) {
  128. AbstractButton b = (AbstractButton)c;
  129. margin = b.getMargin();
  130. } else if (c instanceof JToolBar) {
  131. JToolBar t = (JToolBar)c;
  132. margin = t.getMargin();
  133. } else if (c instanceof JTextComponent) {
  134. JTextComponent t = (JTextComponent)c;
  135. margin = t.getMargin();
  136. }
  137. return (margin != null? margin : new Insets(0, 0, 0, 0));
  138. }
  139. }
  140. public static class FieldBorder extends AbstractBorder implements UIResource {
  141. protected Color shadow;
  142. protected Color darkShadow;
  143. protected Color highlight;
  144. protected Color lightHighlight;
  145. public FieldBorder(Color shadow, Color darkShadow,
  146. Color highlight, Color lightHighlight) {
  147. this.shadow = shadow;
  148. this.highlight = highlight;
  149. this.darkShadow = darkShadow;
  150. this.lightHighlight = lightHighlight;
  151. }
  152. public void paintBorder(Component c, Graphics g, int x, int y,
  153. int width, int height) {
  154. BasicGraphicsUtils.drawEtchedRect(g, x, y, width, height,
  155. shadow, darkShadow,
  156. highlight, lightHighlight);
  157. }
  158. public Insets getBorderInsets(Component c) {
  159. Insets margin = null;
  160. if (c instanceof JTextComponent) {
  161. margin = ((JTextComponent)c).getMargin();
  162. }
  163. if (margin != null) {
  164. return new Insets(2+margin.top, 2+margin.left,
  165. 2+margin.bottom, 2+margin.right);
  166. }
  167. return new Insets(2, 2, 2, 2);
  168. }
  169. }
  170. /**
  171. * This border draws the borders around both of the contained components in the
  172. * the splitter. It is a very odd border.
  173. */
  174. public static class SplitPaneBorder implements Border, UIResource {
  175. protected Color highlight;
  176. protected Color shadow;
  177. public SplitPaneBorder(Color highlight, Color shadow) {
  178. this.highlight = highlight;
  179. this.shadow = shadow;
  180. }
  181. public void paintBorder(Component c, Graphics g, int x, int y,
  182. int width, int height) {
  183. Component child;
  184. Rectangle cBounds;
  185. JSplitPane splitPane = (JSplitPane)c;
  186. child = splitPane.getLeftComponent();
  187. // This is needed for the space between the divider and end of
  188. // splitpane.
  189. g.setColor(c.getBackground());
  190. g.drawRect(x, y, width - 1, height - 1);
  191. if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
  192. if(child != null) {
  193. cBounds = child.getBounds();
  194. g.setColor(shadow);
  195. g.drawLine(0, 0, cBounds.width + 1, 0);
  196. g.drawLine(0, 1, 0, cBounds.height + 2);
  197. g.setColor(highlight);
  198. g.drawLine(1, cBounds.height + 1, cBounds.width + 1,
  199. cBounds.height + 1);
  200. g.drawLine(cBounds.width + 1, 1,
  201. cBounds.width + 1, cBounds.height + 2);
  202. }
  203. child = splitPane.getRightComponent();
  204. if(child != null) {
  205. cBounds = child.getBounds();
  206. int maxX = cBounds.x + cBounds.width;
  207. int maxY = cBounds.y + cBounds.height;
  208. g.setColor(shadow);
  209. g.drawLine(cBounds.x - 1, 0, maxX, 0);
  210. g.drawLine(cBounds.x - 1, maxY, cBounds.x, maxY);
  211. g.drawLine(cBounds.x - 1, 0, cBounds.x -1 , maxY);
  212. g.setColor(highlight);
  213. g.drawLine(cBounds.x, maxY, maxX, maxY);
  214. g.drawLine(maxX, 0, maxX, maxY + 1);
  215. }
  216. } else {
  217. if(child != null) {
  218. cBounds = child.getBounds();
  219. g.setColor(shadow);
  220. g.drawLine(0, 0, cBounds.width + 1, 0);
  221. g.drawLine(0, 1, 0, cBounds.height + 1);
  222. g.setColor(highlight);
  223. g.drawLine(1 + cBounds.width, 0, 1 + cBounds.width,
  224. cBounds.height + 1);
  225. g.drawLine(0, cBounds.height + 1,
  226. cBounds.width, cBounds.height + 1);
  227. }
  228. child = splitPane.getRightComponent();
  229. if(child != null) {
  230. cBounds = child.getBounds();
  231. int maxX = cBounds.x + cBounds.width;
  232. int maxY = cBounds.y + cBounds.height;
  233. g.setColor(shadow);
  234. g.drawLine(0, cBounds.y - 1, 0, maxY);
  235. g.drawLine(maxX, cBounds.y - 1, maxX, cBounds.y);
  236. g.drawLine(0, cBounds.y - 1, cBounds.width, cBounds.y - 1);
  237. g.setColor(highlight);
  238. g.drawLine(0, maxY, cBounds.width + 1, maxY);
  239. g.drawLine(maxX, cBounds.y, maxX, maxY);
  240. }
  241. }
  242. }
  243. public Insets getBorderInsets(Component c) {
  244. return new Insets(1, 1, 1, 1);
  245. }
  246. public boolean isBorderOpaque() { return true; }
  247. }
  248. }