2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
20 package jalview.appletgui;
\r
23 import java.applet.*;
\r
24 import java.awt.event.*;
\r
27 public class Tooltip extends Canvas implements MouseListener,
\r
30 private String [] tip;
\r
31 private String lastTip="";
\r
32 private boolean setPosition = false;
\r
33 protected Component owner;
\r
35 private Container mainContainer;
\r
36 private LayoutManager mainLayout;
\r
38 private boolean shown;
\r
40 private final int VERTICAL_OFFSET = 20;
\r
41 private final int HORIZONTAL_ENLARGE = 10;
\r
49 public Tooltip(String tip, Component owner)
\r
52 owner.addMouseListener(this);
\r
53 owner.addMouseMotionListener(this);
\r
54 setBackground(new Color(255, 255, 220));
\r
56 java.net.URL url = getClass().getResource("/images/link.gif");
\r
59 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
\r
63 public void paint(Graphics g)
\r
65 int w = getSize().width;
\r
66 int h = getSize().height;
\r
68 g.drawRect(0, 0, w - 1, h - 1);
\r
70 for (int i = 0; i < tip.length; i++)
\r
73 lindex = tip[i].indexOf("%LINK%");
\r
78 g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight - 3);
\r
79 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
\r
81 g.drawImage(linkImage, x, i * fontHeight + 1, this);
\r
82 if (lindex + 6 < tip[i].length())
\r
84 g.drawString(tip[i].substring(lindex + 6),
\r
85 x + linkImage.getWidth(this),
\r
86 (i + 1) * fontHeight - 3);
\r
91 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
\r
96 synchronized void setTip(String tip)
\r
104 if(lastTip.equals(tip))
\r
108 setPosition = true;
\r
110 fm = getFontMetrics(owner.getFont());
\r
111 fontHeight = fm.getHeight();
\r
113 int longestLine = 0;
\r
114 StringTokenizer st = new StringTokenizer(tip, "\n");
\r
115 this.tip = new String[st.countTokens()];
\r
117 while(st.hasMoreElements())
\r
119 this.tip[index] = st.nextToken();
\r
120 if(fm.stringWidth(this.tip[index])>longestLine)
\r
121 longestLine = fm.stringWidth(this.tip[index]);
\r
125 setSize(longestLine + HORIZONTAL_ENLARGE,
\r
126 fontHeight*this.tip.length);
\r
132 void setTipLocation(MouseEvent evt)
\r
134 setLocation( (owner.getLocationOnScreen().x
\r
135 - mainContainer.getLocationOnScreen().x) + evt.getX(),
\r
136 (owner.getLocationOnScreen().y -
\r
137 mainContainer.getLocationOnScreen().y
\r
138 + VERTICAL_OFFSET) + evt.getY());
\r
140 // correction, whole tool tip must be visible
\r
141 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
\r
143 setLocation(mainContainer.getSize().width - getSize().width,
\r
149 private void removeToolTip() {
\r
151 mainContainer.remove(0);
\r
152 mainContainer.setLayout(mainLayout);
\r
153 mainContainer.validate();
\r
158 private void findMainContainer() {
\r
159 Container parent = owner.getParent();
\r
161 if ((parent instanceof Applet) || (parent instanceof Frame)) {
\r
162 mainContainer = parent;
\r
165 parent = parent.getParent();
\r
168 mainLayout = mainContainer.getLayout();
\r
171 public void mouseEntered(MouseEvent me)
\r
173 setTipLocation(me);
\r
176 public void mouseExited(MouseEvent me)
\r
181 public void mousePressed(MouseEvent me)
\r
186 public void mouseReleased(MouseEvent me)
\r
189 public void mouseClicked(MouseEvent me)
\r
192 public void mouseMoved(MouseEvent me)
\r
196 findMainContainer();
\r
197 mainContainer.setLayout(null);
\r
198 mainContainer.add(this, 0);
\r
199 mainContainer.validate();
\r
201 setTipLocation(me);
\r
203 else if (setPosition)
\r
205 setTipLocation(me);
\r
206 setPosition = false;
\r
210 public void mouseDragged(MouseEvent me)
\r