1. /*
  2. * @(#)MetalUtils.java 1.28 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.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.28 01/23/03
  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. g.translate(-x, -y);
  47. }
  48. /**
  49. * This draws a variant "Flush 3D Border"
  50. * It is used for things like pressed buttons.
  51. */
  52. static void drawPressed3DBorder(Graphics g, int x, int y, int w, int h) {
  53. g.translate( x, y);
  54. drawFlush3DBorder(g, 0, 0, w, h);
  55. g.setColor( MetalLookAndFeel.getControlShadow() );
  56. g.drawLine( 1, 1, 1, h-2 );
  57. g.drawLine( 1, 1, w-2, 1 );
  58. g.translate( -x, -y);
  59. }
  60. /**
  61. * This draws a variant "Flush 3D Border"
  62. * It is used for things like active toggle buttons.
  63. * This is used rarely.
  64. */
  65. static void drawDark3DBorder(Graphics g, Rectangle r) {
  66. drawDark3DBorder(g, r.x, r.y, r.width, r.height);
  67. }
  68. /**
  69. * This draws a variant "Flush 3D Border"
  70. * It is used for things like active toggle buttons.
  71. * This is used rarely.
  72. */
  73. static void drawDark3DBorder(Graphics g, int x, int y, int w, int h) {
  74. g.translate( x, y);
  75. drawFlush3DBorder(g, 0, 0, w, h);
  76. g.setColor( MetalLookAndFeel.getControl() );
  77. g.drawLine( 1, 1, 1, h-2 );
  78. g.drawLine( 1, 1, w-2, 1 );
  79. g.setColor( MetalLookAndFeel.getControlShadow() );
  80. g.drawLine( 1, h-2, 1, h-2 );
  81. g.drawLine( w-2, 1, w-2, 1 );
  82. g.translate( -x, -y);
  83. }
  84. static void drawButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
  85. if (active) {
  86. drawActiveButtonBorder(g, x, y, w, h);
  87. } else {
  88. drawFlush3DBorder(g, x, y, w, h);
  89. }
  90. }
  91. static void drawActiveButtonBorder(Graphics g, int x, int y, int w, int h) {
  92. drawFlush3DBorder(g, x, y, w, h);
  93. g.setColor( MetalLookAndFeel.getPrimaryControl() );
  94. g.drawLine( x+1, y+1, x+1, h-3 );
  95. g.drawLine( x+1, y+1, w-3, x+1 );
  96. g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  97. g.drawLine( x+2, h-2, w-2, h-2 );
  98. g.drawLine( w-2, y+2, w-2, h-2 );
  99. }
  100. static void drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
  101. drawButtonBorder(g, x+1, y+1, w-1, h-1, active);
  102. g.translate(x, y);
  103. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  104. g.drawRect( 0, 0, w-3, h-3 );
  105. g.drawLine( w-2, 0, w-2, 0);
  106. g.drawLine( 0, h-2, 0, h-2);
  107. g.translate(-x, -y);
  108. }
  109. static void drawDefaultButtonPressedBorder(Graphics g, int x, int y, int w, int h) {
  110. drawPressed3DBorder(g, x + 1, y + 1, w - 1, h - 1);
  111. g.translate(x, y);
  112. g.setColor(MetalLookAndFeel.getControlDarkShadow());
  113. g.drawRect(0, 0, w - 3, h - 3);
  114. g.drawLine(w - 2, 0, w - 2, 0);
  115. g.drawLine(0, h - 2, 0, h - 2);
  116. g.setColor(MetalLookAndFeel.getControl());
  117. g.drawLine(w - 1, 0, w - 1, 0);
  118. g.drawLine(0, h - 1, 0, h - 1);
  119. g.translate(-x, -y);
  120. }
  121. /*
  122. * Convenience function for determining ComponentOrientation. Helps us
  123. * avoid having Munge directives throughout the code.
  124. */
  125. static boolean isLeftToRight( Component c ) {
  126. return c.getComponentOrientation().isLeftToRight();
  127. }
  128. static int getInt(Object key, int defaultValue) {
  129. Object value = UIManager.get(key);
  130. if (value instanceof Integer) {
  131. return ((Integer)value).intValue();
  132. }
  133. if (value instanceof String) {
  134. try {
  135. return Integer.parseInt((String)value);
  136. } catch (NumberFormatException nfe) {}
  137. }
  138. return defaultValue;
  139. }
  140. }