2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
\r
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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),
\r
90 x + 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
146 (owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x)
\r
148 (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