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;
33 import jalview.viewmodel.ViewportRanges;
35 import java.awt.BorderLayout;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.awt.event.MouseMotionListener;
39 import java.util.List;
41 import javax.swing.JPanel;
42 import javax.swing.JPopupMenu;
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.
50 public class IdPanel extends JPanel
51 implements MouseListener, MouseMotionListener
53 private IdCanvas idCanvas;
55 protected AlignmentViewport av;
57 protected AlignmentPanel alignPanel;
59 ScrollThread scrollThread = null;
68 boolean mouseDragging = false;
70 private final SequenceAnnotationReport seqAnnotReport;
73 * Creates a new IdPanel object.
78 public IdPanel(AlignViewport av, AlignmentPanel parent)
82 setIdCanvas(new IdCanvas(av));
83 linkImageURL = getClass().getResource("/images/link.gif").toString();
84 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
85 setLayout(new BorderLayout());
86 add(getIdCanvas(), BorderLayout.CENTER);
87 addMouseListener(this);
88 addMouseMotionListener(this);
89 addMouseWheelListener(alignPanel.getSeqPanel());
90 ToolTipManager.sharedInstance().registerComponent(this);
94 * Responds to mouse movement by setting tooltip text for the sequence id
95 * under the mouse (or possibly annotation label, when in wrapped mode)
100 public void mouseMoved(MouseEvent e)
102 SeqPanel sp = alignPanel.getSeqPanel();
103 MousePos pos = sp.findMousePosition(e);
104 if (pos.isOverAnnotation())
107 * mouse is over an annotation label in wrapped mode
109 AlignmentAnnotation[] anns = av.getAlignment()
110 .getAlignmentAnnotation();
111 AlignmentAnnotation annotation = anns[pos.annotationIndex];
112 setToolTipText(AnnotationLabels.getTooltip(annotation));
113 alignPanel.alignFrame.setStatus(
114 AnnotationLabels.getStatusMessage(annotation, anns));
118 int seq = Math.max(0, pos.seqIndex);
119 if (seq < av.getAlignment().getHeight())
121 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
122 StringBuilder tip = new StringBuilder(64);
123 tip.append(sequence.getDisplayId(true)).append(" ");
124 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
125 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
126 setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
128 StringBuilder text = new StringBuilder();
129 text.append("Sequence ").append(String.valueOf(seq + 1))
131 .append(sequence.getName());
132 alignPanel.alignFrame.setStatus(text.toString());
138 * Responds to a mouse drag by selecting the sequences under the dragged
144 public void mouseDragged(MouseEvent e)
146 mouseDragging = true;
148 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
149 if (pos.isOverAnnotation())
151 // mouse is over annotation label in wrapped mode
155 int seq = Math.max(0, pos.seqIndex);
159 selectSeqs(lastid - 1, seq);
161 else if (seq > lastid)
163 selectSeqs(lastid + 1, seq);
167 alignPanel.paintAlignment(false, false);
171 * Handle a mouse click event. Currently only responds to a double-click. The
172 * action is to try to open a browser window at a URL that searches for the
173 * selected sequence id. The search URL is configured in Preferences |
174 * Connections | URL link from Sequence ID. For example:
176 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
181 public void mouseClicked(MouseEvent e)
184 * Ignore single click. Ignore 'left' click followed by 'right' click (user
185 * selects a row then its pop-up menu).
187 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
189 // reinstate isRightMouseButton check to ignore mouse-related popup events
190 // note - this does nothing on default MacBookPro force-trackpad config!
194 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
195 int seq = pos.seqIndex;
196 if (pos.isOverAnnotation() || seq < 0)
201 String id = av.getAlignment().getSequenceAt(seq).getName();
202 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
206 jalview.util.BrowserLauncher.openURL(url);
207 } catch (Exception ex)
209 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
210 MessageManager.getString("label.web_browser_not_found_unix"),
211 MessageManager.getString("label.web_browser_not_found"),
212 JvOptionPane.WARNING_MESSAGE);
213 ex.printStackTrace();
224 public void mouseEntered(MouseEvent e)
226 if (scrollThread != null)
228 scrollThread.stopScrolling();
239 public void mouseExited(MouseEvent e)
241 if (av.getWrapAlignment())
246 if (mouseDragging && (e.getY() < 0)
247 && (av.getRanges().getStartSeq() > 0))
249 scrollThread = new ScrollThread(true);
252 if (mouseDragging && (e.getY() >= getHeight())
253 && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
255 scrollThread = new ScrollThread(false);
260 * Respond to a mouse press. Does nothing for (left) double-click as this is
261 * handled by mouseClicked().
263 * Right mouse down - construct and show context menu.
265 * Ctrl-down or Shift-down - add to or expand current selection group if there
268 * Mouse down - select this sequence.
273 public void mousePressed(MouseEvent e)
275 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
280 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
282 if (e.isPopupTrigger()) // Mac reports this in mousePressed
284 showPopupMenu(e, pos);
289 * defer right-mouse click handling to mouseReleased on Windows
290 * (where isPopupTrigger() will answer true)
291 * NB isRightMouseButton is also true for Cmd-click on Mac
293 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
298 if ((av.getSelectionGroup() == null)
299 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
300 && av.getSelectionGroup() != null))
302 av.setSelectionGroup(new SequenceGroup());
303 av.getSelectionGroup().setStartRes(0);
304 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
307 if (e.isShiftDown() && (lastid != -1))
309 selectSeqs(lastid, pos.seqIndex);
313 selectSeq(pos.seqIndex);
316 av.isSelectionGroupChanged(true);
318 alignPanel.paintAlignment(false, false);
322 * Build and show the popup-menu at the right-click mouse position
326 void showPopupMenu(MouseEvent e, MousePos pos)
328 if (pos.isOverAnnotation())
330 showAnnotationMenu(e, pos);
334 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
337 * build a new links menu based on the current links
338 * and any non-positional features
340 List<SequenceFeature> features = null;
343 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
344 features = sq.getFeatures().getNonPositionalFeatures();
345 for (SequenceFeature sf : features)
347 if (sf.links != null)
349 nlinks.addAll(sf.links);
354 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
355 Preferences.getGroupURLLinks());
356 pop.show(this, e.getX(), e.getY());
360 * On right mouse click on a Consensus annotation label, shows a limited popup
361 * menu, with options to configure the consensus calculation and rendering.
365 * @see AnnotationLabels#showPopupMenu(MouseEvent)
367 void showAnnotationMenu(MouseEvent e, MousePos pos)
369 if (pos.annotationIndex == -1)
373 AlignmentAnnotation[] anns = this.av.getAlignment()
374 .getAlignmentAnnotation();
375 if (anns == null || pos.annotationIndex >= anns.length)
379 AlignmentAnnotation ann = anns[pos.annotationIndex];
380 if (!ann.label.contains("Consensus"))
385 JPopupMenu pop = new JPopupMenu(
386 MessageManager.getString("label.annotations"));
387 AnnotationLabels.addConsensusMenuOptions(this.alignPanel, ann, pop);
388 pop.show(this, e.getX(), e.getY());
392 * Toggle whether the sequence is part of the current selection group.
396 void selectSeq(int seq)
400 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
401 av.getSelectionGroup().addOrRemove(pickedSeq, false);
405 * Add contiguous rows of the alignment to the current selection group. Does
406 * nothing if there is no selection group.
411 void selectSeqs(int start, int end)
413 if (av.getSelectionGroup() == null)
418 if (end >= av.getAlignment().getHeight())
420 end = av.getAlignment().getHeight() - 1;
433 for (int i = start; i <= end; i++)
435 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
441 * Respond to mouse released. Refreshes the display and triggers broadcast of
442 * the new selection group to any listeners.
447 public void mouseReleased(MouseEvent e)
449 if (scrollThread != null)
451 scrollThread.stopScrolling();
453 MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
455 mouseDragging = false;
456 PaintRefresher.Refresh(this, av.getSequenceSetId());
457 // always send selection message when mouse is released
460 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
462 showPopupMenu(e, pos);
467 * Highlight sequence ids that match the given list, and if necessary scroll
468 * to the start sequence of the list.
472 public void highlightSearchResults(List<SequenceI> list)
474 getIdCanvas().setHighlighted(list);
476 if (list == null || list.isEmpty())
481 int index = av.getAlignment().findIndex(list.get(0));
483 // do we need to scroll the panel?
484 if ((av.getRanges().getStartSeq() > index)
485 || (av.getRanges().getEndSeq() < index))
487 av.getRanges().setStartSeq(index);
491 public IdCanvas getIdCanvas()
496 public void setIdCanvas(IdCanvas idCanvas)
498 this.idCanvas = idCanvas;
502 * Performs scrolling of the visible alignment up or down, adding newly
503 * visible sequences to the current selection
505 class ScrollThread extends Thread
507 private boolean running = false;
512 * Constructor for a thread that scrolls either up or down
516 public ScrollThread(boolean up)
519 setName("IdPanel$ScrollThread$" + String.valueOf(up));
524 * Sets a flag to stop the scrolling
526 public void stopScrolling()
532 * Scrolls the alignment either up or down, one row at a time, adding newly
533 * visible sequences to the current selection. Speed is limited to a maximum
534 * of ten rows per second. The thread exits when the end of the alignment is
535 * reached or a flag is set to stop it.
544 ViewportRanges ranges = IdPanel.this.av.getRanges();
545 if (ranges.scrollUp(up))
547 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
548 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
549 IdPanel.this.selectSeqs(fromSeq, toSeq);
556 * scroll did nothing - reached limit of visible alignment
561 alignPanel.paintAlignment(false, false);
566 } catch (Exception ex)