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