KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TableCellRenderers.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 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.Component JavaDoc;
11 import java.awt.Color JavaDoc;
12 import java.awt.FontMetrics JavaDoc;
13 import java.awt.Graphics JavaDoc;
14 import java.awt.Rectangle JavaDoc;
15
16 import java.awt.geom.Rectangle2D JavaDoc;
17
18 import java.text.DateFormat JavaDoc;
19 import java.text.NumberFormat JavaDoc;
20
21 import java.util.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import javax.swing.Icon JavaDoc;
25 import javax.swing.JCheckBox JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JTable JavaDoc;
28 import javax.swing.SwingConstants JavaDoc;
29 import javax.swing.table.TableCellRenderer JavaDoc;
30 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
31
32 import org.jdesktop.swing.data.Link;
33
34 public class TableCellRenderers {
35
36     private static HashMap JavaDoc typeMap;
37
38     static {
39         // load the handler map with classes designed to handle the
40
// type-specific rendering
41
typeMap = new HashMap JavaDoc();
42         typeMap.put(Number JavaDoc.class,
43             "org.jdesktop.swing.table.TableCellRenderers$NumberRenderer");
44         typeMap.put(Double JavaDoc.class,
45             "org.jdesktop.swing.table.TableCellRenderers$DoubleRenderer");
46         typeMap.put(Float JavaDoc.class,
47             "org.jdesktop.swing.table.TableCellRenderers$DoubleRenderer");
48         typeMap.put(Date JavaDoc.class,
49                     "org.jdesktop.swing.table.TableCellRenderers$DateRenderer");
50         typeMap.put(Icon JavaDoc.class,
51                     "org.jdesktop.swing.table.TableCellRenderers$IconRenderer");
52         typeMap.put(Boolean JavaDoc.class,
53             "org.jdesktop.swing.table.TableCellRenderers$BooleanRenderer");
54         typeMap.put(Link.class,
55                     "org.jdesktop.swing.table.TableCellRenderers$LinkRenderer");
56
57     }
58
59     private static String JavaDoc getRendererClassName(Class JavaDoc columnClass) {
60         String JavaDoc rendererClassName = (String JavaDoc) typeMap.get(columnClass);
61         return rendererClassName != null ? rendererClassName :
62             "javax.swing.table.DefaultTableCellRenderer";
63     }
64
65     /**
66      * @see #getNewDefaultRenderer
67      * @param columnClass Class of value being rendered
68      * @param rendererClassName String containing the class name of the renderer which
69      * should be returned for the specified column class
70      */

71     public static void setDefaultRenderer(Class JavaDoc columnClass, String JavaDoc rendererClassName) {
72         typeMap.put(columnClass, rendererClassName);
73     }
74
75     /**
76      * Returns a new instance of the default renderer for the specified class.
77      * This differs from JTable:getDefaultRenderer() in that it returns a new
78      * instance each time so that the renderer may be set and customized for
79      * a particular column.
80      *
81      * @param columnClass Class of value being rendered
82      * @return TableCellRenderer instance which renders values of the specified type
83      */

84     public static TableCellRenderer JavaDoc getNewDefaultRenderer(Class JavaDoc columnClass) {
85         TableCellRenderer JavaDoc renderer = null;
86         String JavaDoc rendererClassName = getRendererClassName(columnClass);
87         try {
88             Class JavaDoc rendererClass = Class.forName(rendererClassName);
89             renderer = (TableCellRenderer JavaDoc) rendererClass.newInstance();
90         }
91         catch (Exception JavaDoc e) {
92             renderer = new DefaultTableCellRenderer JavaDoc();
93         }
94         return renderer;
95     }
96
97     TableCellRenderers() {
98     }
99
100     /*
101      * Default Type-based Renderers:
102      * JTable's default table cell renderer classes are private and
103      * JTable:getDefaultRenderer() returns a *shared* cell renderer instance,
104      * thus there is no way for us to instantiate a new instance of one of its
105      * default renderers. So, we must replicate the default renderer classes
106      * here so that we can instantiate them when we need to create renderers
107      * to be set on specific columns.
108      */

109     static class NumberRenderer extends DefaultTableCellRenderer JavaDoc {
110         public NumberRenderer() {
111             super();
112             setHorizontalAlignment(JLabel.RIGHT);
113         }
114     }
115
116     static class DoubleRenderer extends NumberRenderer {
117         NumberFormat JavaDoc formatter;
118         public DoubleRenderer() {
119             super();
120         }
121
122         public void setValue(Object JavaDoc value) {
123             if (formatter == null) {
124                 formatter = NumberFormat.getInstance();
125             }
126             setText( (value == null) ? "" : formatter.format(value));
127         }
128     }
129
130     static class DateRenderer extends DefaultTableCellRenderer JavaDoc {
131         DateFormat JavaDoc formatter;
132         public DateRenderer() {
133             super();
134         }
135
136         public void setValue(Object JavaDoc value) {
137             if (formatter == null) {
138                 formatter = DateFormat.getDateInstance();
139             }
140             setText( (value == null) ? "" : formatter.format(value));
141         }
142     }
143
144     static class IconRenderer extends DefaultTableCellRenderer JavaDoc {
145         public IconRenderer() {
146             super();
147             setHorizontalAlignment(JLabel.CENTER);
148         }
149
150         public void setValue(Object JavaDoc value) {
151             setIcon( (value instanceof Icon JavaDoc) ? (Icon JavaDoc) value : null);
152         }
153     }
154
155     static class BooleanRenderer extends JCheckBox JavaDoc
156         implements TableCellRenderer JavaDoc {
157         public BooleanRenderer() {
158             super();
159             setHorizontalAlignment(JLabel.CENTER);
160         }
161
162         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table,
163             Object JavaDoc value,
164             boolean isSelected, boolean hasFocus, int row, int column) {
165             if (isSelected) {
166                 setForeground(table.getSelectionForeground());
167                 super.setBackground(table.getSelectionBackground());
168             }
169             else {
170                 setForeground(table.getForeground());
171                 setBackground(table.getBackground());
172             }
173             setSelected( (value != null && ( (Boolean JavaDoc) value).booleanValue()));
174             return this;
175         }
176     }
177
178     /**
179      * Renders a Link type the link in the table column
180      */

