KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > table > ColumnHeaderRenderer


1 /*
2  * $Id: ColumnHeaderRenderer.java,v 1.3 2004/09/22 05:11:44 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.table;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.awt.Color JavaDoc;
12 import java.awt.Component JavaDoc;
13 import java.awt.Font JavaDoc;
14 import java.awt.Graphics JavaDoc;
15 import java.awt.Graphics2D JavaDoc;
16 import java.awt.Insets JavaDoc;
17 import java.awt.Rectangle JavaDoc;
18 import java.awt.RenderingHints JavaDoc;
19
20 import javax.swing.Icon JavaDoc;
21 import javax.swing.JLabel JavaDoc;
22 import javax.swing.JPanel JavaDoc;
23 import javax.swing.JTable JavaDoc;
24 import javax.swing.UIManager JavaDoc;
25 import javax.swing.border.Border JavaDoc;
26 import javax.swing.border.CompoundBorder JavaDoc;
27 import javax.swing.border.EmptyBorder JavaDoc;
28 import javax.swing.table.JTableHeader JavaDoc;
29 import javax.swing.table.TableCellRenderer JavaDoc;
30
31 import org.jdesktop.swing.JXTable;
32 import org.jdesktop.swing.LabelProperties;
33 import org.jdesktop.swing.decorator.Sorter;
34 import org.jdesktop.swing.icon.SortArrowIcon;
35
36 /**
37  * Header renderer class which renders column sort feedback (arrows).
38  *
39  * @author Amy Fowler
40  * @author Ramesh Gupta
41  */

42 public class ColumnHeaderRenderer extends JPanel JavaDoc implements TableCellRenderer JavaDoc {
43
44     private static TableCellRenderer JavaDoc sharedInstance = null;
45     private static Icon JavaDoc defaultDownIcon = new SortArrowIcon(false);
46     private static Icon JavaDoc defaultUpIcon = new SortArrowIcon(true);
47     private static Border JavaDoc defaultMarginBorder = new EmptyBorder JavaDoc(2,2,2,2);
48
49     private Icon JavaDoc downIcon = defaultDownIcon;
50     private Icon JavaDoc upIcon = defaultUpIcon;
51     private JLabel JavaDoc label = new JLabel JavaDoc("", JLabel.CENTER);
52     private JLabel JavaDoc arrow = new JLabel JavaDoc((Icon JavaDoc) null, JLabel.CENTER);
53     private boolean antiAliasedText = false;
54
55     private boolean backgroundSet = false;
56     private boolean foregroundSet = false;
57     private boolean fontSet = false;
58
59     public static TableCellRenderer JavaDoc getSharedInstance() {
60          if (sharedInstance == null) {
61              sharedInstance = new ColumnHeaderRenderer();
62          }
63          return sharedInstance;
64      }
65
66     public ColumnHeaderRenderer() {
67         setLayout(new BorderLayout JavaDoc());
68         // initialize default properties
69
Font JavaDoc boldFont = label.getFont().deriveFont(Font.BOLD);
70         label.setFont(boldFont);
71         label.setOpaque(false);
72         add(label);
73         add(arrow, BorderLayout.EAST);
74     }
75
76     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
77         boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) {
78         label.setText(value == null ? "" : value.toString());
79         JTableHeader JavaDoc header = table.getTableHeader();
80         if (header != null) {
81             // inherit properties from header only if they were not set
82
// on a per-column-header basis
83
if (!foregroundSet) {
84                 setForeground(header.getForeground());
85                 foregroundSet = false;
86             }
87             if (!backgroundSet) {
88                 setBackground(header.getBackground());
89                 backgroundSet = false;
90             }
91             if (!fontSet) {
92                 setFont(header.getFont());
93                 fontSet = false;
94             }
95         }
96         if (table instanceof JXTable) {
97             // We no longer limit ourselves to a single "currently sorted column"
98
Sorter sorter = ((JXTable) table).getSorter(columnIndex);
99
100             if (sorter == null) {
101                 arrow.setIcon(null);
102             }
103             else {
104                 arrow.setIcon(sorter.isAscending() ? upIcon : downIcon);
105             }
106         }
107         /**@todo aim: setting this every time seems inefficient? */
108         setBorder(new CompoundBorder JavaDoc(
109             UIManager.getBorder("TableHeader.cellBorder"),
110             defaultMarginBorder));
111         return this;
112     }
113
114     public void setAntiAliasedText(boolean antiAlias) {
115         this.antiAliasedText = antiAlias;
116     }
117
118     public boolean getAntiAliasedText() {
119         return antiAliasedText;
120     }
121
122     public void setBackground(Color JavaDoc background) {
123         backgroundSet = true;
124         super.setBackground(background);
125     }
126
127     public void setForeground(Color JavaDoc foreground) {
128         foregroundSet = true;
129         super.setForeground(foreground);
130         if (label != null) {
131             label.setForeground(foreground);
132         }
133     }
134
135     public void setFont(Font JavaDoc font) {
136         fontSet = true;
137         super.setFont(font);
138         if (label != null) {
139             label.setFont(font);
140         }
141     }
142
143     public void setDownIcon(Icon JavaDoc icon) {
144         this.downIcon = icon;
145     }
146
147     public Icon JavaDoc getDownIcon() {
148         return downIcon;
149     }
150
151     public void setUpIcon(Icon JavaDoc icon) {
152         this.upIcon = icon;
153     }
154
155     public Icon JavaDoc getUpIcon() {
156         return upIcon;
157     }
158
159     public void setHorizontalAlignment(int alignment) {
160         label.setHorizontalAlignment(alignment);
161     }
162
163     public int getHorizontalAlignment() {
164         return label.getHorizontalAlignment();
165     }
166
167     public void setHorizontalTextPosition(int textPosition) {
168         label.setHorizontalTextPosition(textPosition);
169     }
170
171     public int getHorizontalTextPosition() {
172         return label.getHorizontalTextPosition();
173     }
174
175     public void setIcon(Icon JavaDoc icon) {
176         label.setIcon(icon);
177     }
178
179     public Icon JavaDoc getIcon() {
180         return label.getIcon();
181     }
182
183     public void setIconTextGap(int iconTextGap) {
184         label.setIconTextGap(iconTextGap);
185     }
186
187     public int getIconTextGap() {
188         return label.getIconTextGap();
189     }
190
191     public void setVerticalAlignment(int alignment) {
192         label.setVerticalAlignment(alignment);
193     }
194
195     public int getVerticalAlignment() {
196         return label.getVerticalAlignment();
197     }
198
199     public void setVerticalTextPosition(int textPosition) {
200         label.setVerticalTextPosition(textPosition);
201     }
202
203     public int getVerticalTextPosition() {
204         return label.getVerticalTextPosition();
205     }
206
207     public void paint(Graphics JavaDoc g) {
208         if (antiAliasedText) {
209             Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
210             Object JavaDoc save = g2.getRenderingHint(RenderingHints.
211                                               KEY_TEXT_ANTIALIASING);
212             g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
213                                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
214
215             super.paint(g2);
216
217             g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, save);
218         } else {
219             super.paint(g);
220         }
221     }
222 }
223
Popular Tags