1 package org.netbeans.modules.tasklist.usertasks.renderers; 2 3 import java.awt.Component ; 4 import java.awt.FontMetrics ; 5 import java.awt.Insets ; 6 import java.text.MessageFormat ; 7 import javax.swing.table.DefaultTableCellRenderer ; 8 import javax.swing.table.TableColumnModel ; 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 18 public class DurationTableCellRenderer extends DefaultTableCellRenderer { 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 getTableCellRendererComponent(javax.swing.JTable table, 25 Object 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 txt; 32 if (duration == null) 33 txt = ""; else { 35 String s = EFFORT_FORMAT.format(duration); 36 FontMetrics fm = getFontMetrics(getFont()); 37 TableColumnModel tcm = table.getColumnModel(); 38 int w = tcm.getColumn(column).getWidth() - 39 tcm.getColumnMargin(); 40 Insets 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 57 protected Duration getDuration(Object obj) { 58 if (obj == null) 59 return null; 60 else 61 return new Duration(((Integer ) obj).intValue(), 62 Settings.getDefault().getMinutesPerDay(), 63 Settings.getDefault().getDaysPerWeek(), true); 64 } 65 66 @Override 68 protected void setValue(Object value) { 69 } 70 } 71 | Popular Tags |