001: /*
002: * RendererFactory.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.renderer;
013:
014: import java.lang.reflect.Constructor;
015: import javax.swing.table.DefaultTableCellRenderer;
016: import javax.swing.table.TableCellRenderer;
017: import workbench.log.LogMgr;
018:
019: /**
020: * A factory for TableCellRenderers. Classes are created using
021: * Class.forName() to avoid unnecessary class loading during startup.
022: * This is used from within WbTable
023: *
024: * @see workbench.gui.components.WbTable#initDefaultRenderers()
025: *
026: * @author support@sql-workbench.net
027: */
028: public class RendererFactory {
029:
030: private static TableCellRenderer createRenderer(String className) {
031: TableCellRenderer rend = null;
032: try {
033: Class cls = Class.forName(className);
034: rend = (TableCellRenderer) cls.newInstance();
035: } catch (Exception e) {
036: LogMgr.logError("RendererFactory.createRenderer()",
037: "Error creating renderer", e);
038: rend = new DefaultTableCellRenderer();
039: }
040: return rend;
041: }
042:
043: public static TableCellRenderer getSortHeaderRenderer() {
044: return createRenderer("workbench.gui.components.SortHeaderRenderer");
045: }
046:
047: public static TableCellRenderer getDateRenderer(String format) {
048: TableCellRenderer rend = null;
049: try {
050: Class cls = Class
051: .forName("workbench.gui.renderer.DateColumnRenderer");
052: Class[] types = new Class[] { String.class };
053: Constructor cons = cls.getConstructor(types);
054: Object[] args = new Object[] { format };
055: rend = (TableCellRenderer) cons.newInstance(args);
056: } catch (Exception e) {
057: LogMgr.logError("RendererFactory.getDateRenderer()",
058: "Error creating renderer", e);
059: return new DefaultTableCellRenderer();
060: }
061: return rend;
062: }
063:
064: public static TableCellRenderer getTooltipRenderer() {
065: return createRenderer("workbench.gui.renderer.ToolTipRenderer");
066: }
067:
068: public static TableCellRenderer getStringRenderer() {
069: return createRenderer("workbench.gui.renderer.StringColumnRenderer");
070: }
071:
072: public static TableCellRenderer getIntegerRenderer() {
073: return createRenderer("workbench.gui.renderer.NumberColumnRenderer");
074: }
075:
076: public static TableCellRenderer createNumberRenderer(int maxDigits,
077: char sep) {
078: try {
079: Class cls = Class
080: .forName("workbench.gui.renderer.NumberColumnRenderer");
081: Class[] types = new Class[] { int.class, char.class };
082: Constructor cons = cls.getConstructor(types);
083: Object[] args = new Object[] { new Integer(maxDigits),
084: new Character(sep) };
085: TableCellRenderer rend = (TableCellRenderer) cons
086: .newInstance(args);
087: return rend;
088: } catch (Exception e) {
089: LogMgr.logError("RendererFactory.getNumberRenderer()",
090: "Error creating renderer", e);
091: return new DefaultTableCellRenderer();
092: }
093: }
094:
095: public static TableCellRenderer getMultiLineRenderer() {
096: return createRenderer("workbench.gui.renderer.TextAreaRenderer");
097: }
098:
099: public static TableCellRenderer getSqlTypeRenderer() {
100: return createRenderer("workbench.gui.renderer.SqlTypeRenderer");
101: }
102:
103: public static TableCellRenderer getBlobRenderer() {
104: return createRenderer("workbench.gui.renderer.BlobColumnRenderer");
105: }
106:
107: }
|