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.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.SeqPanel.MousePos;
28 import jalview.io.SequenceAnnotationReport;
29 import jalview.util.MessageManager;
30 import jalview.util.Platform;
31 import jalview.viewmodel.AlignmentViewport;
32 import jalview.viewmodel.ViewportRanges;
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.stopScrolling();
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 PopupMenu pop = new PopupMenu(alignPanel, sq,
374 Preferences.getGroupURLLinks());
375 pop.show(this, e.getX(), e.getY());
380 * On right mouse click on a Consensus annotation label, shows a limited popup
381 * menu, with options to configure the consensus calculation and rendering.
385 * @see AnnotationLabels#showPopupMenu(MouseEvent)
387 void showAnnotationMenu(MouseEvent e, MousePos pos)
389 if (pos.annotationIndex == -1)
393 AlignmentAnnotation[] anns = this.av.getAlignment()
394 .getAlignmentAnnotation();
395 if (anns == null || pos.annotationIndex >= anns.length)
399 AlignmentAnnotation ann = anns[pos.annotationIndex];
400 if (!ann.label.contains("Consensus"))
405 JPopupMenu pop = new JPopupMenu(
406 MessageManager.getString("label.annotations"));
407 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
408 pop.show(this, e.getX(), e.getY());
412 * Toggle whether the sequence is part of the current selection group.
416 void selectSeq(int seq)
420 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
421 av.getSelectionGroup().addOrRemove(pickedSeq, false);
425 * Add contiguous rows of the alignment to the current selection group. Does
426 * nothing if there is no selection group.
431 void selectSeqs(int start, int end)
433 if (av.getSelectionGroup() == null)
438 if (end >= av.getAlignment().getHeight())
440 end = av.getAlignment().getHeight() - 1;
453 for (int i = start; i <= end; i++)
455 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
461 * Respond to mouse released. Refreshes the display and triggers broadcast of
462 * the new selection group to any listeners.
467 public void mouseReleased(MouseEvent e)
469 if (scrollThread != null)
471 scrollThread.stopScrolling();
473 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
475 mouseDragging = false;
476 PaintRefresher.Refresh(this, av.getSequenceSetId());
477 // always send selection message when mouse is released
480 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
482 showPopupMenu(e, pos);
487 * Highlight sequence ids that match the given list, and if necessary scroll
488 * to the start sequence of the list.
492 public void highlightSearchResults(List<SequenceI> list)
494 getIdCanvas().setHighlighted(list);
496 if (list == null || list.isEmpty())
501 int index = av.getAlignment().findIndex(list.get(0));
503 // do we need to scroll the panel?
504 if ((av.getRanges().getStartSeq() > index)
505 || (av.getRanges().getEndSeq() < index))
507 av.getRanges().setStartSeq(index);
511 public IdCanvas getIdCanvas()
516 public void setIdCanvas(IdCanvas idCanvas)
518 this.idCanvas = idCanvas;
522 * Performs scrolling of the visible alignment up or down, adding newly
523 * visible sequences to the current selection
525 class ScrollThread extends Thread
527 private boolean running = false;
532 * Constructor for a thread that scrolls either up or down
536 public ScrollThread(boolean up)
539 setName("IdPanel$ScrollThread$" + String.valueOf(up));
544 * Sets a flag to stop the scrolling
546 public void stopScrolling()
552 * Scrolls the alignment either up or down, one row at a time, adding newly
553 * visible sequences to the current selection. Speed is limited to a maximum
554 * of ten rows per second. The thread exits when the end of the alignment is
555 * reached or a flag is set to stop it.
564 ViewportRanges ranges = IdPanel.this.av.getRanges();
565 if (ranges.scrollUp(up))
567 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
568 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
569 IdPanel.this.selectSeqs(fromSeq, toSeq);
576 * scroll did nothing - reached limit of visible alignment
581 alignPanel.paintAlignment(false, false);
586 } catch (Exception ex)