KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > gui > jtools > JMultiLineToolTip


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.gui.jtools;
26
27 import java.awt.Dimension JavaDoc;
28 import java.awt.Font JavaDoc;
29 import java.awt.Graphics JavaDoc;
30
31 import javax.swing.CellRendererPane JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JTextArea JavaDoc;
34 import javax.swing.JToolTip JavaDoc;
35 import javax.swing.plaf.ComponentUI JavaDoc;
36 import javax.swing.plaf.basic.BasicToolTipUI JavaDoc;
37
38 /**
39  * This class defines a MultiLineToolTip
40  *
41  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
42  * @author Zafir Anjum
43  * @version 1.0
44  */

45 public class JMultiLineToolTip extends JToolTip JavaDoc
46 {
47
48   /**
49    * Creates a new <code>JMultiLineToolTip.java</code> object
50    */

51   public JMultiLineToolTip()
52   {
53     updateUI();
54   }
55
56   /**
57    * @see javax.swing.JComponent#updateUI()
58    */

59   public void updateUI()
60   {
61     setUI(MultiLineToolTipUI.createUI(this));
62   }
63
64   /**
65    * Set number of columns for the tool tip
66    *
67    * @param columns integer
68    */

69   public void setColumns(int columns)
70   {
71     this.columns = columns;
72     this.fixedwidth = 0;
73   }
74
75   /**
76    * getColumns method
77    *
78    * @return integer
79    */

80   public int getColumns()
81   {
82     return columns;
83   }
84
85   /**
86    * setFixedWidth
87    *
88    * @param width value
89    */

90   public void setFixedWidth(int width)
91   {
92     this.fixedwidth = width;
93     this.columns = 0;
94   }
95
96   /**
97    * getFixedWidth definition.
98    *
99    * @return integer
100    */

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 JavaDoc
111 {
112   static MultiLineToolTipUI sharedInstance = new MultiLineToolTipUI();
113   Font JavaDoc smallFont;
114   static JToolTip JavaDoc tip;
115   protected CellRendererPane JavaDoc rendererPane;
116
117   private static JTextArea JavaDoc textArea;
118
119   /**
120    * Returns the shared <code>ComponentUI</code> instance
121    */

122   public static ComponentUI JavaDoc createUI()
123   {
124     return sharedInstance;
125   }
126
127   /**
128    * Create a new <code>MultiLineToolTipUI</code>
129    */

130   public MultiLineToolTipUI()
131   {
132     super();
133   }
134
135   /**
136    * @see javax.swing.plaf.ComponentUI#installUI(javax.swing.JComponent)
137    */

138   public void installUI(JComponent JavaDoc c)
139   {
140     super.installUI(c);
141     tip = (JToolTip JavaDoc) c;
142     rendererPane = new CellRendererPane JavaDoc();
143     c.add(rendererPane);
144   }
145
146   /**
147    * @see javax.swing.plaf.ComponentUI#uninstallUI(javax.swing.JComponent)
148    */

149   public void uninstallUI(JComponent JavaDoc c)
150   {
151     super.uninstallUI(c);
152
153     c.remove(rendererPane);
154     rendererPane = null;
155   }
156
157   /**
158    * @see javax.swing.plaf.ComponentUI#paint(java.awt.Graphics,
159    * javax.swing.JComponent)
160    */

161   public void paint(Graphics JavaDoc g, JComponent JavaDoc c)
162   {
163     Dimension JavaDoc 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   /**
170    * @see javax.swing.plaf.ComponentUI#getPreferredSize(javax.swing.JComponent)
171    */

172   public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c)
173   {
174     String JavaDoc tipText = ((JToolTip JavaDoc) c).getTipText();
175     if (tipText == null)
176       return new Dimension JavaDoc(0, 0);
177     textArea = new JTextArea JavaDoc(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 JavaDoc d = textArea.getPreferredSize();
195       d.width = width;
196       d.height++;
197       textArea.setSize(d);
198     }
199     else
200       textArea.setLineWrap(false);
201
202     Dimension JavaDoc dim = textArea.getPreferredSize();
203
204     dim.height += 1;
205     dim.width += 1;
206     return dim;
207   }
208
209   /**
210    * @see javax.swing.plaf.ComponentUI#getMinimumSize(javax.swing.JComponent)
211    */

212   public Dimension JavaDoc getMinimumSize(JComponent JavaDoc c)
213   {
214     return getPreferredSize(c);
215   }
216
217   /**
218    * @see javax.swing.plaf.ComponentUI#getMaximumSize(javax.swing.JComponent)
219    */

220   public Dimension JavaDoc getMaximumSize(JComponent JavaDoc c)
221   {
222     return getPreferredSize(c);
223   }
224 }
Popular Tags