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.util.UrlLink;
31 import jalview.viewmodel.AlignmentViewport;
33 import java.awt.BorderLayout;
34 import java.awt.event.MouseEvent;
35 import java.awt.event.MouseListener;
36 import java.awt.event.MouseMotionListener;
37 import java.awt.event.MouseWheelEvent;
38 import java.awt.event.MouseWheelListener;
39 import java.util.List;
40 import java.util.Vector;
42 import javax.swing.JOptionPane;
43 import javax.swing.JPanel;
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 implements MouseListener,
55 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 * Respond to mouse movement by constructing tooltip text for the sequence id
105 public void mouseMoved(MouseEvent e)
107 SeqPanel sp = alignPanel.getSeqPanel();
108 int seq = Math.max(0, sp.findSeq(e));
109 if (seq > -1 && seq < av.getAlignment().getHeight())
111 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
112 StringBuilder tip = new StringBuilder(64);
113 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
114 av.isShowDBRefs(), av.isShowNPFeats(),
115 sp.seqCanvas.fr.getMinMax());
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(true);
148 * Response to the mouse wheel by scrolling the alignment panel.
151 public void mouseWheelMoved(MouseWheelEvent e)
154 if (e.getWheelRotation() > 0)
158 alignPanel.scrollRight(true);
162 alignPanel.scrollUp(false);
169 alignPanel.scrollRight(false);
173 alignPanel.scrollUp(true);
179 * Handle a mouse click event. Currently only responds to a double-click. The
180 * action is to try to open a browser window at a URL that searches for the
181 * selected sequence id. The search URL is configured in Preferences |
182 * Connections | URL link from Sequence ID. For example:
184 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
189 public void mouseClicked(MouseEvent e)
192 * Ignore single click. Ignore 'left' click followed by 'right' click (user
193 * selects a row then its pop-up menu).
195 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
197 // reinstate isRightMouseButton check to ignore mouse-related popup events
198 // note - this does nothing on default MacBookPro force-trackpad config!
202 Vector links = Preferences.sequenceURLLinks;
203 if (links == null || links.size() < 1)
208 int seq = alignPanel.getSeqPanel().findSeq(e);
211 String id = av.getAlignment().getSequenceAt(seq).getName();
212 while (url == null && i < links.size())
214 // DEFAULT LINK IS FIRST IN THE LINK LIST
215 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
216 url = links.elementAt(i++).toString();
217 jalview.util.UrlLink urlLink = null;
220 urlLink = new UrlLink(url);
221 } catch (Exception foo)
223 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
229 if (urlLink.usesDBAccession())
231 // this URL requires an accession id, not the name of a sequence
236 if (!urlLink.isValid())
238 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
243 String urls[] = urlLink.makeUrls(id, true);
244 if (urls == null || urls[0] == null || urls[0].length() < 4)
249 // just take first URL made from regex
254 jalview.util.BrowserLauncher.openURL(url);
255 } catch (Exception ex)
257 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
258 MessageManager.getString("label.web_browser_not_found_unix"),
259 MessageManager.getString("label.web_browser_not_found"),
260 JvOptionPane.WARNING_MESSAGE);
261 ex.printStackTrace();
272 public void mouseEntered(MouseEvent e)
274 if (scrollThread != null)
276 scrollThread.running = false;
287 public void mouseExited(MouseEvent e)
289 if (av.getWrapAlignment())
294 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
296 scrollThread = new ScrollThread(true);
299 if (mouseDragging && (e.getY() >= getHeight())
300 && (av.getAlignment().getHeight() > av.getEndSeq()))
302 scrollThread = new ScrollThread(false);
307 * Respond to a mouse press. Does nothing for (left) double-click as this is
308 * handled by mouseClicked().
310 * Right mouse down - construct and show context menu.
312 * Ctrl-down or Shift-down - add to or expand current selection group if there
315 * Mouse down - select this sequence.
320 public void mousePressed(MouseEvent e)
322 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
327 if (e.isPopupTrigger()) // Mac reports this in mousePressed
334 * defer right-mouse click handling to mouseReleased on Windows
335 * (where isPopupTrigger() will answer true)
336 * NB isRightMouseButton is also true for Cmd-click on Mac
338 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
343 if ((av.getSelectionGroup() == null)
344 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown() && av
345 .getSelectionGroup() != null))
347 av.setSelectionGroup(new SequenceGroup());
348 av.getSelectionGroup().setStartRes(0);
349 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
352 int seq = alignPanel.getSeqPanel().findSeq(e);
353 if (e.isShiftDown() && (lastid != -1))
355 selectSeqs(lastid, seq);
362 av.isSelectionGroupChanged(true);
364 alignPanel.paintAlignment(true);
368 * Build and show the popup-menu at the right-click mouse position
372 void showPopupMenu(MouseEvent e)
374 int seq2 = alignPanel.getSeqPanel().findSeq(e);
375 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq2);
376 // build a new links menu based on the current links + any non-positional
378 Vector<String> nlinks = new Vector<String>(Preferences.sequenceURLLinks);
379 SequenceFeature sfs[] = sq == null ? null : sq.getSequenceFeatures();
382 for (SequenceFeature sf : sfs)
384 if (sf.begin == sf.end && sf.begin == 0)
386 if (sf.links != null && sf.links.size() > 0)
388 for (int l = 0, lSize = sf.links.size(); l < lSize; l++)
390 nlinks.addElement(sf.links.elementAt(l));
397 PopupMenu pop = new PopupMenu(alignPanel, sq, nlinks,
398 Preferences.getGroupURLLinks());
399 pop.show(this, e.getX(), e.getY());
403 * Toggle whether the sequence is part of the current selection group.
407 void selectSeq(int seq)
411 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
412 av.getSelectionGroup().addOrRemove(pickedSeq, true);
416 * Add contiguous rows of the alignment to the current selection group. Does
417 * nothing if there is no selection group.
422 void selectSeqs(int start, int end)
424 if (av.getSelectionGroup() == null)
429 if (end >= av.getAlignment().getHeight())
431 end = av.getAlignment().getHeight() - 1;
444 for (int i = start; i <= end; i++)
446 av.getSelectionGroup().addSequence(
447 av.getAlignment().getSequenceAt(i), i == end);
452 * Respond to mouse released. Refreshes the display and triggers broadcast of
453 * the new selection group to any listeners.
458 public void mouseReleased(MouseEvent e)
460 if (scrollThread != null)
462 scrollThread.running = false;
465 mouseDragging = false;
466 PaintRefresher.Refresh(this, av.getSequenceSetId());
467 // always send selection message when mouse is released
470 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
477 * Highlight sequence ids that match the given list, and if necessary scroll
478 * to the start sequence of the list.
482 public void highlightSearchResults(List<SequenceI> list)
484 getIdCanvas().setHighlighted(list);
491 int index = av.getAlignment().findIndex(list.get(0));
493 // do we need to scroll the panel?
494 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
496 alignPanel.setScrollValues(av.getStartRes(), index);
500 public IdCanvas getIdCanvas()
505 public void setIdCanvas(IdCanvas idCanvas)
507 this.idCanvas = idCanvas;
510 // this class allows scrolling off the bottom of the visible alignment
511 class ScrollThread extends Thread
513 boolean running = false;
517 public ScrollThread(boolean up)
523 public void stopScrolling()
535 if (alignPanel.scrollUp(up))
537 // scroll was ok, so add new sequence to selection
538 int seq = av.getStartSeq();
542 seq = av.getEndSeq();
547 selectSeqs(lastid - 1, seq);
549 else if (seq > lastid)
551 selectSeqs(lastid + 1, seq);
561 alignPanel.paintAlignment(false);
566 } catch (Exception ex)