01: package net.sourceforge.squirrel_sql.plugins.graph;
02:
03: import javax.swing.*;
04: import java.awt.event.MouseEvent;
05: import java.awt.*;
06: import java.awt.geom.AffineTransform;
07:
08: public class ZoomableColumnTextArea extends JPanel {
09: private TableToolTipProvider _toolTipProvider;
10: private ColumnInfo[] _columnInfos;
11: private Zoomer _zoomer;
12:
13: public ZoomableColumnTextArea(TableToolTipProvider toolTipProvider,
14: Zoomer zoomer) {
15: _toolTipProvider = toolTipProvider;
16: _zoomer = zoomer;
17: setToolTipText("Just to make getToolTiptext() to be called");
18: }
19:
20: public String getToolTipText(MouseEvent event) {
21: return _toolTipProvider.getToolTipText(event);
22: }
23:
24: public void setGraphColumns(ColumnInfo[] columnInfos) {
25: _columnInfos = columnInfos;
26: }
27:
28: public void paint(Graphics g) {
29: super .paint(g);
30:
31: Graphics2D g2d = (Graphics2D) g;
32:
33: AffineTransform origTrans = g2d.getTransform();
34:
35: try {
36: AffineTransform at = new AffineTransform(origTrans);
37: at.scale(_zoomer.getZoom(), _zoomer.getZoom());
38: g2d.setTransform(at);
39:
40: int textHeight = getTextHeight();
41:
42: int curBaseLine = textHeight - 3;
43: for (int i = 0; i < _columnInfos.length; i++) {
44: g2d.drawString(_columnInfos[i].toString(), 0,
45: curBaseLine);
46: curBaseLine += textHeight;
47: }
48: } finally {
49: g2d.setTransform(origTrans);
50: }
51: }
52:
53: private int getTextHeight() {
54: FontMetrics fm = getFontMetrics(getFont());
55: int textHeight = (fm.getAscent() - fm.getLeading() - fm
56: .getDescent()) + 6;
57: return textHeight;
58: }
59:
60: private int getMaxTextWidht() {
61: FontMetrics fm = getFontMetrics(getFont());
62:
63: int maxTextWidht = fm.stringWidth(_columnInfos[0].toString());
64: for (int i = 1; i < _columnInfos.length; i++) {
65: maxTextWidht = Math.max(maxTextWidht, fm
66: .stringWidth(_columnInfos[0].toString()));
67: }
68:
69: return (int) (maxTextWidht * _zoomer.getZoom() + 0.5);
70:
71: }
72:
73: public Dimension getPreferredSize() {
74: Dimension ret = new Dimension();
75: ret.width = getMaxTextWidht();
76: ret.height = (int) (_zoomer.getZoom() * _columnInfos.length
77: * getTextHeight() + 0.5);
78: return ret;
79: }
80:
81: }
|