Back to index

wims  3.65+svn20090927
ToolButton.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2005 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package WIMSchem;
00012 
00013 import javax.swing.*;
00014 import javax.swing.plaf.*;
00015 import java.awt.*;
00016 import java.awt.event.*;
00017 import java.awt.font.*;
00018 import java.io.*;
00019 import java.util.*;
00020 
00021 // Subclassed version of the ToolButton which supports tool-tips with multiple lines.
00022 
00023 public class ToolButton extends JToggleButton
00024 {
00025     public ToolButton(Icon icon) {super(icon);}
00026 
00027     public JToolTip createToolTip() 
00028     {
00029         MultiLineToolTip tip=new MultiLineToolTip();
00030         tip.setComponent(this);
00031         return tip;
00032     }
00033 }
00034 
00035 class MultiLineToolTip extends JToolTip 
00036 {
00037     public MultiLineToolTip() 
00038     {
00039        setUI(new MultiLineToolTipUI());
00040     }
00041 }
00042 
00043 class MultiLineToolTipUI extends ToolTipUI 
00044 {
00045     String[] strs;
00046     int maxWidth=0;
00047     
00048     public void paint(Graphics g,JComponent c) 
00049     {
00050        Font font=g.getFont();
00051        FontMetrics metrics=g.getFontMetrics();
00052        FontRenderContext frc=new FontRenderContext(null,false,false);
00053        Dimension size=c.getSize();
00054        g.setColor(c.getBackground());
00055        g.fillRect(0,0,size.width,size.height);
00056        g.setColor(c.getForeground());
00057        g.drawRect(0,0,size.width-1,size.height-1);
00058        if (strs!=null) 
00059        {
00060            int y=0;
00061            for (int i=0;i<strs.length;i++) 
00062            {
00063               y+=(int)font.getLineMetrics(strs[i],frc).getHeight()+2;
00064               g.drawString(strs[i],3,y);
00065               //g.drawString(strs[i],3,(metrics.getHeight())*(i+1));
00066            }
00067        }
00068     }
00069 
00070     public Dimension getPreferredSize(JComponent c) 
00071     {
00072        Font font=c.getFont();
00073        FontRenderContext frc=new FontRenderContext(null,false,false);
00074        String tipText=((JToolTip) c).getTipText();
00075        if (tipText==null) tipText="";
00076        while (tipText.endsWith("\n")) {tipText=tipText.substring(0,tipText.length()-1);}
00077        BufferedReader br=new BufferedReader(new StringReader(tipText));
00078        String line;
00079        int maxWidth=0,totalHeight=0;
00080        Vector<String> v=new Vector<String>();
00081        try 
00082        {
00083            while ((line=br.readLine())!=null) 
00084            {
00085               int width=(int)font.getStringBounds(line,frc).getWidth();
00086               maxWidth=(maxWidth<width) ? width : maxWidth;
00087               v.addElement(line);
00088               totalHeight+=(int)font.getLineMetrics(line,frc).getHeight()+2;
00089            }
00090        } 
00091        catch (IOException ex) 
00092        {
00093            ex.printStackTrace();
00094        }
00095        int lines=v.size();
00096        if (lines<1) 
00097        {
00098            strs=null; 
00099            lines=1;
00100        }
00101        else 
00102        {
00103            strs=new String[lines];
00104            int i=0;
00105            for (Enumeration e=v.elements();e.hasMoreElements();i++) strs[i]=(String)e.nextElement();
00106        }
00107        this.maxWidth=maxWidth;
00108        return new Dimension(maxWidth+6,totalHeight+4);
00109     }
00110 }