JAL-2069 spike updated with latest (FeatureTypeSettings)
[jalview.git] / src / jalview / gui / 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.gui;
22
23 import jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.SequenceAnnotationReport;
28 import jalview.util.MessageManager;
29 import jalview.util.Platform;
30 import jalview.viewmodel.AlignmentViewport;
31
32 import java.awt.BorderLayout;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.MouseListener;
35 import java.awt.event.MouseMotionListener;
36 import java.awt.event.MouseWheelEvent;
37 import java.awt.event.MouseWheelListener;
38 import java.util.List;
39
40 import javax.swing.JPanel;
41 import javax.swing.SwingUtilities;
42 import javax.swing.ToolTipManager;
43
44 /**
45  * This panel hosts alignment sequence ids and responds to mouse clicks on them,
46  * as well as highlighting ids matched by a search from the Find menu.
47  * 
48  * @author $author$
49  * @version $Revision$
50  */
51 public class IdPanel extends JPanel
52         implements MouseListener, MouseMotionListener, MouseWheelListener
53 {
54   private IdCanvas idCanvas;
55
56   protected AlignmentViewport av;
57
58   protected AlignmentPanel alignPanel;
59
60   ScrollThread scrollThread = null;
61
62   String linkImageURL;
63
64   int offy;
65
66   // int width;
67   int lastid = -1;
68
69   boolean mouseDragging = false;
70
71   private final SequenceAnnotationReport seqAnnotReport;
72
73   /**
74    * Creates a new IdPanel object.
75    * 
76    * @param av
77    * @param parent
78    */
79   public IdPanel(AlignViewport av, AlignmentPanel parent)
80   {
81     this.av = av;
82     alignPanel = parent;
83     setIdCanvas(new IdCanvas(av));
84     linkImageURL = getClass().getResource("/images/link.gif").toString();
85     seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
86     setLayout(new BorderLayout());
87     add(getIdCanvas(), BorderLayout.CENTER);
88     addMouseListener(this);
89     addMouseMotionListener(this);
90     addMouseWheelListener(this);
91     ToolTipManager.sharedInstance().registerComponent(this);
92   }
93
94   /**
95    * Respond to mouse movement by constructing tooltip text for the sequence id
96    * under the mouse.
97    * 
98    * @param e
99    *          DOCUMENT ME!
100    */
101   @Override
102   public void mouseMoved(MouseEvent e)
103   {
104     SeqPanel sp = alignPanel.getSeqPanel();
105     int seq = Math.max(0, sp.findSeq(e));
106     if (seq > -1 && seq < av.getAlignment().getHeight())
107     {
108       SequenceI sequence = av.getAlignment().getSequenceAt(seq);
109       StringBuilder tip = new StringBuilder(64);
110       seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
111               av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
112       setToolTipText(JvSwingUtils.wrapTooltip(true,
113               sequence.getDisplayId(true) + " " + tip.toString()));
114     }
115   }
116
117   /**
118    * Responds to a mouse drag by selecting the sequences under the dragged
119    * region.
120    * 
121    * @param e
122    */
123   @Override
124   public void mouseDragged(MouseEvent e)
125   {
126     mouseDragging = true;
127
128     int seq = Math.max(0, alignPanel.getSeqPanel().findSeq(e));
129
130     if (seq < lastid)
131     {
132       selectSeqs(lastid - 1, seq);
133     }
134     else if (seq > lastid)
135     {
136       selectSeqs(lastid + 1, seq);
137     }
138
139     lastid = seq;
140     alignPanel.paintAlignment(false, false);
141   }
142
143   /**
144    * Response to the mouse wheel by scrolling the alignment panel.
145    */
146   @Override
147   public void mouseWheelMoved(MouseWheelEvent e)
148   {
149     e.consume();
150     if (e.getWheelRotation() > 0)
151     {
152       if (e.isShiftDown())
153       {
154         av.getRanges().scrollRight(true);
155       }
156       else
157       {
158         av.getRanges().scrollUp(false);
159       }
160     }
161     else
162     {
163       if (e.isShiftDown())
164       {
165         av.getRanges().scrollRight(false);
166       }
167       else
168       {
169         av.getRanges().scrollUp(true);
170       }
171     }
172   }
173
174   /**
175    * Handle a mouse click event. Currently only responds to a double-click. The
176    * action is to try to open a browser window at a URL that searches for the
177    * selected sequence id. The search URL is configured in Preferences |
178    * Connections | URL link from Sequence ID. For example:
179    * 
180    * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
181    * 
182    * @param e
183    */
184   @Override
185   public void mouseClicked(MouseEvent e)
186   {
187     /*
188      * Ignore single click. Ignore 'left' click followed by 'right' click (user
189      * selects a row then its pop-up menu).
190      */
191     if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
192     {
193       // reinstate isRightMouseButton check to ignore mouse-related popup events
194       // note - this does nothing on default MacBookPro force-trackpad config!
195       return;
196     }
197
198     int seq = alignPanel.getSeqPanel().findSeq(e);
199     String id = av.getAlignment().getSequenceAt(seq).getName();
200     String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
201
202     try
203     {
204       jalview.util.BrowserLauncher.openURL(url);
205     } catch (Exception ex)
206     {
207       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
208               MessageManager.getString("label.web_browser_not_found_unix"),
209               MessageManager.getString("label.web_browser_not_found"),
210               JvOptionPane.WARNING_MESSAGE);
211       ex.printStackTrace();
212     }
213   }
214
215   /**
216    * DOCUMENT ME!
217    * 
218    * @param e
219    *          DOCUMENT ME!
220    */
221   @Override
222   public void mouseEntered(MouseEvent e)
223   {
224     if (scrollThread != null)
225     {
226       scrollThread.running = false;
227     }
228   }
229
230   /**
231    * DOCUMENT ME!
232    * 
233    * @param e
234    *          DOCUMENT ME!
235    */
236   @Override
237   public void mouseExited(MouseEvent e)
238   {
239     if (av.getWrapAlignment())
240     {
241       return;
242     }
243
244     if (mouseDragging && (e.getY() < 0)
245             && (av.getRanges().getStartSeq() > 0))
246     {
247       scrollThread = new ScrollThread(true);
248     }
249
250     if (mouseDragging && (e.getY() >= getHeight())
251             && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
252     {
253       scrollThread = new ScrollThread(false);
254     }
255   }
256
257   /**
258    * Respond to a mouse press. Does nothing for (left) double-click as this is
259    * handled by mouseClicked().
260    * 
261    * Right mouse down - construct and show context menu.
262    * 
263    * Ctrl-down or Shift-down - add to or expand current selection group if there
264    * is one.
265    * 
266    * Mouse down - select this sequence.
267    * 
268    * @param e
269    */
270   @Override
271   public void mousePressed(MouseEvent e)
272   {
273     if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
274     {
275       return;
276     }
277
278     if (e.isPopupTrigger()) // Mac reports this in mousePressed
279     {
280       showPopupMenu(e);
281       return;
282     }
283
284     /*
285      * defer right-mouse click handling to mouseReleased on Windows
286      * (where isPopupTrigger() will answer true)
287      * NB isRightMouseButton is also true for Cmd-click on Mac
288      */
289     if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
290     {
291       return;
292     }
293
294     if ((av.getSelectionGroup() == null)
295             || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
296                     && av.getSelectionGroup() != null))
297     {
298       av.setSelectionGroup(new SequenceGroup());
299       av.getSelectionGroup().setStartRes(0);
300       av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
301     }
302
303     int seq = alignPanel.getSeqPanel().findSeq(e);
304     if (e.isShiftDown() && (lastid != -1))
305     {
306       selectSeqs(lastid, seq);
307     }
308     else
309     {
310       selectSeq(seq);
311     }
312
313     av.isSelectionGroupChanged(true);
314
315     alignPanel.paintAlignment(false, false);
316   }
317
318   /**
319    * Build and show the popup-menu at the right-click mouse position
320    * 
321    * @param e
322    */
323   void showPopupMenu(MouseEvent e)
324   {
325     int seq2 = alignPanel.getSeqPanel().findSeq(e);
326     Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq2);
327
328     /*
329      *  build a new links menu based on the current links
330      *  and any non-positional features
331      */
332     List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
333     List<SequenceFeature> features = sq.getFeatures().getNonPositionalFeatures();
334     for (SequenceFeature sf : features)
335     {
336       if (sf.links != null)
337       {
338         for (String link : sf.links)
339         {
340           nlinks.add(link);
341         }
342       }
343     }
344
345     PopupMenu pop = new PopupMenu(alignPanel, sq, features,
346             Preferences.getGroupURLLinks());
347     pop.show(this, e.getX(), e.getY());
348   }
349
350   /**
351    * Toggle whether the sequence is part of the current selection group.
352    * 
353    * @param seq
354    */
355   void selectSeq(int seq)
356   {
357     lastid = seq;
358
359     SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
360     av.getSelectionGroup().addOrRemove(pickedSeq, true);
361   }
362
363   /**
364    * Add contiguous rows of the alignment to the current selection group. Does
365    * nothing if there is no selection group.
366    * 
367    * @param start
368    * @param end
369    */
370   void selectSeqs(int start, int end)
371   {
372     if (av.getSelectionGroup() == null)
373     {
374       return;
375     }
376
377     if (end >= av.getAlignment().getHeight())
378     {
379       end = av.getAlignment().getHeight() - 1;
380     }
381
382     lastid = start;
383
384     if (end < start)
385     {
386       int tmp = start;
387       start = end;
388       end = tmp;
389       lastid = end;
390     }
391
392     for (int i = start; i <= end; i++)
393     {
394       av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
395               i == end);
396     }
397   }
398
399   /**
400    * Respond to mouse released. Refreshes the display and triggers broadcast of
401    * the new selection group to any listeners.
402    * 
403    * @param e
404    */
405   @Override
406   public void mouseReleased(MouseEvent e)
407   {
408     if (scrollThread != null)
409     {
410       scrollThread.running = false;
411     }
412
413     mouseDragging = false;
414     PaintRefresher.Refresh(this, av.getSequenceSetId());
415     // always send selection message when mouse is released
416     av.sendSelection();
417
418     if (e.isPopupTrigger()) // Windows reports this in mouseReleased
419     {
420       showPopupMenu(e);
421     }
422   }
423
424   /**
425    * Highlight sequence ids that match the given list, and if necessary scroll
426    * to the start sequence of the list.
427    * 
428    * @param list
429    */
430   public void highlightSearchResults(List<SequenceI> list)
431   {
432     getIdCanvas().setHighlighted(list);
433
434     if (list == null)
435     {
436       return;
437     }
438
439     int index = av.getAlignment().findIndex(list.get(0));
440
441     // do we need to scroll the panel?
442     if ((av.getRanges().getStartSeq() > index)
443             || (av.getRanges().getEndSeq() < index))
444     {
445       av.getRanges().setStartSeq(index);
446     }
447   }
448
449   public IdCanvas getIdCanvas()
450   {
451     return idCanvas;
452   }
453
454   public void setIdCanvas(IdCanvas idCanvas)
455   {
456     this.idCanvas = idCanvas;
457   }
458
459   // this class allows scrolling off the bottom of the visible alignment
460   class ScrollThread extends Thread
461   {
462     boolean running = false;
463
464     boolean up = true;
465
466     public ScrollThread(boolean up)
467     {
468       this.up = up;
469       start();
470     }
471
472     public void stopScrolling()
473     {
474       running = false;
475     }
476
477     @Override
478     public void run()
479     {
480       running = true;
481
482       while (running)
483       {
484         if (av.getRanges().scrollUp(up))
485         {
486           // scroll was ok, so add new sequence to selection
487           int seq = av.getRanges().getStartSeq();
488
489           if (!up)
490           {
491             seq = av.getRanges().getEndSeq();
492           }
493
494           if (seq < lastid)
495           {
496             selectSeqs(lastid - 1, seq);
497           }
498           else if (seq > lastid)
499           {
500             selectSeqs(lastid + 1, seq);
501           }
502
503           lastid = seq;
504         }
505         else
506         {
507           running = false;
508         }
509
510         alignPanel.paintAlignment(false, false);
511
512         try
513         {
514           Thread.sleep(100);
515         } catch (Exception ex)
516         {
517         }
518       }
519     }
520   }
521 }