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 javax.swing.JApplet;
24 import java.awt.Color;
25 import java.awt.Component;
26 import java.awt.Container;
27 import java.awt.FontMetrics;
28 import java.awt.Graphics;
29 import java.awt.Image;
30 import java.awt.LayoutManager;
31 import java.awt.event.MouseEvent;
32 import java.awt.event.MouseListener;
33 import java.awt.event.MouseMotionListener;
34 import java.util.StringTokenizer;
36 import javax.swing.JPanel;
37 import javax.swing.JFrame;
39 public class Tooltip extends JPanel implements MouseListener,
44 private String lastTip = "";
46 private boolean setPosition = false;
48 protected Component owner;
50 private Container mainContainer;
52 private LayoutManager mainLayout;
54 private boolean shown;
56 private final int VERTICAL_OFFSET = 20;
58 private final int HORIZONTAL_ENLARGE = 10;
66 public Tooltip(String tip, Component owner)
69 owner.addMouseListener(this);
70 owner.addMouseMotionListener(this);
71 setBackground(new Color(255, 255, 220));
73 java.net.URL url = getClass().getResource("/images/link.gif");
76 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
80 public void paint(Graphics g)
82 int w = getSize().width;
83 int h = getSize().height;
85 g.drawRect(0, 0, w - 1, h - 1);
87 for (int i = 0; i < tip.length; i++)
90 lindex = tip[i].indexOf("%LINK%");
95 g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight
97 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
99 g.drawImage(linkImage, x, i * fontHeight + 1, this);
100 if (lindex + 6 < tip[i].length())
102 g.drawString(tip[i].substring(lindex + 6),
103 x + linkImage.getWidth(this), (i + 1) * fontHeight - 3);
108 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
113 synchronized void setTip(String tip)
121 if (lastTip.equals(tip))
129 fm = getFontMetrics(owner.getFont());
130 fontHeight = fm.getHeight();
133 StringTokenizer st = new StringTokenizer(tip, "\n");
134 this.tip = new String[st.countTokens()];
136 while (st.hasMoreElements())
138 this.tip[index] = st.nextToken();
139 if (fm.stringWidth(this.tip[index]) > longestLine)
141 longestLine = fm.stringWidth(this.tip[index]);
146 setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);
152 void setTipLocation(MouseEvent evt)
154 if (mainContainer == null || owner == null)
159 (owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x)
161 (owner.getLocationOnScreen().y
162 - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)
165 // correction, whole tool tip must be visible
166 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
168 setLocation(mainContainer.getSize().width - getSize().width,
173 private void removeToolTip()
177 mainContainer.remove(0);
178 mainContainer.setLayout(mainLayout);
179 mainContainer.validate();
184 private void findMainContainer()
186 Container parent = owner.getParent();
189 if ((parent instanceof JApplet) || (parent instanceof JFrame))
191 mainContainer = parent;
196 parent = parent.getParent();
199 mainLayout = mainContainer.getLayout();
202 public void mouseEntered(MouseEvent me)
207 public void mouseExited(MouseEvent me)
212 public void mousePressed(MouseEvent me)
217 public void mouseReleased(MouseEvent me)
221 public void mouseClicked(MouseEvent me)
225 public void mouseMoved(MouseEvent me)
230 mainContainer.setLayout(null);
231 mainContainer.add(this, 0);
232 mainContainer.validate();
236 else if (setPosition)
243 public void mouseDragged(MouseEvent me)