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 java.awt.BorderLayout;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.MouseEvent;
27 import java.awt.event.MouseListener;
28 import java.awt.event.MouseMotionListener;
29 import java.awt.event.MouseWheelEvent;
30 import java.awt.event.MouseWheelListener;
31 import java.util.List;
33 import javax.swing.JPanel;
34 import javax.swing.JPopupMenu;
35 import javax.swing.SwingUtilities;
36 import javax.swing.Timer;
37 import javax.swing.ToolTipManager;
39 import jalview.datamodel.AlignmentAnnotation;
40 import jalview.datamodel.Sequence;
41 import jalview.datamodel.SequenceGroup;
42 import jalview.datamodel.SequenceI;
43 import jalview.gui.SeqPanel.MousePos;
44 import jalview.io.SequenceAnnotationReport;
45 import jalview.util.MessageManager;
46 import jalview.util.Platform;
47 import jalview.viewmodel.AlignmentViewport;
48 import jalview.viewmodel.ViewportRanges;
49 import jalview.viewmodel.seqfeatures.IdColumn;
50 import jalview.viewmodel.seqfeatures.IdColumns;
51 import jalview.viewmodel.seqfeatures.IdColumns.ColumnCell;
54 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
55 * as well as highlighting ids matched by a search from the Find menu.
60 public class IdPanel extends JPanel
61 implements MouseListener, MouseMotionListener, MouseWheelListener
63 private IdCanvas idCanvas;
65 protected AlignmentViewport av;
67 protected AlignmentPanel alignPanel;
69 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 seqAnnotReport = new SequenceAnnotationReport(true);
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 IdColumn col = locateColumnFor(e);
134 // tooltip for column
135 tip.append(getTooltipFor(col, sequence));
139 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
140 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
142 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
144 StringBuilder text = new StringBuilder();
145 text.append("Sequence ").append(String.valueOf(seq + 1))
146 .append(" ID: ").append(sequence.getName());
147 alignPanel.alignFrame.setStatus(text.toString());
152 private Object getTooltipFor(IdColumn col, SequenceI seq)
154 ColumnCell cell = av.getIdColumns().getCellFor(seq, col);
157 return "" + col.getLabel() + ": " + cell.label;
162 private IdColumn locateColumnFor(MouseEvent e)
164 // TODO COMBINE SAME CODE IN IDCANVAS!!!
166 IdColumns id_cols = av.getIdColumns();
167 List<IdColumn> visible = id_cols.getVisible();
169 * width of an idColumn
172 int panelWidth = Math.max(idCanvas.getWidth() / 2,
173 idCanvas.getWidth() - (colWid * visible.size()));
175 while (panelWidth < idCanvas.getWidth() && p < visible.size())
178 if (e.getX() >= panelWidth && e.getX() < panelWidth + colWid)
179 return visible.get(p);
181 panelWidth += colWid;
187 * Responds to a mouse drag by selecting the sequences under the dragged
193 public void mouseDragged(MouseEvent e)
195 mouseDragging = true;
197 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
198 if (pos.isOverAnnotation())
200 // mouse is over annotation label in wrapped mode
204 int seq = Math.max(0, pos.seqIndex);
208 selectSeqs(lastid - 1, seq);
210 else if (seq > lastid)
212 selectSeqs(lastid + 1, seq);
216 alignPanel.paintAlignment(false, false);
220 * Response to the mouse wheel by scrolling the alignment panel.
223 public void mouseWheelMoved(MouseWheelEvent e)
226 double wheelRotation = e.getPreciseWheelRotation();
227 if (wheelRotation > 0)
231 av.getRanges().scrollRight(true);
235 av.getRanges().scrollUp(false);
238 else if (wheelRotation < 0)
242 av.getRanges().scrollRight(false);
246 av.getRanges().scrollUp(true);
252 * Handle a mouse click event. Currently only responds to a double-click. The
253 * action is to try to open a browser window at a URL that searches for the
254 * selected sequence id. The search URL is configured in Preferences |
255 * Connections | URL link from Sequence ID. For example:
257 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
262 public void mouseClicked(MouseEvent e)
265 * Ignore single click. Ignore 'left' click followed by 'right' click (user
266 * selects a row then its pop-up menu).
268 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
270 // reinstate isRightMouseButton check to ignore mouse-related popup events
271 // note - this does nothing on default MacBookPro force-trackpad config!
275 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
276 int seq = pos.seqIndex;
277 if (pos.isOverAnnotation() || seq < 0)
282 String id = av.getAlignment().getSequenceAt(seq).getName();
283 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
287 jalview.util.BrowserLauncher.openURL(url);
288 } catch (Exception ex)
290 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
291 MessageManager.getString("label.web_browser_not_found_unix"),
292 MessageManager.getString("label.web_browser_not_found"),
293 JvOptionPane.WARNING_MESSAGE);
294 ex.printStackTrace();
299 * On (re-)entering the panel, stop any scrolling
304 public void mouseEntered(MouseEvent e)
310 * Interrupts the scroll thread if one is running
314 if (scrollThread != null)
316 scrollThread.stopScrolling();
328 public void mouseExited(MouseEvent e)
330 if (av.getWrapAlignment())
338 * on mouse drag above or below the panel, start
339 * scrolling if there are more sequences to show
341 ViewportRanges ranges = av.getRanges();
342 if (e.getY() < 0 && ranges.getStartSeq() > 0)
344 startScrolling(true);
346 else if (e.getY() >= getHeight()
347 && ranges.getEndSeq() <= av.getAlignment().getHeight())
349 startScrolling(false);
355 * Starts scrolling either up or down
359 void startScrolling(boolean up)
361 scrollThread = new ScrollThread(up);
365 * for JalviewJS using Swing Timer
367 Timer t = new Timer(20, new ActionListener()
370 public void actionPerformed(ActionEvent e)
372 if (scrollThread != null)
374 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
375 scrollThread.scrollOnce();
379 t.addActionListener(new ActionListener()
382 public void actionPerformed(ActionEvent e)
384 if (scrollThread == null)
386 // IdPanel.stopScrolling called
400 scrollThread.start();
405 * Respond to a mouse press. Does nothing for (left) double-click as this is
406 * handled by mouseClicked().
408 * Right mouse down - construct and show context menu.
410 * Ctrl-down or Shift-down - add to or expand current selection group if there
413 * Mouse down - select this sequence.
418 public void mousePressed(MouseEvent e)
420 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
425 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
427 if (e.isPopupTrigger()) // Mac reports this in mousePressed
429 showPopupMenu(e, pos);
434 * defer right-mouse click handling to mouseReleased on Windows
435 * (where isPopupTrigger() will answer true)
436 * NB isRightMouseButton is also true for Cmd-click on Mac
438 if (Platform.isWinRightButton(e))
443 if ((av.getSelectionGroup() == null)
444 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
445 && av.getSelectionGroup() != null))
447 av.setSelectionGroup(new SequenceGroup());
448 av.getSelectionGroup().setStartRes(0);
449 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
452 if (e.isShiftDown() && (lastid != -1))
454 selectSeqs(lastid, pos.seqIndex);
458 selectSeq(pos.seqIndex);
461 av.isSelectionGroupChanged(true);
463 alignPanel.paintAlignment(false, false);
467 * Build and show the popup-menu at the right-click mouse position
471 void showPopupMenu(MouseEvent e, MousePos pos)
473 if (pos.isOverAnnotation())
475 showAnnotationMenu(e, pos);
479 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
482 PopupMenu pop = new PopupMenu(alignPanel, sq,
483 Preferences.getGroupURLLinks());
484 pop.show(this, e.getX(), e.getY());
489 * On right mouse click on a Consensus annotation label, shows a limited popup
490 * menu, with options to configure the consensus calculation and rendering.
494 * @see AnnotationLabels#showPopupMenu(MouseEvent)
496 void showAnnotationMenu(MouseEvent e, MousePos pos)
498 if (pos.annotationIndex == -1)
502 AlignmentAnnotation[] anns = this.av.getAlignment()
503 .getAlignmentAnnotation();
504 if (anns == null || pos.annotationIndex >= anns.length)
508 AlignmentAnnotation ann = anns[pos.annotationIndex];
509 if (!ann.label.contains("Consensus"))
514 JPopupMenu pop = new JPopupMenu(
515 MessageManager.getString("label.annotations"));
516 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
517 pop.show(this, e.getX(), e.getY());
521 * Toggle whether the sequence is part of the current selection group.
525 void selectSeq(int seq)
529 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
530 av.getSelectionGroup().addOrRemove(pickedSeq, false);
534 * Add contiguous rows of the alignment to the current selection group. Does
535 * nothing if there is no selection group.
540 void selectSeqs(int start, int end)
542 if (av.getSelectionGroup() == null)
547 if (end >= av.getAlignment().getHeight())
549 end = av.getAlignment().getHeight() - 1;
562 for (int i = start; i <= end; i++)
564 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
570 * Respond to mouse released. Refreshes the display and triggers broadcast of
571 * the new selection group to any listeners.
576 public void mouseReleased(MouseEvent e)
578 if (scrollThread != null)
582 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
584 mouseDragging = false;
585 PaintRefresher.Refresh(this, av.getSequenceSetId());
586 // always send selection message when mouse is released
589 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
591 showPopupMenu(e, pos);
596 * Highlight sequence ids that match the given list, and if necessary scroll
597 * to the start sequence of the list.
601 public void highlightSearchResults(List<SequenceI> list)
603 getIdCanvas().setHighlighted(list);
605 if (list == null || list.isEmpty())
610 int index = av.getAlignment().findIndex(list.get(0));
612 // do we need to scroll the panel?
613 if ((av.getRanges().getStartSeq() > index)
614 || (av.getRanges().getEndSeq() < index))
616 av.getRanges().setStartSeq(index);
620 public IdCanvas getIdCanvas()
625 public void setIdCanvas(IdCanvas idCanvas)
627 this.idCanvas = idCanvas;
631 * Performs scrolling of the visible alignment up or down, adding newly
632 * visible sequences to the current selection
634 class ScrollThread extends Thread
636 private boolean running = false;
641 * Constructor for a thread that scrolls either up or down
645 public ScrollThread(boolean up)
648 setName("IdPanel$ScrollThread$" + String.valueOf(up));
652 * Sets a flag to stop the scrolling
654 public void stopScrolling()
660 * Scrolls the alignment either up or down, one row at a time, adding newly
661 * visible sequences to the current selection. Speed is limited to a maximum
662 * of ten rows per second. The thread exits when the end of the alignment is
663 * reached or a flag is set to stop it by a call to stopScrolling.
672 running = scrollOnce();
676 } catch (Exception ex)
680 IdPanel.this.scrollThread = null;
684 * Scrolls one row up or down. Answers true if a scroll could be done, false
685 * if not (top or bottom of alignment reached).
689 ViewportRanges ranges = IdPanel.this.av.getRanges();
690 if (ranges.scrollUp(up))
692 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
693 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
694 IdPanel.this.selectSeqs(fromSeq, toSeq);
696 alignPanel.paintAlignment(false, false);