If its null, set tip empty
[jalview.git] / src / jalview / appletgui / Tooltip.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\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
9  *\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
14  *\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
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.awt.*;\r
23 import java.applet.*;\r
24 import java.awt.event.*;\r
25 import java.util.*;\r
26 \r
27 public class Tooltip extends Canvas implements MouseListener,\r
28     MouseMotionListener\r
29 {\r
30 \r
31         private String [] tip;\r
32         protected Component owner;\r
33 \r
34         private Container mainContainer;\r
35         private LayoutManager mainLayout;\r
36 \r
37         private boolean shown;\r
38 \r
39         private final int VERTICAL_OFFSET = 20;\r
40         private final int HORIZONTAL_ENLARGE = 10;\r
41 \r
42         int fontHeight = 0;\r
43 \r
44         Image linkImage;\r
45 \r
46         FontMetrics fm;\r
47 \r
48 \r
49 \r
50         public Tooltip(String tip, Component owner)\r
51         {\r
52           this.owner = owner;\r
53           owner.addMouseListener(this);\r
54           owner.addMouseMotionListener(this);\r
55           setBackground(new Color(255, 255, 220));\r
56           setTip(tip);\r
57           java.net.URL url = getClass().getResource("/images/link.gif");\r
58           if (url != null)\r
59           {\r
60             linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);\r
61           }\r
62         }\r
63 \r
64 \r
65         public void paint(Graphics g)\r
66         {\r
67                 g.drawRect(0,0,getSize().width -1, getSize().height -1);\r
68                 int lindex, x;\r
69                 for(int i=0; i<tip.length; i++)\r
70                 {\r
71                   x = 3;\r
72                   lindex = tip[i].indexOf("%LINK%");\r
73                   if(lindex!=-1)\r
74                   {\r
75                    if(lindex>0)\r
76                    {\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
79                    }\r
80                    g.drawImage(linkImage, x, i * fontHeight+1, this);\r
81                    if(lindex+6<tip[i].length())\r
82                    {\r
83                      g.drawString(tip[i].substring(lindex+6),\r
84                                   x + linkImage.getWidth(this),\r
85                                   (i+1)*fontHeight-3);\r
86                    }\r
87 \r
88                   }\r
89                   else\r
90                   g.drawString(tip[i], 3, (i+1)*fontHeight - 3);\r
91                 }\r
92         }\r
93 \r
94         private void addToolTip()\r
95         {\r
96                 mainContainer.setLayout(null);\r
97                 mainContainer.add(this, 0);\r
98                 mainContainer.validate();\r
99                 repaint();\r
100                 shown = true;\r
101         }\r
102 \r
103         void setTip(String tip)\r
104         {\r
105           if(tip==null)\r
106           {\r
107             setTip("");\r
108             return;\r
109           }\r
110           fm = getFontMetrics(owner.getFont());\r
111           fontHeight = fm.getHeight();\r
112 \r
113           int longestLine = 0;\r
114           StringTokenizer st = new StringTokenizer(tip, "\n");\r
115           this.tip = new String[st.countTokens()];\r
116           int index = 0;\r
117           while(st.hasMoreElements())\r
118           {\r
119             this.tip[index] = st.nextToken();\r
120             if(fm.stringWidth(this.tip[index])>longestLine)\r
121               longestLine = fm.stringWidth(this.tip[index]);\r
122             index ++;\r
123           }\r
124 \r
125           setSize(longestLine + HORIZONTAL_ENLARGE,\r
126                   fontHeight*this.tip.length);\r
127         }\r
128 \r
129         void setTipLocation(int x, int y)\r
130         {\r
131 \r
132           setLocation((owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x) +x,\r
133            (owner.getLocationOnScreen().y - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)+y);\r
134 \r
135 \r
136 \r
137         // correction, whole tool tip must be visible\r
138           if (mainContainer.getSize().width < (getLocation().x + getSize().width))\r
139           {\r
140             setLocation(mainContainer.getSize().width - getSize().width,\r
141                         getLocation().y);\r
142           }\r
143 \r
144         }\r
145 \r
146 \r
147         private void removeToolTip() {\r
148                 if (shown) {\r
149                         mainContainer.remove(0);\r
150                         mainContainer.setLayout(mainLayout);\r
151                         mainContainer.validate();\r
152                 }\r
153                 shown = false;\r
154         }\r
155 \r
156         private void findMainContainer() {\r
157                 Container parent = owner.getParent();\r
158                 while (true) {\r
159                         if ((parent instanceof Applet) || (parent instanceof Frame)) {\r
160                                 mainContainer = parent;\r
161                                 break;\r
162                         } else {\r
163                                 parent = parent.getParent();\r
164                         }\r
165                 }\r
166                 mainLayout = mainContainer.getLayout();\r
167         }\r
168 \r
169         public void mouseEntered(MouseEvent me)\r
170         {     }\r
171 \r
172         public void mouseExited(MouseEvent me)\r
173         {\r
174           removeToolTip();\r
175         }\r
176 \r
177         public void mousePressed(MouseEvent me)\r
178         {\r
179           removeToolTip();\r
180         }\r
181 \r
182         public void mouseReleased(MouseEvent me)\r
183         {}\r
184 \r
185         public void mouseClicked(MouseEvent me)\r
186         {}\r
187 \r
188         public void mouseMoved(MouseEvent me)\r
189         {\r
190           if (shown)\r
191             setTipLocation(me.getX(), me.getY());\r
192           else\r
193           {\r
194             findMainContainer();\r
195             addToolTip();\r
196             setTipLocation(me.getX(), me.getY());\r
197           }\r
198         }\r
199 \r
200         public void mouseDragged(MouseEvent me)\r
201         {}\r
202 }\r