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;
34 import java.awt.BorderLayout;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
38 import java.awt.event.MouseWheelEvent;
39 import java.awt.event.MouseWheelListener;
40 import java.util.List;
42 import javax.swing.JPanel;
43 import javax.swing.JPopupMenu;
44 import javax.swing.SwingUtilities;
45 import javax.swing.ToolTipManager;
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;
72 boolean mouseDragging = false;
74 private final SequenceAnnotationReport seqAnnotReport;
77 * Creates a new IdPanel object.
82 public IdPanel(AlignViewport av, AlignmentPanel parent)
86 setIdCanvas(new IdCanvas(av));
87 linkImageURL = getClass().getResource("/images/link.gif").toString();
88 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
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();
260 public void mouseEntered(MouseEvent e)
262 if (scrollThread != null)
264 scrollThread.running = false;
275 public void mouseExited(MouseEvent e)
277 if (av.getWrapAlignment())
282 if (mouseDragging && (e.getY() < 0)
283 && (av.getRanges().getStartSeq() > 0))
285 scrollThread = new ScrollThread(true);
288 if (mouseDragging && (e.getY() >= getHeight())
289 && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
291 scrollThread = new ScrollThread(false);
296 * Respond to a mouse press. Does nothing for (left) double-click as this is
297 * handled by mouseClicked().
299 * Right mouse down - construct and show context menu.
301 * Ctrl-down or Shift-down - add to or expand current selection group if there
304 * Mouse down - select this sequence.
309 public void mousePressed(MouseEvent e)
311 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
316 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
318 if (e.isPopupTrigger()) // Mac reports this in mousePressed
320 showPopupMenu(e, pos);
325 * defer right-mouse click handling to mouseReleased on Windows
326 * (where isPopupTrigger() will answer true)
327 * NB isRightMouseButton is also true for Cmd-click on Mac
329 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
334 if ((av.getSelectionGroup() == null)
335 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
336 && av.getSelectionGroup() != null))
338 av.setSelectionGroup(new SequenceGroup());
339 av.getSelectionGroup().setStartRes(0);
340 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
343 if (e.isShiftDown() && (lastid != -1))
345 selectSeqs(lastid, pos.seqIndex);
349 selectSeq(pos.seqIndex);
352 av.isSelectionGroupChanged(true);
354 alignPanel.paintAlignment(false, false);
358 * Build and show the popup-menu at the right-click mouse position
362 void showPopupMenu(MouseEvent e, MousePos pos)
364 if (pos.isOverAnnotation())
366 showAnnotationMenu(e, pos);
370 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
373 * build a new links menu based on the current links
374 * and any non-positional features
376 List<SequenceFeature> features = null;
379 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
380 features = sq.getFeatures().getNonPositionalFeatures();
381 for (SequenceFeature sf : features)
383 if (sf.links != null)
385 nlinks.addAll(sf.links);
390 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
391 Preferences.getGroupURLLinks());
392 pop.show(this, e.getX(), e.getY());
396 * On right mouse click on a Consensus annotation label, shows a limited popup
397 * menu, with options to configure the consensus calculation and rendering.
401 * @see AnnotationLabels#showPopupMenu(MouseEvent)
403 void showAnnotationMenu(MouseEvent e, MousePos pos)
405 if (pos.annotationIndex == -1)
409 AlignmentAnnotation[] anns = this.av.getAlignment()
410 .getAlignmentAnnotation();
411 if (anns == null || pos.annotationIndex >= anns.length)
415 AlignmentAnnotation ann = anns[pos.annotationIndex];
416 if (!ann.label.contains("Consensus"))
421 JPopupMenu pop = new JPopupMenu(
422 MessageManager.getString("label.annotations"));
423 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
424 pop.show(this, e.getX(), e.getY());
428 * Toggle whether the sequence is part of the current selection group.
432 void selectSeq(int seq)
436 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
437 av.getSelectionGroup().addOrRemove(pickedSeq, false);
441 * Add contiguous rows of the alignment to the current selection group. Does
442 * nothing if there is no selection group.
447 void selectSeqs(int start, int end)
449 if (av.getSelectionGroup() == null)
454 if (end >= av.getAlignment().getHeight())
456 end = av.getAlignment().getHeight() - 1;
469 for (int i = start; i <= end; i++)
471 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
477 * Respond to mouse released. Refreshes the display and triggers broadcast of
478 * the new selection group to any listeners.
483 public void mouseReleased(MouseEvent e)
485 if (scrollThread != null)
487 scrollThread.running = false;
489 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
491 mouseDragging = false;
492 PaintRefresher.Refresh(this, av.getSequenceSetId());
493 // always send selection message when mouse is released
496 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
498 showPopupMenu(e, pos);
503 * Highlight sequence ids that match the given list, and if necessary scroll
504 * to the start sequence of the list.
508 public void highlightSearchResults(List<SequenceI> list)
510 getIdCanvas().setHighlighted(list);
517 int index = av.getAlignment().findIndex(list.get(0));
519 // do we need to scroll the panel?
520 if ((av.getRanges().getStartSeq() > index)
521 || (av.getRanges().getEndSeq() < index))
523 av.getRanges().setStartSeq(index);
527 public IdCanvas getIdCanvas()
532 public void setIdCanvas(IdCanvas idCanvas)
534 this.idCanvas = idCanvas;
537 // this class allows scrolling off the bottom of the visible alignment
538 class ScrollThread extends Thread
540 boolean running = false;
544 public ScrollThread(boolean up)
550 public void stopScrolling()
562 if (av.getRanges().scrollUp(up))
564 // scroll was ok, so add new sequence to selection
565 int seq = av.getRanges().getStartSeq();
569 seq = av.getRanges().getEndSeq();
574 selectSeqs(lastid - 1, seq);
576 else if (seq > lastid)
578 selectSeqs(lastid + 1, seq);
588 alignPanel.paintAlignment(false, false);
593 } catch (Exception ex)