2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.appletgui;
\r
23 import java.applet.*;
\r
24 import java.awt.event.*;
\r
27 public class Tooltip extends Canvas implements MouseListener,
\r
31 private String [] tip;
\r
32 protected Component owner;
\r
34 private Container mainContainer;
\r
35 private LayoutManager mainLayout;
\r
37 private boolean shown;
\r
39 private final int VERTICAL_OFFSET = 20;
\r
40 private final int HORIZONTAL_ENLARGE = 10;
\r
50 public Tooltip(String tip, Component owner)
\r
53 owner.addMouseListener(this);
\r
54 owner.addMouseMotionListener(this);
\r
55 setBackground(new Color(255, 255, 220));
\r
57 java.net.URL url = getClass().getResource("/images/link.gif");
\r
60 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
\r
65 public void paint(Graphics g)
\r
67 g.drawRect(0,0,getSize().width -1, getSize().height -1);
\r
69 for(int i=0; i<tip.length; i++)
\r
72 lindex = tip[i].indexOf("%LINK%");
\r
77 g.drawString(tip[i].substring(0, lindex), 3, (i+1)*fontHeight-3);
\r
78 x+=fm.stringWidth(tip[i].substring(0, lindex)+3);
\r
80 g.drawImage(linkImage, x, i * fontHeight+1, this);
\r
81 if(lindex+6<tip[i].length())
\r
83 g.drawString(tip[i].substring(lindex+6),
\r
84 x + linkImage.getWidth(this),
\r
85 (i+1)*fontHeight-3);
\r
90 g.drawString(tip[i], 3, (i+1)*fontHeight - 3);
\r
94 private void addToolTip()
\r
96 mainContainer.setLayout(null);
\r
97 mainContainer.add(this, 0);
\r
98 mainContainer.validate();
\r
103 void setTip(String tip)
\r
105 fm = getFontMetrics(owner.getFont());
\r
106 fontHeight = fm.getHeight();
\r
108 int longestLine = 0;
\r
109 StringTokenizer st = new StringTokenizer(tip, "\n");
\r
110 this.tip = new String[st.countTokens()];
\r
112 while(st.hasMoreElements())
\r
114 this.tip[index] = st.nextToken();
\r
115 if(fm.stringWidth(this.tip[index])>longestLine)
\r
116 longestLine = fm.stringWidth(this.tip[index]);
\r
120 setSize(longestLine + HORIZONTAL_ENLARGE,
\r
121 fontHeight*this.tip.length);
\r
124 void setTipLocation(int x, int y)
\r
127 setLocation((owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x) +x,
\r
128 (owner.getLocationOnScreen().y - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)+y);
\r
132 // correction, whole tool tip must be visible
\r
133 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
\r
135 setLocation(mainContainer.getSize().width - getSize().width,
\r
142 private void removeToolTip() {
\r
144 mainContainer.remove(0);
\r
145 mainContainer.setLayout(mainLayout);
\r
146 mainContainer.validate();
\r
151 private void findMainContainer() {
\r
152 Container parent = owner.getParent();
\r
154 if ((parent instanceof Applet) || (parent instanceof Frame)) {
\r
155 mainContainer = parent;
\r
158 parent = parent.getParent();
\r
161 mainLayout = mainContainer.getLayout();
\r
164 public void mouseEntered(MouseEvent me)
\r
167 public void mouseExited(MouseEvent me)
\r
172 public void mousePressed(MouseEvent me)
\r
177 public void mouseReleased(MouseEvent me)
\r
180 public void mouseClicked(MouseEvent me)
\r
183 public void mouseMoved(MouseEvent me)
\r
186 setTipLocation(me.getX(), me.getY());
\r
189 findMainContainer();
\r
191 setTipLocation(me.getX(), me.getY());
\r
195 public void mouseDragged(MouseEvent me)
\r