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.Sequence;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.SequenceAnnotationReport;
28 import jalview.util.MessageManager;
29 import jalview.util.Platform;
30 import jalview.viewmodel.AlignmentViewport;
31 import jalview.viewmodel.ViewportRanges;
33 import java.awt.BorderLayout;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.awt.event.MouseMotionListener;
39 import java.awt.event.MouseWheelEvent;
40 import java.awt.event.MouseWheelListener;
41 import java.util.List;
43 import javax.swing.JPanel;
44 import javax.swing.SwingUtilities;
45 import javax.swing.Timer;
46 import javax.swing.ToolTipManager;
49 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
50 * as well as highlighting ids matched by a search from the Find menu.
55 public class IdPanel extends JPanel
56 implements MouseListener, MouseMotionListener, MouseWheelListener
58 private IdCanvas idCanvas;
60 protected AlignmentViewport av;
62 protected AlignmentPanel alignPanel;
64 ScrollThread scrollThread = null;
73 boolean mouseDragging = false;
75 private final SequenceAnnotationReport seqAnnotReport;
78 * Creates a new IdPanel object.
83 public IdPanel(AlignViewport av, AlignmentPanel parent)
87 setIdCanvas(new IdCanvas(av));
88 linkImageURL = getClass().getResource("/images/link.gif").toString();
89 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
90 setLayout(new BorderLayout());
91 add(getIdCanvas(), BorderLayout.CENTER);
92 addMouseListener(this);
93 addMouseMotionListener(this);
94 addMouseWheelListener(this);
95 ToolTipManager.sharedInstance().registerComponent(this);
99 * Respond to mouse movement by constructing tooltip text for the sequence id
106 public void mouseMoved(MouseEvent e)
108 SeqPanel sp = alignPanel.getSeqPanel();
109 int seq = Math.max(0, sp.findSeq(e));
110 if (seq > -1 && seq < av.getAlignment().getHeight())
112 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
113 StringBuilder tip = new StringBuilder(64);
114 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
115 av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
116 setToolTipText(JvSwingUtils.wrapTooltip(true,
117 sequence.getDisplayId(true) + " " + tip.toString()));
122 * Responds to a mouse drag by selecting the sequences under the dragged
128 public void mouseDragged(MouseEvent e)
130 mouseDragging = true;
132 int seq = Math.max(0, alignPanel.getSeqPanel().findSeq(e));
136 selectSeqs(lastid - 1, seq);
138 else if (seq > lastid)
140 selectSeqs(lastid + 1, seq);
144 alignPanel.paintAlignment(false, false);
148 * Response to the mouse wheel by scrolling the alignment panel.
151 public void mouseWheelMoved(MouseWheelEvent e)
154 double wheelRotation = e.getPreciseWheelRotation();
155 if (wheelRotation > 0)
159 av.getRanges().scrollRight(true);
163 av.getRanges().scrollUp(false);
166 else if (wheelRotation < 0)
170 av.getRanges().scrollRight(false);
174 av.getRanges().scrollUp(true);
180 * Handle a mouse click event. Currently only responds to a double-click. The
181 * action is to try to open a browser window at a URL that searches for the
182 * selected sequence id. The search URL is configured in Preferences |
183 * Connections | URL link from Sequence ID. For example:
185 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
190 public void mouseClicked(MouseEvent e)
193 * Ignore single click. Ignore 'left' click followed by 'right' click (user
194 * selects a row then its pop-up menu).
196 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
198 // reinstate isRightMouseButton check to ignore mouse-related popup events
199 // note - this does nothing on default MacBookPro force-trackpad config!
203 int seq = alignPanel.getSeqPanel().findSeq(e);
204 String id = av.getAlignment().getSequenceAt(seq).getName();
205 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
209 jalview.util.BrowserLauncher.openURL(url);
210 } catch (Exception ex)
212 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
213 MessageManager.getString("label.web_browser_not_found_unix"),
214 MessageManager.getString("label.web_browser_not_found"),
215 JvOptionPane.WARNING_MESSAGE);
216 ex.printStackTrace();
221 * On (re-)entering the panel, stop any scrolling
226 public void mouseEntered(MouseEvent e)
232 * Interrupts the scroll thread if one is running
236 if (scrollThread != null)
238 scrollThread.stopScrolling();
249 public void mouseExited(MouseEvent e)
251 if (av.getWrapAlignment())
259 * on mouse drag above or below the panel, start
260 * scrolling if there are more sequences to show
262 ViewportRanges ranges = av.getRanges();
263 if (e.getY() < 0 && ranges.getStartSeq() > 0)
265 startScrolling(true);
267 else if (e.getY() >= getHeight()
268 && ranges.getEndSeq() <= av.getAlignment().getHeight())
270 startScrolling(false);
276 * Starts scrolling either up or down
280 void startScrolling(boolean up)
282 scrollThread = new ScrollThread(up);
283 if (!Platform.isJS())
286 * Java - run in a new thread
288 scrollThread.start();
293 * for JalviewJS using Swing Timer
295 Timer t = new Timer(20, new ActionListener()
298 public void actionPerformed(ActionEvent e)
300 if (scrollThread != null)
302 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
303 scrollThread.scrollOnce();
307 t.addActionListener(new ActionListener()
310 public void actionPerformed(ActionEvent e)
312 if (scrollThread == null)
314 // finished and nulled itself
324 * Respond to a mouse press. Does nothing for (left) double-click as this is
325 * handled by mouseClicked().
327 * Right mouse down - construct and show context menu.
329 * Ctrl-down or Shift-down - add to or expand current selection group if there
332 * Mouse down - select this sequence.
337 public void mousePressed(MouseEvent e)
339 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
344 if (e.isPopupTrigger()) // Mac reports this in mousePressed
351 * defer right-mouse click handling to mouseReleased on Windows
352 * (where isPopupTrigger() will answer true)
353 * NB isRightMouseButton is also true for Cmd-click on Mac
355 if (Platform.isWinRightButton(e))
360 if ((av.getSelectionGroup() == null)
361 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown()
362 && av.getSelectionGroup() != null))
364 av.setSelectionGroup(new SequenceGroup());
365 av.getSelectionGroup().setStartRes(0);
366 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
369 int seq = alignPanel.getSeqPanel().findSeq(e);
370 if (e.isShiftDown() && (lastid != -1))
372 selectSeqs(lastid, seq);
379 av.isSelectionGroupChanged(true);
381 alignPanel.paintAlignment(false, false);
385 * Build and show the popup-menu at the right-click mouse position
389 void showPopupMenu(MouseEvent e)
391 int seq2 = alignPanel.getSeqPanel().findSeq(e);
392 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq2);
395 * build a new links menu based on the current links
396 * and any non-positional features
398 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
399 List<SequenceFeature> features = sq.getFeatures().getNonPositionalFeatures();
400 for (SequenceFeature sf : features)
402 if (sf.links != null)
404 for (String link : sf.links)
411 PopupMenu pop = new PopupMenu(alignPanel, sq, features,
412 Preferences.getGroupURLLinks());
413 pop.show(this, e.getX(), e.getY());
417 * Toggle whether the sequence is part of the current selection group.
421 void selectSeq(int seq)
425 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
426 av.getSelectionGroup().addOrRemove(pickedSeq, true);
430 * Add contiguous rows of the alignment to the current selection group. Does
431 * nothing if there is no selection group.
436 void selectSeqs(int start, int end)
438 if (av.getSelectionGroup() == null)
443 if (end >= av.getAlignment().getHeight())
445 end = av.getAlignment().getHeight() - 1;
458 for (int i = start; i <= end; i++)
460 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
466 * Respond to mouse released. Refreshes the display and triggers broadcast of
467 * the new selection group to any listeners.
472 public void mouseReleased(MouseEvent e)
476 mouseDragging = false;
477 PaintRefresher.Refresh(this, av.getSequenceSetId());
478 // always send selection message when mouse is released
481 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
488 * Highlight sequence ids that match the given list, and if necessary scroll
489 * to the start sequence of the list.
493 public void highlightSearchResults(List<SequenceI> list)
495 getIdCanvas().setHighlighted(list);
502 int index = av.getAlignment().findIndex(list.get(0));
504 // do we need to scroll the panel?
505 if ((av.getRanges().getStartSeq() > index)
506 || (av.getRanges().getEndSeq() < index))
508 av.getRanges().setStartSeq(index);
512 public IdCanvas getIdCanvas()
517 public void setIdCanvas(IdCanvas idCanvas)
519 this.idCanvas = idCanvas;
523 * Performs scrolling of the visible alignment up or down, adding newly
524 * visible sequences to the current selection
526 class ScrollThread extends Thread
528 private boolean running = false;
533 * Constructor for a thread that scrolls either up or down
537 public ScrollThread(boolean up)
540 setName("IdPanel$ScrollThread$" + String.valueOf(up));
545 * Sets a flag to stop the scrolling
547 public void stopScrolling()
553 * Scrolls the alignment either up or down, one row at a time, adding newly
554 * visible sequences to the current selection. Speed is limited to a maximum
555 * of ten rows per second. The thread exits when the end of the alignment is
556 * reached or a flag is set to stop it by a call to stopScrolling.
565 running = scrollOnce();
569 } catch (Exception ex)
573 IdPanel.this.scrollThread = null;
577 * Scrolls one row up or down. Answers true if a scroll could be done, false
578 * if not (top or bottom of alignment reached).
582 ViewportRanges ranges = IdPanel.this.av.getRanges();
583 if (ranges.scrollUp(up))
585 int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq();
586 int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1;
587 IdPanel.this.selectSeqs(fromSeq, toSeq);
589 alignPanel.paintAlignment(false, false);