Merge branch 'bug/JAL-3166npeAllRowsHidden' into develop
[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     double wheelRotation = e.getPreciseWheelRotation();
151     if (wheelRotation > 0)
152     {
153       if (e.isShiftDown())
154       {
155         av.getRanges().scrollRight(true);
156       }
157       else
158       {
159         av.getRanges().scrollUp(false);
160       }
161     }
162     else if (wheelRotation < 0)
163     {
164       if (e.isShiftDown())
165       {
166         av.getRanges().scrollRight(false);
167       }
168       else
169       {
170         av.getRanges().scrollUp(true);
171       }
172     }
173   }
174
175   /**
176    * Handle a mouse click event. Currently only responds to a double-click. The
177    * action is to try to open a browser window at a URL that searches for the
178    * selected sequence id. The search URL is configured in Preferences |
179    * Connections | URL link from Sequence ID. For example:
180    * 
181    * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
182    * 
183    * @param e
184    */
185   @Override
186   public void mouseClicked(MouseEvent e)
187   {
188     /*
189      * Ignore single click. Ignore 'left' click followed by 'right' click (user
190      * selects a row then its pop-up menu).
191      */
192     if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
193     {
194       // reinstate isRightMouseButton check to ignore mouse-related popup events
195       // note - this does nothing on default MacBookPro force-trackpad config!
196       return;
197     }
198
199     int seq = alignPanel.getSeqPanel().findSeq(e);
200     String id = av.getAlignment().getSequenceAt(seq).getName();
201     String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
202
203     try
204     {
205       jalview.util.BrowserLauncher.openURL(url);
206     } catch (Exception ex)
207     {
208       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
209               MessageManager.getString("label.web_browser_not_found_unix"),
210               MessageManager.getString("label.web_browser_not_found"),
211               JvOptionPane.WARNING_MESSAGE);
212       ex.printStackTrace();
213     }
214   }
215
216   /**
217    * DOCUMENT ME!
218    * 
219    * @param e
220    *          DOCUMENT ME!
221    */
222   @Override
223   public void mouseEntered(MouseEvent e)
224   {
225     if (scrollThread != null)
226     {
227       scrollThread.running = false;
228     }
229   }
230
231   /**
232    * DOCUMENT ME!
233    * 
234    * @param e
235    *          DOCUMENT ME!
236    */
237   @Override
238   public void mouseExited(MouseEvent e)
239   {
240     if (av.getWrapAlignment())
241     {
242       return;
243     }
244
245     if (mouseDragging && (e.getY() < 0)
246             && (av.getRanges().getStartSeq() > 0))
247     {
248       scrollThread = new ScrollThread(true);
249     }
250
251     if (mouseDragging && (e.getY() >= getHeight())
252             && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
253     {
254       scrollThread = new ScrollThread(false);
255     }
256   }
257
258   /**
259    * Respond to a mouse press. Does nothing for (left) double-click as this is
260    * handled by mouseClicked().
261    * 
262    * Right mouse down - construct and show context menu.
263    * 
264    * Ctrl-down or Shift-down - add to or expand current selection group if there
265    * is one.
266    * 
267    * Mouse down - select this sequence.
268    * 
269    * @param e
270    */
271   @Override
272   public void mousePressed(MouseEvent e)
273   {
274     if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
275     {
276       return;
277     }
278
279     if (e.isPopupTrigger()) // Mac reports this in mousePressed
280     {
281       showPopupMenu(e);
282       return;
283     }
284
285     /*
286      * defer right-mouse click handling to mouseReleased on Windows
287      * (where isPopupTrigger() will answer true)
288      * NB isRightMouseButton is also true for Cmd-click on Mac
289      */
290     if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
291     {
292       return;
293     }
294
295     if ((av.getSelectionGroup() == null)
296             || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
297                     && av.getSelectionGroup() != null))
298     {
299       av.setSelectionGroup(new SequenceGroup());
300       av.getSelectionGroup().setStartRes(0);
301       av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
302     }
303
304     int seq = alignPanel.getSeqPanel().findSeq(e);
305     if (e.isShiftDown() && (lastid != -1))
306     {
307       selectSeqs(lastid, seq);
308     }
309     else
310     {
311       selectSeq(seq);
312     }
313
314     av.isSelectionGroupChanged(true);
315
316     alignPanel.paintAlignment(false, false);
317   }
318
319   /**
320    * Build and show the popup-menu at the right-click mouse position
321    * 
322    * @param e
323    */
324   void showPopupMenu(MouseEvent e)
325   {
326     int seq2 = alignPanel.getSeqPanel().findSeq(e);
327     Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq2);
328
329     /*
330      *  build a new links menu based on the current links
331      *  and any non-positional features
332      */
333     List<SequenceFeature> features = null;
334     if (sq != null)
335     {
336     List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
337       features = sq.getFeatures().getNonPositionalFeatures();
338     for (SequenceFeature sf : features)
339     {
340       if (sf.links != null)
341       {
342         for (String link : sf.links)
343         {
344           nlinks.add(link);
345         }
346       }
347     }
348     }
349
350     PopupMenu pop = new PopupMenu(alignPanel, sq, features,
351             Preferences.getGroupURLLinks());
352     pop.show(this, e.getX(), e.getY());
353   }
354
355   /**
356    * Toggle whether the sequence is part of the current selection group.
357    * 
358    * @param seq
359    */
360   void selectSeq(int seq)
361   {
362     lastid = seq;
363
364     SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
365     av.getSelectionGroup().addOrRemove(pickedSeq, false);
366   }
367
368   /**
369    * Add contiguous rows of the alignment to the current selection group. Does
370    * nothing if there is no selection group.
371    * 
372    * @param start
373    * @param end
374    */
375   void selectSeqs(int start, int end)
376   {
377     if (av.getSelectionGroup() == null)
378     {
379       return;
380     }
381
382     if (end >= av.getAlignment().getHeight())
383     {
384       end = av.getAlignment().getHeight() - 1;
385     }
386
387     lastid = start;
388
389     if (end < start)
390     {
391       int tmp = start;
392       start = end;
393       end = tmp;
394       lastid = end;
395     }
396
397     for (int i = start; i <= end; i++)
398     {
399       av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
400               false);
401     }
402   }
403
404   /**
405    * Respond to mouse released. Refreshes the display and triggers broadcast of
406    * the new selection group to any listeners.
407    * 
408    * @param e
409    */
410   @Override
411   public void mouseReleased(MouseEvent e)
412   {
413     if (scrollThread != null)
414     {
415       scrollThread.running = false;
416     }
417
418     mouseDragging = false;
419     PaintRefresher.Refresh(this, av.getSequenceSetId());
420     // always send selection message when mouse is released
421     av.sendSelection();
422
423     if (e.isPopupTrigger()) // Windows reports this in mouseReleased
424     {
425       showPopupMenu(e);
426     }
427   }
428
429   /**
430    * Highlight sequence ids that match the given list, and if necessary scroll
431    * to the start sequence of the list.
432    * 
433    * @param list
434    */
435   public void highlightSearchResults(List<SequenceI> list)
436   {
437     getIdCanvas().setHighlighted(list);
438
439     if (list == null)
440     {
441       return;
442     }
443
444     int index = av.getAlignment().findIndex(list.get(0));
445
446     // do we need to scroll the panel?
447     if ((av.getRanges().getStartSeq() > index)
448             || (av.getRanges().getEndSeq() < index))
449     {
450       av.getRanges().setStartSeq(index);
451     }
452   }
453
454   public IdCanvas getIdCanvas()
455   {
456     return idCanvas;
457   }
458
459   public void setIdCanvas(IdCanvas idCanvas)
460   {
461     this.idCanvas = idCanvas;
462   }
463
464   // this class allows scrolling off the bottom of the visible alignment
465   class ScrollThread extends Thread
466   {
467     boolean running = false;
468
469     boolean up = true;
470
471     public ScrollThread(boolean up)
472     {
473       this.up = up;
474       start();
475     }
476
477     public void stopScrolling()
478     {
479       running = false;
480     }
481
482     @Override
483     public void run()
484     {
485       running = true;
486
487       while (running)
488       {
489         if (av.getRanges().scrollUp(up))
490         {
491           // scroll was ok, so add new sequence to selection
492           int seq = av.getRanges().getStartSeq();
493
494           if (!up)
495           {
496             seq = av.getRanges().getEndSeq();
497           }
498
499           if (seq < lastid)
500           {
501             selectSeqs(lastid - 1, seq);
502           }
503           else if (seq > lastid)
504           {
505             selectSeqs(lastid + 1, seq);
506           }
507
508           lastid = seq;
509         }
510         else
511         {
512           running = false;
513         }
514
515         alignPanel.paintAlignment(false, false);
516
517         try
518         {
519           Thread.sleep(100);
520         } catch (Exception ex)
521         {
522         }
523       }
524     }
525   }
526 }