1. /*
  2. * @(#)MetalUtils.java 1.20 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.plaf.*;
  9. import javax.swing.*;
  10. import java.awt.*;
  11. /**
  12. * This is a dumping ground for random stuff we want to use in several places.
  13. *
  14. * @version 1.20 11/29/01
  15. * @author Steve Wilson
  16. */
  17. class MetalUtils {
  18. static void drawFlush3DBorder(Graphics g, Rectangle r) {
  19. drawFlush3DBorder(g, r.x, r.y, r.width, r.height);
  20. }
  21. /**
  22. * This draws the "Flush 3D Border" which is used throughout the Metal L&F
  23. */
  24. static void drawFlush3DBorder(Graphics g, int x, int y, int w, int h) {
  25. g.translate( x, y);
  26. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  27. g.drawRect( 0, 0, w-2, h-2 );
  28. g.setColor( MetalLookAndFeel.getControlHighlight() );
  29. g.drawRect( 1, 1, w-2, h-2 );
  30. g.setColor( MetalLookAndFeel.getControl() );
  31. g.drawLine( 0, h-1, 1, h-2 );
  32. g.drawLine( w-1, 0, w-2, 1 );
  33. g.translate( -x, -y);
  34. }
  35. /**
  36. * This draws a variant "Flush 3D Border"
  37. * It is used for things like pressed buttons.
  38. */
  39. static void drawPressed3DBorder(Graphics g, Rectangle r) {
  40. drawPressed3DBorder( g, r.x, r.y, r.width, r.height );
  41. }
  42. static void drawDisabledBorder(Graphics g, int x, int y, int w, int h) {
  43. g.translate( x, y);
  44. g.setColor( MetalLookAndFeel.getControlShadow() );
  45. g.drawRect( 0, 0, w-1, h-1 );
  46. }
  47. /**
  48. * This draws a variant "Flush 3D Border"
  49. * It is used for things like pressed buttons.
  50. */
  51. static void drawPressed3DBorder(Graphics g, int x, int y, int w, int h) {
  52. g.translate( x, y);
  53. drawFlush3DBorder(g, 0, 0, w, h);
  54. g.setColor( MetalLookAndFeel.getControlShadow() );
  55. g.drawLine( 1, 1, 1, h-2 );
  56. g.drawLine( 1, 1, w-2, 1 );
  57. g.translate( -x, -y);
  58. }
  59. /**
  60. * This draws a variant "Flush 3D Border"
  61. * It is used for things like active toggle buttons.
  62. * This is used rarely.
  63. */
  64. static void drawDark3DBorder(Graphics g, Rectangle r) {
  65. drawDark3DBorder(g, r.x, r.y, r.width, r.height);
  66. }
  67. /**
  68. * This draws a variant "Flush 3D Border"
  69. * It is used for things like active toggle buttons.
  70. * This is used rarely.
  71. */
  72. static void drawDark3DBorder(Graphics g, int x, int y, int w, int h) {
  73. g.translate( x, y);
  74. drawFlush3DBorder(g, 0, 0, w, h);
  75. g.setColor( MetalLookAndFeel.getControl() );
  76. g.drawLine( 1, 1, 1, h-2 );
  77. g.drawLine( 1, 1, w-2, 1 );
  78. g.setColor( MetalLookAndFeel.getControlShadow() );
  79. g.drawLine( 1, h-2, 1, h-2 );
  80. g.drawLine( w-2, 1, w-2, 1 );
  81. g.translate( -x, -y);
  82. }
  83. static void drawButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
  84. if (active) {
  85. drawActiveButtonBorder(g, x, y, w, h);
  86. } else {
  87. drawFlush3DBorder(g, x, y, w, h);
  88. }
  89. }
  90. static void drawActiveButtonBorder(Graphics g, int x, int y, int w, int h) {
  91. drawFlush3DBorder(g, x, y, w, h);
  92. g.setColor( MetalLookAndFeel.getPrimaryControl() );
  93. g.drawLine( x+1, y+1, x+1, h-3 );
  94. g.drawLine( x+1, y+1, w-3, x+1 );
  95. g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  96. g.drawLine( x+2, h-2, w-2, h-2 );
  97. g.drawLine( w-2, y+2, w-2, h-2 );
  98. }
  99. static void drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
  100. drawButtonBorder(g, x+1, y+1, w-1, h-1, active);
  101. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  102. g.drawRect( x, y, w-3, h-3 );
  103. g.drawLine( w-2, 0, w-2, 0);
  104. g.drawLine( 0, h-2, 0, h-2);
  105. }
  106. /*
  107. * Convenience function for determining ComponentOrientation. Helps us
  108. * avoid having Munge directives throughout the code.
  109. */
  110. static boolean isLeftToRight( Component c ) {
  111. return c.getComponentOrientation().isLeftToRight();
  112. }
  113. static class TableHeaderBorder extends javax.swing.border.AbstractBorder {
  114. protected Insets editorBorderInsets = new Insets( 2, 2, 2, 0 );
  115. public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  116. g.translate( x, y );
  117. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  118. g.drawLine( w-1, 0, w-1, h-2 );
  119. g.drawLine( 1, h-2, w-1, h-2 );
  120. g.setColor( MetalLookAndFeel.getControlHighlight() );
  121. g.drawLine( 0, 0, w-2, 0 );
  122. g.drawLine( 0, 0, 0, h-3 );
  123. g.setColor( MetalLookAndFeel.getControlShadow() );
  124. g.drawLine( 0, h-1, w-1, h-1 );
  125. g.translate( -x, -y );
  126. }
  127. public Insets getBorderInsets( Component c ) {
  128. return editorBorderInsets;
  129. }
  130. }
  131. }