KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalUtils


1 /*
2  * @(#)MetalUtils.java 1.36 06/11/24
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7  
8 package javax.swing.plaf.metal;
9
10 import javax.swing.plaf.*;
11 import javax.swing.*;
12 import java.awt.*;
13 import java.awt.image.*;
14 import java.lang.ref.*;
15 import java.util.*;
16
17 /**
18  * This is a dumping ground for random stuff we want to use in several places.
19  *
20  * @version 1.35 02/15/04
21  * @author Steve Wilson
22  */

23
24 class MetalUtils {
25
26     static void drawFlush3DBorder(Graphics g, Rectangle r) {
27         drawFlush3DBorder(g, r.x, r.y, r.width, r.height);
28     }
29
30     /**
31       * This draws the "Flush 3D Border" which is used throughout the Metal L&F
32       */

33     static void drawFlush3DBorder(Graphics g, int x, int y, int w, int h) {
34         g.translate( x, y);
35         g.setColor( MetalLookAndFeel.getControlDarkShadow() );
36     g.drawRect( 0, 0, w-2, h-2 );
37         g.setColor( MetalLookAndFeel.getControlHighlight() );
38     g.drawRect( 1, 1, w-2, h-2 );
39         g.setColor( MetalLookAndFeel.getControl() );
40     g.drawLine( 0, h-1, 1, h-2 );
41     g.drawLine( w-1, 0, w-2, 1 );
42         g.translate( -x, -y);
43     }
44
45     /**
46       * This draws a variant "Flush 3D Border"
47       * It is used for things like pressed buttons.
48       */

49     static void drawPressed3DBorder(Graphics g, Rectangle r) {
50         drawPressed3DBorder( g, r.x, r.y, r.width, r.height );
51     }
52
53     static void drawDisabledBorder(Graphics g, int x, int y, int w, int h) {
54         g.translate( x, y);
55         g.setColor( MetalLookAndFeel.getControlShadow() );
56     g.drawRect( 0, 0, w-1, h-1 );
57         g.translate(-x, -y);
58     }
59
60     /**
61       * This draws a variant "Flush 3D Border"
62       * It is used for things like pressed buttons.
63       */

64     static void drawPressed3DBorder(Graphics g, int x, int y, int w, int h) {
65         g.translate( x, y);
66
67         drawFlush3DBorder(g, 0, 0, w, h);
68
69         g.setColor( MetalLookAndFeel.getControlShadow() );
70     g.drawLine( 1, 1, 1, h-2 );
71     g.drawLine( 1, 1, w-2, 1 );
72         g.translate( -x, -y);
73     }
74
75     /**
76       * This draws a variant "Flush 3D Border"
77       * It is used for things like active toggle buttons.
78       * This is used rarely.
79       */

80     static void drawDark3DBorder(Graphics g, Rectangle r) {
81         drawDark3DBorder(g, r.x, r.y, r.width, r.height);
82     }
83
84     /**
85       * This draws a variant "Flush 3D Border"
86       * It is used for things like active toggle buttons.
87       * This is used rarely.
88       */

89     static void drawDark3DBorder(Graphics g, int x, int y, int w, int h) {
90         g.translate( x, y);
91
92         drawFlush3DBorder(g, 0, 0, w, h);
93
94         g.setColor( MetalLookAndFeel.getControl() );
95     g.drawLine( 1, 1, 1, h-2 );
96     g.drawLine( 1, 1, w-2, 1 );
97         g.setColor( MetalLookAndFeel.getControlShadow() );
98     g.drawLine( 1, h-2, 1, h-2 );
99     g.drawLine( w-2, 1, w-2, 1 );
100         g.translate( -x, -y);
101     }
102
103     static void drawButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
104         if (active) {
105             drawActiveButtonBorder(g, x, y, w, h);
106         } else {
107             drawFlush3DBorder(g, x, y, w, h);
108     }
109     }
110
111     static void drawActiveButtonBorder(Graphics g, int x, int y, int w, int h) {
112         drawFlush3DBorder(g, x, y, w, h);
113         g.setColor( MetalLookAndFeel.getPrimaryControl() );
114     g.drawLine( x+1, y+1, x+1, h-3 );
115     g.drawLine( x+1, y+1, w-3, x+1 );
116         g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
117     g.drawLine( x+2, h-2, w-2, h-2 );
118     g.drawLine( w-2, y+2, w-2, h-2 );
119     }
120
121     static void drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
122         drawButtonBorder(g, x+1, y+1, w-1, h-1, active);
123         g.translate(x, y);
124         g.setColor( MetalLookAndFeel.getControlDarkShadow() );
125     g.drawRect( 0, 0, w-3, h-3 );
126     g.drawLine( w-2, 0, w-2, 0);
127     g.drawLine( 0, h-2, 0, h-2);
128         g.translate(-x, -y);
129     }
130     
131     static void drawDefaultButtonPressedBorder(Graphics g, int x, int y, int w, int h) {
132         drawPressed3DBorder(g, x + 1, y + 1, w - 1, h - 1);
133         g.translate(x, y);
134         g.setColor(MetalLookAndFeel.getControlDarkShadow());
135         g.drawRect(0, 0, w - 3, h - 3);
136         g.drawLine(w - 2, 0, w - 2, 0);
137         g.drawLine(0, h - 2, 0, h - 2);
138         g.setColor(MetalLookAndFeel.getControl());
139         g.drawLine(w - 1, 0, w - 1, 0);
140         g.drawLine(0, h - 1, 0, h - 1);
141         g.translate(-x, -y);
142     }
143
144     /*
145      * Convenience function for determining ComponentOrientation. Helps us
146      * avoid having Munge directives throughout the code.
147      */

