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 jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Sequence;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.SeqPanel.MousePos;
29 import jalview.io.SequenceAnnotationReport;
30 import jalview.util.MessageManager;
31 import jalview.util.Platform;
32 import jalview.viewmodel.AlignmentViewport;
33 import jalview.viewmodel.AlignmentViewport.AutoAnnotation;
34 import jalview.viewmodel.ViewportRanges;
36 import java.awt.BorderLayout;
37 import java.awt.event.MouseEvent;
38 import java.awt.event.MouseListener;
39 import java.awt.event.MouseMotionListener;
40 import java.awt.event.MouseWheelEvent;
41 import java.awt.event.MouseWheelListener;
42 import java.util.List;
44 import javax.swing.JPanel;
45 import javax.swing.JPopupMenu;
46 import javax.swing.SwingUtilities;
47 import javax.swing.ToolTipManager;
50 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
51 * as well as highlighting ids matched by a search from the Find menu.
56 public class IdPanel extends JPanel
57 implements MouseListener, MouseMotionListener, MouseWheelListener
59 private IdCanvas idCanvas;
61 protected AlignmentViewport av;
63 protected AlignmentPanel alignPanel;
65 ScrollThread scrollThread = null;
74 boolean mouseDragging = false;
76 private final SequenceAnnotationReport seqAnnotReport;
79 * Creates a new IdPanel object.
84 public IdPanel(AlignViewport av, AlignmentPanel parent)
88 setIdCanvas(new IdCanvas(av));
89 linkImageURL = getClass().getResource("/images/link.gif").toString();
90 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
91 setLayout(new BorderLayout());
92 add(getIdCanvas(), BorderLayout.CENTER);
93 addMouseListener(this);
94 addMouseMotionListener(this);
95 addMouseWheelListener(this);
96 ToolTipManager.sharedInstance().registerComponent(this);
100 * Responds to mouse movement by setting tooltip text for the sequence id
101 * under the mouse (or possibly annotation label, when in wrapped mode)
106 public void mouseMoved(MouseEvent e)
108 SeqPanel sp = alignPanel.getSeqPanel();
109 MousePos pos = sp.findMousePosition(e);
110 if (pos.isOverAnnotation())
113 * mouse is over an annotation label in wrapped mode
115 AlignmentAnnotation[] anns = av.getAlignment()
116 .getAlignmentAnnotation();
117 AlignmentAnnotation annotation = anns[pos.annotationIndex];
118 setToolTipText(AnnotationLabels.getTooltip(annotation));
119 alignPanel.alignFrame.setStatus(
120 AnnotationLabels.getStatusMessage(annotation, anns));
124 int seq = Math.max(0, pos.seqIndex);
125 if (seq < av.getAlignment().getHeight())
127 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
128 StringBuilder tip = new StringBuilder(64);
129 tip.append(sequence.getDisplayId(true)).append(" ");
130 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
131 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
132 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
134 StringBuilder text = new StringBuilder();
135 text.append("Sequence ").append(String.valueOf(seq + 1))
137 .append(sequence.getName());
138 alignPanel.alignFrame.setStatus(text.toString());
144 * Responds to a mouse drag by selecting the sequences under the dragged
150 public void mouseDragged(MouseEvent e)
152 mouseDragging = true;
154 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
155 if (pos.isOverAnnotation())
157 // mouse is over annotation label in wrapped mode
161 int seq = Math.max(0, pos.seqIndex);
165 selectSeqs(lastid - 1, seq);
167 else if (seq > lastid)
169 selectSeqs(lastid + 1, seq);
173 alignPanel.paintAlignment(false, false);
177 * Response to the mouse wheel by scrolling the alignment panel.
180 public void mouseWheelMoved(MouseWheelEvent e)
183 double wheelRotation = e.getPreciseWheelRotation();
184 if (wheelRotation > 0)
188 av.getRanges().scrollRight(true);
192 av.getRanges().scrollUp(false);
195 else if (wheelRotation < 0)
199 av.getRanges().scrollRight(false);
203 av.getRanges().scrollUp(true);
209 * Handle a mouse click event. Currently only responds to a double-click. The
210 * action is to try to open a browser window at a URL that searches for the
211 * selected sequence id. The search URL is configured in Preferences |
212 * Connections | URL link from Sequence ID. For example:
214 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
219 public void mouseClicked(MouseEvent e)
222 * Ignore single click. Ignore 'left' click followed by 'right' click (user
223 * selects a row then its pop-up menu).
225 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
227 // reinstate isRightMouseButton check to ignore mouse-related popup events
228 // note - this does nothing on default MacBookPro force-trackpad config!
232 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
233 int seq = pos.seqIndex;
234 if (pos.isOverAnnotation() || seq < 0)
239 String id = av.getAlignment().getSequenceAt(seq).getName();
240 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
244 jalview.util.BrowserLauncher.openURL(url);
245 } catch (Exception ex)
247 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
248 MessageManager.getString("label.web_browser_not_found_unix"),
249 MessageManager.getString("label.web_browser_not_found"),
250 JvOptionPane.WARNING_MESSAGE);
251 ex.printStackTrace();
262 public void mouseEntered(MouseEvent e)
264 if (scrollThread != null)
266 scrollThread.stopScrolling();
277 public void mouseExited(MouseEvent e)
279 if (av.getWrapAlignment())
284 if (mouseDragging && (e.getY() < 0)
285 && (av.getRanges().getStartSeq() > 0))
287 scrollThread = new ScrollThread(true);
290 if (mouseDragging && (e.getY() >= getHeight())
291 && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
293 scrollThread = new ScrollThread(false);
298 * Respond to a mouse press. Does nothing for (left) double-click as this is
299 * handled by mouseClicked().
301 * Right mouse down - construct and show context menu.
303 * Ctrl-down or Shift-down - add to or expand current selection group if there
306 * Mouse down - select this sequence.
311 public void mousePressed(MouseEvent e)
313 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
318 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
320 if (e.isPopupTrigger()) // Mac reports this in mousePressed
322 showPopupMenu(e, pos);
327 * defer right-mouse click handling to mouseReleased on Windows
328 * (where isPopupTrigger() will answer true)
329 * NB isRightMouseButton is also true for Cmd-click on Mac
331 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
336 if ((av.getSelectionGroup() == null)
337 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
338 && av.getSelectionGroup() != null))
340 av.setSelectionGroup(new SequenceGroup());
341 av.getSelectionGroup().setStartRes(0);
342 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
345 if (e.isShiftDown() && (lastid != -1))
347 selectSeqs(lastid, pos.seqIndex);
351 selectSeq(pos.seqIndex);
354 av.isSelectionGroupChanged(true);
356 alignPanel.paintAlignment(false, false);
360 * Build and show the popup-menu at the right-click mouse position
364 void showPopupMenu(MouseEvent e, MousePos pos)
366 if (pos.isOverAnnotation())
368 showAnnotationMenu(e, pos);
372 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
375 * build a new links menu based on the current links
376 * and any non-positional features
378 List<SequenceFeature> features = null;
381 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
382 features = sq.getFeatures().getNonPositionalFeatures();
383 for (SequenceFeature sf : features)
385 if (sf.links != null)
387 nlinks.addAll(sf.links);
392 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
393 Preferences.getGroupURLLinks());
394 pop.show(this, e.getX(), e.getY());
398 * On right mouse click on a Consensus annotation label, shows a limited popup
399 * menu, with options to configure the consensus calculation and rendering.
403 * @see AnnotationLabels#showPopupMenu(MouseEvent)
405 void showAnnotationMenu(MouseEvent e, MousePos pos)
407 if (pos.annotationIndex == -1)
411 AlignmentAnnotation[] anns = this.av.getAlignment()
412 .getAlignmentAnnotation();
413 if (anns == null || pos.annotationIndex >= anns.length)
417 AlignmentAnnotation ann = anns[pos.annotationIndex];
418 if (!ann.label.contains(AutoAnnotation.CONSENSUS.label))
423 JPopupMenu pop = new JPopupMenu(
424 MessageManager.getString("label.annotations"));
425 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
426 pop.show(this, e.getX(), e.getY());
430 * Toggle whether the sequence is part of the current selection group.
434 void selectSeq(int seq)
438 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
439 av.getSelectionGroup().addOrRemove(pickedSeq, false);
443 * Add contiguous rows of the alignment to the current selection group. Does
444 * nothing if there is no selection group.
449 void selectSeqs(int start, int end)
451 if (av.getSelectionGroup() == null)
456 if (end >= av.getAlignment().getHeight())
458 end = av.getAlignment().getHeight() - 1;
471 for (int i = start; i <= end; i++)
473 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
479 * Respond to mouse released. Refreshes the display and triggers broadcast of
480 * the new selection group to any listeners.
485 public void mouseReleased(MouseEvent e)
487 if (scrollThread != null)
489 scrollThread.stopScrolling();
491 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
493 mouseDragging = false;
494 PaintRefresher.Refresh(this, av.getSequenceSetId());
495 // always send selection message when mouse is released
498 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
500 showPopupMenu(e, pos);
505 * Highlight sequence ids that match the given list, and if necessary scroll
506 * to the start sequence of the list.
510 public void highlightSearchResults(List<SequenceI> list)
512 getIdCanvas().setHighlighted(list);
514 if (list == null || list.isEmpty())
519 int index = av.getAlignment().findIndex(list.get(0));
521 // do we need to scroll the panel?
522 if ((av.getRanges().getStartSeq() > index)
523 || (av.getRanges().getEndSeq() < index))
525 av.getRanges().setStartSeq(index);
529 public IdCanvas getIdCanvas()
534 public void setIdCanvas(IdCanvas idCanvas)
536 this.idCanvas = idCanvas;
540 * Performs scrolling of the visible alignment up or down, adding newly
541 * visible sequences to the current selection
543 class ScrollThread extends Thread
545 private boolean running = false;
550 * Constructor for a thread that scrolls either up or down
554 public ScrollThread(boolean up)
557 setName("IdPanel$ScrollThread$" + String.valueOf(up));
562 * Sets a flag to stop the scrolling
564 public void stopScrolling()
570 * Scrolls the alignment either up or down, one row at a time, adding newly
571 * visible sequences to the current selection. Speed is limited to a maximum
572 * of ten rows per second. The thread exits when the end of the alignment is
573 * reached or a flag is set to stop it.
582 ViewportRanges ranges = IdPanel.this.av.getRanges();
583 if (ranges.scrollUp(up))
585 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
586 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
587 IdPanel.this.selectSeqs(fromSeq, toSeq);
594 * scroll did nothing - reached limit of visible alignment
599 alignPanel.paintAlignment(false, false);
604 } catch (Exception ex)