2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Sequence;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.SeqPanel.MousePos;
29 import jalview.io.SequenceAnnotationReport;
30 import jalview.util.MessageManager;
31 import jalview.util.Platform;
32 import jalview.viewmodel.AlignmentViewport;
33 import jalview.viewmodel.ViewportRanges;
35 import java.awt.BorderLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.MouseEvent;
39 import java.awt.event.MouseListener;
40 import java.awt.event.MouseMotionListener;
41 import java.awt.event.MouseWheelEvent;
42 import java.awt.event.MouseWheelListener;
43 import java.util.List;
45 import javax.swing.JPanel;
46 import javax.swing.JPopupMenu;
47 import javax.swing.SwingUtilities;
48 import javax.swing.Timer;
49 import javax.swing.ToolTipManager;
52 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
53 * as well as highlighting ids matched by a search from the Find menu.
58 public class IdPanel extends JPanel
59 implements MouseListener, MouseMotionListener, MouseWheelListener
61 private IdCanvas idCanvas;
63 protected AlignmentViewport av;
65 protected AlignmentPanel alignPanel;
67 ScrollThread scrollThread = null;
76 boolean mouseDragging = false;
78 private final SequenceAnnotationReport seqAnnotReport;
81 * Creates a new IdPanel object.
86 public IdPanel(AlignViewport av, AlignmentPanel parent)
90 setIdCanvas(new IdCanvas(av));
91 linkImageURL = getClass().getResource("/images/link.gif").toString();
92 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
93 setLayout(new BorderLayout());
94 add(getIdCanvas(), BorderLayout.CENTER);
95 addMouseListener(this);
96 addMouseMotionListener(this);
97 addMouseWheelListener(this);
98 ToolTipManager.sharedInstance().registerComponent(this);
102 * Responds to mouse movement by setting tooltip text for the sequence id
103 * under the mouse (or possibly annotation label, when in wrapped mode)
108 public void mouseMoved(MouseEvent e)
110 SeqPanel sp = alignPanel.getSeqPanel();
111 MousePos pos = sp.findMousePosition(e);
112 if (pos.isOverAnnotation())
115 * mouse is over an annotation label in wrapped mode
117 AlignmentAnnotation[] anns = av.getAlignment()
118 .getAlignmentAnnotation();
119 AlignmentAnnotation annotation = anns[pos.annotationIndex];
120 setToolTipText(AnnotationLabels.getTooltip(annotation));
121 alignPanel.alignFrame.setStatus(
122 AnnotationLabels.getStatusMessage(annotation, anns));
126 int seq = Math.max(0, pos.seqIndex);
127 if (seq < av.getAlignment().getHeight())
129 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
130 StringBuilder tip = new StringBuilder(64);
131 tip.append(sequence.getDisplayId(true)).append(" ");
132 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
133 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
134 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
136 StringBuilder text = new StringBuilder();
137 text.append("Sequence ").append(String.valueOf(seq + 1))
139 .append(sequence.getName());
140 alignPanel.alignFrame.setStatus(text.toString());
146 * Responds to a mouse drag by selecting the sequences under the dragged
152 public void mouseDragged(MouseEvent e)
154 mouseDragging = true;
156 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
157 if (pos.isOverAnnotation())
159 // mouse is over annotation label in wrapped mode
163 int seq = Math.max(0, pos.seqIndex);
167 selectSeqs(lastid - 1, seq);
169 else if (seq > lastid)
171 selectSeqs(lastid + 1, seq);
175 alignPanel.paintAlignment(false, false);
179 * Response to the mouse wheel by scrolling the alignment panel.
182 public void mouseWheelMoved(MouseWheelEvent e)
185 double wheelRotation = e.getPreciseWheelRotation();
186 if (wheelRotation > 0)
190 av.getRanges().scrollRight(true);
194 av.getRanges().scrollUp(false);
197 else if (wheelRotation < 0)
201 av.getRanges().scrollRight(false);
205 av.getRanges().scrollUp(true);
211 * Handle a mouse click event. Currently only responds to a double-click. The
212 * action is to try to open a browser window at a URL that searches for the
213 * selected sequence id. The search URL is configured in Preferences |
214 * Connections | URL link from Sequence ID. For example:
216 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
221 public void mouseClicked(MouseEvent e)
224 * Ignore single click. Ignore 'left' click followed by 'right' click (user
225 * selects a row then its pop-up menu).
227 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
229 // reinstate isRightMouseButton check to ignore mouse-related popup events
230 // note - this does nothing on default MacBookPro force-trackpad config!
234 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
235 int seq = pos.seqIndex;
236 if (pos.isOverAnnotation() || seq < 0)
241 String id = av.getAlignment().getSequenceAt(seq).getName();
242 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
246 jalview.util.BrowserLauncher.openURL(url);
247 } catch (Exception ex)
249 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
250 MessageManager.getString("label.web_browser_not_found_unix"),
251 MessageManager.getString("label.web_browser_not_found"),
252 JvOptionPane.WARNING_MESSAGE);
253 ex.printStackTrace();
258 * On (re-)entering the panel, stop any scrolling
263 public void mouseEntered(MouseEvent e)
269 * Interrupts the scroll thread if one is running
273 if (scrollThread != null)
275 scrollThread.stopScrolling();
287 public void mouseExited(MouseEvent e)
289 if (av.getWrapAlignment())
297 * on mouse drag above or below the panel, start
298 * scrolling if there are more sequences to show
300 ViewportRanges ranges = av.getRanges();
301 if (e.getY() < 0 && ranges.getStartSeq() > 0)
303 startScrolling(true);
305 else if (e.getY() >= getHeight()
306 && ranges.getEndSeq() <= av.getAlignment().getHeight())
308 startScrolling(false);
314 * Starts scrolling either up or down
318 void startScrolling(boolean up)
320 scrollThread = new ScrollThread(up);
324 * for JalviewJS using Swing Timer
326 Timer t = new Timer(20, new ActionListener()
329 public void actionPerformed(ActionEvent e)
331 if (scrollThread != null)
333 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
334 scrollThread.scrollOnce();
338 t.addActionListener(new ActionListener()
341 public void actionPerformed(ActionEvent e)
343 if (scrollThread == null)
345 // IdPanel.stopScrolling called
359 scrollThread.start();
364 * Respond to a mouse press. Does nothing for (left) double-click as this is
365 * handled by mouseClicked().
367 * Right mouse down - construct and show context menu.
369 * Ctrl-down or Shift-down - add to or expand current selection group if there
372 * Mouse down - select this sequence.
377 public void mousePressed(MouseEvent e)
379 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
384 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
386 if (e.isPopupTrigger()) // Mac reports this in mousePressed
388 showPopupMenu(e, pos);
393 * defer right-mouse click handling to mouseReleased on Windows
394 * (where isPopupTrigger() will answer true)
395 * NB isRightMouseButton is also true for Cmd-click on Mac
397 if (Platform.isWinRightButton(e))
402 if ((av.getSelectionGroup() == null)
403 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
404 && av.getSelectionGroup() != null))
406 av.setSelectionGroup(new SequenceGroup());
407 av.getSelectionGroup().setStartRes(0);
408 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
411 if (e.isShiftDown() && (lastid != -1))
413 selectSeqs(lastid, pos.seqIndex);
417 selectSeq(pos.seqIndex);
420 av.isSelectionGroupChanged(true);
422 alignPanel.paintAlignment(false, false);
426 * Build and show the popup-menu at the right-click mouse position
430 void showPopupMenu(MouseEvent e, MousePos pos)
432 if (pos.isOverAnnotation())
434 showAnnotationMenu(e, pos);
438 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
441 * build a new links menu based on the current links
442 * and any non-positional features
444 List<SequenceFeature> features = null;
447 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
448 features = sq.getFeatures().getNonPositionalFeatures();
449 for (SequenceFeature sf : features)
451 if (sf.links != null)
453 nlinks.addAll(sf.links);
458 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
459 Preferences.getGroupURLLinks());
460 pop.show(this, e.getX(), e.getY());
464 * On right mouse click on a Consensus annotation label, shows a limited popup
465 * menu, with options to configure the consensus calculation and rendering.
469 * @see AnnotationLabels#showPopupMenu(MouseEvent)
471 void showAnnotationMenu(MouseEvent e, MousePos pos)
473 if (pos.annotationIndex == -1)
477 AlignmentAnnotation[] anns = this.av.getAlignment()
478 .getAlignmentAnnotation();
479 if (anns == null || pos.annotationIndex >= anns.length)
483 AlignmentAnnotation ann = anns[pos.annotationIndex];
484 if (!ann.label.contains("Consensus"))
489 JPopupMenu pop = new JPopupMenu(
490 MessageManager.getString("label.annotations"));
491 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
492 pop.show(this, e.getX(), e.getY());
496 * Toggle whether the sequence is part of the current selection group.
500 void selectSeq(int seq)
504 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
505 av.getSelectionGroup().addOrRemove(pickedSeq, false);
509 * Add contiguous rows of the alignment to the current selection group. Does
510 * nothing if there is no selection group.
515 void selectSeqs(int start, int end)
517 if (av.getSelectionGroup() == null)
522 if (end >= av.getAlignment().getHeight())
524 end = av.getAlignment().getHeight() - 1;
537 for (int i = start; i <= end; i++)
539 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
545 * Respond to mouse released. Refreshes the display and triggers broadcast of
546 * the new selection group to any listeners.
551 public void mouseReleased(MouseEvent e)
553 if (scrollThread != null)
557 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
559 mouseDragging = false;
560 PaintRefresher.Refresh(this, av.getSequenceSetId());
561 // always send selection message when mouse is released
564 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
566 showPopupMenu(e, pos);
571 * Highlight sequence ids that match the given list, and if necessary scroll
572 * to the start sequence of the list.
576 public void highlightSearchResults(List<SequenceI> list)
578 getIdCanvas().setHighlighted(list);
580 if (list == null || list.isEmpty())
585 int index = av.getAlignment().findIndex(list.get(0));
587 // do we need to scroll the panel?
588 if ((av.getRanges().getStartSeq() > index)
589 || (av.getRanges().getEndSeq() < index))
591 av.getRanges().setStartSeq(index);
595 public IdCanvas getIdCanvas()
600 public void setIdCanvas(IdCanvas idCanvas)
602 this.idCanvas = idCanvas;
606 * Performs scrolling of the visible alignment up or down, adding newly
607 * visible sequences to the current selection
609 class ScrollThread extends Thread
611 private boolean running = false;
616 * Constructor for a thread that scrolls either up or down
620 public ScrollThread(boolean up)
623 setName("IdPanel$ScrollThread$" + String.valueOf(up));
627 * Sets a flag to stop the scrolling
629 public void stopScrolling()
635 * Scrolls the alignment either up or down, one row at a time, adding newly
636 * visible sequences to the current selection. Speed is limited to a maximum
637 * of ten rows per second. The thread exits when the end of the alignment is
638 * reached or a flag is set to stop it by a call to stopScrolling.
647 running = scrollOnce();
651 } catch (Exception ex)
655 IdPanel.this.scrollThread = null;
659 * Scrolls one row up or down. Answers true if a scroll could be done, false
660 * if not (top or bottom of alignment reached).
664 ViewportRanges ranges = IdPanel.this.av.getRanges();
665 if (ranges.scrollUp(up))
667 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
668 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
669 IdPanel.this.selectSeqs(fromSeq, toSeq);
671 alignPanel.paintAlignment(false, false);