148     static boolean isLeftToRight( Component c ) {
149         return c.getComponentOrientation().isLeftToRight();
150     }
151     
152     static int getInt(Object JavaDoc key, int defaultValue) {
153         Object JavaDoc value = UIManager.get(key);
154
155         if (value instanceof Integer JavaDoc) {
156             return ((Integer JavaDoc)value).intValue();
157         }
158         if (value instanceof String JavaDoc) {
159             try {
160                 return Integer.parseInt((String JavaDoc)value);
161             } catch (NumberFormatException JavaDoc nfe) {}
162         }
163         return defaultValue;
164     }
165
166     //
167
// Ocean specific stuff.
168
//
169
/**
170      * Draws a radial type gradient. The gradient will be drawn vertically if
171      * <code>vertical</code> is true, otherwise horizontally.
172      * The UIManager key consists of five values:
173      * r1 r2 c1 c2 c3. The gradient is broken down into four chunks drawn
174      * in order from the origin.
175      * <ol>
176      * <li>Gradient r1 % of the size from c1 to c2
177      * <li>Rectangle r2 % of the size in c2.
178      * <li>Gradient r1 % of the size from c2 to c1
179      * <li>The remaining size will be filled with a gradient from c1 to c3.
180      * </ol>
181      *
182      * @param c Component rendering to
183      * @param g Graphics to draw to.
184      * @param key UIManager key used to look up gradient values.
185      * @param x X coordinate to draw from
186      * @param y Y coordinate to draw from
187      * @param w Width to draw to
188      * @param h Height to draw to
189      * @param vertical Direction of the gradient
190      * @return true if <code>key</code> exists, otherwise false.
191      */

