KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthTableHeaderUI


1 /*
2  * @(#)SynthTableHeaderUI.java 1.14 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.synth;
9
10 import javax.swing.table.*;
11 import javax.swing.*;
12 import javax.swing.event.*;
13 import java.util.Enumeration JavaDoc;
14 import java.util.Date JavaDoc;
15 import java.awt.event.*;
16 import java.awt.*;
17 import java.beans.*;
18 import javax.swing.border.*;
19 import javax.swing.plaf.*;
20 import javax.swing.plaf.basic.BasicTableHeaderUI JavaDoc;
21 import sun.swing.plaf.synth.SynthUI;
22
23 /**
24  * SynthTableHeaderUI implementation
25  *
26  * @version 1.14, 12/19/03
27  * @author Alan Chung
28  * @author Philip Milne
29  */

30 class SynthTableHeaderUI extends BasicTableHeaderUI JavaDoc implements
31            PropertyChangeListener, SynthUI {
32
33 //
34
// Instance Variables
35
//
36

37     private TableCellRenderer prevRenderer = null;
38
39     private SynthStyle JavaDoc style;
40
41     public static ComponentUI createUI(JComponent h) {
42         return new SynthTableHeaderUI JavaDoc();
43     }
44
45     protected void installDefaults() {
46         prevRenderer = header.getDefaultRenderer();
47         if (prevRenderer instanceof UIResource) {
48             header.setDefaultRenderer(new HeaderRenderer());
49         }
50         updateStyle(header);
51     }
52
53     private void updateStyle(JTableHeader c) {
54         SynthContext JavaDoc context = getContext(c, ENABLED);
55         SynthStyle JavaDoc oldStyle = style;
56         style = SynthLookAndFeel.updateStyle(context, this);
57         if (style != oldStyle) {
58             if (oldStyle != null) {
59                 uninstallKeyboardActions();
60                 installKeyboardActions();
61             }
62         }
63         context.dispose();
64     }
65
66     protected void installListeners() {
67         super.installListeners();
68         header.addPropertyChangeListener(this);
69     }
70
71     protected void uninstallDefaults() {
72         if (header.getDefaultRenderer() instanceof HeaderRenderer) {
73             header.setDefaultRenderer(prevRenderer);
74         }
75                
76         SynthContext JavaDoc context = getContext(header, ENABLED);
77
78         style.uninstallDefaults(context);
79         context.dispose();
80         style = null;
81     }
82
83     protected void uninstallListeners() {
84         header.removePropertyChangeListener(this);
85         super.uninstallListeners();
86     }
87
88     public void update(Graphics g, JComponent c) {
89         SynthContext JavaDoc context = getContext(c);
90
91         SynthLookAndFeel.update(context, g);
92         context.getPainter().paintTableHeaderBackground(context,
93                           g, 0, 0, c.getWidth(), c.getHeight());
94         paint(context, g);
95         context.dispose();
96     }
97
98     public void paint(Graphics g, JComponent c) {
99         SynthContext JavaDoc context = getContext(c);
100
101         paint(context, g);
102         context.dispose();
103     }
104
105     protected void paint(SynthContext JavaDoc context, Graphics g) {
106         super.paint(g, context.getComponent());
107     }
108
109     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
110                             int y, int w, int h) {
111         context.getPainter().paintTableHeaderBorder(context, g, x, y, w, h);
112     }
113 //
114
// SynthUI
115
//
116
public SynthContext JavaDoc getContext(JComponent c) {
117         return getContext(c, getComponentState(c));
118     }
119
120     private SynthContext JavaDoc getContext(JComponent c, int state) {
121         return SynthContext.getContext(SynthContext JavaDoc.class, c,
122                     SynthLookAndFeel.getRegion(c), style, state);
123     }
124
125     private Region JavaDoc getRegion(JComponent c) {
126         return SynthLookAndFeel.getRegion(c);
127     }
128
129     private int getComponentState(JComponent c) {
130         return SynthLookAndFeel.getComponentState(c);
131     }
132
133     public void propertyChange(PropertyChangeEvent evt) {
134         if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
135             updateStyle((JTableHeader)evt.getSource());
136         }
137     }
138
139
140     private static class HeaderRenderer extends DefaultTableCellRenderer
141                                  implements UIResource {
142         HeaderRenderer() {
143             setHorizontalAlignment(JLabel.LEADING);
144         }
145
146         public void setBorder(Border border) {
147             if (border instanceof SynthBorder JavaDoc) {
148                 super.setBorder(border);
149             }
150         }
151
152         public String JavaDoc getName() {
153             String JavaDoc name = super.getName();
154             if (name == null) {
155                 return "TableHeader.renderer";
156             }
157             return name;
158         }
159
160         public Component getTableCellRendererComponent(
161                             JTable table, Object JavaDoc value, boolean isSelected,
162                             boolean hasFocus, int row, int column) {
163             setText((value == null) ? "" : value.toString());
164             return this;
165         }
166     }
167 }
168
Popular Tags