2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
27 import java.awt.event.*;
29 public class Tooltip extends Canvas implements MouseListener,
34 private String lastTip = "";
36 private boolean setPosition = false;
38 protected Component owner;
40 private Container mainContainer;
42 private LayoutManager mainLayout;
44 private boolean shown;
46 private final int VERTICAL_OFFSET = 20;
48 private final int HORIZONTAL_ENLARGE = 10;
56 public Tooltip(String tip, Component owner)
59 owner.addMouseListener(this);
60 owner.addMouseMotionListener(this);
61 setBackground(new Color(255, 255, 220));
63 java.net.URL url = getClass().getResource("/images/link.gif");
66 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
70 public void paint(Graphics g)
72 int w = getSize().width;
73 int h = getSize().height;
75 g.drawRect(0, 0, w - 1, h - 1);
77 for (int i = 0; i < tip.length; i++)
80 lindex = tip[i].indexOf("%LINK%");
85 g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight
87 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
89 g.drawImage(linkImage, x, i * fontHeight + 1, this);
90 if (lindex + 6 < tip[i].length())
92 g.drawString(tip[i].substring(lindex + 6),
93 x + linkImage.getWidth(this), (i + 1) * fontHeight - 3);
98 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
103 synchronized void setTip(String tip)
111 if (lastTip.equals(tip))
119 fm = getFontMetrics(owner.getFont());
120 fontHeight = fm.getHeight();
123 StringTokenizer st = new StringTokenizer(tip, "\n");
124 this.tip = new String[st.countTokens()];
126 while (st.hasMoreElements())
128 this.tip[index] = st.nextToken();
129 if (fm.stringWidth(this.tip[index]) > longestLine)
131 longestLine = fm.stringWidth(this.tip[index]);
136 setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);
142 void setTipLocation(MouseEvent evt)
144 if (mainContainer == null || owner == null)
149 (owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x)
151 (owner.getLocationOnScreen().y
152 - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)
155 // correction, whole tool tip must be visible
156 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
158 setLocation(mainContainer.getSize().width - getSize().width,
163 private void removeToolTip()
167 mainContainer.remove(0);
168 mainContainer.setLayout(mainLayout);
169 mainContainer.validate();
174 private void findMainContainer()
176 Container parent = owner.getParent();
179 if ((parent instanceof Applet) || (parent instanceof Frame))
181 mainContainer = parent;
186 parent = parent.getParent();
189 mainLayout = mainContainer.getLayout();
192 public void mouseEntered(MouseEvent me)
197 public void mouseExited(MouseEvent me)
202 public void mousePressed(MouseEvent me)
207 public void mouseReleased(MouseEvent me)
211 public void mouseClicked(MouseEvent me)
215 public void mouseMoved(MouseEvent me)
220 mainContainer.setLayout(null);
221 mainContainer.add(this, 0);
222 mainContainer.validate();
226 else if (setPosition)
233 public void mouseDragged(MouseEvent me)