1. /*
  2. * @(#)MetalUtils.java 1.35 04/02/15
  3. *
  4. * Copyright 2004 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. import java.awt.image.*;
  12. import java.lang.ref.*;
  13. import java.util.*;
  14. /**
  15. * This is a dumping ground for random stuff we want to use in several places.
  16. *
  17. * @version 1.35 02/15/04
  18. * @author Steve Wilson
  19. */
  20. class MetalUtils {
  21. static void drawFlush3DBorder(Graphics g, Rectangle r) {
  22. drawFlush3DBorder(g, r.x, r.y, r.width, r.height);
  23. }
  24. /**
  25. * This draws the "Flush 3D Border" which is used throughout the Metal L&F
  26. */
  27. static void drawFlush3DBorder(Graphics g, int x, int y, int w, int h) {
  28. g.translate( x, y);
  29. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  30. g.drawRect( 0, 0, w-2, h-2 );
  31. g.setColor( MetalLookAndFeel.getControlHighlight() );
  32. g.drawRect( 1, 1, w-2, h-2 );
  33. g.setColor( MetalLookAndFeel.getControl() );
  34. g.drawLine( 0, h-1, 1, h-2 );
  35. g.drawLine( w-1, 0, w-2, 1 );
  36. g.translate( -x, -y);
  37. }
  38. /**
  39. * This draws a variant "Flush 3D Border"
  40. * It is used for things like pressed buttons.
  41. */
  42. static void drawPressed3DBorder(Graphics g, Rectangle r) {
  43. drawPressed3DBorder( g, r.x, r.y, r.width, r.height );
  44. }
  45. static void drawDisabledBorder(Graphics g, int x, int y, int w, int h) {
  46. g.translate( x, y);
  47. g.setColor( MetalLookAndFeel.getControlShadow() );
  48. g.drawRect( 0, 0, w-1, h-1 );
  49. g.translate(-x, -y);
  50. }
  51. /**
  52. * This draws a variant "Flush 3D Border"
  53. * It is used for things like pressed buttons.
  54. */
  55. static void drawPressed3DBorder(Graphics g, int x, int y, int w, int h) {
  56. g.translate( x, y);
  57. drawFlush3DBorder(g, 0, 0, w, h);
  58. g.setColor( MetalLookAndFeel.getControlShadow() );
  59. g.drawLine( 1, 1, 1, h-2 );
  60. g.drawLine( 1, 1, w-2, 1 );
  61. g.translate( -x, -y);
  62. }
  63. /**
  64. * This draws a variant "Flush 3D Border"
  65. * It is used for things like active toggle buttons.
  66. * This is used rarely.
  67. */
  68. static void drawDark3DBorder(Graphics g, Rectangle r) {
  69. drawDark3DBorder(g, r.x, r.y, r.width, r.height);
  70. }
  71. /**
  72. * This draws a variant "Flush 3D Border"
  73. * It is used for things like active toggle buttons.
  74. * This is used rarely.
  75. */
  76. static void drawDark3DBorder(Graphics g, int x, int y, int w, int h) {
  77. g.translate( x, y);
  78. drawFlush3DBorder(g, 0, 0, w, h);
  79. g.setColor( MetalLookAndFeel.getControl() );
  80. g.drawLine( 1, 1, 1, h-2 );
  81. g.drawLine( 1, 1, w-2, 1 );
  82. g.setColor( MetalLookAndFeel.getControlShadow() );
  83. g.drawLine( 1, h-2, 1, h-2 );
  84. g.drawLine( w-2, 1, w-2, 1 );
  85. g.translate( -x, -y);
  86. }
  87. static void drawButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
  88. if (active) {
  89. drawActiveButtonBorder(g, x, y, w, h);
  90. } else {
  91. drawFlush3DBorder(g, x, y, w, h);
  92. }
  93. }
  94. static void drawActiveButtonBorder(Graphics g, int x, int y, int w, int h) {
  95. drawFlush3DBorder(g, x, y, w, h);
  96. g.setColor( MetalLookAndFeel.getPrimaryControl() );
  97. g.drawLine( x+1, y+1, x+1, h-3 );
  98. g.drawLine( x+1, y+1, w-3, x+1 );
  99. g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  100. g.drawLine( x+2, h-2, w-2, h-2 );
  101. g.drawLine( w-2, y+2, w-2, h-2 );
  102. }
  103. static void drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
  104. drawButtonBorder(g, x+1, y+1, w-1, h-1, active);
  105. g.translate(x, y);
  106. g.setColor( MetalLookAndFeel.getControlDarkShadow() );
  107. g.drawRect( 0, 0, w-3, h-3 );
  108. g.drawLine( w-2, 0, w-2, 0);
  109. g.drawLine( 0, h-2, 0, h-2);
  110. g.translate(-x, -y);
  111. }
  112. static void drawDefaultButtonPressedBorder(Graphics g, int x, int y, int w, int h) {
  113. drawPressed3DBorder(g, x + 1, y + 1, w - 1, h - 1);
  114. g.translate(x, y);
  115. g.setColor(MetalLookAndFeel.getControlDarkShadow());
  116. g.drawRect(0, 0, w - 3, h - 3);
  117. g.drawLine(w - 2, 0, w - 2, 0);
  118. g.drawLine(0, h - 2, 0, h - 2);
  119. g.setColor(MetalLookAndFeel.getControl());
  120. g.drawLine(w - 1, 0, w - 1, 0);
  121. g.drawLine(0, h - 1, 0, h - 1);
  122. g.translate(-x, -y);
  123. }
  124. /*
  125. * Convenience function for determining ComponentOrientation. Helps us
  126. * avoid having Munge directives throughout the code.
  127. */
  128. static boolean isLeftToRight( Component c ) {
  129. return c.getComponentOrientation().isLeftToRight();
  130. }
  131. static int getInt(Object key, int defaultValue) {
  132. Object value = UIManager.get(key);
  133. if (value instanceof Integer) {
  134. return ((Integer)value).intValue();
  135. }
  136. if (value instanceof String) {
  137. try {
  138. return Integer.parseInt((String)value);
  139. } catch (NumberFormatException nfe) {}
  140. }
  141. return defaultValue;
  142. }
  143. //
  144. // Ocean specific stuff.
  145. //
  146. /**
  147. * Draws a radial type gradient. The gradient will be drawn vertically if
  148. * <code>vertical</code> is true, otherwise horizontally.
  149. * The UIManager key consists of five values:
  150. * r1 r2 c1 c2 c3. The gradient is broken down into four chunks drawn
  151. * in order from the origin.
  152. * <ol>
  153. * <li>Gradient r1 % of the size from c1 to c2
  154. * <li>Rectangle r2 % of the size in c2.
  155. * <li>Gradient r1 % of the size from c2 to c1
  156. * <li>The remaining size will be filled with a gradient from c1 to c3.
  157. * </ol>
  158. *
  159. * @param c Component rendering to
  160. * @param g Graphics to draw to.
  161. * @param key UIManager key used to look up gradient values.
  162. * @param x X coordinate to draw from
  163. * @param y Y coordinate to draw from
  164. * @param w Width to draw to
  165. * @param h Height to draw to
  166. * @param vertical Direction of the gradient
  167. * @return true if <code>key</code> exists, otherwise false.
  168. */
  169. static boolean drawGradient(Component c, Graphics g, String key,
  170. int x, int y, int w, int h, boolean vertical) {
  171. java.util.List gradient = (java.util.List)UIManager.get(key);
  172. if (gradient == null || !(g instanceof Graphics2D)) {
  173. return false;
  174. }
  175. if (w <= 0 || h <= 0) {
  176. return true;
  177. }
  178. GradientPainter.INSTANCE.paint(
  179. c, (Graphics2D)g, gradient, x, y, w, h, vertical);
  180. return true;
  181. }
  182. private static class GradientPainter extends CachedPainter {
  183. /**
  184. * Instance used for painting. This is the only instance that is
  185. * ever created.
  186. */
  187. public static final GradientPainter INSTANCE = new GradientPainter(8);
  188. // Size of images to create. For vertical gradients this is the width,
  189. // otherwise it's the height.
  190. private static final int IMAGE_SIZE = 64;
  191. /**
  192. * This is the actual width we're painting in, or last painted to.
  193. */
  194. private int w;
  195. /**
  196. * This is the actual height we're painting in, or last painted to
  197. */
  198. private int h;
  199. GradientPainter(int count) {
  200. super(count);
  201. }
  202. public synchronized void paint(Component c, Graphics2D g,
  203. java.util.List gradient, int x, int y, int w,
  204. int h, boolean isVertical) {
  205. int imageWidth;
  206. int imageHeight;
  207. if (isVertical) {
  208. imageWidth = IMAGE_SIZE;
  209. imageHeight = h;
  210. }
  211. else {
  212. imageWidth = w;
  213. imageHeight = IMAGE_SIZE;
  214. }
  215. this.w = w;
  216. this.h = h;
  217. paint(c, g, x, y, imageWidth, imageHeight,
  218. gradient, isVertical);
  219. }
  220. protected void paintToImage(Component c, Graphics g,
  221. int w, int h, Object[] args) {
  222. Graphics2D g2 = (Graphics2D)g;
  223. java.util.List gradient = (java.util.List)args[0];
  224. boolean isVertical = ((Boolean)args[1]).booleanValue();
  225. // Render to the VolatileImage
  226. if (isVertical) {
  227. drawVerticalGradient(g2,
  228. ((Number)gradient.get(0)).floatValue(),
  229. ((Number)gradient.get(1)).floatValue(),
  230. (Color)gradient.get(2),
  231. (Color)gradient.get(3),
  232. (Color)gradient.get(4), w, h);
  233. }
  234. else {
  235. drawHorizontalGradient(g2,
  236. ((Number)gradient.get(0)).floatValue(),
  237. ((Number)gradient.get(1)).floatValue(),
  238. (Color)gradient.get(2),
  239. (Color)gradient.get(3),
  240. (Color)gradient.get(4), w, h);
  241. }
  242. }
  243. protected void paintImage(Component c, Graphics g,
  244. int x, int y, int imageW, int imageH,
  245. Image image, Object[] args) {
  246. boolean isVertical = ((Boolean)args[1]).booleanValue();
  247. // Render to the screen
  248. g.translate(x, y);
  249. if (isVertical) {
  250. for (int counter = 0; counter < w; counter += IMAGE_SIZE) {
  251. int tileSize = Math.min(IMAGE_SIZE, w - counter);
  252. g.drawImage(image, counter, 0, counter + tileSize, h,
  253. 0, 0, tileSize, h, null);
  254. }
  255. }
  256. else {
  257. for (int counter = 0; counter < h; counter += IMAGE_SIZE) {
  258. int tileSize = Math.min(IMAGE_SIZE, h - counter);
  259. g.drawImage(image, 0, counter, w, counter + tileSize,
  260. 0, 0, w, tileSize, null);
  261. }
  262. }
  263. g.translate(-x, -y);
  264. }
  265. private void drawVerticalGradient(Graphics2D g, float ratio1,
  266. float ratio2, Color c1,Color c2,
  267. Color c3, int w, int h) {
  268. int mid = (int)(ratio1 * h);
  269. int mid2 = (int)(ratio2 * h);
  270. if (mid > 0) {
  271. g.setPaint(getGradient((float)0, (float)0, c1, (float)0,
  272. (float)mid, c2));
  273. g.fillRect(0, 0, w, mid);
  274. }
  275. if (mid2 > 0) {
  276. g.setColor(c2);
  277. g.fillRect(0, mid, w, mid2);
  278. }
  279. if (mid > 0) {
  280. g.setPaint(getGradient((float)0, (float)mid + mid2, c2,
  281. (float)0, (float)mid * 2 + mid2, c1));
  282. g.fillRect(0, mid + mid2, w, mid);
  283. }
  284. if (h - mid * 2 - mid2 > 0) {
  285. g.setPaint(getGradient((float)0, (float)mid * 2 + mid2, c1,
  286. (float)0, (float)h, c3));
  287. g.fillRect(0, mid * 2 + mid2, w, h - mid * 2 - mid2);
  288. }
  289. }
  290. private void drawHorizontalGradient(Graphics2D g, float ratio1,
  291. float ratio2, Color c1,Color c2,
  292. Color c3, int w, int h) {
  293. int mid = (int)(ratio1 * w);
  294. int mid2 = (int)(ratio2 * w);
  295. if (mid > 0) {
  296. g.setPaint(getGradient((float)0, (float)0, c1,
  297. (float)mid, (float)0, c2));
  298. g.fillRect(0, 0, mid, h);
  299. }
  300. if (mid2 > 0) {
  301. g.setColor(c2);
  302. g.fillRect(mid, 0, mid2, h);
  303. }
  304. if (mid > 0) {
  305. g.setPaint(getGradient((float)mid + mid2, (float)0, c2,
  306. (float)mid * 2 + mid2, (float)0, c1));
  307. g.fillRect(mid + mid2, 0, mid, h);
  308. }
  309. if (w - mid * 2 - mid2 > 0) {
  310. g.setPaint(getGradient((float)mid * 2 + mid2, (float)0, c1,
  311. w, (float)0, c3));
  312. g.fillRect(mid * 2 + mid2, 0, w - mid * 2 - mid2, h);
  313. }
  314. }
  315. private GradientPaint getGradient(float x1, float y1,
  316. Color c1, float x2, float y2,
  317. Color c2) {
  318. return new GradientPaint(x1, y1, c1, x2, y2, c2, true);
  319. }
  320. }
  321. /**
  322. * Returns true if the specified widget is in a toolbar.
  323. */
  324. static boolean isToolBarButton(JComponent c) {
  325. return (c.getParent() instanceof JToolBar);
  326. }
  327. static Icon getOceanToolBarIcon(Image i) {
  328. ImageProducer prod = new FilteredImageSource(i.getSource(),
  329. new OceanToolBarImageFilter());
  330. return new IconUIResource(new ImageIcon(
  331. Toolkit.getDefaultToolkit().createImage(prod)));
  332. }
  333. static Icon getOceanDisabledButtonIcon(Image image) {
  334. Object[] range = (Object[])UIManager.get("Button.disabledGrayRange");
  335. int min = 180;
  336. int max = 215;
  337. if (range != null) {
  338. min = ((Integer)range[0]).intValue();
  339. max = ((Integer)range[1]).intValue();
  340. }
  341. ImageProducer prod = new FilteredImageSource(image.getSource(),
  342. new OceanDisabledButtonImageFilter(min , max));
  343. return new IconUIResource(new ImageIcon(
  344. Toolkit.getDefaultToolkit().createImage(prod)));
  345. }
  346. /**
  347. * Used to create a disabled Icon with the ocean look.
  348. */
  349. private static class OceanDisabledButtonImageFilter extends RGBImageFilter{
  350. private float min;
  351. private float factor;
  352. OceanDisabledButtonImageFilter(int min, int max) {
  353. canFilterIndexColorModel = true;
  354. this.min = (float)min;
  355. this.factor = (max - min) / 255f;
  356. }
  357. public int filterRGB(int x, int y, int rgb) {
  358. // Coefficients are from the sRGB color space:
  359. int gray = Math.min(255, (int)(((0.2125f * ((rgb >> 16) & 0xFF)) +
  360. (0.7154f * ((rgb >> 8) & 0xFF)) +
  361. (0.0721f * (rgb & 0xFF)) + .5f) * factor + min));
  362. return (rgb & 0xff000000) | (gray << 16) | (gray << 8) |
  363. (gray << 0);
  364. }
  365. }
  366. /**
  367. * Used to create the rollover icons with the ocean look.
  368. */
  369. private static class OceanToolBarImageFilter extends RGBImageFilter {
  370. OceanToolBarImageFilter() {
  371. canFilterIndexColorModel = true;
  372. }
  373. public int filterRGB(int x, int y, int rgb) {
  374. int r = ((rgb >> 16) & 0xff);
  375. int g = ((rgb >> 8) & 0xff);
  376. int b = (rgb & 0xff);
  377. int gray = Math.max(Math.max(r, g), b);
  378. return (rgb & 0xff000000) | (gray << 16) | (gray << 8) |
  379. (gray << 0);
  380. }
  381. }
  382. }