KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticUtils


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.plastic;
32
33 import java.awt.*;
34
35 import javax.swing.JComponent JavaDoc;
36 import javax.swing.UIManager JavaDoc;
37
38 /**
39  * Consists exclusively of static methods that provide convenience behavior.
40  *
41  * @author Karsten Lentzsch
42  * @version $Revision: 1.3 $
43  */

44
45 public final class PlasticUtils {
46
47     static void drawDark3DBorder(Graphics g, int x, int y, int w, int h) {
48         drawFlush3DBorder(g, x, y, w, h);
49         g.setColor(PlasticLookAndFeel.getControl());
50         g.drawLine(x+1, y+1, 1, h - 3);
51         g.drawLine(y+1, y+1, w - 3, 1);
52     }
53
54
55     static void drawDisabledBorder(Graphics g, int x, int y, int w, int h) {
56         g.setColor(PlasticLookAndFeel.getControlShadow());
57         drawRect(g, x, y, w - 1, h - 1);
58     }
59
60
61     /*
62      * Unlike <code>MetalUtils</code> we first draw with highlight then dark shadow
63      */

64     static void drawFlush3DBorder(Graphics g, int x, int y, int w, int h) {
65         g.translate(x, y);
66         g.setColor(PlasticLookAndFeel.getControlHighlight());
67         drawRect(g, 1, 1, w - 2, h - 2);
68         g.drawLine(0, h - 1, 0, h - 1);
69         g.drawLine(w - 1, 0, w - 1, 0);
70         g.setColor(PlasticLookAndFeel.getControlDarkShadow());
71         drawRect(g, 0, 0, w - 2, h - 2);
72         g.translate(-x, -y);
73     }
74
75
76     /*
77      * Copied from <code>MetalUtils</code>.
78      */

79     static void drawPressed3DBorder(Graphics g, int x, int y, int w, int h) {
80         g.translate(x, y);
81         drawFlush3DBorder(g, 0, 0, w, h);
82         g.setColor(PlasticLookAndFeel.getControlShadow());
83         g.drawLine(1, 1, 1, h - 3);
84         g.drawLine(1, 1, w - 3, 1);
85         g.translate(-x, -y);
86     }
87
88
89     /*
90      * Copied from <code>MetalUtils</code>.
91      */

92     static void drawButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
93         if (active) {
94             drawActiveButtonBorder(g, x, y, w, h);
95         } else {
96             drawFlush3DBorder(g, x, y, w, h);
97         }
98     }
99
100     /*
101      * Copied from <code>MetalUtils</code>.
102      */

103     static void drawActiveButtonBorder(Graphics g, int x, int y, int w, int h) {
104         drawFlush3DBorder(g, x, y, w, h);
105         g.setColor( PlasticLookAndFeel.getPrimaryControl() );
106         g.drawLine( x+1, y+1, x+1, h-3 );
107         g.drawLine( x+1, y+1, w-3, x+1 );
108         g.setColor( PlasticLookAndFeel.getPrimaryControlDarkShadow() );
109         g.drawLine( x+2, h-2, w-2, h-2 );
110         g.drawLine( w-2, y+2, w-2, h-2 );
111     }
112
113     /*
114      * Modified edges.
115      */

