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);
321 if (!Platform.isJS())
324 * Java - run in a new thread
326 scrollThread.start();
331 * for JalviewJS using Swing Timer
333 Timer t = new Timer(20, new ActionListener()
336 public void actionPerformed(ActionEvent e)
338 if (scrollThread != null)
340 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
341 scrollThread.scrollOnce();
345 t.addActionListener(new ActionListener()
348 public void actionPerformed(ActionEvent e)
350 if (scrollThread == null)
352 // IdPanel.stopScrolling called
362 * Respond to a mouse press. Does nothing for (left) double-click as this is
363 * handled by mouseClicked().
365 * Right mouse down - construct and show context menu.
367 * Ctrl-down or Shift-down - add to or expand current selection group if there
370 * Mouse down - select this sequence.
375 public void mousePressed(MouseEvent e)
377 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
382 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
384 if (e.isPopupTrigger()) // Mac reports this in mousePressed
386 showPopupMenu(e, pos);
391 * defer right-mouse click handling to mouseReleased on Windows
392 * (where isPopupTrigger() will answer true)
393 * NB isRightMouseButton is also true for Cmd-click on Mac
395 if (Platform.isWinRightButton(e))
400 if ((av.getSelectionGroup() == null)
401 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
402 && av.getSelectionGroup() != null))
404 av.setSelectionGroup(new SequenceGroup());
405 av.getSelectionGroup().setStartRes(0);
406 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
409 if (e.isShiftDown() && (lastid != -1))
411 selectSeqs(lastid, pos.seqIndex);
415 selectSeq(pos.seqIndex);
418 av.isSelectionGroupChanged(true);
420 alignPanel.paintAlignment(false, false);
424 * Build and show the popup-menu at the right-click mouse position
428 void showPopupMenu(MouseEvent e, MousePos pos)
430 if (pos.isOverAnnotation())
432 showAnnotationMenu(e, pos);
436 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
439 * build a new links menu based on the current links
440 * and any non-positional features
442 List<SequenceFeature> features = null;
445 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
446 features = sq.getFeatures().getNonPositionalFeatures();
447 for (SequenceFeature sf : features)
449 if (sf.links != null)
451 nlinks.addAll(sf.links);
456 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
457 Preferences.getGroupURLLinks());
458 pop.show(this, e.getX(), e.getY());
462 * On right mouse click on a Consensus annotation label, shows a limited popup
463 * menu, with options to configure the consensus calculation and rendering.
467 * @see AnnotationLabels#showPopupMenu(MouseEvent)
469 void showAnnotationMenu(MouseEvent e, MousePos pos)
471 if (pos.annotationIndex == -1)
475 AlignmentAnnotation[] anns = this.av.getAlignment()
476 .getAlignmentAnnotation();
477 if (anns == null || pos.annotationIndex >= anns.length)
481 AlignmentAnnotation ann = anns[pos.annotationIndex];
482 if (!ann.label.contains("Consensus"))
487 JPopupMenu pop = new JPopupMenu(
488 MessageManager.getString("label.annotations"));
489 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
490 pop.show(this, e.getX(), e.getY());
494 * Toggle whether the sequence is part of the current selection group.
498 void selectSeq(int seq)
502 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
503 av.getSelectionGroup().addOrRemove(pickedSeq, false);
507 * Add contiguous rows of the alignment to the current selection group. Does
508 * nothing if there is no selection group.
513 void selectSeqs(int start, int end)
515 if (av.getSelectionGroup() == null)
520 if (end >= av.getAlignment().getHeight())
522 end = av.getAlignment().getHeight() - 1;
535 for (int i = start; i <= end; i++)
537 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
543 * Respond to mouse released. Refreshes the display and triggers broadcast of
544 * the new selection group to any listeners.
549 public void mouseReleased(MouseEvent e)
551 if (scrollThread != null)
555 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
557 mouseDragging = false;
558 PaintRefresher.Refresh(this, av.getSequenceSetId());
559 // always send selection message when mouse is released
562 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
564 showPopupMenu(e, pos);
569 * Highlight sequence ids that match the given list, and if necessary scroll
570 * to the start sequence of the list.
574 public void highlightSearchResults(List<SequenceI> list)
576 getIdCanvas().setHighlighted(list);
578 if (list == null || list.isEmpty())
583 int index = av.getAlignment().findIndex(list.get(0));
585 // do we need to scroll the panel?
586 if ((av.getRanges().getStartSeq() > index)
587 || (av.getRanges().getEndSeq() < index))
589 av.getRanges().setStartSeq(index);
593 public IdCanvas getIdCanvas()
598 public void setIdCanvas(IdCanvas idCanvas)
600 this.idCanvas = idCanvas;
604 * Performs scrolling of the visible alignment up or down, adding newly
605 * visible sequences to the current selection
607 class ScrollThread extends Thread
609 private boolean running = false;
614 * Constructor for a thread that scrolls either up or down
618 public ScrollThread(boolean up)
621 setName("IdPanel$ScrollThread$" + String.valueOf(up));
625 * Sets a flag to stop the scrolling
627 public void stopScrolling()
633 * Scrolls the alignment either up or down, one row at a time, adding newly
634 * visible sequences to the current selection. Speed is limited to a maximum
635 * of ten rows per second. The thread exits when the end of the alignment is
636 * reached or a flag is set to stop it by a call to stopScrolling.
645 running = scrollOnce();
649 } catch (Exception ex)
653 IdPanel.this.scrollThread = null;
657 * Scrolls one row up or down. Answers true if a scroll could be done, false
658 * if not (top or bottom of alignment reached).
662 ViewportRanges ranges = IdPanel.this.av.getRanges();
663 if (ranges.scrollUp(up))
665 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
666 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
667 IdPanel.this.selectSeqs(fromSeq, toSeq);
669 alignPanel.paintAlignment(false, false);