192     static boolean drawGradient(Component c, Graphics g, String JavaDoc key,
193                                 int x, int y, int w, int h, boolean vertical) {
194         java.util.List JavaDoc gradient = (java.util.List JavaDoc)UIManager.get(key);
195         if (gradient == null || !(g instanceof Graphics2D)) {
196             return false;
197         }
198
199         if (w <= 0 || h <= 0) {
200             return true;
201         }
202
203         GradientPainter.INSTANCE.paint(
204                 c, (Graphics2D)g, gradient, x, y, w, h, vertical);
205         return true;
206     }
207
208
209     private static class GradientPainter extends CachedPainter JavaDoc {
210         /**
211          * Instance used for painting. This is the only instance that is
212          * ever created.
213          */

214         public static final GradientPainter INSTANCE = new GradientPainter(8);
215
216         // Size of images to create. For vertical gradients this is the width,
217
// otherwise it's the height.
218
private static final int IMAGE_SIZE = 64;
219
220         /**
221          * This is the actual width we're painting in, or last painted to.
222          */

223         private int w;
224         /**
225          * This is the actual height we're painting in, or last painted to
226          */

227         private int h;
228
229
230         GradientPainter(int count) {
231             super(count);
232         }
233
234         public void paint(Component c, Graphics2D g,
235                           java.util.List JavaDoc gradient, int x, int y, int w,
236                           int h, boolean isVertical) {
237             int imageWidth;
238             int imageHeight;
239             if (isVertical) {
240                 imageWidth = IMAGE_SIZE;
241                 imageHeight = h;
242             }
243             else {
244                 imageWidth = w;
245                 imageHeight = IMAGE_SIZE;
246             }
247             synchronized(c.getTreeLock()) {
248                 this.w = w;
249                 this.h = h;
250                 paint(c, g, x, y, imageWidth, imageHeight,
251                       gradient, isVertical);
252             }
253         }
254
255         protected void paintToImage(Component c, Graphics g,
256                                     int w, int h, Object JavaDoc[] args) {
257             Graphics2D g2 = (Graphics2D)g;
258             java.util.List JavaDoc gradient = (java.util.List JavaDoc)args[0];
259             boolean isVertical = ((Boolean JavaDoc)args[1]).booleanValue();
260             // Render to the VolatileImage
261
if (isVertical) {
262                 drawVerticalGradient(g2,
263                                      ((Number JavaDoc)gradient.get(0)).floatValue(),
264                                      ((Number JavaDoc)gradient.get(1)).floatValue(),
265                                      (Color)gradient.get(2),
266                                      (Color)gradient.get(3),
267                                      (Color)gradient.get(4), w, h);
268             }
269             else {
270                 drawHorizontalGradient(g2,
271                                       ((Number JavaDoc)gradient.get(0)).floatValue(),
272                                       ((Number JavaDoc)gradient.get(1)).floatValue(),
273                                       (Color)gradient.get(2),
274                                       (Color)gradient.get(3),
275                                       (Color)gradient.get(4), w, h);
276             }
277         }
278
279         protected void paintImage(Component c, Graphics g,
280                                   int x, int y, int imageW, int imageH,
281                                   Image image, Object JavaDoc[] args) {
282             boolean isVertical = ((Boolean JavaDoc)args[1]).booleanValue();
283             // Render to the screen
284
g.translate(x, y);
285             if (isVertical) {
286                 for (int counter = 0; counter < w; counter += IMAGE_SIZE) {
287                     int tileSize = Math.min(IMAGE_SIZE, w - counter);
288                     g.drawImage(image, counter, 0, counter + tileSize, h,
289                                 0, 0, tileSize, h, null);
290                 }
291             }
292             else {
293                 for (int counter = 0; counter < h; counter += IMAGE_SIZE) {
294                     int tileSize = Math.min(IMAGE_SIZE, h - counter);
295                     g.drawImage(image, 0, counter, w, counter + tileSize,
296                                 0, 0, w, tileSize, null);
297                 }
298             }
299             g.translate(-x, -y);
300         }
301
302         private void drawVerticalGradient(Graphics2D g, float ratio1,
303                                           float ratio2, Color c1,Color c2,
304                                           Color c3, int w, int h) {
305             int mid = (int)(ratio1 * h);
306             int mid2 = (int)(ratio2 * h);
307             if (mid > 0) {
308                 g.setPaint(getGradient((float)0, (float)0, c1, (float)0,
309                                        (float)mid, c2));
310                 g.fillRect(0, 0, w, mid);
311             }
312             if (mid2 > 0) {
313                 g.setColor(c2);
314                 g.fillRect(0, mid, w, mid2);
315             }
316             if (mid > 0) {
317                 g.setPaint(getGradient((float)0, (float)mid + mid2, c2,
318                                        (float)0, (float)mid * 2 + mid2, c1));
319                 g.fillRect(0, mid + mid2, w, mid);
320             }
321             if (h - mid * 2 - mid2 > 0) {
322                 g.setPaint(getGradient((float)0, (float)mid * 2 + mid2, c1,
323                                        (float)0, (float)h, c3));
324                 g.fillRect(0, mid * 2 + mid2, w, h - mid * 2 - mid2);
325             }
326         }
327
328         private void drawHorizontalGradient(Graphics2D g, float ratio1,
329                                             float ratio2, Color c1,Color c2,
330                                             Color c3, int w, int h) {
331             int mid = (int)(ratio1 * w);
332             int mid2 = (int)(ratio2 * w);
333             if (mid > 0) {
334                 g.setPaint(getGradient((float)0, (float)0, c1,
335                                        (float)mid, (float)0, c2));
336                 g.fillRect(0, 0, mid, h);
337             }
338             if (mid2 > 0) {
339                 g.setColor(c2);
340                 g.fillRect(mid, 0, mid2, h);
341             }
342             if (mid > 0) {
343                 g.setPaint(getGradient((float)mid + mid2, (float)0, c2,
344                                        (float)mid * 2 + mid2, (float)0, c1));
345                 g.fillRect(mid + mid2, 0, mid, h);
346             }
347             if (w - mid * 2 - mid2 > 0) {
348                 g.setPaint(getGradient((float)mid * 2 + mid2, (float)0, c1,
349                                        w, (float)0, c3));
350                 g.fillRect(mid * 2 + mid2, 0, w - mid * 2 - mid2, h);
351             }
352         }
353
354         private GradientPaint getGradient(float x1, float y1,
355                                                  Color c1, float x2, float y2,
356                                                  Color c2) {
357             return new GradientPaint(x1, y1, c1, x2, y2, c2, true);
358         }
359     }
360
361
362     /**
363      * Returns true if the specified widget is in a toolbar.
364      */

