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
00030
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);
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
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
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
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 }