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.util.List;
31 import javax.swing.JPanel;
32 import javax.swing.JPopupMenu;
33 import javax.swing.SwingUtilities;
34 import javax.swing.Timer;
35 import javax.swing.ToolTipManager;
37 import jalview.datamodel.AlignmentAnnotation;
38 import jalview.datamodel.Sequence;
39 import jalview.datamodel.SequenceGroup;
40 import jalview.datamodel.SequenceI;
41 import jalview.gui.SeqPanel.MousePos;
42 import jalview.io.SequenceAnnotationReport;
43 import jalview.util.MessageManager;
44 import jalview.util.Platform;
45 import jalview.viewmodel.AlignmentViewport;
46 import jalview.viewmodel.ViewportRanges;
49 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
50 * as well as highlighting ids matched by a search from the Find menu.
52 public class IdPanel extends JPanel
53 implements MouseListener, MouseMotionListener
55 private IdCanvas idCanvas;
57 protected AlignmentViewport av;
59 protected AlignmentPanel alignPanel;
61 ScrollThread scrollThread = null;
68 boolean mouseDragging = false;
70 private final SequenceAnnotationReport seqAnnotReport;
73 * Creates a new IdPanel object.
78 public IdPanel(AlignViewport av, AlignmentPanel parent)
82 setIdCanvas(new IdCanvas(av));
83 seqAnnotReport = new SequenceAnnotationReport(true);
84 setLayout(new BorderLayout());
85 add(getIdCanvas(), BorderLayout.CENTER);
86 addMouseListener(this);
87 addMouseMotionListener(this);
88 addMouseWheelListener(alignPanel.getSeqPanel());
89 ToolTipManager.sharedInstance().registerComponent(this);
93 * Responds to mouse movement by setting tooltip text for the sequence id
94 * under the mouse (or possibly annotation label, when in wrapped mode)
99 public void mouseMoved(MouseEvent e)
101 SeqPanel sp = alignPanel.getSeqPanel();
102 MousePos pos = sp.findMousePosition(e);
103 if (pos.isOverAnnotation())
106 * mouse is over an annotation label in wrapped mode
108 AlignmentAnnotation[] anns = av.getAlignment()
109 .getAlignmentAnnotation();
110 AlignmentAnnotation annotation = anns[pos.annotationIndex];
111 setToolTipText(AnnotationLabels.getTooltip(annotation));
112 alignPanel.alignFrame.setStatus(
113 AnnotationLabels.getStatusMessage(annotation, anns));
117 int seq = Math.max(0, pos.seqIndex);
118 if (seq < av.getAlignment().getHeight())
120 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
121 StringBuilder tip = new StringBuilder(64);
122 tip.append(sequence.getDisplayId(true)).append(" ");
123 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
124 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
125 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
127 StringBuilder text = new StringBuilder();
128 text.append("Sequence ").append(String.valueOf(seq + 1))
129 .append(" ID: ").append(sequence.getName());
130 alignPanel.alignFrame.setStatus(text.toString());
136 * Responds to a mouse drag by selecting the sequences under the dragged
142 public void mouseDragged(MouseEvent e)
144 mouseDragging = true;
146 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
147 if (pos.isOverAnnotation())
149 // mouse is over annotation label in wrapped mode
153 int seq = Math.max(0, pos.seqIndex);
157 selectSeqs(lastid - 1, seq);
159 else if (seq > lastid)
161 selectSeqs(lastid + 1, seq);
165 alignPanel.paintAlignment(false, false);
169 * Handle a mouse click event. Currently only responds to a double-click. The
170 * action is to try to open a browser window at a URL that searches for the
171 * selected sequence id. The search URL is configured in Preferences |
172 * Connections | URL link from Sequence ID. For example:
174 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
179 public void mouseClicked(MouseEvent e)
182 * Ignore single click. Ignore 'left' click followed by 'right' click (user
183 * selects a row then its pop-up menu).
185 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
187 // reinstate isRightMouseButton check to ignore mouse-related popup events
188 // note - this does nothing on default MacBookPro force-trackpad config!
192 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
193 int seq = pos.seqIndex;
194 if (pos.isOverAnnotation() || seq < 0)
199 String id = av.getAlignment().getSequenceAt(seq).getName();
200 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
204 jalview.util.BrowserLauncher.openURL(url);
205 } catch (Exception ex)
207 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
208 MessageManager.getString("label.web_browser_not_found_unix"),
209 MessageManager.getString("label.web_browser_not_found"),
210 JvOptionPane.WARNING_MESSAGE);
211 ex.printStackTrace();
216 * On (re-)entering the panel, stop any scrolling
221 public void mouseEntered(MouseEvent e)
227 * Interrupts the scroll thread if one is running
231 if (scrollThread != null)
233 scrollThread.stopScrolling();
245 public void mouseExited(MouseEvent e)
247 if (av.getWrapAlignment())
255 * on mouse drag above or below the panel, start
256 * scrolling if there are more sequences to show
258 ViewportRanges ranges = av.getRanges();
259 if (e.getY() < 0 && ranges.getStartSeq() > 0)
261 startScrolling(true);
263 else if (e.getY() >= getHeight()
264 && ranges.getEndSeq() <= av.getAlignment().getHeight())
266 startScrolling(false);
272 * Starts scrolling either up or down
276 void startScrolling(boolean up)
278 scrollThread = new ScrollThread(up);
282 * for JalviewJS using Swing Timer
284 Timer t = new Timer(20, new ActionListener()
287 public void actionPerformed(ActionEvent e)
289 if (scrollThread != null)
291 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
292 scrollThread.scrollOnce();
296 t.addActionListener(new ActionListener()
299 public void actionPerformed(ActionEvent e)
301 if (scrollThread == null)
303 // IdPanel.stopScrolling called
317 scrollThread.start();
322 * Respond to a mouse press. Does nothing for (left) double-click as this is
323 * handled by mouseClicked().
325 * Right mouse down - construct and show context menu.
327 * Ctrl-down or Shift-down - add to or expand current selection group if there
330 * Mouse down - select this sequence.
335 public void mousePressed(MouseEvent e)
337 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
342 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
344 if (e.isPopupTrigger()) // Mac reports this in mousePressed
346 showPopupMenu(e, pos);
351 * defer right-mouse click handling to mouseReleased on Windows
352 * (where isPopupTrigger() will answer true)
353 * NB isRightMouseButton is also true for Cmd-click on Mac
355 if (Platform.isWinRightButton(e))
360 if ((av.getSelectionGroup() == null)
361 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
362 && av.getSelectionGroup() != null))
364 av.setSelectionGroup(new SequenceGroup());
365 av.getSelectionGroup().setStartRes(0);
366 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
369 if (e.isShiftDown() && (lastid != -1))
371 selectSeqs(lastid, pos.seqIndex);
375 selectSeq(pos.seqIndex);
378 av.isSelectionGroupChanged(true);
380 alignPanel.paintAlignment(false, false);
384 * Build and show the popup-menu at the right-click mouse position
388 void showPopupMenu(MouseEvent e, MousePos pos)
390 if (pos.isOverAnnotation())
392 showAnnotationMenu(e, pos);
396 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
399 PopupMenu pop = new PopupMenu(alignPanel, sq,
400 Preferences.getGroupURLLinks());
401 pop.show(this, e.getX(), e.getY());
406 * On right mouse click on a Consensus annotation label, shows a limited popup
407 * menu, with options to configure the consensus calculation and rendering.
411 * @see AnnotationLabels#showPopupMenu(MouseEvent)
413 void showAnnotationMenu(MouseEvent e, MousePos pos)
415 if (pos.annotationIndex == -1)
419 AlignmentAnnotation[] anns = this.av.getAlignment()
420 .getAlignmentAnnotation();
421 if (anns == null || pos.annotationIndex >= anns.length)
425 AlignmentAnnotation ann = anns[pos.annotationIndex];
426 if (!ann.label.contains("Consensus"))
431 JPopupMenu pop = new JPopupMenu(
432 MessageManager.getString("label.annotations"));
433 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
434 pop.show(this, e.getX(), e.getY());
438 * Toggle whether the sequence is part of the current selection group.
442 void selectSeq(int seq)
446 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
447 av.getSelectionGroup().addOrRemove(pickedSeq, false);
451 * Add contiguous rows of the alignment to the current selection group. Does
452 * nothing if there is no selection group.
457 void selectSeqs(int start, int end)
459 if (av.getSelectionGroup() == null)
464 if (end >= av.getAlignment().getHeight())
466 end = av.getAlignment().getHeight() - 1;
479 for (int i = start; i <= end; i++)
481 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
487 * Respond to mouse released. Refreshes the display and triggers broadcast of
488 * the new selection group to any listeners.
493 public void mouseReleased(MouseEvent e)
495 if (scrollThread != null)
499 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
501 mouseDragging = false;
502 PaintRefresher.Refresh(this, av.getSequenceSetId());
503 // always send selection message when mouse is released
506 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
508 showPopupMenu(e, pos);
513 * Highlight sequence ids that match the given list, and if necessary scroll
514 * to the start sequence of the list.
518 public void highlightSearchResults(List<SequenceI> list)
520 getIdCanvas().setHighlighted(list);
522 if (list == null || list.isEmpty())
527 int index = av.getAlignment().findIndex(list.get(0));
529 // do we need to scroll the panel?
530 if ((av.getRanges().getStartSeq() > index)
531 || (av.getRanges().getEndSeq() < index))
533 av.getRanges().setStartSeq(index);
537 public IdCanvas getIdCanvas()
542 public void setIdCanvas(IdCanvas idCanvas)
544 this.idCanvas = idCanvas;
548 * Performs scrolling of the visible alignment up or down, adding newly
549 * visible sequences to the current selection
551 class ScrollThread extends Thread
553 private boolean running = false;
558 * Constructor for a thread that scrolls either up or down
562 public ScrollThread(boolean up)
565 setName("IdPanel$ScrollThread$" + String.valueOf(up));
569 * Sets a flag to stop the scrolling
571 public void stopScrolling()
577 * Scrolls the alignment either up or down, one row at a time, adding newly
578 * visible sequences to the current selection. Speed is limited to a maximum
579 * of ten rows per second. The thread exits when the end of the alignment is
580 * reached or a flag is set to stop it by a call to stopScrolling.
589 running = scrollOnce();
593 } catch (Exception ex)
597 IdPanel.this.scrollThread = null;
601 * Scrolls one row up or down. Answers true if a scroll could be done, false
602 * if not (top or bottom of alignment reached).
606 ViewportRanges ranges = IdPanel.this.av.getRanges();
607 if (ranges.scrollUp(up))
609 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
610 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
611 IdPanel.this.selectSeqs(fromSeq, toSeq);
613 alignPanel.paintAlignment(false, false);