JAL-2675 bump version for branch to 2.10.2b1
[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.urls.api.UrlProviderFactoryI;
28 import jalview.urls.api.UrlProviderI;
29 import jalview.urls.applet.AppletUrlProviderFactory;
30 import jalview.viewmodel.AlignmentViewport;
31
32 import java.awt.BorderLayout;
33 import java.awt.Panel;
34 import java.awt.event.InputEvent;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41
42 public class IdPanel extends Panel
43         implements MouseListener, MouseMotionListener
44 {
45
46   protected IdCanvas idCanvas;
47
48   protected AlignmentViewport av;
49
50   protected AlignmentPanel alignPanel;
51
52   ScrollThread scrollThread = null;
53
54   int lastid = -1;
55
56   boolean mouseDragging = false;
57
58   UrlProviderI urlProvider = null;
59
60   public IdPanel(AlignViewport av, AlignmentPanel parent)
61   {
62     this.av = av;
63     alignPanel = parent;
64     idCanvas = new IdCanvas(av);
65     setLayout(new BorderLayout());
66     add(idCanvas, BorderLayout.CENTER);
67     idCanvas.addMouseListener(this);
68     idCanvas.addMouseMotionListener(this);
69
70     String label, url;
71     // TODO: add in group link parameter
72
73     // make a list of label,url pairs
74     HashMap<String, String> urlList = new HashMap<String, String>();
75     if (av.applet != null)
76     {
77       for (int i = 1; i < 10; i++)
78       {
79         label = av.applet.getParameter("linkLabel_" + i);
80         url = av.applet.getParameter("linkURL_" + i);
81
82         // only add non-null parameters
83         if (label != null)
84         {
85           urlList.put(label, url);
86         }
87       }
88
89       if (!urlList.isEmpty())
90       {
91         // set default as first entry in list
92         String defaultUrl = av.applet.getParameter("linkLabel_1");
93         UrlProviderFactoryI factory = new AppletUrlProviderFactory(
94                 defaultUrl, urlList);
95         urlProvider = factory.createUrlProvider();
96       }
97     }
98   }
99
100   Tooltip tooltip;
101
102   @Override
103   public void mouseMoved(MouseEvent e)
104   {
105     int seq = alignPanel.seqPanel.findSeq(e);
106
107     SequenceI sequence = av.getAlignment().getSequenceAt(seq);
108
109     // look for non-pos features
110     StringBuffer tooltiptext = new StringBuffer();
111     if (sequence != null)
112     {
113       if (sequence.getDescription() != null)
114       {
115         tooltiptext.append(sequence.getDescription());
116         tooltiptext.append("\n");
117       }
118
119       SequenceFeature sf[] = sequence.getSequenceFeatures();
120       for (int sl = 0; sf != null && sl < sf.length; sl++)
121       {
122         if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
123         {
124           boolean nl = false;
125           if (sf[sl].getFeatureGroup() != null)
126           {
127             tooltiptext.append(sf[sl].getFeatureGroup());
128             nl = true;
129           }
130           ;
131           if (sf[sl].getType() != null)
132           {
133             tooltiptext.append(" ");
134             tooltiptext.append(sf[sl].getType());
135             nl = true;
136           }
137           ;
138           if (sf[sl].getDescription() != null)
139           {
140             tooltiptext.append(" ");
141             tooltiptext.append(sf[sl].getDescription());
142             nl = true;
143           }
144           ;
145           if (!Float.isNaN(sf[sl].getScore()) && sf[sl].getScore() != 0f)
146           {
147             tooltiptext.append(" Score = ");
148             tooltiptext.append(sf[sl].getScore());
149             nl = true;
150           }
151           ;
152           if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
153           {
154             tooltiptext.append(" (");
155             tooltiptext.append(sf[sl].getStatus());
156             tooltiptext.append(")");
157             nl = true;
158           }
159           ;
160           if (nl)
161           {
162             tooltiptext.append("\n");
163           }
164         }
165       }
166     }
167     if (tooltiptext.length() == 0)
168     {
169       // nothing to display - so clear tooltip if one is visible
170       if (tooltip != null)
171       {
172         tooltip.setVisible(false);
173       }
174       tooltip = null;
175       tooltiptext = null;
176       return;
177     }
178     if (tooltip == null)
179     {
180       tooltip = new Tooltip(
181               sequence.getDisplayId(true) + "\n" + tooltiptext.toString(),
182               idCanvas);
183     }
184     else
185     {
186       tooltip.setTip(
187               sequence.getDisplayId(true) + "\n" + 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     // get the sequence details
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     // get the default url with the sequence details filled in
230     if (urlProvider == null)
231     {
232       return;
233     }
234     String url = urlProvider.getPrimaryUrl(id);
235     String target = urlProvider.getPrimaryTarget(id);
236     try
237     {
238       alignPanel.alignFrame.showURL(url, target);
239     } catch (Exception ex)
240     {
241       ex.printStackTrace();
242     }
243   }
244
245   @Override
246   public void mouseEntered(MouseEvent e)
247   {
248     if (scrollThread != null)
249     {
250       scrollThread.running = false;
251     }
252   }
253
254   @Override
255   public void mouseExited(MouseEvent e)
256   {
257     if (av.getWrapAlignment())
258     {
259       return;
260     }
261
262     if (mouseDragging && e.getY() < 0 && av.getRanges().getStartSeq() > 0)
263     {
264       scrollThread = new ScrollThread(true);
265     }
266
267     if (mouseDragging && e.getY() >= getSize().height
268             && av.getAlignment().getHeight() > av.getRanges().getEndSeq())
269     {
270       scrollThread = new ScrollThread(false);
271     }
272   }
273
274   @Override
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.getCharHeight();
286     }
287
288     int seq = alignPanel.seqPanel.findSeq(e);
289
290     if ((e.getModifiers()
291             & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
292     {
293       Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
294
295       // build a new links menu based on the current links + any non-positional
296       // features
297       List<String> nlinks;
298       if (urlProvider != null)
299       {
300         nlinks = urlProvider.getLinksForMenu();
301       }
302       else
303       {
304         nlinks = new ArrayList<String>();
305       }
306       SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
307       for (int sl = 0; sf != null && sl < sf.length; sl++)
308       {
309         if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
310         {
311           if (sf[sl].links != null && sf[sl].links.size() > 0)
312           {
313             for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
314             {
315               nlinks.add(sf[sl].links.elementAt(l));
316             }
317           }
318         }
319       }
320
321       APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
322       this.add(popup);
323       popup.show(this, e.getX(), e.getY());
324       return;
325     }
326
327     if ((av.getSelectionGroup() == null)
328             || ((!jalview.util.Platform.isControlDown(e)
329                     && !e.isShiftDown()) && av.getSelectionGroup() != null))
330     {
331       av.setSelectionGroup(new SequenceGroup());
332       av.getSelectionGroup().setStartRes(0);
333       av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
334     }
335
336     if (e.isShiftDown() && lastid != -1)
337     {
338       selectSeqs(lastid, seq);
339     }
340     else
341     {
342       selectSeq(seq);
343     }
344
345     alignPanel.paintAlignment(false);
346   }
347
348   void selectSeq(int seq)
349   {
350     lastid = seq;
351     SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
352     av.getSelectionGroup().addOrRemove(pickedSeq, true);
353   }
354
355   void selectSeqs(int start, int end)
356   {
357
358     lastid = start;
359
360     if (end >= av.getAlignment().getHeight())
361     {
362       end = av.getAlignment().getHeight() - 1;
363     }
364
365     if (end < start)
366     {
367       int tmp = start;
368       start = end;
369       end = tmp;
370       lastid = end;
371     }
372     if (av.getSelectionGroup() == null)
373     {
374       av.setSelectionGroup(new SequenceGroup());
375     }
376     for (int i = start; i <= end; i++)
377     {
378       av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
379               i == end);
380     }
381
382   }
383
384   @Override
385   public void mouseReleased(MouseEvent e)
386   {
387     if (scrollThread != null)
388     {
389       scrollThread.running = false;
390     }
391
392     if (av.getSelectionGroup() != null)
393     {
394       av.getSelectionGroup().recalcConservation();
395     }
396
397     mouseDragging = false;
398     PaintRefresher.Refresh(this, av.getSequenceSetId());
399     // always send selection message when mouse is released
400     av.sendSelection();
401   }
402
403   public void highlightSearchResults(List<SequenceI> list)
404   {
405     idCanvas.setHighlighted(list);
406
407     if (list == null)
408     {
409       return;
410     }
411
412     int index = av.getAlignment().findIndex(list.get(0));
413
414     // do we need to scroll the panel?
415     if (av.getRanges().getStartSeq() > index
416             || av.getRanges().getEndSeq() < index)
417     {
418       av.getRanges().setStartSeq(index);
419     }
420   }
421
422   // this class allows scrolling off the bottom of the visible alignment
423   class ScrollThread extends Thread
424   {
425     boolean running = false;
426
427     boolean up = true;
428
429     public ScrollThread(boolean up)
430     {
431       this.up = up;
432       start();
433     }
434
435     public void stopScrolling()
436     {
437       running = false;
438     }
439
440     @Override
441     public void run()
442     {
443       running = true;
444       while (running)
445       {
446         if (av.getRanges().scrollUp(up))
447         {
448           // scroll was ok, so add new sequence to selection
449           int seq = av.getRanges().getStartSeq();
450           if (!up)
451           {
452             seq = av.getRanges().getEndSeq();
453           }
454
455           if (seq < lastid)
456           {
457             selectSeqs(lastid - 1, seq);
458           }
459           else if (seq > lastid && seq < av.getAlignment().getHeight())
460           {
461             selectSeqs(lastid + 1, seq);
462           }
463
464           lastid = seq;
465         }
466         else
467         {
468           running = false;
469         }
470
471         alignPanel.paintAlignment(true);
472         try
473         {
474           Thread.sleep(100);
475         } catch (Exception ex)
476         {
477         }
478       }
479     }
480   }
481 }