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