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;
32 import java.awt.BorderLayout;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.MouseListener;
35 import java.awt.event.MouseMotionListener;
36 import java.awt.event.MouseWheelEvent;
37 import java.awt.event.MouseWheelListener;
38 import java.util.List;
39 import java.util.Vector;
41 import javax.swing.JOptionPane;
42 import javax.swing.JPanel;
43 import javax.swing.SwingUtilities;
44 import javax.swing.ToolTipManager;
47 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
48 * as well as highlighting ids matched by a search from the Find menu.
53 public class IdPanel extends JPanel implements MouseListener,
54 MouseMotionListener, MouseWheelListener
56 private IdCanvas idCanvas;
58 protected AlignmentViewport av;
60 protected AlignmentPanel alignPanel;
62 ScrollThread scrollThread = null;
71 boolean mouseDragging = false;
73 private final SequenceAnnotationReport seqAnnotReport;
76 * Creates a new IdPanel object.
81 public IdPanel(AlignViewport av, AlignmentPanel parent)
85 setIdCanvas(new IdCanvas(av));
86 linkImageURL = getClass().getResource("/images/link.gif").toString();
87 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
88 setLayout(new BorderLayout());
89 add(getIdCanvas(), BorderLayout.CENTER);
90 addMouseListener(this);
91 addMouseMotionListener(this);
92 addMouseWheelListener(this);
93 ToolTipManager.sharedInstance().registerComponent(this);
97 * Respond to mouse movement by constructing tooltip text for the sequence id
104 public void mouseMoved(MouseEvent e)
106 SeqPanel sp = alignPanel.getSeqPanel();
107 int seq = Math.max(0, sp.findSeq(e));
108 if (seq > -1 && seq < av.getAlignment().getHeight())
110 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
111 StringBuffer tip = new StringBuffer(64);
112 seqAnnotReport.createSequenceAnnotationReport(tip, sequence,
113 av.isShowDBRefs(), av.isShowNPFeats(),
114 sp.seqCanvas.fr.getMinMax());
115 setToolTipText(JvSwingUtils.wrapTooltip(true,
116 sequence.getDisplayId(true) + " " + tip.toString()));
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))
196 // reinstate isRightMouseButton check to ignore mouse-related popup events
197 // note - this does nothing on default MacBookPro force-trackpad config!
201 int seq = alignPanel.getSeqPanel().findSeq(e);
202 String id = av.getAlignment().getSequenceAt(seq).getName();
203 String url = Preferences.sequenceUrlLinks.getDefaultUrl(id);
207 jalview.util.BrowserLauncher.openURL(url);
208 } catch (Exception ex)
210 JOptionPane.showInternalMessageDialog(Desktop.desktop,
211 MessageManager.getString("label.web_browser_not_found_unix"),
212 MessageManager.getString("label.web_browser_not_found"),
213 JOptionPane.WARNING_MESSAGE);
214 ex.printStackTrace();
225 public void mouseEntered(MouseEvent e)
227 if (scrollThread != null)
229 scrollThread.running = false;
240 public void mouseExited(MouseEvent e)
242 if (av.getWrapAlignment())
247 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
249 scrollThread = new ScrollThread(true);
252 if (mouseDragging && (e.getY() >= getHeight())
253 && (av.getAlignment().getHeight() > av.getEndSeq()))
255 scrollThread = new ScrollThread(false);
260 * Respond to a mouse press. Does nothing for (left) double-click as this is
261 * handled by mouseClicked().
263 * Right mouse down - construct and show context menu.
265 * Ctrl-down or Shift-down - add to or expand current selection group if there
268 * Mouse down - select this sequence.
273 public void mousePressed(MouseEvent e)
275 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
280 if (e.isPopupTrigger()) // Mac reports this in mousePressed
287 * defer right-mouse click handling to mouseReleased on Windows
288 * (where isPopupTrigger() will answer true)
289 * NB isRightMouseButton is also true for Cmd-click on Mac
291 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
296 if ((av.getSelectionGroup() == null)
297 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown() && av
298 .getSelectionGroup() != null))
300 av.setSelectionGroup(new SequenceGroup());
301 av.getSelectionGroup().setStartRes(0);
302 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
305 int seq = alignPanel.getSeqPanel().findSeq(e);
306 if (e.isShiftDown() && (lastid != -1))
308 selectSeqs(lastid, seq);
315 av.isSelectionGroupChanged(true);
317 alignPanel.paintAlignment(true);
321 * Build and show the popup-menu at the right-click mouse position
325 void showPopupMenu(MouseEvent e)
327 int seq2 = alignPanel.getSeqPanel().findSeq(e);
328 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq2);
329 // build a new links menu based on the current links + any non-positional
331 Vector<String> nlinks = Preferences.sequenceUrlLinks
332 .getLinksForDisplay(); // new
333 // Vector<String>(Preferences.sequenceURLLinks);
334 SequenceFeature sfs[] = sq == null ? null : sq.getSequenceFeatures();
337 for (SequenceFeature sf : sfs)
339 if (sf.begin == sf.end && sf.begin == 0)
341 if (sf.links != null && sf.links.size() > 0)
343 for (int l = 0, lSize = sf.links.size(); l < lSize; l++)
345 nlinks.addElement(sf.links.elementAt(l));
352 PopupMenu pop = new PopupMenu(alignPanel, sq, nlinks,
353 Preferences.getGroupURLLinks());
354 pop.show(this, e.getX(), e.getY());
358 * Toggle whether the sequence is part of the current selection group.
362 void selectSeq(int seq)
366 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
367 av.getSelectionGroup().addOrRemove(pickedSeq, true);
371 * Add contiguous rows of the alignment to the current selection group. Does
372 * nothing if there is no selection group.
377 void selectSeqs(int start, int end)
379 if (av.getSelectionGroup() == null)
384 if (end >= av.getAlignment().getHeight())
386 end = av.getAlignment().getHeight() - 1;
399 for (int i = start; i <= end; i++)
401 av.getSelectionGroup().addSequence(
402 av.getAlignment().getSequenceAt(i), i == end);
407 * Respond to mouse released. Refreshes the display and triggers broadcast of
408 * the new selection group to any listeners.
413 public void mouseReleased(MouseEvent e)
415 if (scrollThread != null)
417 scrollThread.running = false;
420 mouseDragging = false;
421 PaintRefresher.Refresh(this, av.getSequenceSetId());
422 // always send selection message when mouse is released
425 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
432 * Highlight sequence ids that match the given list, and if necessary scroll
433 * to the start sequence of the list.
437 public void highlightSearchResults(List<SequenceI> list)
439 getIdCanvas().setHighlighted(list);
446 int index = av.getAlignment().findIndex(list.get(0));
448 // do we need to scroll the panel?
449 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
451 alignPanel.setScrollValues(av.getStartRes(), index);
455 public IdCanvas getIdCanvas()
460 public void setIdCanvas(IdCanvas idCanvas)
462 this.idCanvas = idCanvas;
465 // this class allows scrolling off the bottom of the visible alignment
466 class ScrollThread extends Thread
468 boolean running = false;
472 public ScrollThread(boolean up)
478 public void stopScrolling()
490 if (alignPanel.scrollUp(up))
492 // scroll was ok, so add new sequence to selection
493 int seq = av.getStartSeq();
497 seq = av.getEndSeq();
502 selectSeqs(lastid - 1, seq);
504 else if (seq > lastid)
506 selectSeqs(lastid + 1, seq);
516 alignPanel.paintAlignment(false);
521 } catch (Exception ex)