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.Cache;
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.BrowserLauncher;
30 import jalview.util.MessageManager;
31 import jalview.util.UrlLink;
32 import jalview.viewmodel.AlignmentViewport;
34 import java.awt.BorderLayout;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
38 import java.awt.event.MouseWheelEvent;
39 import java.awt.event.MouseWheelListener;
40 import java.util.List;
41 import java.util.Vector;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.SwingUtilities;
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 implements MouseListener,
56 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 StringBuffer tip = new StringBuffer(64);
115 .createSequenceAnnotationReport(tip, sequence,
116 av.isShowDBRefs(), av.isShowNPFeats(),
117 sp.seqCanvas.fr.getMinMax());
118 setToolTipText("<html>" + sequence.getDisplayId(true) + " "
119 + tip.toString() + "</html>");
124 * Responds to a mouse drag by selecting the sequences under the dragged
130 public void mouseDragged(MouseEvent e)
132 mouseDragging = true;
134 int seq = Math.max(0, alignPanel.getSeqPanel().findSeq(e));
138 selectSeqs(lastid - 1, seq);
140 else if (seq > lastid)
142 selectSeqs(lastid + 1, seq);
146 alignPanel.paintAlignment(true);
150 * Response to the mouse wheel by scrolling the alignment panel.
153 public void mouseWheelMoved(MouseWheelEvent e)
156 if (e.getWheelRotation() > 0)
160 alignPanel.scrollRight(true);
164 alignPanel.scrollUp(false);
171 alignPanel.scrollRight(false);
175 alignPanel.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))
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 UrlLink urlLink = null;
220 urlLink = new UrlLink(url);
221 } catch (Exception foo)
223 Cache.log.error("Exception for URLLink '" + url + "'",
229 if (!urlLink.isValid())
231 Cache.log.error(urlLink.getInvalidMessage());
236 String urls[] = urlLink.makeUrls(id, true);
237 if (urls == null || urls[0] == null || urls[0].length() < 4)
242 // just take first URL made from regex
247 BrowserLauncher.openURL(url);
248 } catch (Exception ex)
251 .showInternalMessageDialog(
253 MessageManager.getString("label.web_browser_not_found_unix"),
254 MessageManager.getString("label.web_browser_not_found"), JOptionPane.WARNING_MESSAGE);
255 ex.printStackTrace();
266 public void mouseEntered(MouseEvent e)
268 if (scrollThread != null)
270 scrollThread.running = false;
281 public void mouseExited(MouseEvent e)
283 if (av.getWrapAlignment())
288 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
290 scrollThread = new ScrollThread(true);
293 if (mouseDragging && (e.getY() >= getHeight())
294 && (av.getAlignment().getHeight() > av.getEndSeq()))
296 scrollThread = new ScrollThread(false);
301 * Respond to a mouse press. Does nothing for (left) double-click as this is
302 * handled by mouseClicked().
304 * Right mouse down - construct and show context menu.
306 * Ctrl-down or Shift-down - add to or expand current selection group if there
309 * Mouse down - select this sequence.
314 public void mousePressed(MouseEvent e)
316 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
321 int seq = alignPanel.getSeqPanel().findSeq(e);
323 if (SwingUtilities.isRightMouseButton(e))
325 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
326 // build a new links menu based on the current links + any non-positional
328 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
329 SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
330 for (int sl = 0; sf != null && sl < sf.length; sl++)
332 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
334 if (sf[sl].links != null && sf[sl].links.size() > 0)
336 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
338 nlinks.addElement(sf[sl].links.elementAt(l));
344 PopupMenu pop = new PopupMenu(alignPanel, sq,
345 nlinks, new Vector(Preferences.getGroupURLLinks()));
346 pop.show(this, e.getX(), e.getY());
351 if ((av.getSelectionGroup() == null)
352 || ((!e.isControlDown() && !e.isShiftDown()) && av
353 .getSelectionGroup() != null))
355 av.setSelectionGroup(new SequenceGroup());
356 av.getSelectionGroup().setStartRes(0);
357 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
360 if (e.isShiftDown() && (lastid != -1))
362 selectSeqs(lastid, seq);
368 // TODO is this addition ok here?
369 av.isSelectionGroupChanged(true);
371 alignPanel.paintAlignment(true);
375 * Toggle whether the sequence is part of the current selection group.
379 void selectSeq(int seq)
383 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
384 av.getSelectionGroup().addOrRemove(pickedSeq, true);
388 * Add contiguous rows of the alignment to the current selection group. Does
389 * nothing if there is no selection group.
394 void selectSeqs(int start, int end)
396 if (av.getSelectionGroup() == null)
401 if (end >= av.getAlignment().getHeight())
403 end = av.getAlignment().getHeight() - 1;
416 for (int i = start; i <= end; i++)
418 av.getSelectionGroup().addSequence(
419 av.getAlignment().getSequenceAt(i), i == end);
424 * Respond to mouse released. Refreshes the display and triggers broadcast of
425 * the new selection group to any listeners.
430 public void mouseReleased(MouseEvent e)
432 if (scrollThread != null)
434 scrollThread.running = false;
437 mouseDragging = false;
438 PaintRefresher.Refresh(this, av.getSequenceSetId());
439 // always send selection message when mouse is released
444 * Highlight sequence ids that match the given list, and if necessary scroll
445 * to the start sequence of the list.
449 public void highlightSearchResults(List<SequenceI> list)
451 getIdCanvas().setHighlighted(list);
458 int index = av.getAlignment().findIndex(list.get(0));
460 // do we need to scroll the panel?
461 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
463 alignPanel.setScrollValues(av.getStartRes(), index);
467 public IdCanvas getIdCanvas()
472 public void setIdCanvas(IdCanvas idCanvas)
474 this.idCanvas = idCanvas;
477 // this class allows scrolling off the bottom of the visible alignment
478 class ScrollThread extends Thread
480 boolean running = false;
484 public ScrollThread(boolean up)
490 public void stopScrolling()
502 if (alignPanel.scrollUp(up))
504 // scroll was ok, so add new sequence to selection
505 int seq = av.getStartSeq();
509 seq = av.getEndSeq();
514 selectSeqs(lastid - 1, seq);
516 else if (seq > lastid)
518 selectSeqs(lastid + 1, seq);
528 alignPanel.paintAlignment(false);
533 } catch (Exception ex)