Clover icon

Coverage Report

  1. Project Clover database Tue Oct 29 2024 21:36:55 GMT
  2. Package jalview.appletgui

File Tooltip.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
58% of files have more coverage

Code metrics

30
73
13
1
245
189
30
0.41
5.62
13
2.31

Classes

Class Line # Actions
Tooltip 38 73 30
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.appletgui;
22   
23    import java.applet.Applet;
24    import java.awt.Canvas;
25    import java.awt.Color;
26    import java.awt.Component;
27    import java.awt.Container;
28    import java.awt.FontMetrics;
29    import java.awt.Frame;
30    import java.awt.Graphics;
31    import java.awt.Image;
32    import java.awt.LayoutManager;
33    import java.awt.event.MouseEvent;
34    import java.awt.event.MouseListener;
35    import java.awt.event.MouseMotionListener;
36    import java.util.StringTokenizer;
37   
 
38    public class Tooltip extends Canvas
39    implements MouseListener, MouseMotionListener
40    {
41    private String[] tip;
42   
43    private String lastTip = "";
44   
45    private boolean setPosition = false;
46   
47    protected Component owner;
48   
49    private Container mainContainer;
50   
51    private LayoutManager mainLayout;
52   
53    private boolean shown;
54   
55    private final int VERTICAL_OFFSET = 20;
56   
57    private final int HORIZONTAL_ENLARGE = 10;
58   
59    int fontHeight = 0;
60   
61    Image linkImage;
62   
63    FontMetrics fm;
64   
 
65  0 toggle public Tooltip(String tip, Component owner)
66    {
67  0 this.owner = owner;
68  0 owner.addMouseListener(this);
69  0 owner.addMouseMotionListener(this);
70  0 setBackground(new Color(255, 255, 220));
71  0 setTip(tip);
72  0 java.net.URL url = getClass().getResource("/images/link.gif");
73  0 if (url != null)
74    {
75  0 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
76    }
77    }
78   
 
79  0 toggle public void paint(Graphics g)
80    {
81  0 int w = getSize().width;
82  0 int h = getSize().height;
83   
84  0 g.drawRect(0, 0, w - 1, h - 1);
85  0 int lindex, x;
86  0 for (int i = 0; i < tip.length; i++)
87    {
88  0 x = 3;
89  0 lindex = tip[i].indexOf("%LINK%");
90  0 if (lindex != -1)
91    {
92  0 if (lindex > 0)
93    {
94  0 g.drawString(tip[i].substring(0, lindex), 3,
95    (i + 1) * fontHeight - 3);
96  0 x += fm.stringWidth(tip[i].substring(0, lindex) + 3);
97    }
98  0 g.drawImage(linkImage, x, i * fontHeight + 1, this);
99  0 if (lindex + 6 < tip[i].length())
100    {
101  0 g.drawString(tip[i].substring(lindex + 6),
102    x + linkImage.getWidth(this), (i + 1) * fontHeight - 3);
103    }
104    }
105    else
106    {
107  0 g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);
108    }
109    }
110    }
111   
 
112  0 toggle synchronized void setTip(String tip)
113    {
114  0 if (tip == null)
115    {
116  0 setTip("");
117  0 return;
118    }
119   
120  0 if (lastTip.equals(tip))
121    {
122  0 return;
123    }
124   
125  0 lastTip = tip;
126  0 setPosition = true;
127   
128  0 fm = getFontMetrics(owner.getFont());
129  0 fontHeight = fm.getHeight();
130   
131  0 int longestLine = 0;
132  0 StringTokenizer st = new StringTokenizer(tip, "\n");
133  0 this.tip = new String[st.countTokens()];
134  0 int index = 0;
135  0 while (st.hasMoreElements())
136    {
137  0 this.tip[index] = st.nextToken();
138  0 if (fm.stringWidth(this.tip[index]) > longestLine)
139    {
140  0 longestLine = fm.stringWidth(this.tip[index]);
141    }
142  0 index++;
143    }
144   
145  0 setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);
146   
147  0 repaint();
148   
149    }
150   
 
151  0 toggle void setTipLocation(MouseEvent evt)
152    {
153  0 if (mainContainer == null || owner == null)
154    {
155  0 return;
156    }
157  0 setLocation(
158    (owner.getLocationOnScreen().x
159    - mainContainer.getLocationOnScreen().x) + evt.getX(),
160    (owner.getLocationOnScreen().y
161    - mainContainer.getLocationOnScreen().y
162    + VERTICAL_OFFSET) + evt.getY());
163   
164    // correction, whole tool tip must be visible
165  0 if (mainContainer.getSize().width < (getLocation().x + getSize().width))
166    {
167  0 setLocation(mainContainer.getSize().width - getSize().width,
168    getLocation().y);
169    }
170    }
171   
 
172  0 toggle private void removeToolTip()
173    {
174  0 if (shown)
175    {
176  0 mainContainer.remove(0);
177  0 mainContainer.setLayout(mainLayout);
178  0 mainContainer.validate();
179    }
180  0 shown = false;
181    }
182   
 
183  0 toggle private void findMainContainer()
184    {
185  0 Container parent = owner.getParent();
186  0 while (true)
187    {
188  0 if ((parent instanceof Applet) || (parent instanceof Frame))
189    {
190  0 mainContainer = parent;
191  0 break;
192    }
193    else
194    {
195  0 parent = parent.getParent();
196    }
197    }
198  0 mainLayout = mainContainer.getLayout();
199    }
200   
 
201  0 toggle public void mouseEntered(MouseEvent me)
202    {
203  0 setTipLocation(me);
204    }
205   
 
206  0 toggle public void mouseExited(MouseEvent me)
207    {
208  0 removeToolTip();
209    }
210   
 
211  0 toggle public void mousePressed(MouseEvent me)
212    {
213  0 removeToolTip();
214    }
215   
 
216  0 toggle public void mouseReleased(MouseEvent me)
217    {
218    }
219   
 
220  0 toggle public void mouseClicked(MouseEvent me)
221    {
222    }
223   
 
224  0 toggle public void mouseMoved(MouseEvent me)
225    {
226  0 if (!shown)
227    {
228  0 findMainContainer();
229  0 mainContainer.setLayout(null);
230  0 mainContainer.add(this, 0);
231  0 mainContainer.validate();
232  0 shown = true;
233  0 setTipLocation(me);
234    }
235  0 else if (setPosition)
236    {
237  0 setTipLocation(me);
238  0 setPosition = false;
239    }
240    }
241   
 
242  0 toggle public void mouseDragged(MouseEvent me)
243    {
244    }
245    }