2 package jalview.appletgui;
\r
5 import java.applet.*;
\r
6 import java.awt.event.*;
\r
9 public class Tooltip extends Canvas implements MouseListener,
\r
13 private String [] tip;
\r
14 protected Component owner;
\r
16 private Container mainContainer;
\r
17 private LayoutManager mainLayout;
\r
19 private boolean shown;
\r
21 private final int VERTICAL_OFFSET = 20;
\r
22 private final int HORIZONTAL_ENLARGE = 10;
\r
32 public Tooltip(String tip, Component owner)
\r
35 owner.addMouseListener(this);
\r
36 owner.addMouseMotionListener(this);
\r
37 setBackground(new Color(255, 255, 220));
\r
39 java.net.URL url = getClass().getResource("/images/link.gif");
\r
42 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
\r
47 public void paint(Graphics g)
\r
49 g.drawRect(0,0,getSize().width -1, getSize().height -1);
\r
51 for(int i=0; i<tip.length; i++)
\r
54 lindex = tip[i].indexOf("%LINK%");
\r
59 g.drawString(tip[i].substring(0, lindex), 3, (i+1)*fontHeight-3);
\r
60 x+=fm.stringWidth(tip[i].substring(0, lindex)+3);
\r
62 g.drawImage(linkImage, x, i * fontHeight+1, this);
\r
65 g.drawString(tip[i], 3, (i+1)*fontHeight - 3);
\r
69 private void addToolTip()
\r
71 mainContainer.setLayout(null);
\r
72 mainContainer.add(this, 0);
\r
73 mainContainer.validate();
\r
78 void setTip(String tip)
\r
80 fm = getFontMetrics(owner.getFont());
\r
81 fontHeight = fm.getHeight();
\r
83 int longestLine = 0;
\r
84 StringTokenizer st = new StringTokenizer(tip, "\n");
\r
85 this.tip = new String[st.countTokens()];
\r
87 while(st.hasMoreElements())
\r
89 this.tip[index] = st.nextToken();
\r
90 if(fm.stringWidth(this.tip[index])>longestLine)
\r
91 longestLine = fm.stringWidth(this.tip[index]);
\r
95 setSize(longestLine + HORIZONTAL_ENLARGE,
\r
96 fontHeight*this.tip.length);
\r
99 void setTipLocation(int x, int y)
\r
102 setLocation((owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x) +x,
\r
103 (owner.getLocationOnScreen().y - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)+y);
\r
107 // correction, whole tool tip must be visible
\r
108 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
\r
110 setLocation(mainContainer.getSize().width - getSize().width,
\r
117 private void removeToolTip() {
\r
119 mainContainer.remove(0);
\r
120 mainContainer.setLayout(mainLayout);
\r
121 mainContainer.validate();
\r
126 private void findMainContainer() {
\r
127 Container parent = owner.getParent();
\r
129 if ((parent instanceof Applet) || (parent instanceof Frame)) {
\r
130 mainContainer = parent;
\r
133 parent = parent.getParent();
\r
136 mainLayout = mainContainer.getLayout();
\r
139 public void mouseEntered(MouseEvent me)
\r
142 public void mouseExited(MouseEvent me)
\r
147 public void mousePressed(MouseEvent me)
\r
152 public void mouseReleased(MouseEvent me)
\r
155 public void mouseClicked(MouseEvent me)
\r
158 public void mouseMoved(MouseEvent me)
\r
161 setTipLocation(me.getX(), me.getY());
\r
164 findMainContainer();
\r
166 setTipLocation(me.getX(), me.getY());
\r
170 public void mouseDragged(MouseEvent me)
\r