01: /*
02: * DynamicRenderer.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.lang.reflect.Method;
15: import workbench.log.LogMgr;
16:
17: /**
18: * A renderer that uses reflection to obtain the real data value
19: * from the object retrieved from the database.
20: * @author support@sql-workbench.net
21: */
22: public class DynamicRenderer extends ToolTipRenderer implements
23: WbRenderer {
24: private Method getter;
25: private Class valueClass;
26:
27: public DynamicRenderer(Class clz, String getterName)
28: throws ClassNotFoundException, NoSuchMethodException {
29: this .valueClass = clz;
30: this .getter = valueClass.getMethod(getterName, (Class[]) null);
31: }
32:
33: @SuppressWarnings(value="unchecked")
34: public void prepareDisplay(Object aValue) {
35: // ToolTipRenderer will never pass null
36: this .displayValue = null;
37: if (valueClass.isAssignableFrom(aValue.getClass())) {
38: try {
39: Object value = getter.invoke(aValue);
40: if (value instanceof byte[]) {
41: this .displayValue = new String((byte[]) value);
42: } else if (value != null) {
43: this .displayValue = value.toString();
44: }
45: } catch (Exception e) {
46: String msg = e.getMessage();
47: Throwable cause = e.getCause();
48: while (msg == null && cause != null) {
49: msg = cause.getMessage();
50: if (msg == null && cause != null)
51: cause = cause.getCause();
52: }
53: LogMgr.logError("DynamicRenderer.prepareDisplay()",
54: "Could not obtain real object value: "
55: + e.getMessage(), null);
56: this.displayValue = null;
57: }
58: } else {
59: displayValue = aValue.toString();
60: }
61: setTooltip(displayValue);
62: }
63: }
|