1 24 25 package org.objectweb.cjdbc.console.gui.jtools; 26 27 import java.awt.Dimension ; 28 import java.awt.Font ; 29 import java.awt.Graphics ; 30 31 import javax.swing.CellRendererPane ; 32 import javax.swing.JComponent ; 33 import javax.swing.JTextArea ; 34 import javax.swing.JToolTip ; 35 import javax.swing.plaf.ComponentUI ; 36 import javax.swing.plaf.basic.BasicToolTipUI ; 37 38 45 public class JMultiLineToolTip extends JToolTip 46 { 47 48 51 public JMultiLineToolTip() 52 { 53 updateUI(); 54 } 55 56 59 public void updateUI() 60 { 61 setUI(MultiLineToolTipUI.createUI(this)); 62 } 63 64 69 public void setColumns(int columns) 70 { 71 this.columns = columns; 72 this.fixedwidth = 0; 73 } 74 75 80 public int getColumns() 81 { 82 return columns; 83 } 84 85 90 public void setFixedWidth(int width) 91 { 92 this.fixedwidth = width; 93 this.columns = 0; 94 } 95 96 101 public int getFixedWidth() 102 { 103 return fixedwidth; 104 } 105 106 protected int columns = 0; 107 protected int fixedwidth = 0; 108 } 109 110 class MultiLineToolTipUI extends BasicToolTipUI 111 { 112 static MultiLineToolTipUI sharedInstance = new MultiLineToolTipUI(); 113 Font smallFont; 114 static JToolTip tip; 115 protected CellRendererPane rendererPane; 116 117 private static JTextArea textArea; 118 119 122 public static ComponentUI createUI() 123 { 124 return sharedInstance; 125 } 126 127 130 public MultiLineToolTipUI() 131 { 132 super(); 133 } 134 135 138 public void installUI(JComponent c) 139 { 140 super.installUI(c); 141 tip = (JToolTip ) c; 142 rendererPane = new CellRendererPane (); 143 c.add(rendererPane); 144 } 145 146 149 public void uninstallUI(JComponent c) 150 { 151 super.uninstallUI(c); 152 153 c.remove(rendererPane); 154 rendererPane = null; 155 } 156 157 161 public void paint(Graphics g, JComponent c) 162 { 163 Dimension size = c.getSize(); 164 textArea.setBackground(c.getBackground()); 165 rendererPane.paintComponent(g, textArea, c, 1, 1, size.width - 1, 166 size.height - 1, true); 167 } 168 169 172 public Dimension getPreferredSize(JComponent c) 173 { 174 String tipText = ((JToolTip ) c).getTipText(); 175 if (tipText == null) 176 return new Dimension (0, 0); 177 textArea = new JTextArea (tipText); 178 rendererPane.removeAll(); 179 rendererPane.add(textArea); 180 textArea.setWrapStyleWord(true); 181 int width = ((JMultiLineToolTip) c).getFixedWidth(); 182 int columns = ((JMultiLineToolTip) c).getColumns(); 183 184 if (columns > 0) 185 { 186 textArea.setColumns(columns); 187 textArea.setSize(0, 0); 188 textArea.setLineWrap(true); 189 textArea.setSize(textArea.getPreferredSize()); 190 } 191 else if (width > 0) 192 { 193 textArea.setLineWrap(true); 194 Dimension d = textArea.getPreferredSize(); 195 d.width = width; 196 d.height++; 197 textArea.setSize(d); 198 } 199 else 200 textArea.setLineWrap(false); 201 202 Dimension dim = textArea.getPreferredSize(); 203 204 dim.height += 1; 205 dim.width += 1; 206 return dim; 207 } 208 209 212 public Dimension getMinimumSize(JComponent c) 213 { 214 return getPreferredSize(c); 215 } 216 217 220 public Dimension getMaximumSize(JComponent c) 221 { 222 return getPreferredSize(c); 223 } 224 } | Popular Tags |