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 * Respond to mouse movement by constructing tooltip text for the sequence id
105 public void mouseMoved(MouseEvent e)
107 SeqPanel sp = alignPanel.getSeqPanel();
108 MousePos pos = sp.findMousePosition(e);
109 if (pos.isOverAnnotation())
112 * mouse is over an annotation label in wrapped mode
114 AlignmentAnnotation annotation = av.getAlignment()
115 .getAlignmentAnnotation()[pos.annotationIndex];
116 setToolTipText(AnnotationLabels.getTooltip(annotation));
120 int seq = Math.max(0, pos.seqIndex);
121 if (seq < av.getAlignment().getHeight())
123 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
124 StringBuilder tip = new StringBuilder(64);
125 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
126 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
127 setToolTipText(JvSwingUtils.wrapTooltip(true,
128 sequence.getDisplayId(true) + " " + tip.toString()));
134 * Responds to a mouse drag by selecting the sequences under the dragged
140 public void mouseDragged(MouseEvent e)
142 mouseDragging = true;
144 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
145 if (pos.isOverAnnotation())
147 // mouse is over annotation label in wrapped mode
151 int seq = Math.max(0, pos.seqIndex);
155 selectSeqs(lastid - 1, seq);
157 else if (seq > lastid)
159 selectSeqs(lastid + 1, seq);
163 alignPanel.paintAlignment(false, false);
167 * Response to the mouse wheel by scrolling the alignment panel.
170 public void mouseWheelMoved(MouseWheelEvent e)
173 double wheelRotation = e.getPreciseWheelRotation();
174 if (wheelRotation > 0)
178 av.getRanges().scrollRight(true);
182 av.getRanges().scrollUp(false);
185 else if (wheelRotation < 0)
189 av.getRanges().scrollRight(false);
193 av.getRanges().scrollUp(true);
199 * Handle a mouse click event. Currently only responds to a double-click. The
200 * action is to try to open a browser window at a URL that searches for the
201 * selected sequence id. The search URL is configured in Preferences |
202 * Connections | URL link from Sequence ID. For example:
204 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
209 public void mouseClicked(MouseEvent e)
212 * Ignore single click. Ignore 'left' click followed by 'right' click (user
213 * selects a row then its pop-up menu).
215 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
217 // reinstate isRightMouseButton check to ignore mouse-related popup events
218 // note - this does nothing on default MacBookPro force-trackpad config!
222 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
223 if (pos.isOverAnnotation())
225 // mouse is over annotation label in wrapped mode
229 int seq = pos.seqIndex;
230 String id = av.getAlignment().getSequenceAt(seq).getName();
231 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
235 jalview.util.BrowserLauncher.openURL(url);
236 } catch (Exception ex)
238 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
239 MessageManager.getString("label.web_browser_not_found_unix"),
240 MessageManager.getString("label.web_browser_not_found"),
241 JvOptionPane.WARNING_MESSAGE);
242 ex.printStackTrace();
253 public void mouseEntered(MouseEvent e)
255 if (scrollThread != null)
257 scrollThread.running = false;
268 public void mouseExited(MouseEvent e)
270 if (av.getWrapAlignment())
275 if (mouseDragging && (e.getY() < 0)
276 && (av.getRanges().getStartSeq() > 0))
278 scrollThread = new ScrollThread(true);
281 if (mouseDragging && (e.getY() >= getHeight())
282 && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
284 scrollThread = new ScrollThread(false);
289 * Respond to a mouse press. Does nothing for (left) double-click as this is
290 * handled by mouseClicked().
292 * Right mouse down - construct and show context menu.
294 * Ctrl-down or Shift-down - add to or expand current selection group if there
297 * Mouse down - select this sequence.
302 public void mousePressed(MouseEvent e)
304 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
309 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
311 if (e.isPopupTrigger()) // Mac reports this in mousePressed
313 showPopupMenu(e, pos);
318 * defer right-mouse click handling to mouseReleased on Windows
319 * (where isPopupTrigger() will answer true)
320 * NB isRightMouseButton is also true for Cmd-click on Mac
322 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
327 if ((av.getSelectionGroup() == null)
328 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
329 && av.getSelectionGroup() != null))
331 av.setSelectionGroup(new SequenceGroup());
332 av.getSelectionGroup().setStartRes(0);
333 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
336 if (e.isShiftDown() && (lastid != -1))
338 selectSeqs(lastid, pos.seqIndex);
342 selectSeq(pos.seqIndex);
345 av.isSelectionGroupChanged(true);
347 alignPanel.paintAlignment(false, false);
351 * Build and show the popup-menu at the right-click mouse position
355 void showPopupMenu(MouseEvent e, MousePos pos)
357 if (pos.isOverAnnotation())
359 showAnnotationMenu(e, pos);
363 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
366 * build a new links menu based on the current links
367 * and any non-positional features
369 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
370 List<SequenceFeature> features = sq.getFeatures().getNonPositionalFeatures();
371 for (SequenceFeature sf : features)
373 if (sf.links != null)
375 nlinks.addAll(sf.links);
379 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
380 Preferences.getGroupURLLinks());
381 pop.show(this, e.getX(), e.getY());
385 * On right mouse click on a Consensus annotation label, shows a limited popup
386 * menu, with options to configure the consensus calculation and rendering.
390 * @see AnnotationLabels#showPopupMenu(MouseEvent)
392 void showAnnotationMenu(MouseEvent e, MousePos pos)
394 if (pos.annotationIndex == -1)
398 AlignmentAnnotation[] anns = this.av.getAlignment()
399 .getAlignmentAnnotation();
400 if (anns == null || pos.annotationIndex >= anns.length)
404 AlignmentAnnotation ann = anns[pos.annotationIndex];
405 if (!ann.label.contains("Consensus"))
410 JPopupMenu pop = new JPopupMenu(
411 MessageManager.getString("label.annotations"));
412 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
413 pop.show(this, e.getX(), e.getY());
417 * Toggle whether the sequence is part of the current selection group.
421 void selectSeq(int seq)
425 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
426 av.getSelectionGroup().addOrRemove(pickedSeq, true);
430 * Add contiguous rows of the alignment to the current selection group. Does
431 * nothing if there is no selection group.
436 void selectSeqs(int start, int end)
438 if (av.getSelectionGroup() == null)
443 if (end >= av.getAlignment().getHeight())
445 end = av.getAlignment().getHeight() - 1;
458 for (int i = start; i <= end; i++)
460 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
466 * Respond to mouse released. Refreshes the display and triggers broadcast of
467 * the new selection group to any listeners.
472 public void mouseReleased(MouseEvent e)
474 if (scrollThread != null)
476 scrollThread.running = false;
478 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
480 mouseDragging = false;
481 PaintRefresher.Refresh(this, av.getSequenceSetId());
482 // always send selection message when mouse is released
485 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
487 showPopupMenu(e, pos);
492 * Highlight sequence ids that match the given list, and if necessary scroll
493 * to the start sequence of the list.
497 public void highlightSearchResults(List<SequenceI> list)
499 getIdCanvas().setHighlighted(list);
506 int index = av.getAlignment().findIndex(list.get(0));
508 // do we need to scroll the panel?
509 if ((av.getRanges().getStartSeq() > index)
510 || (av.getRanges().getEndSeq() < index))
512 av.getRanges().setStartSeq(index);
516 public IdCanvas getIdCanvas()
521 public void setIdCanvas(IdCanvas idCanvas)
523 this.idCanvas = idCanvas;
526 // this class allows scrolling off the bottom of the visible alignment
527 class ScrollThread extends Thread
529 boolean running = false;
533 public ScrollThread(boolean up)
539 public void stopScrolling()
551 if (av.getRanges().scrollUp(up))
553 // scroll was ok, so add new sequence to selection
554 int seq = av.getRanges().getStartSeq();
558 seq = av.getRanges().getEndSeq();
563 selectSeqs(lastid - 1, seq);
565 else if (seq > lastid)
567 selectSeqs(lastid + 1, seq);
577 alignPanel.paintAlignment(false, false);
582 } catch (Exception ex)