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.bin.Jalview;
24 import jalview.datamodel.Sequence;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
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.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.MouseEvent;
38 import java.awt.event.MouseListener;
39 import java.awt.event.MouseMotionListener;
40 import java.awt.event.MouseWheelEvent;
41 import java.awt.event.MouseWheelListener;
42 import java.util.List;
44 import javax.swing.JPanel;
45 import javax.swing.SwingUtilities;
46 import javax.swing.Timer;
47 import javax.swing.ToolTipManager;
50 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
51 * as well as highlighting ids matched by a search from the Find menu.
56 public class IdPanel extends JPanel
57 implements MouseListener, MouseMotionListener, MouseWheelListener
59 private IdCanvas idCanvas;
61 protected AlignmentViewport av;
63 protected AlignmentPanel alignPanel;
65 ScrollThread scrollThread = null;
74 boolean mouseDragging = false;
76 private final SequenceAnnotationReport seqAnnotReport;
79 * Creates a new IdPanel object.
84 public IdPanel(AlignViewport av, AlignmentPanel parent)
88 setIdCanvas(new IdCanvas(av));
89 linkImageURL = getClass().getResource("/images/link.gif").toString();
90 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
91 setLayout(new BorderLayout());
92 add(getIdCanvas(), BorderLayout.CENTER);
93 addMouseListener(this);
94 addMouseMotionListener(this);
95 addMouseWheelListener(this);
96 ToolTipManager.sharedInstance().registerComponent(this);
100 * Respond to mouse movement by constructing tooltip text for the sequence id
107 public void mouseMoved(MouseEvent e)
109 SeqPanel sp = alignPanel.getSeqPanel();
110 int seq = Math.max(0, sp.findSeq(e));
111 if (seq > -1 && seq < av.getAlignment().getHeight())
113 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
114 StringBuilder tip = new StringBuilder(64);
115 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
116 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
117 setToolTipText(JvSwingUtils.wrapTooltip(true,
118 sequence.getDisplayId(true) + " " + tip.toString()));
123 * Responds to a mouse drag by selecting the sequences under the dragged
129 public void mouseDragged(MouseEvent e)
131 mouseDragging = true;
133 int seq = Math.max(0, alignPanel.getSeqPanel().findSeq(e));
137 selectSeqs(lastid - 1, seq);
139 else if (seq > lastid)
141 selectSeqs(lastid + 1, seq);
145 alignPanel.paintAlignment(false, false);
149 * Response to the mouse wheel by scrolling the alignment panel.
152 public void mouseWheelMoved(MouseWheelEvent e)
155 double wheelRotation = e.getPreciseWheelRotation();
156 if (wheelRotation > 0)
160 av.getRanges().scrollRight(true);
164 av.getRanges().scrollUp(false);
167 else if (wheelRotation < 0)
171 av.getRanges().scrollRight(false);
175 av.getRanges().scrollUp(true);
181 * Handle a mouse click event. Currently only responds to a double-click. The
182 * action is to try to open a browser window at a URL that searches for the
183 * selected sequence id. The search URL is configured in Preferences |
184 * Connections | URL link from Sequence ID. For example:
186 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
191 public void mouseClicked(MouseEvent e)
194 * Ignore single click. Ignore 'left' click followed by 'right' click (user
195 * selects a row then its pop-up menu).
197 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
199 // reinstate isRightMouseButton check to ignore mouse-related popup events
200 // note - this does nothing on default MacBookPro force-trackpad config!
204 int seq = alignPanel.getSeqPanel().findSeq(e);
205 String id = av.getAlignment().getSequenceAt(seq).getName();
206 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
210 jalview.util.BrowserLauncher.openURL(url);
211 } catch (Exception ex)
213 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
214 MessageManager.getString("label.web_browser_not_found_unix"),
215 MessageManager.getString("label.web_browser_not_found"),
216 JvOptionPane.WARNING_MESSAGE);
217 ex.printStackTrace();
222 * On (re-)entering the panel, stop any scrolling
227 public void mouseEntered(MouseEvent e)
233 * Interrupts the scroll thread if one is running
237 if (scrollThread != null)
239 scrollThread.stopScrolling();
250 public void mouseExited(MouseEvent e)
252 if (av.getWrapAlignment())
260 * on mouse drag above or below the panel, start
261 * scrolling if there are more sequences to show
263 ViewportRanges ranges = av.getRanges();
264 if (e.getY() < 0 && ranges.getStartSeq() > 0)
266 startScrolling(true);
268 else if (e.getY() >= getHeight()
269 && ranges.getEndSeq() <= av.getAlignment().getHeight())
271 startScrolling(false);
277 * Starts scrolling either up or down
281 void startScrolling(boolean up)
283 scrollThread = new ScrollThread(up);
287 * Java - run in a new thread
289 scrollThread.start();
294 * for JalviewJS using Swing Timer
296 Timer t = new Timer(20, new ActionListener()
299 public void actionPerformed(ActionEvent e)
301 if (scrollThread != null)
303 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
304 scrollThread.scrollOnce();
308 t.addActionListener(new ActionListener()
311 public void actionPerformed(ActionEvent e)
313 if (scrollThread == null)
315 // finished and nulled itself
325 * Respond to a mouse press. Does nothing for (left) double-click as this is
326 * handled by mouseClicked().
328 * Right mouse down - construct and show context menu.
330 * Ctrl-down or Shift-down - add to or expand current selection group if there
333 * Mouse down - select this sequence.
338 public void mousePressed(MouseEvent e)
340 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
345 if (e.isPopupTrigger()) // Mac reports this in mousePressed
352 * defer right-mouse click handling to mouseReleased on Windows
353 * (where isPopupTrigger() will answer true)
354 * NB isRightMouseButton is also true for Cmd-click on Mac
356 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
361 if ((av.getSelectionGroup() == null)
362 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
363 && av.getSelectionGroup() != null))
365 av.setSelectionGroup(new SequenceGroup());
366 av.getSelectionGroup().setStartRes(0);
367 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
370 int seq = alignPanel.getSeqPanel().findSeq(e);
371 if (e.isShiftDown() && (lastid != -1))
373 selectSeqs(lastid, seq);
380 av.isSelectionGroupChanged(true);
382 alignPanel.paintAlignment(false, false);
386 * Build and show the popup-menu at the right-click mouse position
390 void showPopupMenu(MouseEvent e)
392 int seq2 = alignPanel.getSeqPanel().findSeq(e);
393 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq2);
396 * build a new links menu based on the current links
397 * and any non-positional features
399 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
400 List<SequenceFeature> features = sq.getFeatures().getNonPositionalFeatures();
401 for (SequenceFeature sf : features)
403 if (sf.links != null)
405 for (String link : sf.links)
412 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
413 Preferences.getGroupURLLinks());
414 pop.show(this, e.getX(), e.getY());
418 * Toggle whether the sequence is part of the current selection group.
422 void selectSeq(int seq)
426 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
427 av.getSelectionGroup().addOrRemove(pickedSeq, true);
431 * Add contiguous rows of the alignment to the current selection group. Does
432 * nothing if there is no selection group.
437 void selectSeqs(int start, int end)
439 if (av.getSelectionGroup() == null)
444 if (end >= av.getAlignment().getHeight())
446 end = av.getAlignment().getHeight() - 1;
459 for (int i = start; i <= end; i++)
461 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
467 * Respond to mouse released. Refreshes the display and triggers broadcast of
468 * the new selection group to any listeners.
473 public void mouseReleased(MouseEvent e)
477 mouseDragging = false;
478 PaintRefresher.Refresh(this, av.getSequenceSetId());
479 // always send selection message when mouse is released
482 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
489 * Highlight sequence ids that match the given list, and if necessary scroll
490 * to the start sequence of the list.
494 public void highlightSearchResults(List<SequenceI> list)
496 getIdCanvas().setHighlighted(list);
503 int index = av.getAlignment().findIndex(list.get(0));
505 // do we need to scroll the panel?
506 if ((av.getRanges().getStartSeq() > index)
507 || (av.getRanges().getEndSeq() < index))
509 av.getRanges().setStartSeq(index);
513 public IdCanvas getIdCanvas()
518 public void setIdCanvas(IdCanvas idCanvas)
520 this.idCanvas = idCanvas;
524 * Performs scrolling of the visible alignment up or down, adding newly
525 * visible sequences to the current selection
527 class ScrollThread extends Thread
529 private boolean running = false;
534 * Constructor for a thread that scrolls either up or down
538 public ScrollThread(boolean up)
541 setName("IdPanel$ScrollThread$" + String.valueOf(up));
546 * Sets a flag to stop the scrolling
548 public void stopScrolling()
554 * Scrolls the alignment either up or down, one row at a time, adding newly
555 * visible sequences to the current selection. Speed is limited to a maximum
556 * of ten rows per second. The thread exits when the end of the alignment is
557 * reached or a flag is set to stop it by a call to stopScrolling.
566 running = scrollOnce();
570 } catch (Exception ex)
574 IdPanel.this.scrollThread = null;
578 * Scrolls one row up or down. Answers true if a scroll could be done, false
579 * if not (top or bottom of alignment reached).
583 ViewportRanges ranges = IdPanel.this.av.getRanges();
584 if (ranges.scrollUp(up))
586 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
587 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
588 IdPanel.this.selectSeqs(fromSeq, toSeq);
590 alignPanel.paintAlignment(false, false);