01: /*
02: * TextAreaRenderer.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.awt.Component;
15: import java.awt.Insets;
16: import javax.swing.JTable;
17: import javax.swing.JTextArea;
18: import javax.swing.SwingConstants;
19: import javax.swing.table.TableCellRenderer;
20: import workbench.gui.WbSwingUtilities;
21: import workbench.util.StringUtil;
22:
23: /**
24: * @author support@sql-workbench.net
25: */
26: public class TextAreaRenderer extends ToolTipRenderer implements
27: TableCellRenderer, WbRenderer {
28: protected JTextArea textDisplay;
29:
30: public TextAreaRenderer() {
31: super ();
32: textDisplay = new JTextArea() {
33: public Insets getInsets() {
34: return WbSwingUtilities.EMPTY_INSETS;
35: }
36: };
37: }
38:
39: public int getHorizontalAlignment() {
40: return SwingConstants.LEFT;
41: }
42:
43: public Component getTableCellRendererComponent(JTable table,
44: Object value, boolean isSelected, boolean hasFocus,
45: int row, int col) {
46: initDisplay(table, value, isSelected, hasFocus, row, col);
47:
48: this .textDisplay.setFont(table.getFont());
49:
50: if (hasFocus) {
51: this .textDisplay
52: .setBorder(WbSwingUtilities.FOCUSED_CELL_BORDER);
53: } else {
54: this .textDisplay.setBorder(WbSwingUtilities.EMPTY_BORDER);
55: }
56:
57: prepareDisplay(value);
58:
59: this .textDisplay.setBackground(getBackgroundColor());
60: this .textDisplay.setForeground(getForegroundColor());
61:
62: return textDisplay;
63: }
64:
65: public Insets getInsets() {
66: return WbSwingUtilities.EMPTY_INSETS;
67: }
68:
69: public void prepareDisplay(Object value) {
70: if (value == null) {
71: this .displayValue = null;
72: this .textDisplay.setText("");
73: this .textDisplay.setToolTipText(null);
74: } else {
75: this .displayValue = value.toString();
76: this .textDisplay.setText(this .displayValue);
77: this .textDisplay.setToolTipText(StringUtil.getMaxSubstring(
78: this .displayValue, maxTooltipSize));
79: }
80: }
81:
82: /**
83: * This is used by WbTable to calculate the optimal column
84: * width. Assuming that most of the time only the first line
85: * is visible, only that is returned.
86: */
87: public String getDisplayValue() {
88: if (displayValue == null)
89: return null;
90: int pos = displayValue.indexOf('\n');
91: if (pos > 0) {
92: return displayValue.substring(0, pos);
93: }
94: return displayValue;
95: }
96:
97: }
|