00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.console.gui.jtools;
00026
00027 import java.awt.Dimension;
00028 import java.awt.Font;
00029 import java.awt.Graphics;
00030
00031 import javax.swing.CellRendererPane;
00032 import javax.swing.JComponent;
00033 import javax.swing.JTextArea;
00034 import javax.swing.JToolTip;
00035 import javax.swing.plaf.ComponentUI;
00036 import javax.swing.plaf.basic.BasicToolTipUI;
00037
00038
00039
00040
00041
00042
00043
00044
00045 public class JMultiLineToolTip extends JToolTip
00046 {
00047
00048
00049
00050
00051 public JMultiLineToolTip()
00052 {
00053 updateUI();
00054 }
00055
00056
00057
00058
00059 public void updateUI()
00060 {
00061 setUI(MultiLineToolTipUI.createUI(this));
00062 }
00063
00064
00065
00066
00067
00068
00069 public void setColumns(int columns)
00070 {
00071 this.columns = columns;
00072 this.fixedwidth = 0;
00073 }
00074
00075
00076
00077
00078
00079
00080 public int getColumns()
00081 {
00082 return columns;
00083 }
00084
00085
00086
00087
00088
00089
00090 public void setFixedWidth(int width)
00091 {
00092 this.fixedwidth = width;
00093 this.columns = 0;
00094 }
00095
00096
00097
00098
00099
00100
00101 public int getFixedWidth()
00102 {
00103 return fixedwidth;
00104 }
00105
00106 protected int columns = 0;
00107 protected int fixedwidth = 0;
00108 }
00109
00110 class MultiLineToolTipUI extends BasicToolTipUI
00111 {
00112 static MultiLineToolTipUI sharedInstance = new MultiLineToolTipUI();
00113 Font smallFont;
00114 static JToolTip tip;
00115 protected CellRendererPane rendererPane;
00116
00117 private static JTextArea textArea;
00118
00119
00120
00121
00122 public static ComponentUI createUI(JComponent c)
00123 {
00124 return sharedInstance;
00125 }
00126
00127
00128
00129
00130 public MultiLineToolTipUI()
00131 {
00132 super();
00133 }
00134
00135
00136
00137
00138 public void installUI(JComponent c)
00139 {
00140 super.installUI(c);
00141 tip = (JToolTip) c;
00142 rendererPane = new CellRendererPane();
00143 c.add(rendererPane);
00144 }
00145
00146
00147
00148
00149 public void uninstallUI(JComponent c)
00150 {
00151 super.uninstallUI(c);
00152
00153 c.remove(rendererPane);
00154 rendererPane = null;
00155 }
00156
00157
00158
00159
00160
00161 public void paint(Graphics g, JComponent c)
00162 {
00163 Dimension size = c.getSize();
00164 textArea.setBackground(c.getBackground());
00165 rendererPane.paintComponent(g, textArea, c, 1, 1, size.width - 1,
00166 size.height - 1, true);
00167 }
00168
00169
00170
00171
00172 public Dimension getPreferredSize(JComponent c)
00173 {
00174 String tipText = ((JToolTip) c).getTipText();
00175 if (tipText == null)
00176 return new Dimension(0, 0);
00177 textArea = new JTextArea(tipText);
00178 rendererPane.removeAll();
00179 rendererPane.add(textArea);
00180 textArea.setWrapStyleWord(true);
00181 int width = ((JMultiLineToolTip) c).getFixedWidth();
00182 int columns = ((JMultiLineToolTip) c).getColumns();
00183
00184 if (columns > 0)
00185 {
00186 textArea.setColumns(columns);
00187 textArea.setSize(0, 0);
00188 textArea.setLineWrap(true);
00189 textArea.setSize(textArea.getPreferredSize());
00190 }
00191 else if (width > 0)
00192 {
00193 textArea.setLineWrap(true);
00194 Dimension d = textArea.getPreferredSize();
00195 d.width = width;
00196 d.height++;
00197 textArea.setSize(d);
00198 }
00199 else
00200 textArea.setLineWrap(false);
00201
00202 Dimension dim = textArea.getPreferredSize();
00203
00204 dim.height += 1;
00205 dim.width += 1;
00206 return dim;
00207 }
00208
00209
00210
00211
00212 public Dimension getMinimumSize(JComponent c)
00213 {
00214 return getPreferredSize(c);
00215 }
00216
00217
00218
00219
00220 public Dimension getMaximumSize(JComponent c)
00221 {
00222 return getPreferredSize(c);
00223 }
00224 }