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;
40 import javax.swing.JPanel;
41 import javax.swing.SwingUtilities;
42 import javax.swing.ToolTipManager;
45 * This panel hosts alignment sequence ids and responds to mouse clicks on them,
46 * as well as highlighting ids matched by a search from the Find menu.
51 public class IdPanel extends JPanel implements MouseListener,
52 MouseMotionListener, MouseWheelListener
54 private IdCanvas idCanvas;
56 protected AlignmentViewport av;
58 protected AlignmentPanel alignPanel;
60 ScrollThread scrollThread = null;
69 boolean mouseDragging = false;
71 private final SequenceAnnotationReport seqAnnotReport;
74 * Creates a new IdPanel object.
79 public IdPanel(AlignViewport av, AlignmentPanel parent)
83 setIdCanvas(new IdCanvas(av));
84 linkImageURL = getClass().getResource("/images/link.gif").toString();
85 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
86 setLayout(new BorderLayout());
87 add(getIdCanvas(), BorderLayout.CENTER);
88 addMouseListener(this);
89 addMouseMotionListener(this);
90 addMouseWheelListener(this);
91 ToolTipManager.sharedInstance().registerComponent(this);
95 * Respond to mouse movement by constructing tooltip text for the sequence id
102 public void mouseMoved(MouseEvent e)
104 SeqPanel sp = alignPanel.getSeqPanel();
105 int seq = Math.max(0, sp.findSeq(e));
106 if (seq > -1 && seq < av.getAlignment().getHeight())
108 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
109 StringBuilder tip = new StringBuilder(64);
110 seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
111 av.isShowDBRefs(), av.isShowNPFeats(),
112 sp.seqCanvas.fr.getMinMax());
113 setToolTipText(JvSwingUtils.wrapTooltip(true,
114 sequence.getDisplayId(true) + " " + tip.toString()));
119 * Responds to a mouse drag by selecting the sequences under the dragged
125 public void mouseDragged(MouseEvent e)
127 mouseDragging = true;
129 int seq = Math.max(0, alignPanel.getSeqPanel().findSeq(e));
133 selectSeqs(lastid - 1, seq);
135 else if (seq > lastid)
137 selectSeqs(lastid + 1, seq);
141 alignPanel.paintAlignment(false);
145 * Response to the mouse wheel by scrolling the alignment panel.
148 public void mouseWheelMoved(MouseWheelEvent e)
151 if (e.getWheelRotation() > 0)
155 av.getRanges().scrollRight(true);
159 av.getRanges().scrollUp(false);
166 av.getRanges().scrollRight(false);
170 av.getRanges().scrollUp(true);
176 * Handle a mouse click event. Currently only responds to a double-click. The
177 * action is to try to open a browser window at a URL that searches for the
178 * selected sequence id. The search URL is configured in Preferences |
179 * Connections | URL link from Sequence ID. For example:
181 * http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$
186 public void mouseClicked(MouseEvent e)
189 * Ignore single click. Ignore 'left' click followed by 'right' click (user
190 * selects a row then its pop-up menu).
192 if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
194 // reinstate isRightMouseButton check to ignore mouse-related popup events
195 // note - this does nothing on default MacBookPro force-trackpad config!
199 int seq = alignPanel.getSeqPanel().findSeq(e);
200 String id = av.getAlignment().getSequenceAt(seq).getName();
201 String url = Preferences.sequenceUrlLinks.getPrimaryUrl(id);
205 jalview.util.BrowserLauncher.openURL(url);
206 } catch (Exception ex)
208 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
209 MessageManager.getString("label.web_browser_not_found_unix"),
210 MessageManager.getString("label.web_browser_not_found"),
211 JvOptionPane.WARNING_MESSAGE);
212 ex.printStackTrace();
223 public void mouseEntered(MouseEvent e)
225 if (scrollThread != null)
227 scrollThread.running = false;
238 public void mouseExited(MouseEvent e)
240 if (av.getWrapAlignment())
245 if (mouseDragging && (e.getY() < 0)
246 && (av.getRanges().getStartSeq() > 0))
248 scrollThread = new ScrollThread(true);
251 if (mouseDragging && (e.getY() >= getHeight())
252 && (av.getAlignment().getHeight() > av.getRanges().getEndSeq()))
254 scrollThread = new ScrollThread(false);
259 * Respond to a mouse press. Does nothing for (left) double-click as this is
260 * handled by mouseClicked().
262 * Right mouse down - construct and show context menu.
264 * Ctrl-down or Shift-down - add to or expand current selection group if there
267 * Mouse down - select this sequence.
272 public void mousePressed(MouseEvent e)
274 if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
279 if (e.isPopupTrigger()) // Mac reports this in mousePressed
286 * defer right-mouse click handling to mouseReleased on Windows
287 * (where isPopupTrigger() will answer true)
288 * NB isRightMouseButton is also true for Cmd-click on Mac
290 if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
295 if ((av.getSelectionGroup() == null)
296 || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown() && av
297 .getSelectionGroup() != null))
299 av.setSelectionGroup(new SequenceGroup());
300 av.getSelectionGroup().setStartRes(0);
301 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
304 int seq = alignPanel.getSeqPanel().findSeq(e);
305 if (e.isShiftDown() && (lastid != -1))
307 selectSeqs(lastid, seq);
314 av.isSelectionGroupChanged(true);
316 alignPanel.paintAlignment(false);
320 * Build and show the popup-menu at the right-click mouse position
324 void showPopupMenu(MouseEvent e)
326 int seq2 = alignPanel.getSeqPanel().findSeq(e);
327 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq2);
328 // build a new links menu based on the current links + any non-positional
330 List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
331 SequenceFeature sfs[] = sq == null ? null : sq.getSequenceFeatures();
334 for (SequenceFeature sf : sfs)
336 if (sf.begin == sf.end && sf.begin == 0)
338 if (sf.links != null && sf.links.size() > 0)
340 for (int l = 0, lSize = sf.links.size(); l < lSize; l++)
342 nlinks.add(sf.links.elementAt(l));
349 PopupMenu pop = new PopupMenu(alignPanel, sq, nlinks,
350 Preferences.getGroupURLLinks());
351 pop.show(this, e.getX(), e.getY());
355 * Toggle whether the sequence is part of the current selection group.
359 void selectSeq(int seq)
363 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
364 av.getSelectionGroup().addOrRemove(pickedSeq, true);
368 * Add contiguous rows of the alignment to the current selection group. Does
369 * nothing if there is no selection group.
374 void selectSeqs(int start, int end)
376 if (av.getSelectionGroup() == null)
381 if (end >= av.getAlignment().getHeight())
383 end = av.getAlignment().getHeight() - 1;
396 for (int i = start; i <= end; i++)
398 av.getSelectionGroup().addSequence(
399 av.getAlignment().getSequenceAt(i), i == end);
404 * Respond to mouse released. Refreshes the display and triggers broadcast of
405 * the new selection group to any listeners.
410 public void mouseReleased(MouseEvent e)
412 if (scrollThread != null)
414 scrollThread.running = false;
417 mouseDragging = false;
418 PaintRefresher.Refresh(this, av.getSequenceSetId());
419 // always send selection message when mouse is released
422 if (e.isPopupTrigger()) // Windows reports this in mouseReleased
429 * Highlight sequence ids that match the given list, and if necessary scroll
430 * to the start sequence of the list.
434 public void highlightSearchResults(List<SequenceI> list)
436 getIdCanvas().setHighlighted(list);
443 int index = av.getAlignment().findIndex(list.get(0));
445 // do we need to scroll the panel?
446 if ((av.getRanges().getStartSeq() > index)
447 || (av.getRanges().getEndSeq() < index))
449 av.getRanges().setStartSeq(index);
453 public IdCanvas getIdCanvas()
458 public void setIdCanvas(IdCanvas idCanvas)
460 this.idCanvas = idCanvas;
463 // this class allows scrolling off the bottom of the visible alignment
464 class ScrollThread extends Thread
466 boolean running = false;
470 public ScrollThread(boolean up)
476 public void stopScrolling()
488 if (av.getRanges().scrollUp(up))
490 // scroll was ok, so add new sequence to selection
491 int seq = av.getRanges().getStartSeq();
495 seq = av.getRanges().getEndSeq();
500 selectSeqs(lastid - 1, seq);
502 else if (seq > lastid)
504 selectSeqs(lastid + 1, seq);
514 alignPanel.paintAlignment(false);
519 } catch (Exception ex)