KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TextAreaPainter.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 TextAreaPainter 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 TextAreaPainter 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_DISABLED_NOTINSCROLLPANE = 3;
25     static final int BACKGROUND_ENABLED_NOTINSCROLLPANE = 4;
26     static final int BACKGROUND_SELECTED = 5;
27     static final int BORDER_DISABLED_NOTINSCROLLPANE = 6;
28     static final int BORDER_FOCUSED_NOTINSCROLLPANE = 7;
29     static final int BORDER_ENABLED_NOTINSCROLLPANE = 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 TextAreaPainter would be created. For the moment at least,
43
//however, all are created for each instance.
44
private Color color1 = decodeColor("nimbusBlueGrey", -0.015872955f, -0.07995863f, 0.15294117f, 0);
45     private Color color2 = decodeColor("nimbusLightBackground", 0.0f, 0.0f, 0.0f, 0);
46     private Color color3 = decodeColor("nimbusBlueGrey", -0.006944418f, -0.07187897f, 0.06666666f, 0);
47     private Color color4 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07826825f, 0.10588235f, 0);
48     private Color color5 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07856284f, 0.11372548f, 0);
49     private Color color6 = decodeColor("nimbusBlueGrey", 0.007936537f, -0.07796818f, 0.09803921f, 0);
50     private Color color7 = decodeColor("nimbusBlueGrey", -0.027777791f, -0.0965403f, -0.18431371f, 0);
51     private Color color8 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.1048766f, -0.05098039f, 0);
52     private Color color9 = decodeColor("nimbusLightBackground", 0.6666667f, 0.004901961f, -0.19999999f, 0);
53     private Color color10 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.10512091f, -0.019607842f, 0);
54     private Color color11 = decodeColor("nimbusBlueGrey", 0.055555582f, -0.105344966f, 0.011764705f, 0);
55     private Color color12 = decodeColor("nimbusFocus", 0.0f, 0.0f, 0.0f, 0);
56
57
58     //Array of current component colors, updated in each paint call
59
private Object JavaDoc[] componentColors;
60
61     public TextAreaPainter(PaintContext ctx, int state) {
62         super();
63         this.state = state;
64         this.ctx = ctx;
65     }
66
67     @Override JavaDoc
68     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
69         //populate componentColors array with colors calculated in getExtendedCacheKeys call
70
componentColors = extendedCacheKeys;
71         //generate this entire method. Each state/bg/fg/border combo that has
72
//been painted gets its own KEY and paint method.
73
switch(state) {
74             case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break;
75             case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break;
76             case BACKGROUND_DISABLED_NOTINSCROLLPANE: paintBackgroundDisabledAndNotInScrollPane(g); break;
77             case BACKGROUND_ENABLED_NOTINSCROLLPANE: paintBackgroundEnabledAndNotInScrollPane(g); break;
78             case BACKGROUND_SELECTED: paintBackgroundSelected(g); break;
79             case BORDER_DISABLED_NOTINSCROLLPANE: paintBorderDisabledAndNotInScrollPane(g); break;
80             case BORDER_FOCUSED_NOTINSCROLLPANE: paintBorderFocusedAndNotInScrollPane(g); break;
81             case BORDER_ENABLED_NOTINSCROLLPANE: paintBorderEnabledAndNotInScrollPane(g); break;
82
83         }
84     }
85         
86     protected Object JavaDoc[] getExtendedCacheKeys(JComponent c) {
87         Object JavaDoc[] extendedCacheKeys = null;
88         switch(state) {
89             case BACKGROUND_ENABLED:
90                 extendedCacheKeys = new Object JavaDoc[] {
91                      getComponentColor(c, "background", color2, 0.0f, 0.0f, 0)};
92                 break;
93             case BACKGROUND_ENABLED_NOTINSCROLLPANE:
94                 extendedCacheKeys = new Object JavaDoc[] {
95                      getComponentColor(c, "background", color2, 0.0f, 0.0f, 0)};
96                 break;
97             case BORDER_FOCUSED_NOTINSCROLLPANE:
98                 extendedCacheKeys = new Object JavaDoc[] {
99                      getComponentColor(c, "background", color9, 0.004901961f, -0.19999999f, 0),
100                      getComponentColor(c, "background", color2, 0.0f, 0.0f, 0)};
101                 break;
102             case BORDER_ENABLED_NOTINSCROLLPANE:
103                 extendedCacheKeys = new Object JavaDoc[] {
104                      getComponentColor(c, "background", color9, 0.004901961f, -0.19999999f, 0),
105                      getComponentColor(c, "background", color2, 0.0f, 0.0f, 0)};
106                 break;
107         }
108         return extendedCacheKeys;
109     }
110
111     @Override JavaDoc
112     protected final PaintContext getPaintContext() {
113         return ctx;
114     }
115
116     private void paintBackgroundDisabled(Graphics2D g) {
117         rect = decodeRect1();
118         g.setPaint(color1);
119         g.fill(rect);
120
121     }
122
123     private void paintBackgroundEnabled(Graphics2D g) {
124         rect = decodeRect1();
125         g.setPaint((Color)componentColors[0]);
126         g.fill(rect);
127
128     }
129
130     private void paintBackgroundDisabledAndNotInScrollPane(Graphics2D g) {
131         rect = decodeRect2();
132         g.setPaint(color1);
133         g.fill(rect);
134
135     }
136
137     private void paintBackgroundEnabledAndNotInScrollPane(Graphics2D g) {
138         rect = decodeRect2();
139         g.setPaint((Color)componentColors[0]);
140         g.fill(rect);
141
142     }
143
144     private void paintBackgroundSelected(Graphics2D g) {
145         rect = decodeRect2();
146         g.setPaint(color2);
147         g.fill(rect);
148
149     }
150
151     private void paintBorderDisabledAndNotInScrollPane(Graphics2D g) {
152         rect = decodeRect3();
153         g.setPaint(decodeGradient1(rect));
154         g.fill(rect);
155         rect = decodeRect4();
156         g.setPaint(decodeGradient2(rect));
157         g.fill(rect);
158         rect = decodeRect5();
159         g.setPaint(color6);
160         g.fill(rect);
161         rect = decodeRect6();
162         g.setPaint(color4);
163         g.fill(rect);
164         rect = decodeRect7();
165         g.setPaint(color4);
166         g.fill(rect);
167
168     }
169
170     private void paintBorderFocusedAndNotInScrollPane(Graphics2D g) {
171         rect = decodeRect8();
172         g.setPaint(decodeGradient3(rect));
173         g.fill(rect);
174         rect = decodeRect9();
175         g.setPaint(decodeGradient4(rect));
176         g.fill(rect);
177         rect = decodeRect10();
178         g.setPaint(color10);
179         g.fill(rect);
180         rect = decodeRect11();
181         g.setPaint(color10);
182         g.fill(rect);
183         rect = decodeRect12();
184         g.setPaint(color11);
185         g.fill(rect);
186         path = decodePath1();
187         g.setPaint(color12);
188         g.fill(path);
189
190     }
191
192     private void paintBorderEnabledAndNotInScrollPane(Graphics2D g) {
193         rect = decodeRect8();
194         g.setPaint(decodeGradient5(rect));
195         g.fill(rect);
196         rect = decodeRect9();
197         g.setPaint(decodeGradient4(rect));
198         g.fill(rect);
199         rect = decodeRect10();
200         g.setPaint(color10);
201         g.fill(rect);
202         rect = decodeRect11();
203         g.setPaint(color10);
204         g.fill(rect);
205         rect = decodeRect12();
206         g.setPaint(color11);
207         g.fill(rect);
208
209     }
210
211
212
213     private Rectangle2D decodeRect1() {
214             rect.setRect(decodeX(0.0f), //x
215
decodeY(0.0f), //y
216
decodeX(3.0f) - decodeX(0.0f), //width
217
decodeY(3.0f) - decodeY(0.0f)); //height
218
return rect;
219     }
220
221     private Rectangle2D decodeRect2() {
222             rect.setRect(decodeX(0.4f), //x
223
decodeY(0.4f), //y
224
decodeX(2.6f) - decodeX(0.4f), //width
225
decodeY(2.6f) - decodeY(0.4f)); //height
226
return rect;
227     }
228
229     private Rectangle2D decodeRect3() {
230             rect.setRect(decodeX(0.6666667f), //x
231
decodeY(0.4f), //y
232
decodeX(2.3333333f) - decodeX(0.6666667f), //width
233
decodeY(1.0f) - decodeY(0.4f)); //height
234
return rect;
235     }
236
237     private Rectangle2D decodeRect4() {
238             rect.setRect(decodeX(1.0f), //x
239
decodeY(0.6f), //y
240
decodeX(2.0f) - decodeX(1.0f), //width
241
decodeY(1.0f) - decodeY(0.6f)); //height
242
return rect;
243     }
244
245     private Rectangle2D decodeRect5() {
246             rect.setRect(decodeX(0.6666667f), //x
247
decodeY(1.0f), //y
248
decodeX(1.0f) - decodeX(0.6666667f), //width
249
decodeY(2.0f) - decodeY(1.0f)); //height
250
return rect;
251     }
252
253     private Rectangle2D decodeRect6() {
254             rect.setRect(decodeX(0.6666667f), //x
255
decodeY(2.3333333f), //y
256
decodeX(2.3333333f) - decodeX(0.6666667f), //width
257
decodeY(2.0f) - decodeY(2.3333333f)); //height
258
return rect;
259     }
260
261     private Rectangle2D decodeRect7() {
262             rect.setRect(decodeX(2.0f), //x
263
decodeY(1.0f), //y
264
decodeX(2.3333333f) - decodeX(2.0f), //width
265
decodeY(2.0f) - decodeY(1.0f)); //height
266
return rect;
267     }
268
269     private Rectangle2D decodeRect8() {
270             rect.setRect(decodeX(0.4f), //x
271
decodeY(0.4f), //y
272
decodeX(2.6f) - decodeX(0.4f), //width
273
decodeY(1.0f) - decodeY(0.4f)); //height
274
return rect;
275     }
276
277     private Rectangle2D decodeRect9() {
278             rect.setRect(decodeX(0.6f), //x
279
decodeY(0.6f), //y
280
decodeX(2.4f) - decodeX(0.6f), //width
281
decodeY(1.0f) - decodeY(0.6f)); //height
282
return rect;
283     }
284
285     private Rectangle2D decodeRect10() {
286             rect.setRect(decodeX(0.4f), //x
287
decodeY(1.0f), //y
288
decodeX(0.6f) - decodeX(0.4f), //width
289
decodeY(2.6f) - decodeY(1.0f)); //height
290
return rect;
291     }
292
293     private Rectangle2D decodeRect11() {
294             rect.setRect(decodeX(2.4f), //x
295
decodeY(1.0f), //y
296
decodeX(2.6f) - decodeX(2.4f), //width
297
decodeY(2.6f) - decodeY(1.0f)); //height
298
return rect;
299     }
300
301     private Rectangle2D decodeRect12() {
302             rect.setRect(decodeX(0.6f), //x
303
decodeY(2.4f), //y
304
decodeX(2.4f) - decodeX(0.6f), //width
305
decodeY(2.6f) - decodeY(2.4f)); //height
306
return rect;
307     }
308
309     private Path2D decodePath1() {
310         path.reset();
311         path.moveTo(decodeX(0.4f), decodeY(0.4f));
312         path.lineTo(decodeX(0.4f), decodeY(2.6f));
313         path.lineTo(decodeX(2.6f), decodeY(2.6f));
314         path.lineTo(decodeX(2.6f), decodeY(0.4f));
315         path.curveTo(decodeAnchorX(2.5999999046325684f, 0.0f), decodeAnchorY(0.4000000059604645f, 0.0f), decodeAnchorX(2.880000352859497f, 0.09999999999999432f), decodeAnchorY(0.4000000059604645f, 0.0f), decodeX(2.8800004f), decodeY(0.4f));
316         path.curveTo(decodeAnchorX(2.880000352859497f, 0.09999999999999432f), decodeAnchorY(0.4000000059604645f, 0.0f), decodeAnchorX(2.880000352859497f, 0.0f), decodeAnchorY(2.879999876022339f, 0.0f), decodeX(2.8800004f), decodeY(2.8799999f));
317         path.lineTo(decodeX(0.120000005f), decodeY(2.8799999f));
318         path.lineTo(decodeX(0.120000005f), decodeY(0.120000005f));
319         path.lineTo(decodeX(2.8800004f), decodeY(0.120000005f));
320         path.lineTo(decodeX(2.8800004f), decodeY(0.4f));
321         path.lineTo(decodeX(0.4f), decodeY(0.4f));
322         path.closePath();
323         return path;
324     }
325
326
327
328     private Paint decodeGradient1(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.5f,1.0f },
336                 new Color[] { color3,
337                             decodeColor(color3,color4,0.5f),
338                             color4});
339     }
340
341     private Paint decodeGradient2(Shape s) {
342         Rectangle2D bounds = s.getBounds2D();
343         float x = (float)bounds.getX();
344         float y = (float)bounds.getY();
345         float w = (float)bounds.getWidth();
346         float h = (float)bounds.getHeight();
347         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
348                 new float[] { 0.0f,0.5f,1.0f },
349                 new Color[] { color5,
350                             decodeColor(color5,color1,0.5f),
351                             color1});
352     }
353
354     private Paint decodeGradient3(Shape s) {
355         Rectangle2D bounds = s.getBounds2D();
356         float x = (float)bounds.getX();
357         float y = (float)bounds.getY();
358         float w = (float)bounds.getWidth();
359         float h = (float)bounds.getHeight();
360         return decodeGradient((0.25f * w) + x, (0.0f * h) + y, (0.25f * w) + x, (0.1625f * h) + y,
361                 new float[] { 0.1f,0.49999997f,0.9f },
362                 new Color[] { color7,
363                             decodeColor(color7,color8,0.5f),
364                             color8});
365     }
366
367     private Paint decodeGradient4(Shape s) {
368         Rectangle2D bounds = s.getBounds2D();
369         float x = (float)bounds.getX();
370         float y = (float)bounds.getY();
371         float w = (float)bounds.getWidth();
372         float h = (float)bounds.getHeight();
373         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
374                 new float[] { 0.1f,0.49999997f,0.9f },
375                 new Color[] { (Color)componentColors[0],
376                             decodeColor((Color)componentColors[0],(Color)componentColors[1],0.5f),
377                             (Color)componentColors[1]});
378     }
379
380     private Paint decodeGradient5(Shape s) {
381         Rectangle2D bounds = s.getBounds2D();
382         float x = (float)bounds.getX();
383         float y = (float)bounds.getY();
384         float w = (float)bounds.getWidth();
385         float h = (float)bounds.getHeight();
386         return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
387                 new float[] { 0.1f,0.49999997f,0.9f },
388                 new Color[] { color7,
389                             decodeColor(color7,color8,0.5f),
390                             color8});
391     }
392
393
394 }
395
Popular Tags