2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.UrlLink;
31 import java.awt.BorderLayout;
32 import java.awt.event.MouseEvent;
33 import java.awt.event.MouseListener;
34 import java.awt.event.MouseMotionListener;
35 import java.awt.event.MouseWheelEvent;
36 import java.awt.event.MouseWheelListener;
37 import java.util.List;
38 import java.util.Vector;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.SwingUtilities;
43 import javax.swing.ToolTipManager;
46 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
47 * as well as highlighting ids matched by a search from the Find menu.
52 public class IdPanel extends JPanel implements MouseListener,
53 MouseMotionListener, MouseWheelListener
55 private IdCanvas idCanvas;
57 protected AlignViewport av;
59 protected AlignmentPanel alignPanel;
61 ScrollThread scrollThread = null;
70 boolean mouseDragging = false;
72 private final SequenceAnnotationReport seqAnnotReport;
75 * Creates a new IdPanel object.
80 public IdPanel(AlignViewport av, AlignmentPanel parent)
84 setIdCanvas(new IdCanvas(av));
85 linkImageURL = getClass().getResource("/images/link.gif").toString();
86 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
87 setLayout(new BorderLayout());
88 add(getIdCanvas(), BorderLayout.CENTER);
89 addMouseListener(this);
90 addMouseMotionListener(this);
91 addMouseWheelListener(this);
92 ToolTipManager.sharedInstance().registerComponent(this);
96 * Respond to mouse movement by constructing tooltip text for the sequence id
103 public void mouseMoved(MouseEvent e)
105 SeqPanel sp = alignPanel.getSeqPanel();
106 int seq = Math.max(0, sp.findSeq(e));
107 if (seq > -1 && seq < av.getAlignment().getHeight())
109 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
110 StringBuffer tip = new StringBuffer(64);
112 .createSequenceAnnotationReport(tip, sequence,
113 av.isShowDBRefs(), av.isShowNPFeats(),
114 sp.seqCanvas.fr.getMinMax());
115 setToolTipText("<html>" + sequence.getDisplayId(true) + " "
116 + tip.toString() + "</html>");
121 * Responds to a mouse drag by selecting the sequences under the dragged
127 public void mouseDragged(MouseEvent e)
129 mouseDragging = true;
131 int seq = Math.max(0, alignPanel.getSeqPanel().findSeq(e));
135 selectSeqs(lastid - 1, seq);
137 else if (seq > lastid)
139 selectSeqs(lastid + 1, seq);
143 alignPanel.paintAlignment(true);
147 * Response to the mouse wheel by scrolling the alignment panel.
150 public void mouseWheelMoved(MouseWheelEvent e)
153 if (e.getWheelRotation() > 0)
157 alignPanel.scrollRight(true);
161 alignPanel.scrollUp(false);
168 alignPanel.scrollRight(false);
172 alignPanel.scrollUp(true);
178 * Handle a mouse click event. Currently only responds to a double-click. The
179 * action is to try to open a browser window at a URL that searches for the
180 * selected sequence id. The search URL is configured in Preferences |
181 * Connections | URL link from Sequence ID. For example:
183 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
188 public void mouseClicked(MouseEvent e)
191 * Ignore single click. Ignore 'left' click followed by 'right' click (user
192 * selects a row then its pop-up menu).
194 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
199 Vector links = Preferences.sequenceURLLinks;
200 if (links == null || links.size() < 1)
205 int seq = alignPanel.getSeqPanel().findSeq(e);
208 String id = av.getAlignment().getSequenceAt(seq).getName();
209 while (url == null && i < links.size())
211 // DEFAULT LINK IS FIRST IN THE LINK LIST
212 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
213 url = links.elementAt(i++).toString();
214 jalview.util.UrlLink urlLink = null;
217 urlLink = new UrlLink(url);
218 } catch (Exception foo)
220 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
226 if (!urlLink.isValid())
228 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
233 String urls[] = urlLink.makeUrls(id, true);
234 if (urls == null || urls[0] == null || urls[0].length() < 4)
239 // just take first URL made from regex
244 jalview.util.BrowserLauncher.openURL(url);
245 } catch (Exception ex)
248 .showInternalMessageDialog(
250 MessageManager.getString("label.web_browser_not_found_unix"),
251 MessageManager.getString("label.web_browser_not_found"), JOptionPane.WARNING_MESSAGE);
252 ex.printStackTrace();
263 public void mouseEntered(MouseEvent e)
265 if (scrollThread != null)
267 scrollThread.running = false;
278 public void mouseExited(MouseEvent e)
280 if (av.getWrapAlignment())
285 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
287 scrollThread = new ScrollThread(true);
290 if (mouseDragging && (e.getY() >= getHeight())
291 && (av.getAlignment().getHeight() > av.getEndSeq()))
293 scrollThread = new ScrollThread(false);
298 * Respond to a mouse press. Does nothing for (left) double-click as this is
299 * handled by mouseClicked().
301 * Right mouse down - construct and show context menu.
303 * Ctrl-down or Shift-down - add to or expand current selection group if there
306 * Mouse down - select this sequence.
311 public void mousePressed(MouseEvent e)
313 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
318 int seq = alignPanel.getSeqPanel().findSeq(e);
320 if (SwingUtilities.isRightMouseButton(e))
322 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
323 // build a new links menu based on the current links + any non-positional
325 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
326 SequenceFeature sf[] = sq == null ? null : sq.getDatasetSequence()
327 .getSequenceFeatures();
328 for (int sl = 0; sf != null && sl < sf.length; sl++)
330 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
332 if (sf[sl].links != null && sf[sl].links.size() > 0)
334 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
336 nlinks.addElement(sf[sl].links.elementAt(l));
342 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
343 nlinks, new Vector(Preferences.getGroupURLLinks()));
344 pop.show(this, e.getX(), e.getY());
349 if ((av.getSelectionGroup() == null)
350 || ((!e.isControlDown() && !e.isShiftDown()) && av
351 .getSelectionGroup() != null))
353 av.setSelectionGroup(new SequenceGroup());
354 av.getSelectionGroup().setStartRes(0);
355 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
358 if (e.isShiftDown() && (lastid != -1))
360 selectSeqs(lastid, seq);
366 alignPanel.paintAlignment(true);
370 * Toggle whether the sequence is part of the current selection group.
374 void selectSeq(int seq)
378 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
379 av.getSelectionGroup().addOrRemove(pickedSeq, true);
383 * Add contiguous rows of the alignment to the current selection group. Does
384 * nothing if there is no selection group.
389 void selectSeqs(int start, int end)
391 if (av.getSelectionGroup() == null)
396 if (end >= av.getAlignment().getHeight())
398 end = av.getAlignment().getHeight() - 1;
411 for (int i = start; i <= end; i++)
413 av.getSelectionGroup().addSequence(
414 av.getAlignment().getSequenceAt(i), i == end);
419 * Respond to mouse released. Refreshes the display and triggers broadcast of
420 * the new selection group to any listeners.
425 public void mouseReleased(MouseEvent e)
427 if (scrollThread != null)
429 scrollThread.running = false;
432 mouseDragging = false;
433 PaintRefresher.Refresh(this, av.getSequenceSetId());
434 // always send selection message when mouse is released
439 * Highlight sequence ids that match the given list, and if necessary scroll
440 * to the start sequence of the list.
444 public void highlightSearchResults(List<SequenceI> list)
446 getIdCanvas().setHighlighted(list);
453 int index = av.getAlignment().findIndex(list.get(0));
455 // do we need to scroll the panel?
456 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
458 alignPanel.setScrollValues(av.getStartRes(), index);
462 public IdCanvas getIdCanvas()
467 public void setIdCanvas(IdCanvas idCanvas)
469 this.idCanvas = idCanvas;
472 // this class allows scrolling off the bottom of the visible alignment
473 class ScrollThread extends Thread
475 boolean running = false;
479 public ScrollThread(boolean up)
485 public void stopScrolling()
497 if (alignPanel.scrollUp(up))
499 // scroll was ok, so add new sequence to selection
500 int seq = av.getStartSeq();
504 seq = av.getEndSeq();
509 selectSeqs(lastid - 1, seq);
511 else if (seq > lastid)
513 selectSeqs(lastid + 1, seq);
523 alignPanel.paintAlignment(false);
528 } catch (Exception ex)