todos re adding groupUrl menu item code
[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     // TODO: add in group link parameter
62     if (av.applet != null)
63     {
64       for (int i = 1; i < 10; i++)
65       {
66         label = av.applet.getParameter("linkLabel_" + i);
67         url = av.applet.getParameter("linkURL_" + i);
68
69         if (label != null && url != null)
70         {
71           links.addElement(label + "|" + url);
72         }
73
74       }
75     }
76     if (links.size() < 1)
77     {
78       links = new java.util.Vector();
79       links
80               .addElement("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
81     }
82   }
83
84   Tooltip tooltip;
85
86   public void mouseMoved(MouseEvent e)
87   {
88     int seq = alignPanel.seqPanel.findSeq(e);
89
90     SequenceI sequence = av.getAlignment().getSequenceAt(seq);
91     
92     // look for non-pos features
93     StringBuffer tooltiptext = new StringBuffer();
94     
95
96     if (sequence.getDescription() != null)
97     {
98       tooltiptext.append(sequence.getDescription());
99       tooltiptext.append("\n");
100     }
101     
102     SequenceFeature sf[] = sequence.getSequenceFeatures();
103     for (int sl=0;sf!=null && sl<sf.length;sl++)
104     {
105       if (sf[sl].begin==sf[sl].end && sf[sl].begin==0)
106       {
107         boolean nl=false;
108         if (sf[sl].getFeatureGroup()!=null) { tooltiptext.append(sf[sl].getFeatureGroup()); nl=true;};
109         if (sf[sl].getType()!=null) { tooltiptext.append(" "); tooltiptext.append(sf[sl].getType()); nl=true;};
110         if (sf[sl].getDescription()!=null) { tooltiptext.append(" "); tooltiptext.append(sf[sl].getDescription()); nl=true;};
111         if (sf[sl].getScore()!=Float.NaN && sf[sl].getScore()!=0f) { tooltiptext.append(" Score = "); tooltiptext.append(sf[sl].getScore()); nl=true;};
112         if (sf[sl].getStatus()!=null && sf[sl].getStatus().length()>0) { tooltiptext.append(" ("); tooltiptext.append(sf[sl].getStatus()); tooltiptext.append(")");nl=true;};
113         if (nl) {tooltiptext.append("\n"); }
114       }
115     }
116     
117     if (tooltiptext.length()==0)
118     {
119       // nothing to display - so clear tooltip if one is visible
120       if (tooltip != null)
121       {
122         tooltip.setVisible(false);
123       }
124       tooltip = null;
125       tooltiptext = null;
126       return;
127     }
128     if (tooltip == null)
129     {
130       tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
131               + tooltiptext.toString(), idCanvas);
132     }
133     else
134     {
135       tooltip.setTip(sequence.getDisplayId(true) + "\n"
136               + tooltiptext.toString());
137     }
138     tooltiptext=null;
139   }
140
141   public void mouseDragged(MouseEvent e)
142   {
143     mouseDragging = true;
144
145     int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
146
147     if (seq < lastid)
148     {
149       selectSeqs(lastid - 1, seq);
150     }
151     else if (seq > lastid)
152     {
153       selectSeqs(lastid + 1, seq);
154     }
155
156     lastid = seq;
157     alignPanel.paintAlignment(false);
158   }
159
160   public void mouseClicked(MouseEvent e)
161   {
162     if (e.getClickCount() < 2)
163     {
164       return;
165     }
166
167     // DEFAULT LINK IS FIRST IN THE LINK LIST
168     int seq = alignPanel.seqPanel.findSeq(e);
169     String id = av.getAlignment().getSequenceAt(seq).getName();
170
171     String target = null;
172     String url = null;
173     int i = 0;
174     while (url == null && i < links.size())
175     {
176       // DEFAULT LINK IS FIRST IN THE LINK LIST
177       // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
178       url = links.elementAt(i++).toString();
179       jalview.util.UrlLink urlLink = null;
180       try
181       {
182         urlLink = new UrlLink(url);
183         target = urlLink.getTarget();
184       } catch (Exception foo)
185       {
186         System.err.println("Exception for URLLink '" + url + "'");
187         foo.printStackTrace();
188         url = null;
189         continue;
190       }
191       ;
192       if (!urlLink.isValid())
193       {
194         System.err.println(urlLink.getInvalidMessage());
195         url = null;
196         continue;
197       }
198
199       String urls[] = urlLink.makeUrls(id, true);
200       if (urls == null || urls[0] == null || urls[0].length() < 1)
201       {
202         url = null;
203         continue;
204       }
205       // just take first URL made from regex
206       url = urls[1];
207     }
208     try
209     {
210
211       alignPanel.alignFrame.showURL(url, target);
212     } catch (Exception ex)
213     {
214       ex.printStackTrace();
215     }
216   }
217
218   public void mouseEntered(MouseEvent e)
219   {
220     if (scrollThread != null)
221     {
222       scrollThread.running = false;
223     }
224   }
225
226   public void mouseExited(MouseEvent e)
227   {
228     if (av.getWrapAlignment())
229     {
230       return;
231     }
232
233     if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
234     {
235       scrollThread = new ScrollThread(true);
236     }
237
238     if (mouseDragging && e.getY() >= getSize().height
239             && av.alignment.getHeight() > av.getEndSeq())
240     {
241       scrollThread = new ScrollThread(false);
242     }
243   }
244
245   public void mousePressed(MouseEvent e)
246   {
247     if (e.getClickCount() > 1)
248     {
249       return;
250     }
251
252     int y = e.getY();
253     if (av.getWrapAlignment())
254     {
255       y -= 2 * av.charHeight;
256     }
257
258     int seq = alignPanel.seqPanel.findSeq(e);
259
260     if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
261     {
262       Sequence sq = (Sequence) av
263       .getAlignment().getSequenceAt(seq);
264       // build a new links menu based on the current links + any non-positional features
265       Vector nlinks = new Vector();
266       for (int l=0,lSize=links.size();l<lSize; l++)
267       {
268         nlinks.addElement(links.elementAt(l));
269       }
270       SequenceFeature sf[] = sq.getSequenceFeatures();
271       for (int sl=0;sf!=null && sl<sf.length;sl++)
272       {
273         if (sf[sl].begin==sf[sl].end && sf[sl].begin==0)
274         {
275           if (sf[sl].links!=null && sf[sl].links.size()>0)
276           {
277             for (int l=0, lSize=sf[sl].links.size(); l<lSize; l++)
278             { 
279               nlinks.addElement(sf[sl].links.elementAt(l));
280             }
281           }
282         }
283       }
284       
285       APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
286       this.add(popup);
287       popup.show(this, e.getX(), e.getY());
288       return;
289     }
290
291     if ((av.getSelectionGroup() == null)
292             || ((!e.isControlDown() && !e.isShiftDown()) && av
293                     .getSelectionGroup() != null))
294     {
295       av.setSelectionGroup(new SequenceGroup());
296       av.getSelectionGroup().setStartRes(0);
297       av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
298     }
299
300     if (e.isShiftDown() && lastid != -1)
301     {
302       selectSeqs(lastid, seq);
303     }
304     else
305     {
306       selectSeq(seq);
307     }
308
309     alignPanel.paintAlignment(false);
310   }
311
312   void selectSeq(int seq)
313   {
314     lastid = seq;
315     SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
316     av.getSelectionGroup().addOrRemove(pickedSeq, false);
317   }
318
319   void selectSeqs(int start, int end)
320   {
321
322     lastid = start;
323
324     if (end >= av.getAlignment().getHeight())
325     {
326       end = av.getAlignment().getHeight() - 1;
327     }
328
329     if (end < start)
330     {
331       int tmp = start;
332       start = end;
333       end = tmp;
334       lastid = end;
335     }
336
337     for (int i = start; i <= end; i++)
338     {
339       av.getSelectionGroup().addSequence(
340               av.getAlignment().getSequenceAt(i), false);
341     }
342
343   }
344
345   public void mouseReleased(MouseEvent e)
346   {
347     if (scrollThread != null)
348     {
349       scrollThread.running = false;
350     }
351
352     if (av.getSelectionGroup() != null)
353     {
354       av.getSelectionGroup().recalcConservation();
355     }
356
357     mouseDragging = false;
358     PaintRefresher.Refresh(this, av.getSequenceSetId());
359   }
360
361   public void highlightSearchResults(java.util.Vector found)
362   {
363     idCanvas.setHighlighted(found);
364
365     if (found == null)
366     {
367       return;
368     }
369
370     int index = av.alignment.findIndex((SequenceI) found.elementAt(0));
371
372     // do we need to scroll the panel?
373     if (av.getStartSeq() > index || av.getEndSeq() < index)
374     {
375       alignPanel.setScrollValues(av.getStartRes(), index);
376     }
377   }
378
379   // this class allows scrolling off the bottom of the visible alignment
380   class ScrollThread extends Thread
381   {
382     boolean running = false;
383
384     boolean up = true;
385
386     public ScrollThread(boolean up)
387     {
388       this.up = up;
389       start();
390     }
391
392     public void stopScrolling()
393     {
394       running = false;
395     }
396
397     public void run()
398     {
399       running = true;
400       while (running)
401       {
402         if (alignPanel.scrollUp(up))
403         {
404           // scroll was ok, so add new sequence to selection
405           int seq = av.getStartSeq();
406           if (!up)
407           {
408             seq = av.getEndSeq();
409           }
410
411           if (seq < lastid)
412           {
413             selectSeqs(lastid - 1, seq);
414           }
415           else if (seq > lastid && seq < av.alignment.getHeight())
416           {
417             selectSeqs(lastid + 1, seq);
418           }
419
420           lastid = seq;
421         }
422         else
423         {
424           running = false;
425         }
426
427         alignPanel.paintAlignment(true);
428         try
429         {
430           Thread.sleep(100);
431         } catch (Exception ex)
432         {
433         }
434       }
435     }
436   }
437
438 }