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.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.SeqPanel.MousePos;
28 import jalview.io.SequenceAnnotationReport;
29 import jalview.util.MessageManager;
30 import jalview.util.Platform;
31 import jalview.viewmodel.AlignmentViewport;
32 import jalview.viewmodel.ViewportRanges;
34 import java.awt.BorderLayout;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.MouseEvent;
38 import java.awt.event.MouseListener;
39 import java.awt.event.MouseMotionListener;
40 import java.awt.event.MouseWheelEvent;
41 import java.awt.event.MouseWheelListener;
42 import java.util.List;
44 import javax.swing.JPanel;
45 import javax.swing.JPopupMenu;
46 import javax.swing.SwingUtilities;
47 import javax.swing.Timer;
48 import javax.swing.ToolTipManager;
51 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
52 * as well as highlighting ids matched by a search from the Find menu.
57 public class IdPanel extends JPanel
58 implements MouseListener, MouseMotionListener, MouseWheelListener
60 private IdCanvas idCanvas;
62 protected AlignmentViewport av;
64 protected AlignmentPanel alignPanel;
66 ScrollThread scrollThread = null;
75 boolean mouseDragging = false;
77 private final SequenceAnnotationReport seqAnnotReport;
80 * Creates a new IdPanel object.
85 public IdPanel(AlignViewport av, AlignmentPanel parent)
89 setIdCanvas(new IdCanvas(av));
90 linkImageURL = getClass().getResource("/images/link.gif").toString();
91 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
92 setLayout(new BorderLayout());
93 add(getIdCanvas(), BorderLayout.CENTER);
94 addMouseListener(this);
95 addMouseMotionListener(this);
96 addMouseWheelListener(this);
97 ToolTipManager.sharedInstance().registerComponent(this);
101 * Responds to mouse movement by setting tooltip text for the sequence id
102 * under the mouse (or possibly annotation label, when in wrapped mode)
107 public void mouseMoved(MouseEvent e)
109 SeqPanel sp = alignPanel.getSeqPanel();
110 MousePos pos = sp.findMousePosition(e);
111 if (pos.isOverAnnotation())
114 * mouse is over an annotation label in wrapped mode
116 AlignmentAnnotation[] anns = av.getAlignment()
117 .getAlignmentAnnotation();
118 AlignmentAnnotation annotation = anns[pos.annotationIndex];
119 setToolTipText(AnnotationLabels.getTooltip(annotation));
120 alignPanel.alignFrame.setStatus(
121 AnnotationLabels.getStatusMessage(annotation, anns));
125 int seq = Math.max(0, pos.seqIndex);
126 if (seq < av.getAlignment().getHeight())
128 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
129 StringBuilder tip = new StringBuilder(64);
130 tip.append(sequence.getDisplayId(true)).append(" ");
131 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
132 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
133 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
135 StringBuilder text = new StringBuilder();
136 text.append("Sequence ").append(String.valueOf(seq + 1))
138 .append(sequence.getName());
139 alignPanel.alignFrame.setStatus(text.toString());
145 * Responds to a mouse drag by selecting the sequences under the dragged
151 public void mouseDragged(MouseEvent e)
153 mouseDragging = true;
155 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
156 if (pos.isOverAnnotation())
158 // mouse is over annotation label in wrapped mode
162 int seq = Math.max(0, pos.seqIndex);
166 selectSeqs(lastid - 1, seq);
168 else if (seq > lastid)
170 selectSeqs(lastid + 1, seq);
174 alignPanel.paintAlignment(false, false);
178 * Response to the mouse wheel by scrolling the alignment panel.
181 public void mouseWheelMoved(MouseWheelEvent e)
184 double wheelRotation = e.getPreciseWheelRotation();
185 if (wheelRotation > 0)
189 av.getRanges().scrollRight(true);
193 av.getRanges().scrollUp(false);
196 else if (wheelRotation < 0)
200 av.getRanges().scrollRight(false);
204 av.getRanges().scrollUp(true);
210 * Handle a mouse click event. Currently only responds to a double-click. The
211 * action is to try to open a browser window at a URL that searches for the
212 * selected sequence id. The search URL is configured in Preferences |
213 * Connections | URL link from Sequence ID. For example:
215 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
220 public void mouseClicked(MouseEvent e)
223 * Ignore single click. Ignore 'left' click followed by 'right' click (user
224 * selects a row then its pop-up menu).
226 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
228 // reinstate isRightMouseButton check to ignore mouse-related popup events
229 // note - this does nothing on default MacBookPro force-trackpad config!
233 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
234 int seq = pos.seqIndex;
235 if (pos.isOverAnnotation() || seq < 0)
240 String id = av.getAlignment().getSequenceAt(seq).getName();
241 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
245 jalview.util.BrowserLauncher.openURL(url);
246 } catch (Exception ex)
248 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
249 MessageManager.getString("label.web_browser_not_found_unix"),
250 MessageManager.getString("label.web_browser_not_found"),
251 JvOptionPane.WARNING_MESSAGE);
252 ex.printStackTrace();
257 * On (re-)entering the panel, stop any scrolling
262 public void mouseEntered(MouseEvent e)
268 * Interrupts the scroll thread if one is running
272 if (scrollThread != null)
274 scrollThread.stopScrolling();
286 public void mouseExited(MouseEvent e)
288 if (av.getWrapAlignment())
296 * on mouse drag above or below the panel, start
297 * scrolling if there are more sequences to show
299 ViewportRanges ranges = av.getRanges();
300 if (e.getY() < 0 && ranges.getStartSeq() > 0)
302 startScrolling(true);
304 else if (e.getY() >= getHeight()
305 && ranges.getEndSeq() <= av.getAlignment().getHeight())
307 startScrolling(false);
313 * Starts scrolling either up or down
317 void startScrolling(boolean up)
319 scrollThread = new ScrollThread(up);
323 * for JalviewJS using Swing Timer
325 Timer t = new Timer(20, new ActionListener()
328 public void actionPerformed(ActionEvent e)
330 if (scrollThread != null)
332 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
333 scrollThread.scrollOnce();
337 t.addActionListener(new ActionListener()
340 public void actionPerformed(ActionEvent e)
342 if (scrollThread == null)
344 // IdPanel.stopScrolling called
358 scrollThread.start();
363 * Respond to a mouse press. Does nothing for (left) double-click as this is
364 * handled by mouseClicked().
366 * Right mouse down - construct and show context menu.
368 * Ctrl-down or Shift-down - add to or expand current selection group if there
371 * Mouse down - select this sequence.
376 public void mousePressed(MouseEvent e)
378 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
383 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
385 if (e.isPopupTrigger()) // Mac reports this in mousePressed
387 showPopupMenu(e, pos);
392 * defer right-mouse click handling to mouseReleased on Windows
393 * (where isPopupTrigger() will answer true)
394 * NB isRightMouseButton is also true for Cmd-click on Mac
396 if (Platform.isWinRightButton(e))
401 if ((av.getSelectionGroup() == null)
402 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
403 && av.getSelectionGroup() != null))
405 av.setSelectionGroup(new SequenceGroup());
406 av.getSelectionGroup().setStartRes(0);
407 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
410 if (e.isShiftDown() && (lastid != -1))
412 selectSeqs(lastid, pos.seqIndex);
416 selectSeq(pos.seqIndex);
419 av.isSelectionGroupChanged(true);
421 alignPanel.paintAlignment(false, false);
425 * Build and show the popup-menu at the right-click mouse position
429 void showPopupMenu(MouseEvent e, MousePos pos)
431 if (pos.isOverAnnotation())
433 showAnnotationMenu(e, pos);
437 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
440 PopupMenu pop = new PopupMenu(alignPanel, sq,
441 Preferences.getGroupURLLinks());
442 pop.show(this, e.getX(), e.getY());
447 * On right mouse click on a Consensus annotation label, shows a limited popup
448 * menu, with options to configure the consensus calculation and rendering.
452 * @see AnnotationLabels#showPopupMenu(MouseEvent)
454 void showAnnotationMenu(MouseEvent e, MousePos pos)
456 if (pos.annotationIndex == -1)
460 AlignmentAnnotation[] anns = this.av.getAlignment()
461 .getAlignmentAnnotation();
462 if (anns == null || pos.annotationIndex >= anns.length)
466 AlignmentAnnotation ann = anns[pos.annotationIndex];
467 if (!ann.label.contains("Consensus"))
472 JPopupMenu pop = new JPopupMenu(
473 MessageManager.getString("label.annotations"));
474 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
475 pop.show(this, e.getX(), e.getY());
479 * Toggle whether the sequence is part of the current selection group.
483 void selectSeq(int seq)
487 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
488 av.getSelectionGroup().addOrRemove(pickedSeq, false);
492 * Add contiguous rows of the alignment to the current selection group. Does
493 * nothing if there is no selection group.
498 void selectSeqs(int start, int end)
500 if (av.getSelectionGroup() == null)
505 if (end >= av.getAlignment().getHeight())
507 end = av.getAlignment().getHeight() - 1;
520 for (int i = start; i <= end; i++)
522 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
528 * Respond to mouse released. Refreshes the display and triggers broadcast of
529 * the new selection group to any listeners.
534 public void mouseReleased(MouseEvent e)
536 if (scrollThread != null)
540 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
542 mouseDragging = false;
543 PaintRefresher.Refresh(this, av.getSequenceSetId());
544 // always send selection message when mouse is released
547 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
549 showPopupMenu(e, pos);
554 * Highlight sequence ids that match the given list, and if necessary scroll
555 * to the start sequence of the list.
559 public void highlightSearchResults(List<SequenceI> list)
561 getIdCanvas().setHighlighted(list);
563 if (list == null || list.isEmpty())
568 int index = av.getAlignment().findIndex(list.get(0));
570 // do we need to scroll the panel?
571 if ((av.getRanges().getStartSeq() > index)
572 || (av.getRanges().getEndSeq() < index))
574 av.getRanges().setStartSeq(index);
578 public IdCanvas getIdCanvas()
583 public void setIdCanvas(IdCanvas idCanvas)
585 this.idCanvas = idCanvas;
589 * Performs scrolling of the visible alignment up or down, adding newly
590 * visible sequences to the current selection
592 class ScrollThread extends Thread
594 private boolean running = false;
599 * Constructor for a thread that scrolls either up or down
603 public ScrollThread(boolean up)
606 setName("IdPanel$ScrollThread$" + String.valueOf(up));
610 * Sets a flag to stop the scrolling
612 public void stopScrolling()
618 * Scrolls the alignment either up or down, one row at a time, adding newly
619 * visible sequences to the current selection. Speed is limited to a maximum
620 * of ten rows per second. The thread exits when the end of the alignment is
621 * reached or a flag is set to stop it by a call to stopScrolling.
630 running = scrollOnce();
634 } catch (Exception ex)
638 IdPanel.this.scrollThread = null;
642 * Scrolls one row up or down. Answers true if a scroll could be done, false
643 * if not (top or bottom of alignment reached).
647 ViewportRanges ranges = IdPanel.this.av.getRanges();
648 if (ranges.scrollUp(up))
650 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
651 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
652 IdPanel.this.selectSeqs(fromSeq, toSeq);
654 alignPanel.paintAlignment(false, false);