001: /*
002: * Created on 21/08/2005 Swing Components - visit http://sf.net/projects/gfd
003: * This program is free software; you can redistribute it and/or modify it under the terms of the
004: * GNU General Public License as published by the Free Software Foundation; either version 2 of the License,
005: * or (at your option) any later version. This program is distributed in the hope that it will be useful, but
006: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
007: * PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU
008: * General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59
009: * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
010: */
011:
012: package br.com.igor.plaf;
013:
014: import java.awt.Dimension;
015: import java.awt.FontMetrics;
016: import java.awt.Graphics;
017: import java.awt.Toolkit;
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.StringReader;
021: import java.util.ArrayList;
022:
023: import javax.swing.JComponent;
024: import javax.swing.JToolTip;
025: import javax.swing.SwingUtilities;
026: import javax.swing.plaf.metal.MetalToolTipUI;
027:
028: /**
029: * This code is available at http://www.objects.com.au/java/examples/src/tooltip/MultiLineToolTipUI.java
030: * There was made some improvments by Igor Regis da Silva Simoes
031: * @version 1.0 11/09/98
032: */
033: public class MultiLineToolTipUI extends MetalToolTipUI {
034: private String[] strs;
035:
036: /**
037: * Pinta o componente na tela levando-se em conta a quantidade de linhas
038: * @see javax.swing.plaf.ComponentUI#paint(java.awt.Graphics, javax.swing.JComponent)
039: */
040: @SuppressWarnings("deprecation")
041: @Override
042: public void paint(Graphics g, JComponent c) {
043: FontMetrics metrics = Toolkit.getDefaultToolkit()
044: .getFontMetrics(g.getFont());
045: Dimension size = c.getSize();
046: g.setColor(c.getBackground());
047: g.fillRect(0, 0, size.width, size.height);
048: g.setColor(c.getForeground());
049: if (strs != null) {
050: for (int i = 0; i < strs.length; i++) {
051: g.drawString(strs[i], 3, (metrics.getHeight())
052: * (i + 1));
053: }
054: }
055: }
056:
057: /**
058: * Retorna o tamanho do tooltip de acordo com a quantidade de linhas que ele terá
059: * @see javax.swing.plaf.ComponentUI#getPreferredSize(javax.swing.JComponent)
060: */
061: @SuppressWarnings("deprecation")
062: @Override
063: public Dimension getPreferredSize(JComponent c) {
064: FontMetrics metrics = Toolkit.getDefaultToolkit()
065: .getFontMetrics(c.getFont());
066: String tipText = ((JToolTip) c).getTipText();
067: if (tipText == null) {
068: tipText = "";
069: }
070: BufferedReader br = new BufferedReader(
071: new StringReader(tipText));
072: String line;
073: int maxWidth = 0;
074: ArrayList<String> v = new ArrayList<String>();
075: try {
076: while ((line = br.readLine()) != null) {
077: int width = SwingUtilities.computeStringWidth(metrics,
078: line);
079: maxWidth = (maxWidth < width) ? width : maxWidth;
080: v.add(line);
081: }
082: } catch (IOException ex) {
083: ex.printStackTrace();
084: }
085: int lines = v.size();
086: if (lines < 1) {
087: strs = null;
088: lines = 1;
089: } else {
090: strs = new String[lines];
091: int i = 0;
092: for (String e : v) {
093: strs[i] = e;
094: i++;
095: }
096: }
097: int height = metrics.getHeight() * lines;
098: return new Dimension(maxWidth + 6, height + 4);
099: }
100: }
|