2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
\r
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.appletgui;
\r
21 import java.applet.*;
\r
25 import java.awt.event.*;
\r
27 public class Tooltip extends Canvas implements MouseListener,
\r
30 private String[] tip;
\r
32 private String lastTip = "";
\r
34 private boolean setPosition = false;
\r
36 protected Component owner;
\r
38 private Container mainContainer;
\r
40 private LayoutManager mainLayout;
\r
42 private boolean shown;
\r
44 private final int VERTICAL_OFFSET = 20;
\r
46 private final int HORIZONTAL_ENLARGE = 10;
\r
54 public Tooltip(String tip, Component owner)
\r
57 owner.addMouseListener(this);
\r
58 owner.addMouseMotionListener(this);
\r
59 setBackground(new Color(255, 255, 220));
\r
61 java.net.URL url = getClass().getResource("/images/link.gif");
\r
64 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
\r
68 public void paint(Graphics g)
\r
70 int w = getSize().width;
\r
71 int h = getSize().height;
\r
73 g.drawRect(0, 0, w - 1, h - 1);
\r
75 for (int i = 0; i < tip.length; i++)
\r
78 lindex = tip[i].indexOf("%LINK%");
\r
83 g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight
\r
85 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
\r
87 g.drawImage(linkImage, x, i * fontHeight + 1, this);
\r
88 if (lindex + 6 < tip[i].length())
\r
90 g.drawString(tip[i].substring(lindex + 6), x
\r
91 + linkImage.getWidth(this), (i + 1) * fontHeight - 3);
\r
96 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
\r
101 synchronized void setTip(String tip)
\r
109 if (lastTip.equals(tip))
\r
115 setPosition = true;
\r
117 fm = getFontMetrics(owner.getFont());
\r
118 fontHeight = fm.getHeight();
\r
120 int longestLine = 0;
\r
121 StringTokenizer st = new StringTokenizer(tip, "\n");
\r
122 this.tip = new String[st.countTokens()];
\r
124 while (st.hasMoreElements())
\r
126 this.tip[index] = st.nextToken();
\r
127 if (fm.stringWidth(this.tip[index]) > longestLine)
\r
129 longestLine = fm.stringWidth(this.tip[index]);
\r
134 setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);
\r
140 void setTipLocation(MouseEvent evt)
\r
142 if (mainContainer == null || owner == null)
\r
146 setLocation((owner.getLocationOnScreen().x - mainContainer
\r
147 .getLocationOnScreen().x)
\r
148 + evt.getX(), (owner.getLocationOnScreen().y
\r
149 - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)
\r
152 // correction, whole tool tip must be visible
\r
153 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
\r
155 setLocation(mainContainer.getSize().width - getSize().width,
\r
160 private void removeToolTip()
\r
164 mainContainer.remove(0);
\r
165 mainContainer.setLayout(mainLayout);
\r
166 mainContainer.validate();
\r
171 private void findMainContainer()
\r
173 Container parent = owner.getParent();
\r
176 if ((parent instanceof Applet) || (parent instanceof Frame))
\r
178 mainContainer = parent;
\r
183 parent = parent.getParent();
\r
186 mainLayout = mainContainer.getLayout();
\r
189 public void mouseEntered(MouseEvent me)
\r
191 setTipLocation(me);
\r
194 public void mouseExited(MouseEvent me)
\r
199 public void mousePressed(MouseEvent me)
\r
204 public void mouseReleased(MouseEvent me)
\r
208 public void mouseClicked(MouseEvent me)
\r
212 public void mouseMoved(MouseEvent me)
\r
216 findMainContainer();
\r
217 mainContainer.setLayout(null);
\r
218 mainContainer.add(this, 0);
\r
219 mainContainer.validate();
\r
221 setTipLocation(me);
\r
223 else if (setPosition)
\r
225 setTipLocation(me);
\r
226 setPosition = false;
\r
230 public void mouseDragged(MouseEvent me)
\r