365     static boolean isToolBarButton(JComponent c) {
366         return (c.getParent() instanceof JToolBar);
367     }
368
369     static Icon getOceanToolBarIcon(Image i) {
370     ImageProducer prod = new FilteredImageSource(i.getSource(),
371                              new OceanToolBarImageFilter());
372     return new IconUIResource(new ImageIcon(
373                          Toolkit.getDefaultToolkit().createImage(prod)));
374     }
375
376     static Icon getOceanDisabledButtonIcon(Image image) {
377         Object JavaDoc[] range = (Object JavaDoc[])UIManager.get("Button.disabledGrayRange");
378         int min = 180;
379         int max = 215;
380         if (range != null) {
381             min = ((Integer JavaDoc)range[0]).intValue();
382             max = ((Integer JavaDoc)range[1]).intValue();
383         }
384     ImageProducer prod = new FilteredImageSource(image.getSource(),
385                       new OceanDisabledButtonImageFilter(min , max));
386     return new IconUIResource(new ImageIcon(
387                              Toolkit.getDefaultToolkit().createImage(prod)));
388     }
389
390
391
392
393     /**
394      * Used to create a disabled Icon with the ocean look.
395      */

396     private static class OceanDisabledButtonImageFilter extends RGBImageFilter{
397         private float min;
398         private float factor;
399
400         OceanDisabledButtonImageFilter(int min, int max) {
401             canFilterIndexColorModel = true;
402             this.min = (float)min;
403             this.factor = (max - min) / 255f;
404         }
405     
406         public int filterRGB(int x, int y, int rgb) {
407             // Coefficients are from the sRGB color space:
408
int gray = Math.min(255, (int)(((0.2125f * ((rgb >> 16) & 0xFF)) +
409                     (0.7154f * ((rgb >> 8) & 0xFF)) +
410                     (0.0721f * (rgb & 0xFF)) + .5f) * factor + min));
411
412             return (rgb & 0xff000000) | (gray << 16) | (gray << 8) |
413                 (gray << 0);
414         }
415     }
416
417
418     /**
419      * Used to create the rollover icons with the ocean look.
420      */

421     private static class OceanToolBarImageFilter extends RGBImageFilter {
422         OceanToolBarImageFilter() {
423             canFilterIndexColorModel = true;
424         }
425     
426         public int filterRGB(int x, int y, int rgb) {
427             int r = ((rgb >> 16) & 0xff);
428             int g = ((rgb >> 8) & 0xff);
429             int b = (rgb & 0xff);
430             int gray = Math.max(Math.max(r, g), b);
431             return (rgb & 0xff000000) | (gray << 16) | (gray << 8) |
432                 (gray << 0);
433         }
434     }
435 }
436
Popular Tags