KickJava   Java API By Example, From Geeks To Geeks.

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


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