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