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