KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjMultiLineToolTip


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.Dimension JavaDoc;
33 import java.awt.FontMetrics JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.io.BufferedReader JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.StringReader JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.JLabel JavaDoc;
43 import javax.swing.JToolTip JavaDoc;
44 import javax.swing.SwingUtilities JavaDoc;
45 import javax.swing.plaf.metal.MetalToolTipUI JavaDoc;
46
47 import com.genimen.djeneric.util.DjLogger;
48
49 public class DjMultiLineToolTip extends JToolTip JavaDoc
50 {
51   private static final long serialVersionUID = 1L;
52
53   public DjMultiLineToolTip()
54   {
55     setUI(new MultiLineToolTipUI());
56   }
57 }
58
59 class MultiLineToolTipUI extends MetalToolTipUI JavaDoc
60 {
61   private String JavaDoc[] strs;
62
63   public void paint(Graphics JavaDoc g, JComponent JavaDoc c)
64   {
65     FontMetrics JavaDoc metrics = new JLabel JavaDoc().getFontMetrics(c.getFont());
66     Dimension JavaDoc size = c.getSize();
67     g.setColor(c.getBackground());
68     g.fillRect(0, 0, size.width, size.height);
69     g.setColor(c.getForeground());
70     if (strs != null)
71     {
72       for (int i = 0; i < strs.length; i++)
73       {
74         g.drawString(strs[i], 3, (metrics.getHeight()) * (i + 1));
75       }
76     }
77   }
78
79   public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c)
80   {
81     FontMetrics JavaDoc metrics = new JLabel JavaDoc().getFontMetrics(c.getFont());
82     String JavaDoc tipText = ((JToolTip JavaDoc) c).getTipText();
83     if (tipText == null)
84     {
85       tipText = "";
86     }
87     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc(tipText));
88     String JavaDoc line;
89     int maxWidth = 0;
90     Vector JavaDoc v = new Vector JavaDoc();
91     try
92     {
93       while ((line = br.readLine()) != null)
94       {
95         int width = SwingUtilities.computeStringWidth(metrics, line);
96         maxWidth = (maxWidth < width) ? width : maxWidth;
97         v.addElement(line);
98       }
99     }
100     catch (IOException JavaDoc ex)
101     {
102       DjLogger.log(ex);
103     }
104     int lines = v.size();
105     if (lines < 1)
106     {
107       strs = null;
108       lines = 1;
109     }
110     else
111     {
112       strs = new String JavaDoc[lines];
113       int i = 0;
114       for (Enumeration JavaDoc e = v.elements(); e.hasMoreElements(); i++)
115       {
116         strs[i] = (String JavaDoc) e.nextElement();
117       }
118     }
119     int height = metrics.getHeight() * lines;
120     return new Dimension JavaDoc(maxWidth + 6, height + 4);
121   }
122 }
Popular Tags