2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import java.applet.Applet;
24 import java.awt.Canvas;
25 import java.awt.Color;
26 import java.awt.Component;
27 import java.awt.Container;
28 import java.awt.FontMetrics;
29 import java.awt.Frame;
30 import java.awt.Graphics;
31 import java.awt.Image;
32 import java.awt.LayoutManager;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.MouseListener;
35 import java.awt.event.MouseMotionListener;
36 import java.util.StringTokenizer;
38 public class Tooltip extends Canvas
39 implements MouseListener, MouseMotionListener
43 private String lastTip = "";
45 private boolean setPosition = false;
47 protected Component owner;
49 private Container mainContainer;
51 private LayoutManager mainLayout;
53 private boolean shown;
55 private final int VERTICAL_OFFSET = 20;
57 private final int HORIZONTAL_ENLARGE = 10;
65 public Tooltip(String tip, Component owner)
68 owner.addMouseListener(this);
69 owner.addMouseMotionListener(this);
70 setBackground(new Color(255, 255, 220));
72 java.net.URL url = getClass().getResource("/images/link.gif");
75 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
79 public void paint(Graphics g)
81 int w = getSize().width;
82 int h = getSize().height;
84 g.drawRect(0, 0, w - 1, h - 1);
86 for (int i = 0; i < tip.length; i++)
89 lindex = tip[i].indexOf("%LINK%");
94 g.drawString(tip[i].substring(0, lindex), 3,
95 (i + 1) * fontHeight - 3);
96 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
98 g.drawImage(linkImage, x, i * fontHeight + 1, this);
99 if (lindex + 6 < tip[i].length())
101 g.drawString(tip[i].substring(lindex + 6),
102 x + linkImage.getWidth(this), (i + 1) * fontHeight - 3);
107 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
112 synchronized void setTip(String tip)
120 if (lastTip.equals(tip))
128 fm = getFontMetrics(owner.getFont());
129 fontHeight = fm.getHeight();
132 StringTokenizer st = new StringTokenizer(tip, "\n");
133 this.tip = new String[st.countTokens()];
135 while (st.hasMoreElements())
137 this.tip[index] = st.nextToken();
138 if (fm.stringWidth(this.tip[index]) > longestLine)
140 longestLine = fm.stringWidth(this.tip[index]);
145 setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);
151 void setTipLocation(MouseEvent evt)
153 if (mainContainer == null || owner == null)
158 (owner.getLocationOnScreen().x
159 - mainContainer.getLocationOnScreen().x) + evt.getX(),
160 (owner.getLocationOnScreen().y
161 - mainContainer.getLocationOnScreen().y
162 + VERTICAL_OFFSET) + evt.getY());
164 // correction, whole tool tip must be visible
165 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
167 setLocation(mainContainer.getSize().width - getSize().width,
172 private void removeToolTip()
176 mainContainer.remove(0);
177 mainContainer.setLayout(mainLayout);
178 mainContainer.validate();
183 private void findMainContainer()
185 Container parent = owner.getParent();
188 if ((parent instanceof Applet) || (parent instanceof Frame))
190 mainContainer = parent;
195 parent = parent.getParent();
198 mainLayout = mainContainer.getLayout();
201 public void mouseEntered(MouseEvent me)
206 public void mouseExited(MouseEvent me)
211 public void mousePressed(MouseEvent me)
216 public void mouseReleased(MouseEvent me)
220 public void mouseClicked(MouseEvent me)
224 public void mouseMoved(MouseEvent me)
229 mainContainer.setLayout(null);
230 mainContainer.add(this, 0);
231 mainContainer.validate();
235 else if (setPosition)
242 public void mouseDragged(MouseEvent me)