402a8bf0824339ac918291b3ae4e7da183b97cac
[jalview.git] / src / jalview / appletgui / IdPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.appletgui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import java.util.Vector;
23
24 import jalview.datamodel.*;
25 import jalview.util.UrlLink;
26
27 public class IdPanel extends Panel implements MouseListener,
28         MouseMotionListener
29 {
30
31   protected IdCanvas idCanvas;
32
33   protected AlignViewport av;
34
35   protected AlignmentPanel alignPanel;
36
37   ScrollThread scrollThread = null;
38
39   int offy;
40
41   int width;
42
43   int lastid = -1;
44
45   boolean mouseDragging = false;
46
47   java.util.Vector links = new java.util.Vector();
48
49   public IdPanel(AlignViewport av, AlignmentPanel parent)
50   {
51     this.av = av;
52     alignPanel = parent;
53     idCanvas = new IdCanvas(av);
54     setLayout(new BorderLayout());
55     add(idCanvas, BorderLayout.CENTER);
56     idCanvas.addMouseListener(this);
57     idCanvas.addMouseMotionListener(this);
58
59     String label, url;
60     // TODO: add in group link parameter
61     if (av.applet != null)
62     {
63       for (int i = 1; i < 10; i++)
64       {
65         label = av.applet.getParameter("linkLabel_" + i);
66         url = av.applet.getParameter("linkURL_" + i);
67
68         if (label != null && url != null)
69         {
70           links.addElement(label + "|" + url);
71         }
72
73       }
74     }
75     if (links.size() < 1)
76     {
77       links = new java.util.Vector();
78       links.addElement("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
79     }
80   }
81
82   Tooltip tooltip;
83
84   public void mouseMoved(MouseEvent e)
85   {
86     int seq = alignPanel.seqPanel.findSeq(e);
87
88     SequenceI sequence = av.getAlignment().getSequenceAt(seq);
89
90     // look for non-pos features
91     StringBuffer tooltiptext = new StringBuffer();
92
93     if (sequence.getDescription() != null)
94     {
95       tooltiptext.append(sequence.getDescription());
96       tooltiptext.append("\n");
97     }
98
99     SequenceFeature sf[] = sequence.getSequenceFeatures();
100     for (int sl = 0; sf != null && sl < sf.length; sl++)
101     {
102       if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
103       {
104         boolean nl = false;
105         if (sf[sl].getFeatureGroup() != null)
106         {
107           tooltiptext.append(sf[sl].getFeatureGroup());
108           nl = true;
109         }
110         ;
111         if (sf[sl].getType() != null)
112         {
113           tooltiptext.append(" ");
114           tooltiptext.append(sf[sl].getType());
115           nl = true;
116         }
117         ;
118         if (sf[sl].getDescription() != null)
119         {
120           tooltiptext.append(" ");
121           tooltiptext.append(sf[sl].getDescription());
122           nl = true;
123         }
124         ;
125         if (sf[sl].getScore() != Float.NaN && sf[sl].getScore() != 0f)
126         {
127           tooltiptext.append(" Score = ");
128           tooltiptext.append(sf[sl].getScore());
129           nl = true;
130         }
131         ;
132         if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
133         {
134           tooltiptext.append(" (");
135           tooltiptext.append(sf[sl].getStatus());
136           tooltiptext.append(")");
137           nl = true;
138         }
139         ;
140         if (nl)
141         {
142           tooltiptext.append("\n");
143         }
144       }
145     }
146
147     if (tooltiptext.length() == 0)
148     {
149       // nothing to display - so clear tooltip if one is visible
150       if (tooltip != null)
151       {
152         tooltip.setVisible(false);
153       }
154       tooltip = null;
155       tooltiptext = null;
156       return;
157     }
158     if (tooltip == null)
159     {
160       tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
161               + tooltiptext.toString(), idCanvas);
162     }
163     else
164     {
165       tooltip.setTip(sequence.getDisplayId(true) + "\n"
166               + tooltiptext.toString());
167     }
168     tooltiptext = null;
169   }
170
171   public void mouseDragged(MouseEvent e)
172   {
173     mouseDragging = true;
174
175     int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
176
177     if (seq < lastid)
178     {
179       selectSeqs(lastid - 1, seq);
180     }
181     else if (seq > lastid)
182     {
183       selectSeqs(lastid + 1, seq);
184     }
185
186     lastid = seq;
187     alignPanel.paintAlignment(false);
188   }
189
190   public void mouseClicked(MouseEvent e)
191   {
192     if (e.getClickCount() < 2)
193     {
194       return;
195     }
196
197     // DEFAULT LINK IS FIRST IN THE LINK LIST
198     int seq = alignPanel.seqPanel.findSeq(e);
199     String id = av.getAlignment().getSequenceAt(seq).getName();
200
201     String target = null;
202     String url = null;
203     int i = 0;
204     while (url == null && i < links.size())
205     {
206       // DEFAULT LINK IS FIRST IN THE LINK LIST
207       // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
208       url = links.elementAt(i++).toString();
209       jalview.util.UrlLink urlLink = null;
210       try
211       {
212         urlLink = new UrlLink(url);
213         target = urlLink.getTarget();
214       } catch (Exception foo)
215       {
216         System.err.println("Exception for URLLink '" + url + "'");
217         foo.printStackTrace();
218         url = null;
219         continue;
220       }
221       ;
222       if (!urlLink.isValid())
223       {
224         System.err.println(urlLink.getInvalidMessage());
225         url = null;
226         continue;
227       }
228
229       String urls[] = urlLink.makeUrls(id, true);
230       if (urls == null || urls[0] == null || urls[0].length() < 1)
231       {
232         url = null;
233         continue;
234       }
235       // just take first URL made from regex
236       url = urls[1];
237     }
238     try
239     {
240
241       alignPanel.alignFrame.showURL(url, target);
242     } catch (Exception ex)
243     {
244       ex.printStackTrace();
245     }
246   }
247
248   public void mouseEntered(MouseEvent e)
249   {
250     if (scrollThread != null)
251     {
252       scrollThread.running = false;
253     }
254   }
255
256   public void mouseExited(MouseEvent e)
257   {
258     if (av.getWrapAlignment())
259     {
260       return;
261     }
262
263     if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
264     {
265       scrollThread = new ScrollThread(true);
266     }
267
268     if (mouseDragging && e.getY() >= getSize().height
269             && av.alignment.getHeight() > av.getEndSeq())
270     {
271       scrollThread = new ScrollThread(false);
272     }
273   }
274
275   public void mousePressed(MouseEvent e)
276   {
277     if (e.getClickCount() > 1)
278     {
279       return;
280     }
281
282     int y = e.getY();
283     if (av.getWrapAlignment())
284     {
285       y -= 2 * av.charHeight;
286     }
287
288     int seq = alignPanel.seqPanel.findSeq(e);
289
290     if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
291     {
292       Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
293       // build a new links menu based on the current links + any non-positional
294       // features
295       Vector nlinks = new Vector();
296       for (int l = 0, lSize = links.size(); l < lSize; l++)
297       {
298         nlinks.addElement(links.elementAt(l));
299       }
300       SequenceFeature sf[] = sq.getSequenceFeatures();
301       for (int sl = 0; sf != null && sl < sf.length; sl++)
302       {
303         if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
304         {
305           if (sf[sl].links != null && sf[sl].links.size() > 0)
306           {
307             for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
308             {
309               nlinks.addElement(sf[sl].links.elementAt(l));
310             }
311           }
312         }
313       }
314
315       APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
316       this.add(popup);
317       popup.show(this, e.getX(), e.getY());
318       return;
319     }
320
321     if ((av.getSelectionGroup() == null)
322             || ((!e.isControlDown() && !e.isShiftDown()) && av
323                     .getSelectionGroup() != null))
324     {
325       av.setSelectionGroup(new SequenceGroup());
326       av.getSelectionGroup().setStartRes(0);
327       av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
328     }
329
330     if (e.isShiftDown() && lastid != -1)
331     {
332       selectSeqs(lastid, seq);
333     }
334     else
335     {
336       selectSeq(seq);
337     }
338
339     alignPanel.paintAlignment(false);
340   }
341
342   void selectSeq(int seq)
343   {
344     lastid = seq;
345     SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
346     av.getSelectionGroup().addOrRemove(pickedSeq, false);
347   }
348
349   void selectSeqs(int start, int end)
350   {
351
352     lastid = start;
353
354     if (end >= av.getAlignment().getHeight())
355     {
356       end = av.getAlignment().getHeight() - 1;
357     }
358
359     if (end < start)
360     {
361       int tmp = start;
362       start = end;
363       end = tmp;
364       lastid = end;
365     }
366     if (av.getSelectionGroup()==null)
367     {
368       av.setSelectionGroup(new SequenceGroup());
369     }
370     for (int i = start; i <= end; i++)
371     {
372       av.getSelectionGroup().addSequence(
373               av.getAlignment().getSequenceAt(i), false);
374     }
375
376   }
377
378   public void mouseReleased(MouseEvent e)
379   {
380     if (scrollThread != null)
381     {
382       scrollThread.running = false;
383     }
384
385     if (av.getSelectionGroup() != null)
386     {
387       av.getSelectionGroup().recalcConservation();
388     }
389
390     mouseDragging = false;
391     PaintRefresher.Refresh(this, av.getSequenceSetId());
392     // always send selection message when mouse is released
393     av.sendSelection();
394   }
395
396   public void highlightSearchResults(java.util.Vector found)
397   {
398     idCanvas.setHighlighted(found);
399
400     if (found == null)
401     {
402       return;
403     }
404
405     int index = av.alignment.findIndex((SequenceI) found.elementAt(0));
406
407     // do we need to scroll the panel?
408     if (av.getStartSeq() > index || av.getEndSeq() < index)
409     {
410       alignPanel.setScrollValues(av.getStartRes(), index);
411     }
412   }
413
414   // this class allows scrolling off the bottom of the visible alignment
415   class ScrollThread extends Thread
416   {
417     boolean running = false;
418
419     boolean up = true;
420
421     public ScrollThread(boolean up)
422     {
423       this.up = up;
424       start();
425     }
426
427     public void stopScrolling()
428     {
429       running = false;
430     }
431
432     public void run()
433     {
434       running = true;
435       while (running)
436       {
437         if (alignPanel.scrollUp(up))
438         {
439           // scroll was ok, so add new sequence to selection
440           int seq = av.getStartSeq();
441           if (!up)
442           {
443             seq = av.getEndSeq();
444           }
445
446           if (seq < lastid)
447           {
448             selectSeqs(lastid - 1, seq);
449           }
450           else if (seq > lastid && seq < av.alignment.getHeight())
451           {
452             selectSeqs(lastid + 1, seq);
453           }
454
455           lastid = seq;
456         }
457         else
458         {
459           running = false;
460         }
461
462         alignPanel.paintAlignment(true);
463         try
464         {
465           Thread.sleep(100);
466         } catch (Exception ex)
467         {
468         }
469       }
470     }
471   }
472
473 }