1 /* 2 * Copyright (c) 2003-2008 by Cosylab d. d. 3 * 4 * This file is part of CosyBeans-Common. 5 * 6 * CosyBeans-Common is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * CosyBeans-Common is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package com.cosylab.gui.components.table.renderers; 21 22 import java.awt.Component; 23 import java.text.SimpleDateFormat; 24 import java.util.Date; 25 26 import javax.swing.JLabel; 27 import javax.swing.JTable; 28 import javax.swing.UIManager; 29 import javax.swing.border.Border; 30 import javax.swing.border.EmptyBorder; 31 import javax.swing.table.TableCellRenderer; 32 33 /** 34 * This is default date formatter for ObjectTable. It uses <code> 35 * SimpleDateFormat</code> to format the <code>Date</code> object. Format 36 * is specified as yyyy-MM-dd hh:mm:ss.SSS 37 * 38 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a> 39 * @version $id$ 40 */ 41 public class DateRenderer extends JLabel implements TableCellRenderer { 42 43 private static final long serialVersionUID = 1L; 44 45 private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 46 47 private static final SimpleDateFormat dateFormatter = 48 new SimpleDateFormat("yyyy'-'MM'-'dd' 'hh':'mm':'ss'.'SSS"); 49 50 /** 51 * Constructor for DateFormatter. 52 */ 53 public DateRenderer() { 54 super(); 55 setOpaque(true); 56 } 57 58 /** 59 * This method is sent to the renderer by the drawing table to 60 * configure the renderer appropriately before drawing. Return 61 * the Component used for drawing. 62 * 63 * @param table the JTable that is asking the renderer to draw. 64 * This parameter can be null. 65 * @param value the value of the cell to be rendered. It is 66 * up to the specific renderer to interpret 67 * and draw the value. eg. if value is the 68 * String "true", it could be rendered as a 69 * string or it could be rendered as a check 70 * box that is checked. null is a valid value. 71 * @param isSelected true is the cell is to be renderer with 72 * selection highlighting 73 * @param row the row index of the cell being drawn. When 74 * drawing the header the rowIndex is -1. 75 * @param column the column index of the cell being drawn 76 */ 77 public Component getTableCellRendererComponent( 78 JTable table, 79 Object value, 80 boolean isSelected, 81 boolean hasFocus, 82 int row, 83 int column) { 84 85 if (isSelected) { 86 setForeground(table.getSelectionForeground()); 87 setBackground(table.getSelectionBackground()); 88 } else { 89 setForeground(table.getForeground()); 90 setBackground(table.getBackground()); 91 } 92 93 if (hasFocus) { 94 setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); 95 if (table.isCellEditable(row, column)) { 96 setForeground(UIManager.getColor("Table.focusCellForeground")); 97 setBackground(UIManager.getColor("Table.focusCellBackground")); 98 } 99 } else { 100 setBorder(noFocusBorder); 101 } 102 103 setFont(table.getFont()); 104 105 if (value == null) 106 return this; 107 108 setText(dateFormatter.format((Date) value)); 109 110 return this; 111 } 112 }