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.MouseEvent;
25 import java.awt.event.MouseListener;
26 import java.awt.event.MouseMotionListener;
27 import java.awt.event.MouseWheelEvent;
28 import java.awt.event.MouseWheelListener;
29 import java.util.List;
31 import javax.swing.JPanel;
32 import javax.swing.JPopupMenu;
33 import javax.swing.SwingUtilities;
34 import javax.swing.ToolTipManager;
36 import jalview.datamodel.AlignmentAnnotation;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.datamodel.SequenceI;
40 import jalview.gui.SeqPanel.MousePos;
41 import jalview.io.SequenceAnnotationReport;
42 import jalview.util.MessageManager;
43 import jalview.util.Platform;
44 import jalview.viewmodel.AlignmentViewport;
45 import jalview.viewmodel.ViewportRanges;
48 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
49 * as well as highlighting ids matched by a search from the Find menu.
54 public class IdPanel extends JPanel
55 implements MouseListener, MouseMotionListener, MouseWheelListener
57 private IdCanvas idCanvas;
59 protected AlignmentViewport av;
61 protected AlignmentPanel alignPanel;
63 ScrollThread scrollThread = null;
70 boolean mouseDragging = false;
72 private final SequenceAnnotationReport seqAnnotReport;
75 * Creates a new IdPanel object.
80 public IdPanel(AlignViewport av, AlignmentPanel parent)
84 setIdCanvas(new IdCanvas(av));
85 seqAnnotReport = new SequenceAnnotationReport(true);
86 setLayout(new BorderLayout());
87 add(getIdCanvas(), BorderLayout.CENTER);
88 addMouseListener(this);
89 addMouseMotionListener(this);
90 addMouseWheelListener(this);
91 ToolTipManager.sharedInstance().registerComponent(this);
95 * Responds to mouse movement by setting tooltip text for the sequence id
96 * under the mouse (or possibly annotation label, when in wrapped mode)
101 public void mouseMoved(MouseEvent e)
103 SeqPanel sp = alignPanel.getSeqPanel();
104 MousePos pos = sp.findMousePosition(e);
105 if (pos.isOverAnnotation())
108 * mouse is over an annotation label in wrapped mode
110 AlignmentAnnotation[] anns = av.getAlignment()
111 .getAlignmentAnnotation();
112 AlignmentAnnotation annotation = anns[pos.annotationIndex];
113 setToolTipText(AnnotationLabels.getTooltip(annotation));
114 alignPanel.alignFrame.setStatus(
115 AnnotationLabels.getStatusMessage(annotation, anns));
119 int seq = Math.max(0, pos.seqIndex);
120 if (seq < av.getAlignment().getHeight())
122 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
123 StringBuilder tip = new StringBuilder(64);
124 tip.append(sequence.getDisplayId(true)).append(" ");
125 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
126 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
127 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
129 StringBuilder text = new StringBuilder();
130 text.append("Sequence ").append(String.valueOf(seq + 1))
132 .append(sequence.getName());
133 alignPanel.alignFrame.setStatus(text.toString());
139 * Responds to a mouse drag by selecting the sequences under the dragged
145 public void mouseDragged(MouseEvent e)
147 mouseDragging = true;
149 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
150 if (pos.isOverAnnotation())
152 // mouse is over annotation label in wrapped mode
156 int seq = Math.max(0, pos.seqIndex);
160 selectSeqs(lastid - 1, seq);
162 else if (seq > lastid)
164 selectSeqs(lastid + 1, seq);
168 alignPanel.paintAlignment(false, false);
172 * Response to the mouse wheel by scrolling the alignment panel.
175 public void mouseWheelMoved(MouseWheelEvent e)
178 double wheelRotation = e.getPreciseWheelRotation();
179 if (wheelRotation > 0)
183 av.getRanges().scrollRight(true);
187 av.getRanges().scrollUp(false);
190 else if (wheelRotation < 0)
194 av.getRanges().scrollRight(false);
198 av.getRanges().scrollUp(true);
204 * Handle a mouse click event. Currently only responds to a double-click. The
205 * action is to try to open a browser window at a URL that searches for the
206 * selected sequence id. The search URL is configured in Preferences |
207 * Connections | URL link from Sequence ID. For example:
209 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
214 public void mouseClicked(MouseEvent e)
217 * Ignore single click. Ignore 'left' click followed by 'right' click (user
218 * selects a row then its pop-up menu).
220 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
222 // reinstate isRightMouseButton check to ignore mouse-related popup events
223 // note - this does nothing on default MacBookPro force-trackpad config!
227 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
228 int seq = pos.seqIndex;
229 if (pos.isOverAnnotation() || seq < 0)
234 String id = av.getAlignment().getSequenceAt(seq).getName();
235 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
239 jalview.util.BrowserLauncher.openURL(url);
240 } catch (Exception ex)
242 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
243 MessageManager.getString("label.web_browser_not_found_unix"),
244 MessageManager.getString("label.web_browser_not_found"),
245 JvOptionPane.WARNING_MESSAGE);
246 ex.printStackTrace();
257 public void mouseEntered(MouseEvent e)
259 if (scrollThread != null)
261 scrollThread.stopScrolling();
272 public void mouseExited(MouseEvent e)
274 if (av.getWrapAlignment())
279 if (mouseDragging && (e.getY() < 0)
280 && (av.getRanges().getStartSeq() > 0))
282 scrollThread = new ScrollThread(true);
285 if (mouseDragging && (e.getY() >= getHeight())
286 && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
288 scrollThread = new ScrollThread(false);
293 * Respond to a mouse press. Does nothing for (left) double-click as this is
294 * handled by mouseClicked().
296 * Right mouse down - construct and show context menu.
298 * Ctrl-down or Shift-down - add to or expand current selection group if there
301 * Mouse down - select this sequence.
306 public void mousePressed(MouseEvent e)
308 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
313 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
315 if (e.isPopupTrigger()) // Mac reports this in mousePressed
317 showPopupMenu(e, pos);
322 * defer right-mouse click handling to mouseReleased on Windows
323 * (where isPopupTrigger() will answer true)
324 * NB isRightMouseButton is also true for Cmd-click on Mac
326 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
331 if ((av.getSelectionGroup() == null)
332 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
333 && av.getSelectionGroup() != null))
335 av.setSelectionGroup(new SequenceGroup());
336 av.getSelectionGroup().setStartRes(0);
337 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
340 if (e.isShiftDown() && (lastid != -1))
342 selectSeqs(lastid, pos.seqIndex);
346 selectSeq(pos.seqIndex);
349 av.isSelectionGroupChanged(true);
351 alignPanel.paintAlignment(false, false);
355 * Build and show the popup-menu at the right-click mouse position
359 void showPopupMenu(MouseEvent e, MousePos pos)
361 if (pos.isOverAnnotation())
363 showAnnotationMenu(e, pos);
367 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
370 PopupMenu pop = new PopupMenu(alignPanel, sq,
371 Preferences.getGroupURLLinks());
372 pop.show(this, e.getX(), e.getY());
377 * On right mouse click on a Consensus annotation label, shows a limited popup
378 * menu, with options to configure the consensus calculation and rendering.
382 * @see AnnotationLabels#showPopupMenu(MouseEvent)
384 void showAnnotationMenu(MouseEvent e, MousePos pos)
386 if (pos.annotationIndex == -1)
390 AlignmentAnnotation[] anns = this.av.getAlignment()
391 .getAlignmentAnnotation();
392 if (anns == null || pos.annotationIndex >= anns.length)
396 AlignmentAnnotation ann = anns[pos.annotationIndex];
397 if (!ann.label.contains("Consensus"))
402 JPopupMenu pop = new JPopupMenu(
403 MessageManager.getString("label.annotations"));
404 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
405 pop.show(this, e.getX(), e.getY());
409 * Toggle whether the sequence is part of the current selection group.
413 void selectSeq(int seq)
417 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
418 av.getSelectionGroup().addOrRemove(pickedSeq, false);
422 * Add contiguous rows of the alignment to the current selection group. Does
423 * nothing if there is no selection group.
428 void selectSeqs(int start, int end)
430 if (av.getSelectionGroup() == null)
435 if (end >= av.getAlignment().getHeight())
437 end = av.getAlignment().getHeight() - 1;
450 for (int i = start; i <= end; i++)
452 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
458 * Respond to mouse released. Refreshes the display and triggers broadcast of
459 * the new selection group to any listeners.
464 public void mouseReleased(MouseEvent e)
466 if (scrollThread != null)
468 scrollThread.stopScrolling();
470 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
472 mouseDragging = false;
473 PaintRefresher.Refresh(this, av.getSequenceSetId());
474 // always send selection message when mouse is released
477 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
479 showPopupMenu(e, pos);
484 * Highlight sequence ids that match the given list, and if necessary scroll
485 * to the start sequence of the list.
489 public void highlightSearchResults(List<SequenceI> list)
491 getIdCanvas().setHighlighted(list);
493 if (list == null || list.isEmpty())
498 int index = av.getAlignment().findIndex(list.get(0));
500 // do we need to scroll the panel?
501 if ((av.getRanges().getStartSeq() > index)
502 || (av.getRanges().getEndSeq() < index))
504 av.getRanges().setStartSeq(index);
508 public IdCanvas getIdCanvas()
513 public void setIdCanvas(IdCanvas idCanvas)
515 this.idCanvas = idCanvas;
519 * Performs scrolling of the visible alignment up or down, adding newly
520 * visible sequences to the current selection
522 class ScrollThread extends Thread
524 private boolean running = false;
529 * Constructor for a thread that scrolls either up or down
533 public ScrollThread(boolean up)
536 setName("IdPanel$ScrollThread$" + String.valueOf(up));
541 * Sets a flag to stop the scrolling
543 public void stopScrolling()
549 * Scrolls the alignment either up or down, one row at a time, adding newly
550 * visible sequences to the current selection. Speed is limited to a maximum
551 * of ten rows per second. The thread exits when the end of the alignment is
552 * reached or a flag is set to stop it.
561 ViewportRanges ranges = IdPanel.this.av.getRanges();
562 if (ranges.scrollUp(up))
564 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
565 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
566 IdPanel.this.selectSeqs(fromSeq, toSeq);
573 * scroll did nothing - reached limit of visible alignment
578 alignPanel.paintAlignment(false, false);
583 } catch (Exception ex)