Subversion Repositories wimsdev

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     WIMSchem Elements: Chemistry molecular diagram drawing tool.
  3.    
  4.     (c) 2005 Dr. Alex M. Clark
  5.    
  6.     Released as GNUware, under the Gnu Public License (GPL)
  7.    
  8.     See www.gnu.org for details.
  9. */
  10.  
  11. package WIMSchem;
  12.  
  13. import javax.swing.*;
  14. import javax.swing.plaf.*;
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import java.awt.font.*;
  18. import java.io.*;
  19. import java.util.*;
  20.  
  21. // Subclassed version of the ToolButton which supports tool-tips with multiple lines.
  22.  
  23. public class ToolButton extends JToggleButton
  24. {
  25.     public ToolButton(Icon icon) {super(icon);}
  26.  
  27.     public JToolTip createToolTip()
  28.     {
  29.         MultiLineToolTip tip=new MultiLineToolTip();
  30.         startup();
  31.         tip.setComponent(this);
  32.         return tip;
  33.     }
  34.     // jm.evers 3/2010 adjusting tooltip timeouts
  35.     public void startup(){
  36.         ToolTipManager.sharedInstance().setInitialDelay(0);
  37.         ToolTipManager.sharedInstance().setDismissDelay(100000);
  38.         ToolTipManager.sharedInstance().setReshowDelay(0);
  39.     }
  40. }
  41.  
  42. class MultiLineToolTip extends JToolTip
  43. {
  44.     public MultiLineToolTip()
  45.     {
  46.         setUI(new MultiLineToolTipUI());
  47.     }
  48. }
  49.  
  50. class MultiLineToolTipUI extends ToolTipUI
  51. {
  52.     String[] strs;
  53.     int maxWidth=0;
  54.  
  55.     public void setDismissDelay(){
  56.    
  57.     }
  58.    
  59.     public void paint(Graphics g,JComponent c)
  60.     {
  61.         Font font=g.getFont();
  62.         FontMetrics metrics=g.getFontMetrics();
  63.         FontRenderContext frc=new FontRenderContext(null,false,false);
  64.         Dimension size=c.getSize();
  65.         g.setColor(c.getBackground());
  66.         g.fillRect(0,0,size.width,size.height);
  67.         g.setColor(c.getForeground());
  68.         g.drawRect(0,0,size.width-1,size.height-1);
  69.         if (strs!=null)
  70.         {
  71.             int y=0;
  72.             for (int i=0;i<strs.length;i++)
  73.             {
  74.                 y+=(int)font.getLineMetrics(strs[i],frc).getHeight()+2;
  75.                 g.drawString(strs[i],3,y);
  76.                 //g.drawString(strs[i],3,(metrics.getHeight())*(i+1));
  77.             }
  78.         }
  79.     }
  80.  
  81.     public Dimension getPreferredSize(JComponent c)
  82.     {
  83.         Font font=c.getFont();
  84.         FontRenderContext frc=new FontRenderContext(null,false,false);
  85.         String tipText=((JToolTip) c).getTipText();
  86.         if (tipText==null) tipText="";
  87.         while (tipText.endsWith("\n")) {tipText=tipText.substring(0,tipText.length()-1);}
  88.         BufferedReader br=new BufferedReader(new StringReader(tipText));
  89.         String line;
  90.         int maxWidth=0,totalHeight=0;
  91.         Vector<String> v=new Vector<String>();
  92.         try
  93.         {
  94.             while ((line=br.readLine())!=null)
  95.             {
  96.                 int width=(int)font.getStringBounds(line,frc).getWidth();
  97.                 maxWidth=(maxWidth<width) ? width : maxWidth;
  98.                 v.addElement(line);
  99.                 totalHeight+=(int)font.getLineMetrics(line,frc).getHeight()+2;
  100.             }
  101.         }
  102.         catch (IOException ex)
  103.         {
  104.             ex.printStackTrace();
  105.         }
  106.         int lines=v.size();
  107.         if (lines<1)
  108.         {
  109.             strs=null;
  110.             lines=1;
  111.         }
  112.         else
  113.         {
  114.             strs=new String[lines];
  115.             int i=0;
  116.             for (Enumeration e=v.elements();e.hasMoreElements();i++) strs[i]=(String)e.nextElement();
  117.         }
  118.         this.maxWidth=maxWidth;
  119.         return new Dimension(maxWidth+6,totalHeight+4);
  120.     }
  121. }
  122.