2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
\r
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
18 package jalview.appletgui;
\r
20 import java.applet.*;
\r
24 import java.awt.event.*;
\r
26 public class Tooltip extends Canvas implements MouseListener,
\r
29 private String[] tip;
\r
31 private String lastTip = "";
\r
33 private boolean setPosition = false;
\r
35 protected Component owner;
\r
37 private Container mainContainer;
\r
39 private LayoutManager mainLayout;
\r
41 private boolean shown;
\r
43 private final int VERTICAL_OFFSET = 20;
\r
45 private final int HORIZONTAL_ENLARGE = 10;
\r
53 public Tooltip(String tip, Component owner)
\r
56 owner.addMouseListener(this);
\r
57 owner.addMouseMotionListener(this);
\r
58 setBackground(new Color(255, 255, 220));
\r
60 java.net.URL url = getClass().getResource("/images/link.gif");
\r
63 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
\r
67 public void paint(Graphics g)
\r
69 int w = getSize().width;
\r
70 int h = getSize().height;
\r
72 g.drawRect(0, 0, w - 1, h - 1);
\r
74 for (int i = 0; i < tip.length; i++)
\r
77 lindex = tip[i].indexOf("%LINK%");
\r
82 g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight
\r
84 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
\r
86 g.drawImage(linkImage, x, i * fontHeight + 1, this);
\r
87 if (lindex + 6 < tip[i].length())
\r
89 g.drawString(tip[i].substring(lindex + 6), x
\r
90 + linkImage.getWidth(this), (i + 1) * fontHeight - 3);
\r
95 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
\r
100 synchronized void setTip(String tip)
\r
108 if (lastTip.equals(tip))
\r
114 setPosition = true;
\r
116 fm = getFontMetrics(owner.getFont());
\r
117 fontHeight = fm.getHeight();
\r
119 int longestLine = 0;
\r
120 StringTokenizer st = new StringTokenizer(tip, "\n");
\r
121 this.tip = new String[st.countTokens()];
\r
123 while (st.hasMoreElements())
\r
125 this.tip[index] = st.nextToken();
\r
126 if (fm.stringWidth(this.tip[index]) > longestLine)
\r
128 longestLine = fm.stringWidth(this.tip[index]);
\r
133 setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);
\r
139 void setTipLocation(MouseEvent evt)
\r
141 if (mainContainer == null || owner == null)
\r
145 setLocation((owner.getLocationOnScreen().x - mainContainer
\r
146 .getLocationOnScreen().x)
\r
147 + evt.getX(), (owner.getLocationOnScreen().y
\r
148 - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)
\r
151 // correction, whole tool tip must be visible
\r
152 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
\r
154 setLocation(mainContainer.getSize().width - getSize().width,
\r
159 private void removeToolTip()
\r
163 mainContainer.remove(0);
\r
164 mainContainer.setLayout(mainLayout);
\r
165 mainContainer.validate();
\r
170 private void findMainContainer()
\r
172 Container parent = owner.getParent();
\r
175 if ((parent instanceof Applet) || (parent instanceof Frame))
\r
177 mainContainer = parent;
\r
182 parent = parent.getParent();
\r
185 mainLayout = mainContainer.getLayout();
\r
188 public void mouseEntered(MouseEvent me)
\r
190 setTipLocation(me);
\r
193 public void mouseExited(MouseEvent me)
\r
198 public void mousePressed(MouseEvent me)
\r
203 public void mouseReleased(MouseEvent me)
\r
207 public void mouseClicked(MouseEvent me)
\r
211 public void mouseMoved(MouseEvent me)
\r
215 findMainContainer();
\r
216 mainContainer.setLayout(null);
\r
217 mainContainer.add(this, 0);
\r
218 mainContainer.validate();
\r
220 setTipLocation(me);
\r
222 else if (setPosition)
\r
224 setTipLocation(me);
\r
225 setPosition = false;
\r
229 public void mouseDragged(MouseEvent me)
\r