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 annotation = av.getAlignment()
114 .getAlignmentAnnotation()[pos.annotationIndex];
115 setToolTipText(AnnotationLabels.getTooltip(annotation));
116 alignPanel.alignFrame.setStatus(annotation.label);
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 tip.append(sequence.getDisplayId(true)).append(" ");
126 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
127 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
128 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
130 StringBuilder text = new StringBuilder();
131 text.append("Sequence ").append(String.valueOf(seq + 1))
133 .append(sequence.getName());
134 alignPanel.alignFrame.setStatus(text.toString());
140 * Responds to a mouse drag by selecting the sequences under the dragged
146 public void mouseDragged(MouseEvent e)
148 mouseDragging = true;
150 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
151 if (pos.isOverAnnotation())
153 // mouse is over annotation label in wrapped mode
157 int seq = Math.max(0, pos.seqIndex);
161 selectSeqs(lastid - 1, seq);
163 else if (seq > lastid)
165 selectSeqs(lastid + 1, seq);
169 alignPanel.paintAlignment(false, false);
173 * Response to the mouse wheel by scrolling the alignment panel.
176 public void mouseWheelMoved(MouseWheelEvent e)
179 double wheelRotation = e.getPreciseWheelRotation();
180 if (wheelRotation > 0)
184 av.getRanges().scrollRight(true);
188 av.getRanges().scrollUp(false);
191 else if (wheelRotation < 0)
195 av.getRanges().scrollRight(false);
199 av.getRanges().scrollUp(true);
205 * Handle a mouse click event. Currently only responds to a double-click. The
206 * action is to try to open a browser window at a URL that searches for the
207 * selected sequence id. The search URL is configured in Preferences |
208 * Connections | URL link from Sequence ID. For example:
210 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
215 public void mouseClicked(MouseEvent e)
218 * Ignore single click. Ignore 'left' click followed by 'right' click (user
219 * selects a row then its pop-up menu).
221 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
223 // reinstate isRightMouseButton check to ignore mouse-related popup events
224 // note - this does nothing on default MacBookPro force-trackpad config!
228 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
229 int seq = pos.seqIndex;
230 if (pos.isOverAnnotation() || seq < 0)
235 String id = av.getAlignment().getSequenceAt(seq).getName();
236 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
240 jalview.util.BrowserLauncher.openURL(url);
241 } catch (Exception ex)
243 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
244 MessageManager.getString("label.web_browser_not_found_unix"),
245 MessageManager.getString("label.web_browser_not_found"),
246 JvOptionPane.WARNING_MESSAGE);
247 ex.printStackTrace();
258 public void mouseEntered(MouseEvent e)
260 if (scrollThread != null)
262 scrollThread.running = false;
273 public void mouseExited(MouseEvent e)
275 if (av.getWrapAlignment())
280 if (mouseDragging && (e.getY() < 0)
281 && (av.getRanges().getStartSeq() > 0))
283 scrollThread = new ScrollThread(true);
286 if (mouseDragging && (e.getY() >= getHeight())
287 && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
289 scrollThread = new ScrollThread(false);
294 * Respond to a mouse press. Does nothing for (left) double-click as this is
295 * handled by mouseClicked().
297 * Right mouse down - construct and show context menu.
299 * Ctrl-down or Shift-down - add to or expand current selection group if there
302 * Mouse down - select this sequence.
307 public void mousePressed(MouseEvent e)
309 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
314 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
316 if (e.isPopupTrigger()) // Mac reports this in mousePressed
318 showPopupMenu(e, pos);
323 * defer right-mouse click handling to mouseReleased on Windows
324 * (where isPopupTrigger() will answer true)
325 * NB isRightMouseButton is also true for Cmd-click on Mac
327 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
332 if ((av.getSelectionGroup() == null)
333 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
334 && av.getSelectionGroup() != null))
336 av.setSelectionGroup(new SequenceGroup());
337 av.getSelectionGroup().setStartRes(0);
338 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
341 if (e.isShiftDown() && (lastid != -1))
343 selectSeqs(lastid, pos.seqIndex);
347 selectSeq(pos.seqIndex);
350 av.isSelectionGroupChanged(true);
352 alignPanel.paintAlignment(false, false);
356 * Build and show the popup-menu at the right-click mouse position
360 void showPopupMenu(MouseEvent e, MousePos pos)
362 if (pos.isOverAnnotation())
364 showAnnotationMenu(e, pos);
368 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
371 * build a new links menu based on the current links
372 * and any non-positional features
374 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
375 List<SequenceFeature> features = sq.getFeatures().getNonPositionalFeatures();
376 for (SequenceFeature sf : features)
378 if (sf.links != null)
380 nlinks.addAll(sf.links);
384 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
385 Preferences.getGroupURLLinks());
386 pop.show(this, e.getX(), e.getY());
390 * On right mouse click on a Consensus annotation label, shows a limited popup
391 * menu, with options to configure the consensus calculation and rendering.
395 * @see AnnotationLabels#showPopupMenu(MouseEvent)
397 void showAnnotationMenu(MouseEvent e, MousePos pos)
399 if (pos.annotationIndex == -1)
403 AlignmentAnnotation[] anns = this.av.getAlignment()
404 .getAlignmentAnnotation();
405 if (anns == null || pos.annotationIndex >= anns.length)
409 AlignmentAnnotation ann = anns[pos.annotationIndex];
410 if (!ann.label.contains("Consensus"))
415 JPopupMenu pop = new JPopupMenu(
416 MessageManager.getString("label.annotations"));
417 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
418 pop.show(this, e.getX(), e.getY());
422 * Toggle whether the sequence is part of the current selection group.
426 void selectSeq(int seq)
430 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
431 av.getSelectionGroup().addOrRemove(pickedSeq, true);
435 * Add contiguous rows of the alignment to the current selection group. Does
436 * nothing if there is no selection group.
441 void selectSeqs(int start, int end)
443 if (av.getSelectionGroup() == null)
448 if (end >= av.getAlignment().getHeight())
450 end = av.getAlignment().getHeight() - 1;
463 for (int i = start; i <= end; i++)
465 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
471 * Respond to mouse released. Refreshes the display and triggers broadcast of
472 * the new selection group to any listeners.
477 public void mouseReleased(MouseEvent e)
479 if (scrollThread != null)
481 scrollThread.running = false;
483 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
485 mouseDragging = false;
486 PaintRefresher.Refresh(this, av.getSequenceSetId());
487 // always send selection message when mouse is released
490 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
492 showPopupMenu(e, pos);
497 * Highlight sequence ids that match the given list, and if necessary scroll
498 * to the start sequence of the list.
502 public void highlightSearchResults(List<SequenceI> list)
504 getIdCanvas().setHighlighted(list);
511 int index = av.getAlignment().findIndex(list.get(0));
513 // do we need to scroll the panel?
514 if ((av.getRanges().getStartSeq() > index)
515 || (av.getRanges().getEndSeq() < index))
517 av.getRanges().setStartSeq(index);
521 public IdCanvas getIdCanvas()
526 public void setIdCanvas(IdCanvas idCanvas)
528 this.idCanvas = idCanvas;
531 // this class allows scrolling off the bottom of the visible alignment
532 class ScrollThread extends Thread
534 boolean running = false;
538 public ScrollThread(boolean up)
544 public void stopScrolling()
556 if (av.getRanges().scrollUp(up))
558 // scroll was ok, so add new sequence to selection
559 int seq = av.getRanges().getStartSeq();
563 seq = av.getRanges().getEndSeq();
568 selectSeqs(lastid - 1, seq);
570 else if (seq > lastid)
572 selectSeqs(lastid + 1, seq);
582 alignPanel.paintAlignment(false, false);
587 } catch (Exception ex)