181     public static class LinkRenderer extends DefaultTableCellRenderer JavaDoc {
182
183         // Should have a way of setting these statically
184
private static Color JavaDoc colorLive = new Color JavaDoc(0, 0, 238);
185         private static Color JavaDoc colorVisited = new Color JavaDoc(82, 24, 139);
186
187         public void setValue(Object JavaDoc value) {
188             if (value != null && value instanceof Link) {
189                 Link link = (Link) value;
190
191                 setText(link.getText());
192                 setToolTipText(link.getURL().toString());
193
194                 if (link.getVisited()) {
195                     setForeground(colorVisited);
196                 }
197                 else {
198                     setForeground(colorLive);
199                 }
200             }
201             else {
202                 super.setValue(value != null? value.toString() : "");
203             }
204         }
205
206         public void paintComponent(Graphics JavaDoc g) {
207             super.paintComponent(g);
208             if (!getText().equals("")) {
209                 // Render an underline. A really smart person
210
// would actually render an underline font but
211
// that's too much for my little brain.
212
Rectangle JavaDoc rect = TableCellRenderers.getTextBounds(g, this);
213
214                 FontMetrics JavaDoc fm = g.getFontMetrics();
215                 int descent = fm.getDescent();
216
217                 //REMIND(aim): should we be basing the underline on
218
//the font's baseline instead of the text bounds?
219

220                 g.drawLine(rect.x, (rect.y + rect.height) - descent + 1,
221                            rect.x + rect.width,
222                            (rect.y + rect.height) - descent + 1);
223             }
224         }
225     }
226
227
228     /**
229      * Returns the bounds that the text of a label will be drawn into.
230      * Takes into account the current font metrics.
231      */

232     static Rectangle JavaDoc getTextBounds(Graphics JavaDoc g, JLabel JavaDoc label) {
233
234     FontMetrics JavaDoc fm = g.getFontMetrics();
235     Rectangle2D JavaDoc r2d = fm.getStringBounds(label.getText(), g);
236     Rectangle JavaDoc rect = r2d.getBounds();
237
238     int xOffset = 0;
239     switch (label.getHorizontalAlignment()) {
240     case SwingConstants.RIGHT:
241     case SwingConstants.TRAILING:
242         xOffset = label.getBounds().width - rect.width;
243         break;
244
245     case SwingConstants.CENTER:
246         xOffset = (label.getBounds().width - rect.width)/2;
247         break;
248
249     default:
250     case SwingConstants.LEFT:
251     case SwingConstants.LEADING:
252         xOffset = 0;
253         break;
254     }
255
256     int yOffset = 0;
257     switch (label.getVerticalAlignment()) {
258     case SwingConstants.TOP:
259         yOffset = 0;
260         break;
261     case SwingConstants.CENTER:
262         yOffset = (label.getBounds().height - rect.height)/2;
263         break;
264     case SwingConstants.BOTTOM:
265         yOffset = label.getBounds().height - rect.height;
266         break;
267     }
268     return new Rectangle JavaDoc(xOffset, yOffset, rect.width, rect.height);
269     }
270
271
272
273 }
274
Popular Tags