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))
134 .append(" ID: ").append(sequence.getName());
135 alignPanel.alignFrame.setStatus(text.toString());
141 * Responds to a mouse drag by selecting the sequences under the dragged
147 public void mouseDragged(MouseEvent e)
149 mouseDragging = true;
151 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
152 if (pos.isOverAnnotation())
154 // mouse is over annotation label in wrapped mode
158 int seq = Math.max(0, pos.seqIndex);
162 selectSeqs(lastid - 1, seq);
164 else if (seq > lastid)
166 selectSeqs(lastid + 1, seq);
170 alignPanel.paintAlignment(false, false);
174 * Response to the mouse wheel by scrolling the alignment panel.
177 public void mouseWheelMoved(MouseWheelEvent e)
180 double wheelRotation = e.getPreciseWheelRotation();
181 if (wheelRotation > 0)
185 av.getRanges().scrollRight(true);
189 av.getRanges().scrollUp(false);
192 else if (wheelRotation < 0)
196 av.getRanges().scrollRight(false);
200 av.getRanges().scrollUp(true);
206 * Handle a mouse click event. Currently only responds to a double-click. The
207 * action is to try to open a browser window at a URL that searches for the
208 * selected sequence id. The search URL is configured in Preferences |
209 * Connections | URL link from Sequence ID. For example:
211 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
216 public void mouseClicked(MouseEvent e)
219 * Ignore single click. Ignore 'left' click followed by 'right' click (user
220 * selects a row then its pop-up menu).
222 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
224 // reinstate isRightMouseButton check to ignore mouse-related popup events
225 // note - this does nothing on default MacBookPro force-trackpad config!
229 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
230 int seq = pos.seqIndex;
231 if (pos.isOverAnnotation() || seq < 0)
236 String id = av.getAlignment().getSequenceAt(seq).getName();
237 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
241 jalview.util.BrowserLauncher.openURL(url);
242 } catch (Exception ex)
244 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
245 MessageManager.getString("label.web_browser_not_found_unix"),
246 MessageManager.getString("label.web_browser_not_found"),
247 JvOptionPane.WARNING_MESSAGE);
248 ex.printStackTrace();
253 * On (re-)entering the panel, stop any scrolling
258 public void mouseEntered(MouseEvent e)
264 * Interrupts the scroll thread if one is running
268 if (scrollThread != null)
270 scrollThread.stopScrolling();
282 public void mouseExited(MouseEvent e)
284 if (av.getWrapAlignment())
292 * on mouse drag above or below the panel, start
293 * scrolling if there are more sequences to show
295 ViewportRanges ranges = av.getRanges();
296 if (e.getY() < 0 && ranges.getStartSeq() > 0)
298 startScrolling(true);
300 else if (e.getY() >= getHeight()
301 && ranges.getEndSeq() <= av.getAlignment().getHeight())
303 startScrolling(false);
309 * Starts scrolling either up or down
313 void startScrolling(boolean up)
315 scrollThread = new ScrollThread(up);
319 * for JalviewJS using Swing Timer
321 Timer t = new Timer(20, new ActionListener()
324 public void actionPerformed(ActionEvent e)
326 if (scrollThread != null)
328 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
329 scrollThread.scrollOnce();
333 t.addActionListener(new ActionListener()
336 public void actionPerformed(ActionEvent e)
338 if (scrollThread == null)
340 // IdPanel.stopScrolling called
354 scrollThread.start();
359 * Respond to a mouse press. Does nothing for (left) double-click as this is
360 * handled by mouseClicked().
362 * Right mouse down - construct and show context menu.
364 * Ctrl-down or Shift-down - add to or expand current selection group if there
367 * Mouse down - select this sequence.
372 public void mousePressed(MouseEvent e)
374 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
379 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
381 if (e.isPopupTrigger()) // Mac reports this in mousePressed
383 showPopupMenu(e, pos);
388 * defer right-mouse click handling to mouseReleased on Windows
389 * (where isPopupTrigger() will answer true)
390 * NB isRightMouseButton is also true for Cmd-click on Mac
392 if (Platform.isWinRightButton(e))
397 if ((av.getSelectionGroup() == null)
398 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
399 && av.getSelectionGroup() != null))
401 av.setSelectionGroup(new SequenceGroup());
402 av.getSelectionGroup().setStartRes(0);
403 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
406 if (e.isShiftDown() && (lastid != -1))
408 selectSeqs(lastid, pos.seqIndex);
412 selectSeq(pos.seqIndex);
415 av.isSelectionGroupChanged(true);
417 alignPanel.paintAlignment(false, false);
421 * Build and show the popup-menu at the right-click mouse position
425 void showPopupMenu(MouseEvent e, MousePos pos)
427 if (pos.isOverAnnotation())
429 showAnnotationMenu(e, pos);
433 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
436 PopupMenu pop = new PopupMenu(alignPanel, sq,
437 Preferences.getGroupURLLinks());
438 pop.show(this, e.getX(), e.getY());
443 * On right mouse click on a Consensus annotation label, shows a limited popup
444 * menu, with options to configure the consensus calculation and rendering.
448 * @see AnnotationLabels#showPopupMenu(MouseEvent)
450 void showAnnotationMenu(MouseEvent e, MousePos pos)
452 if (pos.annotationIndex == -1)
456 AlignmentAnnotation[] anns = this.av.getAlignment()
457 .getAlignmentAnnotation();
458 if (anns == null || pos.annotationIndex >= anns.length)
462 AlignmentAnnotation ann = anns[pos.annotationIndex];
463 if (!ann.label.contains("Consensus"))
468 JPopupMenu pop = new JPopupMenu(
469 MessageManager.getString("label.annotations"));
470 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
471 pop.show(this, e.getX(), e.getY());
475 * Toggle whether the sequence is part of the current selection group.
479 void selectSeq(int seq)
483 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
484 av.getSelectionGroup().addOrRemove(pickedSeq, false);
488 * Add contiguous rows of the alignment to the current selection group. Does
489 * nothing if there is no selection group.
494 void selectSeqs(int start, int end)
496 if (av.getSelectionGroup() == null)
501 if (end >= av.getAlignment().getHeight())
503 end = av.getAlignment().getHeight() - 1;
516 for (int i = start; i <= end; i++)
518 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
524 * Respond to mouse released. Refreshes the display and triggers broadcast of
525 * the new selection group to any listeners.
530 public void mouseReleased(MouseEvent e)
532 if (scrollThread != null)
536 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
538 mouseDragging = false;
539 PaintRefresher.Refresh(this, av.getSequenceSetId());
540 // always send selection message when mouse is released
543 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
545 showPopupMenu(e, pos);
550 * Highlight sequence ids that match the given list, and if necessary scroll
551 * to the start sequence of the list.
555 public void highlightSearchResults(List<SequenceI> list)
557 getIdCanvas().setHighlighted(list);
559 if (list == null || list.isEmpty())
564 int index = av.getAlignment().findIndex(list.get(0));
566 // do we need to scroll the panel?
567 if ((av.getRanges().getStartSeq() > index)
568 || (av.getRanges().getEndSeq() < index))
570 av.getRanges().setStartSeq(index);
574 public IdCanvas getIdCanvas()
579 public void setIdCanvas(IdCanvas idCanvas)
581 this.idCanvas = idCanvas;
585 * Performs scrolling of the visible alignment up or down, adding newly
586 * visible sequences to the current selection
588 class ScrollThread extends Thread
590 private boolean running = false;
595 * Constructor for a thread that scrolls either up or down
599 public ScrollThread(boolean up)
602 setName("IdPanel$ScrollThread$" + String.valueOf(up));
606 * Sets a flag to stop the scrolling
608 public void stopScrolling()
614 * Scrolls the alignment either up or down, one row at a time, adding newly
615 * visible sequences to the current selection. Speed is limited to a maximum
616 * of ten rows per second. The thread exits when the end of the alignment is
617 * reached or a flag is set to stop it by a call to stopScrolling.
626 running = scrollOnce();
630 } catch (Exception ex)
634 IdPanel.this.scrollThread = null;
638 * Scrolls one row up or down. Answers true if a scroll could be done, false
639 * if not (top or bottom of alignment reached).
643 ViewportRanges ranges = IdPanel.this.av.getRanges();
644 if (ranges.scrollUp(up))
646 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
647 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
648 IdPanel.this.selectSeqs(fromSeq, toSeq);
650 alignPanel.paintAlignment(false, false);