2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
\r
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
\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
\r
10 * of the License, or (at your option) any later version.
\r
12 * Jalview is distributed in the hope that it will be useful, but
\r
13 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
15 * PURPOSE. See the GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
\r
21 package jalview.appletgui;
\r
23 import javax.swing.JApplet;
\r
24 import java.awt.Color;
\r
25 import java.awt.Component;
\r
26 import java.awt.Container;
\r
27 import java.awt.FontMetrics;
\r
28 import java.awt.Graphics;
\r
29 import java.awt.Image;
\r
30 import java.awt.LayoutManager;
\r
31 import java.awt.event.MouseEvent;
\r
32 import java.awt.event.MouseListener;
\r
33 import java.awt.event.MouseMotionListener;
\r
34 import java.util.StringTokenizer;
\r
36 import javax.swing.JPanel;
\r
37 import javax.swing.JFrame;
\r
39 public class Tooltip extends JPanel implements MouseListener,
\r
42 private String[] tip;
\r
44 private String lastTip = "";
\r
46 private boolean setPosition = false;
\r
48 protected Component owner;
\r
50 private Container mainContainer;
\r
52 private LayoutManager mainLayout;
\r
54 private boolean shown;
\r
56 private final int VERTICAL_OFFSET = 20;
\r
58 private final int HORIZONTAL_ENLARGE = 10;
\r
66 public Tooltip(String tip, Component owner)
\r
69 owner.addMouseListener(this);
\r
70 owner.addMouseMotionListener(this);
\r
71 setBackground(new Color(255, 255, 220));
\r
73 java.net.URL url = getClass().getResource("/images/link.gif");
\r
76 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
\r
80 public void paint(Graphics g)
\r
82 int w = getSize().width;
\r
83 int h = getSize().height;
\r
85 g.drawRect(0, 0, w - 1, h - 1);
\r
87 for (int i = 0; i < tip.length; i++)
\r
90 lindex = tip[i].indexOf("%LINK%");
\r
95 g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight
\r
97 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
\r
99 g.drawImage(linkImage, x, i * fontHeight + 1, this);
\r
100 if (lindex + 6 < tip[i].length())
\r
102 g.drawString(tip[i].substring(lindex + 6),
\r
103 x + linkImage.getWidth(this), (i + 1) * fontHeight - 3);
\r
108 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
\r
113 synchronized void setTip(String tip)
\r
121 if (lastTip.equals(tip))
\r
127 setPosition = true;
\r
129 fm = getFontMetrics(owner.getFont());
\r
130 fontHeight = fm.getHeight();
\r
132 int longestLine = 0;
\r
133 StringTokenizer st = new StringTokenizer(tip, "\n");
\r
134 this.tip = new String[st.countTokens()];
\r
136 while (st.hasMoreElements())
\r
138 this.tip[index] = st.nextToken();
\r
139 if (fm.stringWidth(this.tip[index]) > longestLine)
\r
141 longestLine = fm.stringWidth(this.tip[index]);
\r
146 setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);
\r
152 void setTipLocation(MouseEvent evt)
\r
154 if (mainContainer == null || owner == null)
\r
159 (owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x)
\r
161 (owner.getLocationOnScreen().y
\r
162 - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)
\r
165 // correction, whole tool tip must be visible
\r
166 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
\r
168 setLocation(mainContainer.getSize().width - getSize().width,
\r
173 private void removeToolTip()
\r
177 mainContainer.remove(0);
\r
178 mainContainer.setLayout(mainLayout);
\r
179 mainContainer.validate();
\r
184 private void findMainContainer()
\r
186 Container parent = owner.getParent();
\r
189 if ((parent instanceof JApplet) || (parent instanceof JFrame))
\r
191 mainContainer = parent;
\r
196 parent = parent.getParent();
\r
199 mainLayout = mainContainer.getLayout();
\r
202 public void mouseEntered(MouseEvent me)
\r
204 setTipLocation(me);
\r
207 public void mouseExited(MouseEvent me)
\r
212 public void mousePressed(MouseEvent me)
\r
217 public void mouseReleased(MouseEvent me)
\r
221 public void mouseClicked(MouseEvent me)
\r
225 public void mouseMoved(MouseEvent me)
\r
229 findMainContainer();
\r
230 mainContainer.setLayout(null);
\r
231 mainContainer.add(this, 0);
\r
232 mainContainer.validate();
\r
234 setTipLocation(me);
\r
236 else if (setPosition)
\r
238 setTipLocation(me);
\r
239 setPosition = false;
\r
243 public void mouseDragged(MouseEvent me)
\r