KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > nimbus > TableHeaderTableHeaderRendererPainter


1 /*
2  * TableHeaderTableHeaderRendererPainter.java %E%
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.nimbus;
8
9 import java.awt.*;
10 import java.awt.geom.*;
11 import java.awt.image.*;
12 import javax.swing.*;
13 import com.sun.java.swing.Painter;
14
15 /**
16  */

17 public final class TableHeaderTableHeaderRendererPainter extends AbstractRegionPainter {
18     //package private integers representing the available states that
19
//this painter will paint. These are used when creating a new instance
20
//of TableHeaderTableHeaderRendererPainter to determine which region/state is being painted
21
//by that instance.
22
static final int BACKGROUND_DISABLED = 1;
23     static final int BACKGROUND_ENABLED = 2;
24     static final int BACKGROUND_ENABLED_FOCUSED = 3;
25     static final int BACKGROUND_MOUSEOVER = 4;
26     static final int BACKGROUND_PRESSED = 5;
27     static final int BACKGROUND_ENABLED_SORTED = 6;
28     static final int BACKGROUND_ENABLED_FOCUSED_SORTED = 7;
29     static final int BACKGROUND_DISABLED_SORTED = 8;
30
31
32     private int state; //refers to one of the static final ints above
33
private PaintContext ctx;
34
35     //the following 4 variables are reused during the painting code of the layers
36
private Path2D path = new Path2D.Float();
37     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
38     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
39     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
40
41     //All Colors used for painting are stored here. Ideally, only those colors being used
42
//by a particular instance of TableHeaderTableHeaderRendererPainter would be created. For the moment at least,
43
//however, all are created for each instance.
44
private Color color1 = decodeColor("nimbusBorder", -0.013888836f, 5.823001E-4f, -0.12941176f, 0);
45     private Color color2 = decodeColor("nimbusBlueGrey", -0.01111114f, -0.08625447f, 0.062745094f, 0);
46     private Color color3 = decodeColor("nimbusBlueGrey", -0.013888836f, -0.028334536f, -0.17254901f, 0);
47     private Color color4 = decodeColor("nimbusBlueGrey", -0.013888836f, -0.029445238f, -0.16470587f, 0);
48     private Color color5 = decodeColor("nimbusBlueGrey", -0.02020204f, -0.053531498f, 0.011764705f, 0);
49     private Color color6 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.10655806f, 0.24313724f, 0);
50     private Color color7 = decodeColor("nimbusBlueGrey", 0.0f, -0.08455229f, 0.1607843f, 0);
51     private Color color8 = decodeColor("nimbusBlueGrey", 0.0f, -0.07016757f, 0.12941176f, 0);
52     private Color color9 = decodeColor("nimbusBlueGrey", 0.0f, -0.07466974f, 0.23921567f, 0);
53     private Color color10 = decodeColor("nimbusFocus", 0.0f, 0.0f, 0.0f, 0);
54     private Color color11 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.10658931f, 0.25098038f, 0);
55     private Color color12 = decodeColor("nimbusBlueGrey", 0.0f, -0.08613607f, 0.21960783f, 0);
56     private Color color13 = decodeColor("nimbusBlueGrey", 0.0f, -0.07333623f, 0.20392156f, 0);
57     private Color color14 = decodeColor("nimbusBlueGrey", 0.0f, -0.110526316f, 0.25490195f, 0);
58     private Color color15 = decodeColor("nimbusBlueGrey", -0.00505054f, -0.05960039f, 0.10196078f, 0);
59     private Color color16 = decodeColor("nimbusBlueGrey", 0.0f, -0.017742813f, 0.015686274f, 0);
60     private Color color17 = decodeColor("nimbusBlueGrey", -0.0027777553f, -0.0018306673f, -0.02352941f, 0);
61     private Color color18 = decodeColor("nimbusBlueGrey", 0.0055555105f, -0.020436227f, 0.12549019f, 0);
62     private Color color19 = decodeColor("nimbusBase", -0.023096085f, -0.62376213f, 0.4352941f, 0);
63     private Color color20 = decodeColor("nimbusBase", -0.0012707114f, -0.50901747f, 0.31764704f, 0);
64     private Color color21 = decodeColor("nimbusBase", -0.002461195f, -0.47139505f, 0.2862745f, 0);
65     private Color color22 = decodeColor("nimbusBase", -0.0051222444f, -0.49103343f, 0.372549f, 0);
66     private Color color23 = decodeColor("nimbusBase", -8.738637E-4f, -0.49872798f, 0.3098039f, 0);
67     private Color color24 = decodeColor("nimbusBase", -2.2029877E-4f, -0.4916465f, 0.37647057f, 0);
68
69
70     //Array of current component colors, updated in each paint call
71
private Object JavaDoc[] componentColors;
72
73     public TableHeaderTableHeaderRendererPainter(PaintContext ctx, int state) {
74         super();
75         this.state = state;
76         this.ctx = ctx;
77     }
78
79     @Override JavaDoc
80     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
81         //populate componentColors array with colors calculated in getExtendedCacheKeys call
82
componentColors = extendedCacheKeys;
83         //generate this entire method. Each state/bg/fg/border combo that has
84
//been painted gets its own KEY and paint method.
85
switch(state) {
86             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
87             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
88             case BACKGROUND_ENABLED_FOCUSED: paintBackgroundEnabledAndFocused(g); break;
89             case BACKGROUND_MOUSEOVER: paintBackgroundMouseOver(g); break;
90             case BACKGROUND_PRESSED: paintBackgroundPressed(g); break;
91             case BACKGROUND_ENABLED_SORTED: paintBackgroundEnabledAndSorted(g); break;
92             case BACKGROUND_ENABLED_FOCUSED_SORTED: paintBackgroundEnabledAndFocusedAndSorted(g); break;
93             case BACKGROUND_DISABLED_SORTED: paintBackgroundDisabledAndSorted(g); break;
94
95         }
96     }
97         
98
99
100     @Override JavaDoc
101     protected final PaintContext getPaintContext() {
102         return ctx;
103     }
104
105     private void paintBackgroundDisabled(Graphics2D g) {
106         rect = decodeRect1();
107         g.setPaint(color1);
108         g.fill(rect);
109         rect = decodeRect2();
110         g.setPaint(decodeGradient1(rect));
111         g.fill(rect);
112         rect = decodeRect3();
113         g.setPaint(decodeGradient2(rect));
114         g.fill(rect);
115
116     }
117
118     private void paintBackgroundEnabled(Graphics2D g) {
119         rect = decodeRect1();
120         g.setPaint(color1);
121         g.fill(rect);
122         rect = decodeRect2();
123         g.setPaint(decodeGradient1(rect));
124         g.fill(rect);
125         rect = decodeRect3();
126         g.setPaint(decodeGradient2(rect));
127         g.fill(rect);
128
129     }
130
131     private void paintBackgroundEnabledAndFocused(Graphics2D g) {
132         rect = decodeRect1();
133         g.setPaint(color1);
134         g.fill(rect);
135         rect = decodeRect2();
136         g.setPaint(decodeGradient1(rect));
137         g.fill(rect);
138         rect = decodeRect3();
139         g.setPaint(decodeGradient2(rect));
140         g.fill(rect);
141         path = decodePath1();
142         g.setPaint(color10);
143         g.fill(path);
144
145     }
146
147     private void paintBackgroundMouseOver(Graphics2D g) {
148         rect = decodeRect1();
149         g.setPaint(color1);
150         g.fill(rect);
151         rect = decodeRect2();
152         g.setPaint(decodeGradient1(rect));
153         g.fill(rect);
154         rect = decodeRect3();
155         g.setPaint(decodeGradient3(rect));
156         g.fill(rect);
157
158     }
159
160     private void paintBackgroundPressed(Graphics2D g) {
161         rect = decodeRect1();
162         g.setPaint(color1);
163         g.fill(rect);
164         rect = decodeRect2();
165         g.setPaint(decodeGradient1(rect));
166         g.fill(rect);
167         rect = decodeRect3();
168         g.setPaint(decodeGradient4(rect));
169         g.fill(rect);
170
171     }
172
173     private void paintBackgroundEnabledAndSorted(Graphics2D g) {
174         rect = decodeRect1();
175         g.setPaint(color1);
176         g.fill(rect);
177         rect = decodeRect2();
178         g.setPaint(decodeGradient1(rect));
179         g.fill(rect);
180         rect = decodeRect3();
181         g.setPaint(decodeGradient5(rect));
182         g.fill(rect);
183
184     }
185
186     private void paintBackgroundEnabledAndFocusedAndSorted(Graphics2D g) {
187         rect = decodeRect1();
188         g.setPaint(color1);
189         g.fill(rect);
190         rect = decodeRect2();
191         g.setPaint(decodeGradient1(rect));
192         g.fill(rect);
193         rect = decodeRect3();
194         g.setPaint(decodeGradient6(rect));
195         g.fill(rect);
196         path = decodePath1();
197         g.setPaint(color10);
198         g.fill(path);
199
200     }
201
202     private void paintBackgroundDisabledAndSorted(Graphics2D g) {
203         rect = decodeRect1();
204         g.setPaint(color1);
205         g.fill(rect);
206         rect = decodeRect2();
207         g.setPaint(decodeGradient1(rect));
208         g.fill(rect);
209         rect = decodeRect3();
210         g.setPaint(decodeGradient2(rect));
211         g.fill(rect);
212
213     }
214
215
216
217     private Rectangle2D decodeRect1() {
218             rect.setRect(decodeX(0.0f), //x
219
decodeY(2.8f), //y
220
decodeX(3.0f) - decodeX(0.0f), //width
221
decodeY(3.0f) - decodeY(2.8f)); //height
222
return rect;
223     }
224
225     private Rectangle2D decodeRect2() {
226             rect.setRect(decodeX(2.8f), //x
227
decodeY(0.0f), //y
228
decodeX(3.0f) - decodeX(2.8f), //width
229
decodeY(2.8f) - decodeY(0.0f)); //height
230
return rect;
231     }
232
233     private Rectangle2D decodeRect3() {
234             rect.setRect(decodeX(0.0f), //x
235
decodeY(0.0f), //y
236
decodeX(2.8f) - decodeX(0.0f), //width
237
decodeY(2.8f) - decodeY(0.0f)); //height
238
return rect;
239     }
240
241     private Path2D decodePath1() {
242         path.reset();
243         path.moveTo(decodeX(0.0f), decodeY(0.0f));
244         path.lineTo(decodeX(0.0f), decodeY(3.0f));
245         path.lineTo(decodeX(3.0f), decodeY(3.0f));
246         path.lineTo(decodeX(3.0f), decodeY(0.0f));
247         path.lineTo(decodeX(0.24000001f), decodeY(0.0f));
248         path.lineTo(decodeX(0.24000001f), decodeY(0.24000001f));
249         path.lineTo(decodeX(2.7599998f), decodeY(0.24000001f));
250         path.lineTo(decodeX(2.7599998f), decodeY(2.7599998f));
251         path.lineTo(decodeX(0.24000001f), decodeY(2.7599998f));
252         path.lineTo(decodeX(0.24000001f), decodeY(0.0f));
253         path.lineTo(decodeX(0.0f), decodeY(0.0f));
254         path.closePath();
255         return path;
256     }
257
258
259
260     private Paint decodeGradient1(Shape s) {
261         Rectangle2D bounds = s.getBounds2D();
262         float x = (float)bounds.getX();
263         float y = (float)bounds.getY();
264         float w = (float)bounds.getWidth();
265         float h = (float)bounds.getHeight();
266         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
267                 new float[] { 0.0f,0.14441223f,0.43703705f,0.59444445f,0.75185186f,0.8759259f,1.0f },
268                 new Color[] { color2,
269                             decodeColor(color2,color3,0.5f),
270                             color3,
271                             decodeColor(color3,color4,0.5f),
272                             color4,
273                             decodeColor(color4,color5,0.5f),
274                             color5});
275     }
276
277     private Paint decodeGradient2(Shape s) {
278         Rectangle2D bounds = s.getBounds2D();
279         float x = (float)bounds.getX();
280         float y = (float)bounds.getY();
281         float w = (float)bounds.getWidth();
282         float h = (float)bounds.getHeight();
283         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
284                 new float[] { 0.0f,0.07147767f,0.2888889f,0.5490909f,0.7037037f,0.8518518f,1.0f },
285                 new Color[] { color6,
286                             decodeColor(color6,color7,0.5f),
287                             color7,
288                             decodeColor(color7,color8,0.5f),
289                             color8,
290                             decodeColor(color8,color9,0.5f),
291                             color9});
292     }
293
294     private Paint decodeGradient3(Shape s) {
295         Rectangle2D bounds = s.getBounds2D();
296         float x = (float)bounds.getX();
297         float y = (float)bounds.getY();
298         float w = (float)bounds.getWidth();
299         float h = (float)bounds.getHeight();
300         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
301                 new float[] { 0.0f,0.07147767f,0.2888889f,0.5490909f,0.7037037f,0.7919203f,0.88013697f },
302                 new Color[] { color11,
303                             decodeColor(color11,color12,0.5f),
304                             color12,
305                             decodeColor(color12,color13,0.5f),
306                             color13,
307                             decodeColor(color13,color14,0.5f),
308                             color14});
309     }
310
311     private Paint decodeGradient4(Shape s) {
312         Rectangle2D bounds = s.getBounds2D();
313         float x = (float)bounds.getX();
314         float y = (float)bounds.getY();
315         float w = (float)bounds.getWidth();
316         float h = (float)bounds.getHeight();
317         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
318                 new float[] { 0.0f,0.07147767f,0.2888889f,0.5490909f,0.7037037f,0.8518518f,1.0f },
319                 new Color[] { color15,
320                             decodeColor(color15,color16,0.5f),
321                             color16,
322                             decodeColor(color16,color17,0.5f),
323                             color17,
324                             decodeColor(color17,color18,0.5f),
325                             color18});
326     }
327
328     private Paint decodeGradient5(Shape s) {
329         Rectangle2D bounds = s.getBounds2D();
330         float x = (float)bounds.getX();
331         float y = (float)bounds.getY();
332         float w = (float)bounds.getWidth();
333         float h = (float)bounds.getHeight();
334         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
335                 new float[] { 0.0f,0.08049711f,0.32534248f,0.56267816f,0.7037037f,0.83986557f,0.97602737f },
336                 new Color[] { color19,
337                             decodeColor(color19,color20,0.5f),
338                             color20,
339                             decodeColor(color20,color21,0.5f),
340                             color21,
341                             decodeColor(color21,color22,0.5f),
342                             color22});
343     }
344
345     private Paint decodeGradient6(Shape s) {
346         Rectangle2D bounds = s.getBounds2D();
347         float x = (float)bounds.getX();
348         float y = (float)bounds.getY();
349         float w = (float)bounds.getWidth();
350         float h = (float)bounds.getHeight();
351         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
352                 new float[] { 0.0f,0.07147767f,0.2888889f,0.5490909f,0.7037037f,0.8518518f,1.0f },
353                 new Color[] { color19,
354                             decodeColor(color19,color23,0.5f),
355                             color23,
356                             decodeColor(color23,color21,0.5f),
357                             color21,
358                             decodeColor(color21,color24,0.5f),
359                             color24});
360     }
361
362
363 }
364
Popular Tags