01: /*
02: * DateColumnRenderer.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.renderer;
13:
14: import java.text.SimpleDateFormat;
15: import javax.swing.SwingConstants;
16: import workbench.log.LogMgr;
17:
18: /**
19: *
20: * @author support@sql-workbench.net
21: */
22: public class DateColumnRenderer extends ToolTipRenderer {
23: private SimpleDateFormat dateFormatter;
24:
25: public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
26:
27: public DateColumnRenderer() {
28: this .dateFormatter = new SimpleDateFormat(DEFAULT_FORMAT);
29: this .setHorizontalAlignment(SwingConstants.RIGHT);
30: }
31:
32: public DateColumnRenderer(String aDateFormat) {
33: this ();
34: this .setFormat(aDateFormat);
35: }
36:
37: public void setFormat(String aDateFormat) {
38: try {
39: synchronized (this .dateFormatter) {
40: this .dateFormatter.applyPattern(aDateFormat);
41: }
42: } catch (Exception e) {
43: LogMgr.logWarning("DateColumnRenderer.setFormat()",
44: "Error when setting date format [" + aDateFormat
45: + "] default format [" + DEFAULT_FORMAT
46: + "] will be used instead", e);
47: this .dateFormatter.applyPattern(DEFAULT_FORMAT);
48: }
49: }
50:
51: public void prepareDisplay(Object value) {
52: try {
53: java.util.Date d = (java.util.Date) value;
54: synchronized (this .dateFormatter) {
55: this .displayValue = this .dateFormatter.format(d);
56: }
57: tooltip = d.toString();
58: } catch (Throwable cc) {
59: this.displayValue = value.toString();
60: setTooltip(displayValue);
61: }
62: }
63:
64: }
|