1. /*
  2. * @(#)WindowsBorders.java 1.30 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.windows;
  8. import javax.swing.*;
  9. import javax.swing.border.*;
  10. import javax.swing.plaf.*;
  11. import javax.swing.plaf.basic.*;
  12. import java.awt.Component;
  13. import java.awt.Insets;
  14. import java.awt.Dimension;
  15. import java.awt.Image;
  16. import java.awt.Rectangle;
  17. import java.awt.Color;
  18. import java.awt.Graphics;
  19. import java.io.Serializable;
  20. /**
  21. * Factory object that can vend Borders appropriate for the Windows 95 L & F.
  22. * @version 1.30 12/19/03
  23. * @author Rich Schiavi
  24. */
  25. public class WindowsBorders {
  26. /**
  27. * Returns a border instance for a Windows Progress Bar
  28. * @since 1.4
  29. */
  30. public static Border getProgressBarBorder() {
  31. UIDefaults table = UIManager.getLookAndFeelDefaults();
  32. Border progressBarBorder = new BorderUIResource.CompoundBorderUIResource(
  33. new WindowsBorders.ProgressBarBorder(
  34. table.getColor("ProgressBar.shadow"),
  35. table.getColor("ProgressBar.highlight")),
  36. new EmptyBorder(1,1,1,1)
  37. );
  38. return progressBarBorder;
  39. }
  40. /**
  41. * Returns a border instance for a Windows ToolBar
  42. *
  43. * @return a border used for the toolbar
  44. * @since 1.4
  45. */
  46. public static Border getToolBarBorder() {
  47. UIDefaults table = UIManager.getLookAndFeelDefaults();
  48. Border toolBarBorder = new WindowsBorders.ToolBarBorder(
  49. table.getColor("ToolBar.shadow"),
  50. table.getColor("ToolBar.highlight"));
  51. return toolBarBorder;
  52. }
  53. /**
  54. * Returns an new instance of a border used to indicate which cell item
  55. * has focus.
  56. *
  57. * @return a border to indicate which cell item has focus
  58. * @since 1.4
  59. */
  60. public static Border getFocusCellHighlightBorder() {
  61. return new ComplementDashedBorder();
  62. }
  63. public static Border getTableHeaderBorder() {
  64. UIDefaults table = UIManager.getLookAndFeelDefaults();
  65. Border tableHeaderBorder = new BorderUIResource.CompoundBorderUIResource(
  66. new BasicBorders.ButtonBorder(
  67. table.getColor("Table.shadow"),
  68. table.getColor("Table.darkShadow"),
  69. table.getColor("Table.light"),
  70. table.getColor("Table.highlight")),
  71. new BasicBorders.MarginBorder());
  72. return tableHeaderBorder;
  73. }
  74. public static Border getInternalFrameBorder() {
  75. UIDefaults table = UIManager.getLookAndFeelDefaults();
  76. Border internalFrameBorder = new
  77. BorderUIResource.CompoundBorderUIResource(
  78. BorderFactory.createBevelBorder(BevelBorder.RAISED,
  79. table.getColor("InternalFrame.borderColor"),
  80. table.getColor("InternalFrame.borderHighlight"),
  81. table.getColor("InternalFrame.borderDarkShadow"),
  82. table.getColor("InternalFrame.borderShadow")),
  83. new WindowsBorders.InternalFrameLineBorder(
  84. table.getColor("InternalFrame.activeBorderColor"),
  85. table.getColor("InternalFrame.inactiveBorderColor"),
  86. table.getInt("InternalFrame.borderWidth")));
  87. return internalFrameBorder;
  88. }
  89. public static class ProgressBarBorder extends AbstractBorder implements UIResource {
  90. protected Color shadow;
  91. protected Color highlight;
  92. public ProgressBarBorder(Color shadow, Color highlight) {
  93. this.highlight = highlight;
  94. this.shadow = shadow;
  95. }
  96. public void paintBorder(Component c, Graphics g, int x, int y,
  97. int width, int height) {
  98. g.setColor(shadow);
  99. g.drawLine(x,y, width-1,y); // draw top
  100. g.drawLine(x,y, x,height-1); // draw left
  101. g.setColor(highlight);
  102. g.drawLine(x,height-1, width-1,height-1); // draw bottom
  103. g.drawLine(width-1,y, width-1,height-1); // draw right
  104. }
  105. public Insets getBorderInsets(Component c) {
  106. return new Insets(1,1,1,1);
  107. }
  108. public Insets getBorderInsets(Component c, Insets insets) {
  109. insets.top = insets.left = insets.bottom = insets.right = 1;
  110. return insets;
  111. }
  112. }
  113. /**
  114. * A border for the ToolBar. If the ToolBar is floatable then the handle grip is drawn
  115. * <p>
  116. * @since 1.4
  117. */
  118. public static class ToolBarBorder extends AbstractBorder implements UIResource, SwingConstants {
  119. protected Color shadow;
  120. protected Color highlight;
  121. private XPStyle xp = XPStyle.getXP();
  122. public ToolBarBorder(Color shadow, Color highlight) {
  123. this.highlight = highlight;
  124. this.shadow = shadow;
  125. }
  126. public void paintBorder(Component c, Graphics g, int x, int y,
  127. int width, int height) {
  128. g.translate(x, y);
  129. if (xp != null) {
  130. Border xpBorder = xp.getBorder("toolbar");
  131. if (xpBorder != null) {
  132. xpBorder.paintBorder(c, g, 0, 0, width, height);
  133. }
  134. }
  135. if (((JToolBar)c).isFloatable()) {
  136. boolean vertical = ((JToolBar)c).getOrientation() == VERTICAL;
  137. if (xp != null) {
  138. String category = vertical ? "rebar.grippervert" : "rebar.gripper";
  139. XPStyle.Skin skin = xp.getSkin(category);
  140. int dx, dy, dw, dh;
  141. if (vertical) {
  142. dx = 0;
  143. dy = 2;
  144. dw = width - 1;
  145. dh = skin.getHeight();
  146. } else {
  147. dw = skin.getWidth();
  148. dh = height - 1;
  149. dx = c.getComponentOrientation().isLeftToRight() ? 2 : (width-dw-2);
  150. dy = 0;
  151. }
  152. skin.paintSkin(g, dx, dy, dw, dh, 0);
  153. } else {
  154. if (!vertical) {
  155. if (c.getComponentOrientation().isLeftToRight()) {
  156. g.setColor(shadow);
  157. g.drawLine(4, 3, 4, height - 4);
  158. g.drawLine(4, height - 4, 2, height - 4);
  159. g.setColor(highlight);
  160. g.drawLine(2, 3, 3, 3);
  161. g.drawLine(2, 3, 2, height - 5);
  162. } else {
  163. g.setColor(shadow);
  164. g.drawLine(width - 3, 3, width - 3, height - 4);
  165. g.drawLine(width - 4, height - 4, width - 4, height - 4);
  166. g.setColor(highlight);
  167. g.drawLine(width - 5, 3, width - 4, 3);
  168. g.drawLine(width - 5, 3, width - 5, height - 5);
  169. }
  170. } else { // Vertical
  171. g.setColor(shadow);
  172. g.drawLine(3, 4, width - 4, 4);
  173. g.drawLine(width - 4, 2, width - 4, 4);
  174. g.setColor(highlight);
  175. g.drawLine(3, 2, width - 4, 2);
  176. g.drawLine(3, 2, 3, 3);
  177. }
  178. }
  179. }
  180. g.translate(-x, -y);
  181. }
  182. public Insets getBorderInsets(Component c) {
  183. return getBorderInsets(c, new Insets(1,1,1,1));
  184. }
  185. public Insets getBorderInsets(Component c, Insets insets) {
  186. insets.top = insets.left = insets.bottom = insets.right = 1;
  187. if (((JToolBar)c).isFloatable()) {
  188. int gripInset = (XPStyle.getXP() != null) ? 12 : 9;
  189. if (((JToolBar)c).getOrientation() == HORIZONTAL) {
  190. if (c.getComponentOrientation().isLeftToRight()) {
  191. insets.left = gripInset;
  192. } else {
  193. insets.right = gripInset;
  194. }
  195. } else {
  196. insets.top = gripInset;
  197. }
  198. }
  199. return insets;
  200. }
  201. }
  202. /**
  203. * This class is an implementation of a dashed border.
  204. * @since 1.4
  205. */
  206. public static class DashedBorder extends LineBorder implements UIResource {
  207. public DashedBorder(Color color) {
  208. super(color);
  209. }
  210. public DashedBorder(Color color, int thickness) {
  211. super(color, thickness);
  212. }
  213. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  214. Color oldColor = g.getColor();
  215. int i;
  216. g.setColor(lineColor);
  217. for(i = 0; i < thickness; i++) {
  218. BasicGraphicsUtils.drawDashedRect(g, x+i, y+i, width-i-i, height-i-i);
  219. }
  220. g.setColor(oldColor);
  221. }
  222. }
  223. /**
  224. * A dashed border that paints itself in the complementary color
  225. * of the component's background color.
  226. */
  227. static class ComplementDashedBorder extends LineBorder implements UIResource {
  228. private Color origColor;
  229. private Color paintColor;
  230. public ComplementDashedBorder() {
  231. super(null);
  232. }
  233. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  234. Color color = c.getBackground();
  235. if (origColor != color) {
  236. origColor = color;
  237. paintColor = new Color(~origColor.getRGB());
  238. }
  239. g.setColor(paintColor);
  240. BasicGraphicsUtils.drawDashedRect(g, x, y, width, height);
  241. }
  242. }
  243. /**
  244. * This class is an implementation of the InternalFrameLine border.
  245. * @since 1.4
  246. */
  247. public static class InternalFrameLineBorder extends LineBorder implements
  248. UIResource {
  249. protected Color activeColor;
  250. protected Color inactiveColor;
  251. public InternalFrameLineBorder(Color activeBorderColor,
  252. Color inactiveBorderColor,
  253. int thickness) {
  254. super(activeBorderColor, thickness);
  255. activeColor = activeBorderColor;
  256. inactiveColor = inactiveBorderColor;
  257. }
  258. public void paintBorder(Component c, Graphics g, int x, int y,
  259. int width, int height) {
  260. JInternalFrame jif = null;
  261. if (c instanceof JInternalFrame) {
  262. jif = (JInternalFrame)c;
  263. } else if (c instanceof JInternalFrame.JDesktopIcon) {
  264. jif = ((JInternalFrame.JDesktopIcon)c).getInternalFrame();
  265. } else {
  266. return;
  267. }
  268. if (jif.isSelected()) {
  269. // Set the line color so the line border gets the correct
  270. // color.
  271. lineColor = activeColor;
  272. super.paintBorder(c, g, x, y, width, height);
  273. } else {
  274. lineColor = inactiveColor;
  275. super.paintBorder(c, g, x, y, width, height);
  276. }
  277. }
  278. }
  279. }