2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
25 import java.awt.event.*;
27 public class Tooltip extends Canvas implements MouseListener,
32 private String lastTip = "";
34 private boolean setPosition = false;
36 protected Component owner;
38 private Container mainContainer;
40 private LayoutManager mainLayout;
42 private boolean shown;
44 private final int VERTICAL_OFFSET = 20;
46 private final int HORIZONTAL_ENLARGE = 10;
54 public Tooltip(String tip, Component owner)
57 owner.addMouseListener(this);
58 owner.addMouseMotionListener(this);
59 setBackground(new Color(255, 255, 220));
61 java.net.URL url = getClass().getResource("/images/link.gif");
64 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
68 public void paint(Graphics g)
70 int w = getSize().width;
71 int h = getSize().height;
73 g.drawRect(0, 0, w - 1, h - 1);
75 for (int i = 0; i < tip.length; i++)
78 lindex = tip[i].indexOf("%LINK%");
83 g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight
85 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
87 g.drawImage(linkImage, x, i * fontHeight + 1, this);
88 if (lindex + 6 < tip[i].length())
90 g.drawString(tip[i].substring(lindex + 6),
91 x + linkImage.getWidth(this), (i + 1) * fontHeight - 3);
96 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
101 synchronized void setTip(String tip)
109 if (lastTip.equals(tip))
117 fm = getFontMetrics(owner.getFont());
118 fontHeight = fm.getHeight();
121 StringTokenizer st = new StringTokenizer(tip, "\n");
122 this.tip = new String[st.countTokens()];
124 while (st.hasMoreElements())
126 this.tip[index] = st.nextToken();
127 if (fm.stringWidth(this.tip[index]) > longestLine)
129 longestLine = fm.stringWidth(this.tip[index]);
134 setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);
140 void setTipLocation(MouseEvent evt)
142 if (mainContainer == null || owner == null)
147 (owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x)
149 (owner.getLocationOnScreen().y
150 - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)
153 // correction, whole tool tip must be visible
154 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
156 setLocation(mainContainer.getSize().width - getSize().width,
161 private void removeToolTip()
165 mainContainer.remove(0);
166 mainContainer.setLayout(mainLayout);
167 mainContainer.validate();
172 private void findMainContainer()
174 Container parent = owner.getParent();
177 if ((parent instanceof Applet) || (parent instanceof Frame))
179 mainContainer = parent;
184 parent = parent.getParent();
187 mainLayout = mainContainer.getLayout();
190 public void mouseEntered(MouseEvent me)
195 public void mouseExited(MouseEvent me)
200 public void mousePressed(MouseEvent me)
205 public void mouseReleased(MouseEvent me)
209 public void mouseClicked(MouseEvent me)
213 public void mouseMoved(MouseEvent me)
218 mainContainer.setLayout(null);
219 mainContainer.add(this, 0);
220 mainContainer.validate();
224 else if (setPosition)
231 public void mouseDragged(MouseEvent me)