Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

JMultiLineToolTip.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.console.gui.jtools;
00026 
00027 import java.awt.Dimension;
00028 import java.awt.Font;
00029 import java.awt.Graphics;
00030 
00031 import javax.swing.CellRendererPane;
00032 import javax.swing.JComponent;
00033 import javax.swing.JTextArea;
00034 import javax.swing.JToolTip;
00035 import javax.swing.plaf.ComponentUI;
00036 import javax.swing.plaf.basic.BasicToolTipUI;
00037 
00038 /**
00039  * This class defines a MultiLineToolTip
00040  * 
00041  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00042  * @author Zafir Anjum
00043  * @version 1.0
00044  */
00045 public class JMultiLineToolTip extends JToolTip
00046 {
00047 
00048   /**
00049    * Creates a new <code>JMultiLineToolTip.java</code> object
00050    */
00051   public JMultiLineToolTip()
00052   {
00053     updateUI();
00054   }
00055 
00056   /**
00057    * @see javax.swing.JComponent#updateUI()
00058    */
00059   public void updateUI()
00060   {
00061     setUI(MultiLineToolTipUI.createUI(this));
00062   }
00063 
00064   /**
00065    * Set number of columns for the tool tip
00066    * 
00067    * @param columns integer
00068    */
00069   public void setColumns(int columns)
00070   {
00071     this.columns = columns;
00072     this.fixedwidth = 0;
00073   }
00074 
00075   /**
00076    * getColumns method
00077    * 
00078    * @return integer
00079    */
00080   public int getColumns()
00081   {
00082     return columns;
00083   }
00084 
00085   /**
00086    * setFixedWidth
00087    * 
00088    * @param width value
00089    */
00090   public void setFixedWidth(int width)
00091   {
00092     this.fixedwidth = width;
00093     this.columns = 0;
00094   }
00095 
00096   /**
00097    * getFixedWidth definition.
00098    * 
00099    * @return integer
00100    */
00101   public int getFixedWidth()
00102   {
00103     return fixedwidth;
00104   }
00105 
00106   protected int columns    = 0;
00107   protected int fixedwidth = 0;
00108 }
00109 
00110 class MultiLineToolTipUI extends BasicToolTipUI
00111 {
00112   static MultiLineToolTipUI  sharedInstance = new MultiLineToolTipUI();
00113   Font                       smallFont;
00114   static JToolTip            tip;
00115   protected CellRendererPane rendererPane;
00116 
00117   private static JTextArea   textArea;
00118 
00119   /**
00120    * Returns the shared <code>ComponentUI</code> instance
00121    */
00122   public static ComponentUI createUI(JComponent c)
00123   {
00124     return sharedInstance;
00125   }
00126 
00127   /**
00128    * Create a new <code>MultiLineToolTipUI</code>
00129    */
00130   public MultiLineToolTipUI()
00131   {
00132     super();
00133   }
00134 
00135   /**
00136    * @see javax.swing.plaf.ComponentUI#installUI(javax.swing.JComponent)
00137    */
00138   public void installUI(JComponent c)
00139   {
00140     super.installUI(c);
00141     tip = (JToolTip) c;
00142     rendererPane = new CellRendererPane();
00143     c.add(rendererPane);
00144   }
00145 
00146   /**
00147    * @see javax.swing.plaf.ComponentUI#uninstallUI(javax.swing.JComponent)
00148    */
00149   public void uninstallUI(JComponent c)
00150   {
00151     super.uninstallUI(c);
00152 
00153     c.remove(rendererPane);
00154     rendererPane = null;
00155   }
00156 
00157   /**
00158    * @see javax.swing.plaf.ComponentUI#paint(java.awt.Graphics,
00159    *      javax.swing.JComponent)
00160    */
00161   public void paint(Graphics g, JComponent c)
00162   {
00163     Dimension size = c.getSize();
00164     textArea.setBackground(c.getBackground());
00165     rendererPane.paintComponent(g, textArea, c, 1, 1, size.width - 1,
00166         size.height - 1, true);
00167   }
00168 
00169   /**
00170    * @see javax.swing.plaf.ComponentUI#getPreferredSize(javax.swing.JComponent)
00171    */
00172   public Dimension getPreferredSize(JComponent c)
00173   {
00174     String tipText = ((JToolTip) c).getTipText();
00175     if (tipText == null)
00176       return new Dimension(0, 0);
00177     textArea = new JTextArea(tipText);
00178     rendererPane.removeAll();
00179     rendererPane.add(textArea);
00180     textArea.setWrapStyleWord(true);
00181     int width = ((JMultiLineToolTip) c).getFixedWidth();
00182     int columns = ((JMultiLineToolTip) c).getColumns();
00183 
00184     if (columns > 0)
00185     {
00186       textArea.setColumns(columns);
00187       textArea.setSize(0, 0);
00188       textArea.setLineWrap(true);
00189       textArea.setSize(textArea.getPreferredSize());
00190     }
00191     else if (width > 0)
00192     {
00193       textArea.setLineWrap(true);
00194       Dimension d = textArea.getPreferredSize();
00195       d.width = width;
00196       d.height++;
00197       textArea.setSize(d);
00198     }
00199     else
00200       textArea.setLineWrap(false);
00201 
00202     Dimension dim = textArea.getPreferredSize();
00203 
00204     dim.height += 1;
00205     dim.width += 1;
00206     return dim;
00207   }
00208 
00209   /**
00210    * @see javax.swing.plaf.ComponentUI#getMinimumSize(javax.swing.JComponent)
00211    */
00212   public Dimension getMinimumSize(JComponent c)
00213   {
00214     return getPreferredSize(c);
00215   }
00216 
00217   /**
00218    * @see javax.swing.plaf.ComponentUI#getMaximumSize(javax.swing.JComponent)
00219    */
00220   public Dimension getMaximumSize(JComponent c)
00221   {
00222     return getPreferredSize(c);
00223   }
00224 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1