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;
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;
73 boolean mouseDragging = false;
75 private final SequenceAnnotationReport seqAnnotReport;
78 * Creates a new IdPanel object.
83 public IdPanel(AlignViewport av, AlignmentPanel parent)
87 setIdCanvas(new IdCanvas(av));
88 seqAnnotReport = new SequenceAnnotationReport(true);
89 setLayout(new BorderLayout());
90 add(getIdCanvas(), BorderLayout.CENTER);
91 addMouseListener(this);
92 addMouseMotionListener(this);
93 addMouseWheelListener(this);
94 ToolTipManager.sharedInstance().registerComponent(this);
98 * Responds to mouse movement by setting tooltip text for the sequence id
99 * under the mouse (or possibly annotation label, when in wrapped mode)
104 public void mouseMoved(MouseEvent e)
106 SeqPanel sp = alignPanel.getSeqPanel();
107 MousePos pos = sp.findMousePosition(e);
108 if (pos.isOverAnnotation())
111 * mouse is over an annotation label in wrapped mode
113 AlignmentAnnotation[] anns = av.getAlignment()
114 .getAlignmentAnnotation();
115 AlignmentAnnotation annotation = anns[pos.annotationIndex];
116 setToolTipText(AnnotationLabels.getTooltip(annotation));
117 alignPanel.alignFrame.setStatus(
118 AnnotationLabels.getStatusMessage(annotation, anns));
122 int seq = Math.max(0, pos.seqIndex);
123 if (seq < av.getAlignment().getHeight())
125 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
126 StringBuilder tip = new StringBuilder(64);
127 tip.append(sequence.getDisplayId(true)).append(" ");
128 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
129 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
130 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
132 StringBuilder text = new StringBuilder();
133 text.append("Sequence ").append(String.valueOf(seq + 1))
135 .append(sequence.getName());
136 alignPanel.alignFrame.setStatus(text.toString());
142 * Responds to a mouse drag by selecting the sequences under the dragged
148 public void mouseDragged(MouseEvent e)
150 mouseDragging = true;
152 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
153 if (pos.isOverAnnotation())
155 // mouse is over annotation label in wrapped mode
159 int seq = Math.max(0, pos.seqIndex);
163 selectSeqs(lastid - 1, seq);
165 else if (seq > lastid)
167 selectSeqs(lastid + 1, seq);
171 alignPanel.paintAlignment(false, false);
175 * Response to the mouse wheel by scrolling the alignment panel.
178 public void mouseWheelMoved(MouseWheelEvent e)
181 double wheelRotation = e.getPreciseWheelRotation();
182 if (wheelRotation > 0)
186 av.getRanges().scrollRight(true);
190 av.getRanges().scrollUp(false);
193 else if (wheelRotation < 0)
197 av.getRanges().scrollRight(false);
201 av.getRanges().scrollUp(true);
207 * Handle a mouse click event. Currently only responds to a double-click. The
208 * action is to try to open a browser window at a URL that searches for the
209 * selected sequence id. The search URL is configured in Preferences |
210 * Connections | URL link from Sequence ID. For example:
212 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
217 public void mouseClicked(MouseEvent e)
220 * Ignore single click. Ignore 'left' click followed by 'right' click (user
221 * selects a row then its pop-up menu).
223 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
225 // reinstate isRightMouseButton check to ignore mouse-related popup events
226 // note - this does nothing on default MacBookPro force-trackpad config!
230 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
231 int seq = pos.seqIndex;
232 if (pos.isOverAnnotation() || seq < 0)
237 String id = av.getAlignment().getSequenceAt(seq).getName();
238 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
242 jalview.util.BrowserLauncher.openURL(url);
243 } catch (Exception ex)
245 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
246 MessageManager.getString("label.web_browser_not_found_unix"),
247 MessageManager.getString("label.web_browser_not_found"),
248 JvOptionPane.WARNING_MESSAGE);
249 ex.printStackTrace();
254 * On (re-)entering the panel, stop any scrolling
259 public void mouseEntered(MouseEvent e)
265 * Interrupts the scroll thread if one is running
269 if (scrollThread != null)
271 scrollThread.stopScrolling();
283 public void mouseExited(MouseEvent e)
285 if (av.getWrapAlignment())
293 * on mouse drag above or below the panel, start
294 * scrolling if there are more sequences to show
296 ViewportRanges ranges = av.getRanges();
297 if (e.getY() < 0 && ranges.getStartSeq() > 0)
299 startScrolling(true);
301 else if (e.getY() >= getHeight()
302 && ranges.getEndSeq() <= av.getAlignment().getHeight())
304 startScrolling(false);
310 * Starts scrolling either up or down
314 void startScrolling(boolean up)
316 scrollThread = new ScrollThread(up);
320 * for JalviewJS using Swing Timer
322 Timer t = new Timer(20, new ActionListener()
325 public void actionPerformed(ActionEvent e)
327 if (scrollThread != null)
329 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
330 scrollThread.scrollOnce();
334 t.addActionListener(new ActionListener()
337 public void actionPerformed(ActionEvent e)
339 if (scrollThread == null)
341 // IdPanel.stopScrolling called
355 scrollThread.start();
360 * Respond to a mouse press. Does nothing for (left) double-click as this is
361 * handled by mouseClicked().
363 * Right mouse down - construct and show context menu.
365 * Ctrl-down or Shift-down - add to or expand current selection group if there
368 * Mouse down - select this sequence.
373 public void mousePressed(MouseEvent e)
375 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
380 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
382 if (e.isPopupTrigger()) // Mac reports this in mousePressed
384 showPopupMenu(e, pos);
389 * defer right-mouse click handling to mouseReleased on Windows
390 * (where isPopupTrigger() will answer true)
391 * NB isRightMouseButton is also true for Cmd-click on Mac
393 if (Platform.isWinRightButton(e))
398 if ((av.getSelectionGroup() == null)
399 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
400 && av.getSelectionGroup() != null))
402 av.setSelectionGroup(new SequenceGroup());
403 av.getSelectionGroup().setStartRes(0);
404 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
407 if (e.isShiftDown() && (lastid != -1))
409 selectSeqs(lastid, pos.seqIndex);
413 selectSeq(pos.seqIndex);
416 av.isSelectionGroupChanged(true);
418 alignPanel.paintAlignment(false, false);
422 * Build and show the popup-menu at the right-click mouse position
426 void showPopupMenu(MouseEvent e, MousePos pos)
428 if (pos.isOverAnnotation())
430 showAnnotationMenu(e, pos);
434 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
437 PopupMenu pop = new PopupMenu(alignPanel, sq,
438 Preferences.getGroupURLLinks());
439 pop.show(this, e.getX(), e.getY());
444 * On right mouse click on a Consensus annotation label, shows a limited popup
445 * menu, with options to configure the consensus calculation and rendering.
449 * @see AnnotationLabels#showPopupMenu(MouseEvent)
451 void showAnnotationMenu(MouseEvent e, MousePos pos)
453 if (pos.annotationIndex == -1)
457 AlignmentAnnotation[] anns = this.av.getAlignment()
458 .getAlignmentAnnotation();
459 if (anns == null || pos.annotationIndex >= anns.length)
463 AlignmentAnnotation ann = anns[pos.annotationIndex];
464 if (!ann.label.contains("Consensus"))
469 JPopupMenu pop = new JPopupMenu(
470 MessageManager.getString("label.annotations"));
471 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
472 pop.show(this, e.getX(), e.getY());
476 * Toggle whether the sequence is part of the current selection group.
480 void selectSeq(int seq)
484 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
485 av.getSelectionGroup().addOrRemove(pickedSeq, false);
489 * Add contiguous rows of the alignment to the current selection group. Does
490 * nothing if there is no selection group.
495 void selectSeqs(int start, int end)
497 if (av.getSelectionGroup() == null)
502 if (end >= av.getAlignment().getHeight())
504 end = av.getAlignment().getHeight() - 1;
517 for (int i = start; i <= end; i++)
519 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
525 * Respond to mouse released. Refreshes the display and triggers broadcast of
526 * the new selection group to any listeners.
531 public void mouseReleased(MouseEvent e)
533 if (scrollThread != null)
537 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
539 mouseDragging = false;
540 PaintRefresher.Refresh(this, av.getSequenceSetId());
541 // always send selection message when mouse is released
544 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
546 showPopupMenu(e, pos);
551 * Highlight sequence ids that match the given list, and if necessary scroll
552 * to the start sequence of the list.
556 public void highlightSearchResults(List<SequenceI> list)
558 getIdCanvas().setHighlighted(list);
560 if (list == null || list.isEmpty())
565 int index = av.getAlignment().findIndex(list.get(0));
567 // do we need to scroll the panel?
568 if ((av.getRanges().getStartSeq() > index)
569 || (av.getRanges().getEndSeq() < index))
571 av.getRanges().setStartSeq(index);
575 public IdCanvas getIdCanvas()
580 public void setIdCanvas(IdCanvas idCanvas)
582 this.idCanvas = idCanvas;
586 * Performs scrolling of the visible alignment up or down, adding newly
587 * visible sequences to the current selection
589 class ScrollThread extends Thread
591 private boolean running = false;
596 * Constructor for a thread that scrolls either up or down
600 public ScrollThread(boolean up)
603 setName("IdPanel$ScrollThread$" + String.valueOf(up));
607 * Sets a flag to stop the scrolling
609 public void stopScrolling()
615 * Scrolls the alignment either up or down, one row at a time, adding newly
616 * visible sequences to the current selection. Speed is limited to a maximum
617 * of ten rows per second. The thread exits when the end of the alignment is
618 * reached or a flag is set to stop it by a call to stopScrolling.
627 running = scrollOnce();
631 } catch (Exception ex)
635 IdPanel.this.scrollThread = null;
639 * Scrolls one row up or down. Answers true if a scroll could be done, false
640 * if not (top or bottom of alignment reached).
644 ViewportRanges ranges = IdPanel.this.av.getRanges();
645 if (ranges.scrollUp(up))
647 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
648 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
649 IdPanel.this.selectSeqs(fromSeq, toSeq);
651 alignPanel.paintAlignment(false, false);