KickJava   Java API By Example, From Geeks To Geeks.

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


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