VisClient/org/hfbk/util/Tooltip.java

Go to the documentation of this file.
00001 package org.hfbk.util;
00002 
00003 import java.awt.Component;
00004 import java.awt.Container;
00005 import java.awt.DisplayMode;
00006 import java.awt.FontMetrics;
00007 import java.awt.Frame;
00008 import java.awt.Graphics;
00009 import java.awt.MouseInfo;
00010 import java.awt.Point;
00011 import java.awt.Window;
00012 import java.awt.event.MouseAdapter;
00013 import java.awt.event.MouseEvent;
00014 
00015 public class Tooltip extends Window {
00016 
00017         String tip;
00018 
00019         Component owner;
00020 
00021         Container mainContainer;
00022 
00023         final int HORIZONTAL_ENLARGE = 10;
00024 
00025         int linecount, lineheight = 0;
00026 
00027         public Tooltip(String tip, Component owner) {
00028                 super(new Frame());
00029         //      super(findTop(owner));
00030                 // setUndecorated(true);
00031                 this.tip = tip;
00032                 this.owner = owner;
00033                 this.linecount = tip.split("\\n").length;
00034 
00035                 owner.addMouseListener(new MAdapter());
00036                 UIUtils.blackify(this); // apply the classic vis look!
00037         }
00038 
00039         public void paint(Graphics g) {
00040                 g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
00041                 String[] lines = tip.split("\\n");
00042                 for (int i = 0; i < lines.length; i++)
00043                         g.drawString(lines[i], 3, lineheight * (i + 1) - 3);
00044         }
00045 
00046         private void addToolTip(int x, int y) {
00047                 FontMetrics fm = getFontMetrics(owner.getFont());
00048                 lineheight = fm.getHeight();
00049                 int width=0;
00050                 for (String line : tip.split("\\n")){
00051                         width=Math.max(width, fm.stringWidth(line));
00052                 }
00053                 setSize(width + HORIZONTAL_ENLARGE, lineheight
00054                                 * linecount);
00055                 setLocation(x, y);
00056                 setVisible(true);
00057         }
00058 
00059         static Window findTop(Component c){             
00060                 while (c!=null && !(c instanceof Window)) c=c.getParent();
00061                 return (Window)c;
00062         }
00063         /*
00064          * private void addToolTip() { mainContainer.setLayout(null);
00065          * 
00066          * FontMetrics fm = getFontMetrics(owner.getFont());
00067          * lineheight=fm.getHeight(); setSize(fm.stringWidth(tip) +
00068          * HORIZONTAL_ENLARGE, lineheight*linecount);
00069          * 
00070          * setLocation((owner.getLocationOnScreen().x -
00071          * mainContainer.getLocationOnScreen().x) , (owner.getLocationOnScreen().y -
00072          * mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET));
00073          * 
00074          * 
00075          * if (mainContainer.getSize().width < ( getLocation().x + getSize().width )) {
00076          * setLocation(mainContainer.getSize().width - getSize().width,
00077          * getLocation().y); } mainContainer.add(this, 0); mainContainer.validate();
00078          * repaint(); shown = true; } /* private void removeToolTip() { if (shown) {
00079          * mainContainer.remove(0); mainContainer.setLayout(mainLayout);
00080          * mainContainer.validate(); } shown = false; }
00081          * 
00082          * 
00083          * private void findMainContainer() { Container parent = owner.getParent();
00084          * while (true) { if ((parent instanceof Applet) || (parent instanceof
00085          * Frame)) { mainContainer = parent; break; } else { parent =
00086          * parent.getParent(); } } mainLayout = mainContainer.getLayout(); }
00087          */
00088         
00089         class MAdapter extends MouseAdapter {
00090                 public void mouseEntered(MouseEvent me) {
00091                         
00092                         DisplayMode dp=owner.getGraphicsConfiguration().getDevice().getDisplayMode();
00093                         Point mouse=MouseInfo.getPointerInfo().getLocation();
00094                         int x=mouse.x + 1;
00095                         int y=mouse.y + 16;
00096                         
00097                         if (x+ getWidth()>dp.getWidth ()) x=dp.getWidth ()-getWidth();
00098                         //if (y+getHeight()>dp.getHeight()) y=dp.getHeight()-getHeight();
00099                         if (y+getHeight()>dp.getHeight()) y-=32+getHeight();
00100                         
00101                         addToolTip(x,y);
00102                 }
00103 
00104                 public void mouseExited(MouseEvent me) {
00105                         setVisible(false);
00106                 }
00107 
00108                 public void mousePressed(MouseEvent me) {
00109                         setVisible(false);
00110                 }
00111         }
00112 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1