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