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