1. /*
  2. * @(#)MetalBorders.java 1.13 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.metal;
  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 metal L & F.
  21. * @author Steve Wilson
  22. * @version 1.13 11/29/01
  23. */
  24. public class MetalBorders {
  25. public static class Flush3DBorder extends AbstractBorder implements UIResource{
  26. private static final Insets insets = new Insets(2, 2, 2, 2);
  27. public void paintBorder(Component c, Graphics g, int x, int y,
  28. int w, int h) {
  29. if (c.isEnabled()) {
  30. MetalUtils.drawFlush3DBorder(g, x, y, w, h);
  31. } else {
  32. MetalUtils.drawDisabledBorder(g, x, y, w, h);
  33. }
  34. }
  35. public Insets getBorderInsets(Component c) {
  36. return insets;
  37. }
  38. }
  39. public static class ButtonBorder extends AbstractBorder implements UIResource {
  40. protected static Insets borderInsets = new Insets( 3, 3, 3, 3 );
  41. public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  42. JButton button = (JButton)c;
  43. ButtonModel model = button.getModel();
  44. if ( model.isEnabled() ) {
  45. if ( model.isPressed() && model.isArmed() ) {
  46. MetalUtils.drawPressed3DBorder( g, x, y, w, h );
  47. }
  48. else {
  49. if (button.isDefaultButton()) {
  50. MetalUtils.drawDefaultButtonBorder( g, x, y, w, h, button.hasFocus() && false );
  51. } else {
  52. MetalUtils.drawButtonBorder( g, x, y, w, h, button.hasFocus() && false);
  53. }
  54. }
  55. } else { // disabled state
  56. MetalUtils.drawDisabledBorder( g, x, y, w-1, h-1 );
  57. }
  58. }
  59. public Insets getBorderInsets( Component c ) {
  60. return borderInsets;
  61. }
  62. }
  63. public static class InternalFrameBorder extends AbstractBorder implements UIResource {
  64. private static final Insets insets = new Insets(5, 5, 5, 5);
  65. private static final int corner = 14;
  66. public void paintBorder(Component c, Graphics g, int x, int y,
  67. int w, int h) {
  68. Color background;
  69. Color highlight;
  70. Color shadow;
  71. if (c instanceof JInternalFrame && ((JInternalFrame)c).isSelected()) {
  72. background = MetalLookAndFeel.getPrimaryControlDarkShadow();
  73. highlight = MetalLookAndFeel.getPrimaryControlShadow();
  74. shadow = MetalLookAndFeel.getPrimaryControlInfo();
  75. } else {
  76. background = MetalLookAndFeel.getControlDarkShadow();
  77. highlight = MetalLookAndFeel.getControlShadow();
  78. shadow = MetalLookAndFeel.getControlInfo();
  79. }
  80. g.setColor(background);
  81. // Draw outermost lines
  82. g.drawLine( 1, 0, w-2, 0);
  83. g.drawLine( 0, 1, 0, h-2);
  84. g.drawLine( w-1, 1, w-1, h-2);
  85. g.drawLine( 1, h-1, w-2, h-1);
  86. // Draw the bulk of the border
  87. for (int i = 1; i < 5; i++) {
  88. g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);
  89. }
  90. g.setColor(highlight);
  91. // Draw the Long highlight lines
  92. g.drawLine( corner+1, 3, w-corner, 3);
  93. g.drawLine( 3, corner+1, 3, h-corner);
  94. g.drawLine( w-2, corner+1, w-2, h-corner);
  95. g.drawLine( corner+1, h-2, w-corner, h-2);
  96. g.setColor(shadow);
  97. // Draw the Long shadow lines
  98. g.drawLine( corner, 2, w-corner-1, 2);
  99. g.drawLine( 2, corner, 2, h-corner-1);
  100. g.drawLine( w-3, corner, w-3, h-corner-1);
  101. g.drawLine( corner, h-3, w-corner-1, h-3);
  102. }
  103. public Insets getBorderInsets(Component c) {
  104. return insets;
  105. }
  106. }
  107. static class PaletteBorder extends AbstractBorder implements UIResource {
  108. private static final Insets insets = new Insets(1, 1, 1, 1);
  109. int titleHeight = 0;
  110. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  111. g.translate(x,y);
  112. g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  113. g.drawLine(0, 1, 0, h-2);
  114. g.drawLine(1, h-1, w-2, h-1);
  115. g.drawLine(w-1, 1, w-1, h-2);
  116. g.drawLine( 1, 0, w-2, 0);
  117. g.drawRect(1,1, w-3, h-3);
  118. /*
  119. titleHeight = UIManager.getInt("JInternalFrame.paletteTitleHeight");
  120. g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  121. g.drawLine(0, titleHeight, 0, h-2);
  122. g.drawLine(1, h-1, w-2, h-1);
  123. g.drawLine(w-1, titleHeight, w-1, h-2);
  124. g.setColor(MetalLookAndFeel.getPrimaryControl());
  125. g.drawLine(1,0, w-2, 0);
  126. g.drawLine(0, 1, 0, titleHeight-1);
  127. g.drawLine(w-1, 1, w-1, titleHeight-1);
  128. */
  129. g.translate(-x,-y);
  130. }
  131. public Insets getBorderInsets(Component c) {
  132. return insets;
  133. }
  134. }
  135. public static class MenuBarBorder extends AbstractBorder implements UIResource {
  136. protected static Insets borderInsets = new Insets( 1, 0, 1, 0 );
  137. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  138. g.translate( x, y );
  139. g.setColor( MetalLookAndFeel.getControlShadow() );
  140. g.drawLine( 0, h-1, w, h-1 );
  141. g.translate( -x, -y );
  142. }
  143. public Insets getBorderInsets( Component c ) {
  144. return borderInsets;
  145. }
  146. }
  147. public static class MenuItemBorder extends AbstractBorder implements UIResource {
  148. protected static Insets borderInsets = new Insets( 2, 2, 2, 2 );
  149. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  150. JMenuItem b = (JMenuItem) c;
  151. ButtonModel model = b.getModel();
  152. g.translate( x, y );
  153. if ( c.getParent() instanceof JMenuBar ) {
  154. if ( model.isArmed() || model.isSelected() ) {
  155. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  156. g.drawLine( 0, 0, w - 2, 0 );
  157. g.drawLine( 0, 0, 0, h - 1 );
  158. g.drawLine( w - 2, 2, w - 2, h - 1 );
  159. g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  160. g.drawLine( w - 1, 1, w - 1, h - 1 );
  161. g.setColor( MetalLookAndFeel.getMenuBackground() );
  162. g.drawLine( w - 1, 0, w - 1, 0 );
  163. }
  164. } else {
  165. if ( model.isArmed() || ( c instanceof JMenu && model.isSelected() ) ) {
  166. g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  167. g.drawLine( 0, 0, w - 1, 0 );
  168. g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  169. g.drawLine( 0, h - 1, w - 1, h - 1 );
  170. } else {
  171. g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  172. g.drawLine( 0, 0, 0, h - 1 );
  173. }
  174. }
  175. g.translate( -x, -y );
  176. }
  177. public Insets getBorderInsets( Component c ) {
  178. return borderInsets;
  179. }
  180. }
  181. public static class PopupMenuBorder extends AbstractBorder implements UIResource {
  182. protected static Insets borderInsets = new Insets( 3, 1, 2, 1 );
  183. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  184. g.translate( x, y );
  185. g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  186. g.drawRect( 0, 0, w - 1, h - 1 );
  187. g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
  188. g.drawLine( 1, 1, w - 2, 1 );
  189. g.drawLine( 1, 2, 1, 2 );
  190. g.drawLine( 1, h - 2, 1, h - 2 );
  191. g.translate( -x, -y );
  192. }
  193. public Insets getBorderInsets( Component c ) {
  194. return borderInsets;
  195. }
  196. }
  197. public static class RolloverButtonBorder extends MetalBorders.ButtonBorder {
  198. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
  199. AbstractButton b = (AbstractButton) c;
  200. ButtonModel model = b.getModel();
  201. if ( model.isRollover() && !( model.isPressed() && !model.isArmed() ) ) {
  202. super.paintBorder( c, g, x, y, w, h );
  203. }
  204. }
  205. }
  206. public static class ToolBarBorder extends AbstractBorder implements UIResource, SwingConstants
  207. {
  208. protected MetalBumps bumps = new MetalBumps( 10, 10,
  209. MetalLookAndFeel.getControlHighlight(),
  210. MetalLookAndFeel.getControlDarkShadow(),
  211. MetalLookAndFeel.getMenuBackground() );
  212. public void paintBorder( Component c, Graphics g, int x, int y, int w, int h )
  213. {
  214. g.translate( x, y );
  215. if ( ((JToolBar) c).isFloatable() )
  216. {
  217. if ( ((JToolBar) c).getOrientation() == HORIZONTAL )
  218. {
  219. bumps.setBumpArea( 10, c.getSize().height - 4 );
  220. if( MetalUtils.isLeftToRight(c) ) {
  221. bumps.paintIcon( c, g, 2, 2 );
  222. } else {
  223. bumps.paintIcon( c, g, c.getBounds().width-12, 2 );
  224. }
  225. }
  226. else // vertical
  227. {
  228. bumps.setBumpArea( c.getSize().width - 4, 10 );
  229. bumps.paintIcon( c, g, 2, 2 );
  230. }
  231. }
  232. g.translate( -x, -y );
  233. }
  234. public Insets getBorderInsets( Component c )
  235. {
  236. Insets borderInsets = new Insets( 2, 2, 2, 2 );
  237. if ( ((JToolBar) c).isFloatable() )
  238. {
  239. if ( ((JToolBar) c).getOrientation() == HORIZONTAL )
  240. {
  241. borderInsets.left = 16;
  242. }
  243. else // vertical
  244. {
  245. borderInsets.top = 16;
  246. }
  247. }
  248. Insets margin = ((JToolBar) c).getMargin();
  249. if ( margin != null )
  250. {
  251. borderInsets.left += margin.left;
  252. borderInsets.top += margin.top;
  253. borderInsets.right += margin.right;
  254. borderInsets.bottom += margin.bottom;
  255. }
  256. return borderInsets;
  257. }
  258. }
  259. public static class TextFieldBorder extends Flush3DBorder {
  260. public void paintBorder(Component c, Graphics g, int x, int y,
  261. int w, int h) {
  262. if (!(c instanceof JTextComponent)) {
  263. // special case for non-text components (bug ID 4144840)
  264. if (c.isEnabled()) {
  265. MetalUtils.drawFlush3DBorder(g, x, y, w, h);
  266. } else {
  267. MetalUtils.drawDisabledBorder(g, x, y, w, h);
  268. }
  269. return;
  270. }
  271. if (c.isEnabled() && ((JTextComponent)c).isEditable()) {
  272. MetalUtils.drawFlush3DBorder(g, x, y, w, h);
  273. } else {
  274. MetalUtils.drawDisabledBorder(g, x, y, w, h);
  275. }
  276. }
  277. }
  278. public static class ScrollPaneBorder extends AbstractBorder implements UIResource {
  279. private static final Insets insets = new Insets(1, 1, 2, 2);
  280. public void paintBorder(Component c, Graphics g, int x, int y,
  281. int w, int h) {
  282. JScrollPane scroll = (JScrollPane)c;
  283. JComponent colHeader = scroll.getColumnHeader();
  284. int colHeaderHeight = 0;
  285. if (colHeader != null)
  286. colHeaderHeight = colHeader.getHeight();
  287. JComponent rowHeader = scroll.getRowHeader();
  288. int rowHeaderWidth = 0;
  289. if (rowHeader != null)
  290. rowHeaderWidth = rowHeader.getWidth();
  291. g.translate( x, y);
  292. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  293. g.drawRect( 0, 0, w-2, h-2 );
  294. g.setColor( MetalLookAndFeel.getControlHighlight() );
  295. g.drawLine( w-1, 1, w-1, h-1);
  296. g.drawLine( 1, h-1, w-1, h-1);
  297. g.setColor( MetalLookAndFeel.getControl() );
  298. g.drawLine( w-2, 2+colHeaderHeight, w-2, 2+colHeaderHeight );
  299. g.drawLine( 1+rowHeaderWidth, h-2, 1+rowHeaderWidth, h-2 );
  300. g.translate( -x, -y);
  301. }
  302. public Insets getBorderInsets(Component c) {
  303. return insets;
  304. }
  305. }
  306. static class ToggleButtonBorder extends ButtonBorder {
  307. public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  308. JToggleButton button = (JToggleButton)c;
  309. ButtonModel model = button.getModel();
  310. if (! c.isEnabled() ) {
  311. MetalUtils.drawDisabledBorder(g, x, y, w, h);
  312. } else {
  313. if ( model.isPressed() && model.isArmed() ) {
  314. MetalUtils.drawPressed3DBorder( g, x, y, w, h );
  315. } else if ( model.isSelected() ) {
  316. MetalUtils.drawDark3DBorder( g, x, y, w, h );
  317. } else {
  318. MetalUtils.drawFlush3DBorder( g, x, y, w, h );
  319. }
  320. }
  321. }
  322. }
  323. }