KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > swing > JMultiLineToolTip


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.swing;
20
21 import java.awt.*;
22
23 import javax.swing.*;
24 import javax.swing.plaf.*;
25 import javax.swing.plaf.basic.*;
26
27 /**
28  *
29  * @author Matthew Large
30  * @version $Revision: 1.1 $
31  *
32  */

33 public class JMultiLineToolTip extends JToolTip
34 {
35     private static final String JavaDoc uiClassID = "ToolTipUI";
36     
37     String JavaDoc tipText;
38     JComponent component;
39     
40     public JMultiLineToolTip() {
41         updateUI();
42     }
43     
44     public void updateUI() {
45         setUI(MultiLineToolTipUI.createUI(this));
46     }
47     
48     public void setColumns(int columns)
49     {
50         this.columns = columns;
51         this.fixedwidth = 0;
52     }
53     
54     public int getColumns()
55     {
56         return columns;
57     }
58     
59     public void setFixedWidth(int width)
60     {
61         this.fixedwidth = width;
62         this.columns = 0;
63     }
64     
65     public int getFixedWidth()
66     {
67         return fixedwidth;
68     }
69     
70     protected int columns = 0;
71     protected int fixedwidth = 0;
72 }
73
74
75
76 class MultiLineToolTipUI extends BasicToolTipUI {
77     static MultiLineToolTipUI sharedInstance = new MultiLineToolTipUI();
78     Font smallFont;
79     static JToolTip tip;
80     protected CellRendererPane rendererPane;
81     
82     private static JTextArea textArea ;
83     
84     public static ComponentUI createUI(JComponent c) {
85         return sharedInstance;
86     }
87     
88     public MultiLineToolTipUI() {
89         super();
90     }
91     
92     public void installUI(JComponent c) {
93         super.installUI(c);
94         tip = (JToolTip)c;
95         rendererPane = new CellRendererPane();
96         c.add(rendererPane);
97     }
98     
99     public void uninstallUI(JComponent c) {
100         super.uninstallUI(c);
101         
102         c.remove(rendererPane);
103         rendererPane = null;
104     }
105     
106     public void paint(Graphics g, JComponent c) {
107         Dimension size = c.getSize();
108         textArea.setBackground(c.getBackground());
109         rendererPane.paintComponent(g, textArea, c, 1, 1,
110                         size.width - 1, size.height - 1, true);
111     }
112     
113     public Dimension getPreferredSize(JComponent c) {
114         String JavaDoc tipText = ((JToolTip)c).getTipText();
115         if (tipText == null)
116             return new Dimension(0,0);
117         textArea = new JTextArea(tipText );
118         rendererPane.removeAll();
119         rendererPane.add(textArea );
120         textArea.setWrapStyleWord(true);
121         int width = ((JMultiLineToolTip)c).getFixedWidth();
122         int columns = ((JMultiLineToolTip)c).getColumns();
123         
124         if( columns > 0 )
125         {
126             textArea.setColumns(columns);
127             textArea.setSize(0,0);
128         textArea.setLineWrap(true);
129             textArea.setSize( textArea.getPreferredSize() );
130         }
131         else if( width > 0 )
132         {
133         textArea.setLineWrap(true);
134             Dimension d = textArea.getPreferredSize();
135             d.width = width;
136             d.height++;
137             textArea.setSize(d);
138         }
139         else
140             textArea.setLineWrap(false);
141
142
143         Dimension dim = textArea.getPreferredSize();
144         
145         dim.height += 1;
146         dim.width += 1;
147         return dim;
148     }
149     
150     public Dimension getMinimumSize(JComponent c) {
151         return getPreferredSize(c);
152     }
153     
154     public Dimension getMaximumSize(JComponent c) {
155         return getPreferredSize(c);
156     }
157 }
158
Popular Tags