2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 import java.awt.event.*;
23 import java.util.List;
24 import java.util.Vector;
28 import jalview.datamodel.*;
29 import jalview.io.SequenceAnnotationReport;
30 import jalview.util.UrlLink;
38 public class IdPanel extends JPanel implements MouseListener,
39 MouseMotionListener, MouseWheelListener
41 protected IdCanvas idCanvas;
43 protected AlignViewport av;
45 protected AlignmentPanel alignPanel;
47 ScrollThread scrollThread = null;
56 boolean mouseDragging = false;
58 private final SequenceAnnotationReport seqAnnotReport;
61 * Creates a new IdPanel object.
68 public IdPanel(AlignViewport av, AlignmentPanel parent)
72 idCanvas = new IdCanvas(av);
73 linkImageURL = getClass().getResource("/images/link.gif").toString();
74 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
75 setLayout(new BorderLayout());
76 add(idCanvas, BorderLayout.CENTER);
77 addMouseListener(this);
78 addMouseMotionListener(this);
79 addMouseWheelListener(this);
80 ToolTipManager.sharedInstance().registerComponent(this);
90 public void mouseMoved(MouseEvent e)
92 SeqPanel sp = alignPanel.seqPanel;
93 int seq = Math.max(0, sp.findSeq(e));
94 if (seq > -1 && seq < av.getAlignment().getHeight())
96 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
97 StringBuffer tip = new StringBuffer();
99 .createSequenceAnnotationReport(tip, sequence,
100 av.isShowDbRefs(), av.isShowNpFeats(),
101 sp.seqCanvas.fr.minmax);
102 setToolTipText("<html>" + sequence.getDisplayId(true) + " "
103 + tip.toString() + "</html>");
114 public void mouseDragged(MouseEvent e)
116 mouseDragging = true;
118 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
122 selectSeqs(lastid - 1, seq);
124 else if (seq > lastid)
126 selectSeqs(lastid + 1, seq);
130 alignPanel.paintAlignment(true);
134 public void mouseWheelMoved(MouseWheelEvent e)
137 if (e.getWheelRotation() > 0)
141 alignPanel.scrollRight(true);
146 alignPanel.scrollUp(false);
153 alignPanel.scrollRight(false);
157 alignPanel.scrollUp(true);
169 public void mouseClicked(MouseEvent e)
171 if (e.getClickCount() < 2)
176 java.util.Vector links = Preferences.sequenceURLLinks;
177 if (links == null || links.size() < 1)
182 int seq = alignPanel.seqPanel.findSeq(e);
185 String id = av.getAlignment().getSequenceAt(seq).getName();
186 while (url == null && i < links.size())
188 // DEFAULT LINK IS FIRST IN THE LINK LIST
189 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
190 url = links.elementAt(i++).toString();
191 jalview.util.UrlLink urlLink = null;
194 urlLink = new UrlLink(url);
195 } catch (Exception foo)
197 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
203 if (!urlLink.isValid())
205 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
210 String urls[] = urlLink.makeUrls(id, true);
211 if (urls == null || urls[0] == null || urls[0].length() < 4)
216 // just take first URL made from regex
221 jalview.util.BrowserLauncher.openURL(url);
222 } catch (Exception ex)
225 .showInternalMessageDialog(
227 "Unixers: Couldn't find default web browser."
228 + "\nAdd the full path to your browser in Preferences.",
229 "Web browser not found", JOptionPane.WARNING_MESSAGE);
230 ex.printStackTrace();
242 public void mouseEntered(MouseEvent e)
244 if (scrollThread != null)
246 scrollThread.running = false;
257 public void mouseExited(MouseEvent e)
259 if (av.getWrapAlignment())
264 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
266 scrollThread = new ScrollThread(true);
269 if (mouseDragging && (e.getY() >= getHeight())
270 && (av.getAlignment().getHeight() > av.getEndSeq()))
272 scrollThread = new ScrollThread(false);
283 public void mousePressed(MouseEvent e)
285 if (e.getClickCount() == 2)
290 int seq = alignPanel.seqPanel.findSeq(e);
292 if (javax.swing.SwingUtilities.isRightMouseButton(e))
294 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
295 // build a new links menu based on the current links + any non-positional
297 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
298 SequenceFeature sf[] = sq == null ? null : sq.getDatasetSequence()
299 .getSequenceFeatures();
300 for (int sl = 0; sf != null && sl < sf.length; sl++)
302 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
304 if (sf[sl].links != null && sf[sl].links.size() > 0)
306 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
308 nlinks.addElement(sf[sl].links.elementAt(l));
314 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
315 nlinks, new Vector(Preferences.getGroupURLLinks()));
316 pop.show(this, e.getX(), e.getY());
321 if ((av.getSelectionGroup() == null)
322 || ((!e.isControlDown() && !e.isShiftDown()) && av
323 .getSelectionGroup() != null))
325 av.setSelectionGroup(new SequenceGroup());
326 av.getSelectionGroup().setStartRes(0);
327 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
330 if (e.isShiftDown() && (lastid != -1))
332 selectSeqs(lastid, seq);
338 alignPanel.paintAlignment(true);
347 void selectSeq(int seq)
351 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
352 av.getSelectionGroup().addOrRemove(pickedSeq, true);
363 void selectSeqs(int start, int end)
365 if (av.getSelectionGroup() == null)
370 if (end >= av.getAlignment().getHeight())
372 end = av.getAlignment().getHeight() - 1;
385 for (int i = start; i <= end; i++)
387 av.getSelectionGroup().addSequence(
388 av.getAlignment().getSequenceAt(i), i == end);
399 public void mouseReleased(MouseEvent e)
401 if (scrollThread != null)
403 scrollThread.running = false;
406 mouseDragging = false;
407 PaintRefresher.Refresh(this, av.getSequenceSetId());
408 // always send selection message when mouse is released
418 public void highlightSearchResults(List<SequenceI> list)
420 idCanvas.setHighlighted(list);
427 int index = av.getAlignment().findIndex(list.get(0));
429 // do we need to scroll the panel?
430 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
432 alignPanel.setScrollValues(av.getStartRes(), index);
436 // this class allows scrolling off the bottom of the visible alignment
437 class ScrollThread extends Thread
439 boolean running = false;
443 public ScrollThread(boolean up)
449 public void stopScrolling()
461 if (alignPanel.scrollUp(up))
463 // scroll was ok, so add new sequence to selection
464 int seq = av.getStartSeq();
468 seq = av.getEndSeq();
473 selectSeqs(lastid - 1, seq);
475 else if (seq > lastid)
477 selectSeqs(lastid + 1, seq);
487 alignPanel.paintAlignment(false);
492 } catch (Exception ex)