KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TreeTreeCellPainter.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 TreeTreeCellPainter 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 TreeTreeCellPainter to determine which region/state is being painted
21
//by that instance.
22
static final int BACKGROUND_ENABLED = 1;
23     static final int BACKGROUND_ENABLED_FOCUSED = 2;
24     static final int BACKGROUND_ENABLED_SELECTED = 3;
25     static final int BACKGROUND_SELECTED_FOCUSED = 4;
26
27
28     private int state; //refers to one of the static final ints above
29
private PaintContext ctx;
30
31     //the following 4 variables are reused during the painting code of the layers
32
private Path2D path = new Path2D.Float();
33     private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0);
34     private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0);
35     private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0);
36
37     //All Colors used for painting are stored here. Ideally, only those colors being used
38
//by a particular instance of TreeTreeCellPainter would be created. For the moment at least,
39
//however, all are created for each instance.
40
private Color color1 = decodeColor("nimbusFocus", 0.0f, 0.0f, 0.0f, 0);
41     private Color color2 = decodeColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0);
42
43
44     //Array of current component colors, updated in each paint call
45
private Object JavaDoc[] componentColors;
46
47     public TreeTreeCellPainter(PaintContext ctx, int state) {
48         super();
49         this.state = state;
50         this.ctx = ctx;
51     }
52
53     @Override JavaDoc
54     protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object JavaDoc[] extendedCacheKeys) {
55         //populate componentColors array with colors calculated in getExtendedCacheKeys call
56
componentColors = extendedCacheKeys;
57         //generate this entire method. Each state/bg/fg/border combo that has
58
//been painted gets its own KEY and paint method.
59
switch(state) {
60             case BACKGROUND_ENABLED_FOCUSED: paintBackgroundEnabledAndFocused(g); break;
61             case BACKGROUND_ENABLED_SELECTED: paintBackgroundEnabledAndSelected(g); break;
62             case BACKGROUND_SELECTED_FOCUSED: paintBackgroundSelectedAndFocused(g); break;
63
64         }
65     }
66         
67
68
69     @Override JavaDoc
70     protected final PaintContext getPaintContext() {
71         return ctx;
72     }
73
74     private void paintBackgroundEnabledAndFocused(Graphics2D g) {
75         path = decodePath1();
76         g.setPaint(color1);
77         g.fill(path);
78
79     }
80
81     private void paintBackgroundEnabledAndSelected(Graphics2D g) {
82         rect = decodeRect1();
83         g.setPaint(color2);
84         g.fill(rect);
85
86     }
87
88     private void paintBackgroundSelectedAndFocused(Graphics2D g) {
89         rect = decodeRect1();
90         g.setPaint(color2);
91         g.fill(rect);
92         path = decodePath1();
93         g.setPaint(color1);
94         g.fill(path);
95
96     }
97
98
99
100     private Path2D decodePath1() {
101         path.reset();
102         path.moveTo(decodeX(0.0f), decodeY(0.0f));
103         path.lineTo(decodeX(0.0f), decodeY(3.0f));
104         path.lineTo(decodeX(3.0f), decodeY(3.0f));
105         path.lineTo(decodeX(3.0f), decodeY(0.0f));
106         path.lineTo(decodeX(0.24000001f), decodeY(0.0f));
107         path.lineTo(decodeX(0.24000001f), decodeY(0.24000001f));
108         path.lineTo(decodeX(2.7600007f), decodeY(0.24000001f));
109         path.lineTo(decodeX(2.7600007f), decodeY(2.7599998f));
110         path.lineTo(decodeX(0.24000001f), decodeY(2.7599998f));
111         path.lineTo(decodeX(0.24000001f), decodeY(0.0f));
112         path.lineTo(decodeX(0.0f), decodeY(0.0f));
113         path.closePath();
114         return path;
115     }
116
117     private Rectangle2D decodeRect1() {
118             rect.setRect(decodeX(0.0f), //x
119
decodeY(0.0f), //y
120
decodeX(3.0f) - decodeX(0.0f), //width
121
decodeY(3.0f) - decodeY(0.0f)); //height
122
return rect;
123     }
124
125
126
127
128 }
129
Popular Tags