116     static void drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
117         drawButtonBorder(g, x+1, y+1, w-1, h-1, active);
118         g.translate(x, y);
119         g.setColor(PlasticLookAndFeel.getControlDarkShadow() );
120         drawRect(g, 0, 0, w-3, h-3 );
121         g.drawLine(w-2, 0, w-2, 0);
122         g.drawLine(0, h-2, 0, h-2);
123         g.setColor(PlasticLookAndFeel.getControl());
124         g.drawLine(w-1, 0, w-1, 0);
125         g.drawLine(0, h-1, 0, h-1);
126         g.translate(-x, -y);
127     }
128     
129     static void drawDefaultButtonPressedBorder(Graphics g, int x, int y, int w, int h) {
130         drawPressed3DBorder(g, x + 1, y + 1, w - 1, h - 1);
131         g.translate(x, y);
132         g.setColor(PlasticLookAndFeel.getControlDarkShadow());
133         drawRect(g, 0, 0, w - 3, h - 3);
134         g.drawLine(w - 2, 0, w - 2, 0);
135         g.drawLine(0, h - 2, 0, h - 2);
136         g.setColor(PlasticLookAndFeel.getControl());
137         g.drawLine(w - 1, 0, w - 1, 0);
138         g.drawLine(0, h - 1, 0, h - 1);
139         g.translate(-x, -y);
140     }
141
142     static void drawThinFlush3DBorder(Graphics g, int x, int y, int w, int h) {
143         g.translate(x, y);
144         g.setColor(PlasticLookAndFeel.getControlHighlight());
145         g.drawLine(0, 0, w - 2, 0);
146         g.drawLine(0, 0, 0, h - 2);
147         g.setColor(PlasticLookAndFeel.getControlDarkShadow());
148         g.drawLine(w - 1, 0, w - 1, h - 1);
149         g.drawLine(0, h - 1, w - 1, h - 1);
150         g.translate(-x, -y);
151     }
152     
153     
154     static void drawThinPressed3DBorder(Graphics g, int x, int y, int w, int h) {
155         g.translate(x, y);
156         g.setColor(PlasticLookAndFeel.getControlDarkShadow());
157         g.drawLine(0, 0, w - 2, 0);
158         g.drawLine(0, 0, 0, h - 2);
159         g.setColor(PlasticLookAndFeel.getControlHighlight());
160         g.drawLine(w - 1, 0, w - 1, h - 1);
161         g.drawLine(0, h - 1, w - 1, h - 1);
162         g.translate(-x, -y);
163     }
164     
165     /*
166      * Convenience function for determining ComponentOrientation. Helps us
167      * avoid having Munge directives throughout the code.
168      */

169     static boolean isLeftToRight(Component JavaDoc c) {
170         return c.getComponentOrientation().isLeftToRight();
171     }
172     
173     
174     // 3D Effects ***********************************************************************
175

176     /**
177      * Checks and returns whether the specified component type has 3D effects.
178      *
179      * @param keyPrefix the prefix of the key used to lookup the setting
180      * @return true if the component type shall be rendered with a 3D effect
181      * @see #force3D(JComponent)
182      * @see #forceFlat(JComponent)
183      */

184     static boolean is3D(String JavaDoc keyPrefix) {
185         Object JavaDoc value = UIManager.get(keyPrefix + "is3DEnabled");
186         return Boolean.TRUE.equals(value);
187     }
188
189
190     /**
191      * Checks and returns whether we have a custom hint that forces the 3D mode.
192      *
193      * @param c the component to inspect
194      * @return true if the given component has a 3D hint set
195      * @see #forceFlat(JComponent)
196      */

197     static boolean force3D(JComponent JavaDoc c) {
198         Object JavaDoc value = c.getClientProperty(PlasticLookAndFeel.IS_3D_KEY);
199         return Boolean.TRUE.equals(value);
200     }
201
202
203     /**
204      * Checks and returns whether we have a custom hint that prevents the 3D mode.
205      *
206      * @param c the component to inspect
207      * @return true if the given component has a flat hint set
208      * @see #force3D(JComponent)
209      */

210     static boolean forceFlat(JComponent JavaDoc c) {
211         Object JavaDoc value = c.getClientProperty(PlasticLookAndFeel.IS_3D_KEY);
212         return Boolean.FALSE.equals(value);
213     }
214
215
216     // Painting 3D Effects *************************************************************
217

