KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > renderers > DurationTableCellRenderer


1 package org.netbeans.modules.tasklist.usertasks.renderers;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.FontMetrics JavaDoc;
5 import java.awt.Insets JavaDoc;
6 import java.text.MessageFormat JavaDoc;
7 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
8 import javax.swing.table.TableColumnModel JavaDoc;
9 import org.netbeans.modules.tasklist.usertasks.options.Settings;
10 import org.netbeans.modules.tasklist.usertasks.model.Duration;
11 import org.netbeans.modules.tasklist.usertasks.util.DurationFormat;
12
13 /**
14  * Cell renderer for duration values
15  *
16  * @author tl
17  */

18 public class DurationTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
19     private static final DurationFormat EFFORT_FORMAT =
20             new DurationFormat(DurationFormat.Type.LONG);
21     private static final DurationFormat SHORT_EFFORT_FORMAT =
22             new DurationFormat(DurationFormat.Type.SHORT);
23
24     public Component JavaDoc getTableCellRendererComponent(javax.swing.JTable JavaDoc table,
25         Object JavaDoc value, boolean isSelected, boolean hasFocus,
26         int row, int column) {
27         super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
28             row, column);
29         
30         Duration duration = getDuration(value);
31         String JavaDoc txt;
32         if (duration == null)
33             txt = ""; // NOI18N
34
else {
35             String JavaDoc s = EFFORT_FORMAT.format(duration);
36             FontMetrics JavaDoc fm = getFontMetrics(getFont());
37             TableColumnModel JavaDoc tcm = table.getColumnModel();
38             int w = tcm.getColumn(column).getWidth() -
39                     tcm.getColumnMargin();
40             Insets JavaDoc insets = getInsets();
41             w -= insets.left + insets.right;
42             if (w >= fm.stringWidth(s))
43                 txt = s;
44             else
45                 txt = SHORT_EFFORT_FORMAT.format(duration).trim();
46         }
47         setText(txt);
48         return this;
49     }
50
51     /**
52      * Retrieves duration from the given object
53      *
54      * @param obj object from the getTableCellRendererComponent method
55      * @return duration or null
56      */

57     protected Duration getDuration(Object JavaDoc obj) {
58         if (obj == null)
59             return null;
60         else
61             return new Duration(((Integer JavaDoc) obj).intValue(),
62                     Settings.getDefault().getMinutesPerDay(),
63                     Settings.getDefault().getDaysPerWeek(), true);
64     }
65
66     // overriden for performance reasons
67
@Override JavaDoc
68     protected void setValue(Object JavaDoc value) {
69     }
70 }
71
Popular Tags