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.SwingUtilities;
44 import javax.swing.ToolTipManager;
47 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
48 * as well as highlighting ids matched by a search from the Find menu.
53 public class IdPanel extends JPanel
54 implements MouseListener, MouseMotionListener, MouseWheelListener
56 private IdCanvas idCanvas;
58 protected AlignmentViewport av;
60 protected AlignmentPanel alignPanel;
62 ScrollThread scrollThread = null;
71 boolean mouseDragging = false;
73 private final SequenceAnnotationReport seqAnnotReport;
76 * Creates a new IdPanel object.
81 public IdPanel(AlignViewport av, AlignmentPanel parent)
85 setIdCanvas(new IdCanvas(av));
86 linkImageURL = getClass().getResource("/images/link.gif").toString();
87 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
88 setLayout(new BorderLayout());
89 add(getIdCanvas(), BorderLayout.CENTER);
90 addMouseListener(this);
91 addMouseMotionListener(this);
92 addMouseWheelListener(this);
93 ToolTipManager.sharedInstance().registerComponent(this);
97 * Respond to mouse movement by constructing tooltip text for the sequence id
104 public void mouseMoved(MouseEvent e)
106 SeqPanel sp = alignPanel.getSeqPanel();
107 MousePos pos = sp.findMousePosition(e);
108 if (pos.annotationIndex != -1)
111 * mouse is over an annotation label in wrapped mode
113 AlignmentAnnotation annotation = av.getAlignment()
114 .getAlignmentAnnotation()[pos.annotationIndex];
115 setToolTipText(AnnotationLabels.getTooltip(annotation));
119 int seq = Math.max(0, pos.seqIndex);
120 if (seq < av.getAlignment().getHeight())
122 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
123 StringBuilder tip = new StringBuilder(64);
124 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
125 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
126 setToolTipText(JvSwingUtils.wrapTooltip(true,
127 sequence.getDisplayId(true) + " " + tip.toString()));
133 * Responds to a mouse drag by selecting the sequences under the dragged
139 public void mouseDragged(MouseEvent e)
141 mouseDragging = true;
143 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
144 if (pos.annotationIndex != -1)
146 // mouse is over annotation label in wrapped mode
150 int seq = Math.max(0, pos.seqIndex);
154 selectSeqs(lastid - 1, seq);
156 else if (seq > lastid)
158 selectSeqs(lastid + 1, seq);
162 alignPanel.paintAlignment(false, false);
166 * Response to the mouse wheel by scrolling the alignment panel.
169 public void mouseWheelMoved(MouseWheelEvent e)
172 double wheelRotation = e.getPreciseWheelRotation();
173 if (wheelRotation > 0)
177 av.getRanges().scrollRight(true);
181 av.getRanges().scrollUp(false);
184 else if (wheelRotation < 0)
188 av.getRanges().scrollRight(false);
192 av.getRanges().scrollUp(true);
198 * Handle a mouse click event. Currently only responds to a double-click. The
199 * action is to try to open a browser window at a URL that searches for the
200 * selected sequence id. The search URL is configured in Preferences |
201 * Connections | URL link from Sequence ID. For example:
203 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
208 public void mouseClicked(MouseEvent e)
211 * Ignore single click. Ignore 'left' click followed by 'right' click (user
212 * selects a row then its pop-up menu).
214 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
216 // reinstate isRightMouseButton check to ignore mouse-related popup events
217 // note - this does nothing on default MacBookPro force-trackpad config!
221 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
222 if (pos.annotationIndex != -1)
224 // mouse is over annotation label in wrapped mode
228 int seq = pos.seqIndex;
229 String id = av.getAlignment().getSequenceAt(seq).getName();
230 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
234 jalview.util.BrowserLauncher.openURL(url);
235 } catch (Exception ex)
237 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
238 MessageManager.getString("label.web_browser_not_found_unix"),
239 MessageManager.getString("label.web_browser_not_found"),
240 JvOptionPane.WARNING_MESSAGE);
241 ex.printStackTrace();
252 public void mouseEntered(MouseEvent e)
254 if (scrollThread != null)
256 scrollThread.running = false;
267 public void mouseExited(MouseEvent e)
269 if (av.getWrapAlignment())
274 if (mouseDragging && (e.getY() < 0)
275 && (av.getRanges().getStartSeq() > 0))
277 scrollThread = new ScrollThread(true);
280 if (mouseDragging && (e.getY() >= getHeight())
281 && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
283 scrollThread = new ScrollThread(false);
288 * Respond to a mouse press. Does nothing for (left) double-click as this is
289 * handled by mouseClicked().
291 * Right mouse down - construct and show context menu.
293 * Ctrl-down or Shift-down - add to or expand current selection group if there
296 * Mouse down - select this sequence.
301 public void mousePressed(MouseEvent e)
303 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
308 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
309 if (pos.annotationIndex != -1)
311 // mouse is over an annotation label in wrapped mode
315 if (e.isPopupTrigger()) // Mac reports this in mousePressed
317 showPopupMenu(e, pos);
322 * defer right-mouse click handling to mouseReleased on Windows
323 * (where isPopupTrigger() will answer true)
324 * NB isRightMouseButton is also true for Cmd-click on Mac
326 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
331 if ((av.getSelectionGroup() == null)
332 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
333 && av.getSelectionGroup() != null))
335 av.setSelectionGroup(new SequenceGroup());
336 av.getSelectionGroup().setStartRes(0);
337 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
340 if (e.isShiftDown() && (lastid != -1))
342 selectSeqs(lastid, pos.seqIndex);
346 selectSeq(pos.seqIndex);
349 av.isSelectionGroupChanged(true);
351 alignPanel.paintAlignment(false, false);
355 * Build and show the popup-menu at the right-click mouse position
359 void showPopupMenu(MouseEvent e, MousePos pos)
361 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
364 * build a new links menu based on the current links
365 * and any non-positional features
367 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
368 List<SequenceFeature> features = sq.getFeatures().getNonPositionalFeatures();
369 for (SequenceFeature sf : features)
371 if (sf.links != null)
373 nlinks.addAll(sf.links);
377 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
378 Preferences.getGroupURLLinks());
379 pop.show(this, e.getX(), e.getY());
383 * Toggle whether the sequence is part of the current selection group.
387 void selectSeq(int seq)
391 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
392 av.getSelectionGroup().addOrRemove(pickedSeq, true);
396 * Add contiguous rows of the alignment to the current selection group. Does
397 * nothing if there is no selection group.
402 void selectSeqs(int start, int end)
404 if (av.getSelectionGroup() == null)
409 if (end >= av.getAlignment().getHeight())
411 end = av.getAlignment().getHeight() - 1;
424 for (int i = start; i <= end; i++)
426 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
432 * Respond to mouse released. Refreshes the display and triggers broadcast of
433 * the new selection group to any listeners.
438 public void mouseReleased(MouseEvent e)
440 if (scrollThread != null)
442 scrollThread.running = false;
444 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
445 if (pos.annotationIndex != -1)
447 // mouse is over an annotation label in wrapped mode
451 mouseDragging = false;
452 PaintRefresher.Refresh(this, av.getSequenceSetId());
453 // always send selection message when mouse is released
456 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
458 showPopupMenu(e, pos);
463 * Highlight sequence ids that match the given list, and if necessary scroll
464 * to the start sequence of the list.
468 public void highlightSearchResults(List<SequenceI> list)
470 getIdCanvas().setHighlighted(list);
477 int index = av.getAlignment().findIndex(list.get(0));
479 // do we need to scroll the panel?
480 if ((av.getRanges().getStartSeq() > index)
481 || (av.getRanges().getEndSeq() < index))
483 av.getRanges().setStartSeq(index);
487 public IdCanvas getIdCanvas()
492 public void setIdCanvas(IdCanvas idCanvas)
494 this.idCanvas = idCanvas;
497 // this class allows scrolling off the bottom of the visible alignment
498 class ScrollThread extends Thread
500 boolean running = false;
504 public ScrollThread(boolean up)
510 public void stopScrolling()
522 if (av.getRanges().scrollUp(up))
524 // scroll was ok, so add new sequence to selection
525 int seq = av.getRanges().getStartSeq();
529 seq = av.getRanges().getEndSeq();
534 selectSeqs(lastid - 1, seq);
536 else if (seq > lastid)
538 selectSeqs(lastid + 1, seq);
548 alignPanel.paintAlignment(false, false);
553 } catch (Exception ex)