218     private static float FRACTION_3D = 0.5f;
219     
220
221     private static void add3DEffekt(Graphics g, Rectangle r, boolean isHorizontal,
222         Color startC0, Color stopC0, Color startC1, Color stopC1) {
223             
224         Graphics2D g2 = (Graphics2D) g;
225         int xb0, yb0, xb1, yb1, xd0, yd0, xd1, yd1, width, height;
226         if (isHorizontal) {
227             width = r.width;
228             height = (int) (r.height * FRACTION_3D);
229             xb0 = r.x;
230             yb0 = r.y;
231             xb1 = xb0;
232             yb1 = yb0 + height;
233             xd0 = xb1;
234             yd0 = yb1;
235             xd1 = xd0;
236             yd1 = r.y + r.height;
237         } else {
238             width = (int) (r.width * FRACTION_3D);
239             height = r.height;
240             xb0 = r.x;
241             yb0 = r.y;
242             xb1 = xb0 + width;
243             yb1 = yb0;
244             xd0 = xb1;
245             yd0 = yb0;
246             xd1 = r.x + r.width;
247             yd1 = yd0;
248         }
249         g2.setPaint(new GradientPaint(xb0, yb0, stopC0, xb1, yb1, startC0));
250         g2.fillRect(r.x, r.y, width, height);
251         g2.setPaint(new GradientPaint(xd0, yd0, startC1, xd1, yd1, stopC1));
252         g2.fillRect(xd0, yd0, width, height);
253     }
254
255
256     static void add3DEffekt(Graphics g, Rectangle r) {
257         Color brightenStop = UIManager.getColor("Plastic.brightenStop");
258         if (null == brightenStop)
259             brightenStop = PlasticTheme.BRIGHTEN_STOP;
260
261         // Add round sides
262
Graphics2D g2 = (Graphics2D) g;
263         int border = 10;
264         g2.setPaint(new GradientPaint(r.x, r.y, brightenStop, r.x + border, r.y, PlasticTheme.BRIGHTEN_START));
265         g2.fillRect(r.x, r.y, border, r.height);
266         int x = r.x + r.width -border;
267         int y = r.y;
268         g2.setPaint(new GradientPaint(x, y, PlasticTheme.DARKEN_START, x + border, y, PlasticTheme.LT_DARKEN_STOP));
269         g2.fillRect(x, y, border, r.height);
270
271         add3DEffekt(g, r, true, PlasticTheme.BRIGHTEN_START, brightenStop, PlasticTheme.DARKEN_START, PlasticTheme.LT_DARKEN_STOP);
272     }
273
274
275     static void addLight3DEffekt(Graphics g, Rectangle r, boolean isHorizontal) {
276         Color ltBrightenStop = UIManager.getColor("Plastic.ltBrightenStop");
277         if (null == ltBrightenStop)
278             ltBrightenStop = PlasticTheme.LT_BRIGHTEN_STOP;
279
280         add3DEffekt(g, r, isHorizontal, PlasticTheme.BRIGHTEN_START, ltBrightenStop, PlasticTheme.DARKEN_START, PlasticTheme.LT_DARKEN_STOP);
281     }
282     
283     
284     /*
285      * TODO: Required by the Chartster and JPathReport Filler; move to a
286      * FillerUI.
287      */

288     public static void addLight3DEffekt(Graphics g, Rectangle r) {
289         Color ltBrightenStop = UIManager.getColor("Plastic.ltBrightenStop");
290         if (null == ltBrightenStop)
291             ltBrightenStop = PlasticTheme.LT_BRIGHTEN_STOP;
292
293         add3DEffekt(g, r, true, PlasticTheme.DARKEN_START, PlasticTheme.LT_DARKEN_STOP, PlasticTheme.BRIGHTEN_START, ltBrightenStop);
294     }
295     
296
297     // Low level graphics ***************************************************
298

299     /*
300      * An optimized version of Graphics.drawRect.
301      */

302     private static void drawRect(Graphics g, int x, int y, int w, int h) {
303         g.fillRect(x, y, w+1, 1);
304         g.fillRect(x, y+1, 1, h);
305         g.fillRect(x+1, y+h, w, 1);
306         g.fillRect(x+w, y+1, 1, h);
307     }
308
309
310 }
Popular Tags