2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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/>.
21 import java.awt.event.*;
22 import java.util.List;
23 import java.util.Vector;
27 import jalview.datamodel.*;
28 import jalview.io.SequenceAnnotationReport;
29 import jalview.util.UrlLink;
37 public class IdPanel extends JPanel implements MouseListener,
38 MouseMotionListener, MouseWheelListener
40 protected IdCanvas idCanvas;
42 protected AlignViewport av;
44 protected AlignmentPanel alignPanel;
46 ScrollThread scrollThread = null;
55 boolean mouseDragging = false;
56 private final SequenceAnnotationReport seqAnnotReport;
59 * Creates a new IdPanel object.
66 public IdPanel(AlignViewport av, AlignmentPanel parent)
70 idCanvas = new IdCanvas(av);
71 linkImageURL = getClass().getResource("/images/link.gif").toString();
72 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
73 setLayout(new BorderLayout());
74 add(idCanvas, BorderLayout.CENTER);
75 addMouseListener(this);
76 addMouseMotionListener(this);
77 addMouseWheelListener(this);
78 ToolTipManager.sharedInstance().registerComponent(this);
87 public void mouseMoved(MouseEvent e)
89 SeqPanel sp = alignPanel.seqPanel;
90 int seq = Math.max(0, sp.findSeq(e));
91 if (seq > -1 && seq < av.getAlignment().getHeight())
93 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
94 StringBuffer tip = new StringBuffer();
95 seqAnnotReport.createSequenceAnnotationReport(tip, sequence,
96 av.isShowDbRefs(), av.isShowNpFeats(),
97 sp.seqCanvas.fr.minmax);
98 setToolTipText("<html>" + sequence.getDisplayId(true) + " "
99 + tip.toString()+"</html>");
110 public void mouseDragged(MouseEvent e)
112 mouseDragging = true;
114 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
118 selectSeqs(lastid - 1, seq);
120 else if (seq > lastid)
122 selectSeqs(lastid + 1, seq);
126 alignPanel.paintAlignment(true);
130 public void mouseWheelMoved(MouseWheelEvent e)
133 if (e.getWheelRotation() > 0)
135 alignPanel.scrollUp(false);
139 alignPanel.scrollUp(true);
150 public void mouseClicked(MouseEvent e)
152 if (e.getClickCount() < 2)
157 java.util.Vector links = Preferences.sequenceURLLinks;
158 if (links == null || links.size() < 1)
163 int seq = alignPanel.seqPanel.findSeq(e);
166 String id = av.getAlignment().getSequenceAt(seq).getName();
167 while (url == null && i < links.size())
169 // DEFAULT LINK IS FIRST IN THE LINK LIST
170 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
171 url = links.elementAt(i++).toString();
172 jalview.util.UrlLink urlLink = null;
175 urlLink = new UrlLink(url);
176 } catch (Exception foo)
178 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
184 if (!urlLink.isValid())
186 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
191 String urls[] = urlLink.makeUrls(id, true);
192 if (urls == null || urls[0] == null || urls[0].length() < 4)
197 // just take first URL made from regex
202 jalview.util.BrowserLauncher.openURL(url);
203 } catch (Exception ex)
206 .showInternalMessageDialog(
208 "Unixers: Couldn't find default web browser."
209 + "\nAdd the full path to your browser in Preferences.",
210 "Web browser not found", JOptionPane.WARNING_MESSAGE);
211 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) && (av.getStartSeq() > 0))
247 scrollThread = new ScrollThread(true);
250 if (mouseDragging && (e.getY() >= getHeight())
251 && (av.getAlignment().getHeight() > av.getEndSeq()))
253 scrollThread = new ScrollThread(false);
264 public void mousePressed(MouseEvent e)
266 if (e.getClickCount() == 2)
271 int seq = alignPanel.seqPanel.findSeq(e);
273 if (javax.swing.SwingUtilities.isRightMouseButton(e))
275 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
276 // build a new links menu based on the current links + any non-positional
278 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
279 SequenceFeature sf[] = sq==null ? null : sq.getDatasetSequence().getSequenceFeatures();
280 for (int sl = 0; sf != null && sl < sf.length; sl++)
282 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
284 if (sf[sl].links != null && sf[sl].links.size() > 0)
286 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
288 nlinks.addElement(sf[sl].links.elementAt(l));
294 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
295 nlinks, new Vector(Preferences.getGroupURLLinks()));
296 pop.show(this, e.getX(), e.getY());
301 if ((av.getSelectionGroup() == null)
302 || ((!e.isControlDown() && !e.isShiftDown()) && av
303 .getSelectionGroup() != null))
305 av.setSelectionGroup(new SequenceGroup());
306 av.getSelectionGroup().setStartRes(0);
307 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
310 if (e.isShiftDown() && (lastid != -1))
312 selectSeqs(lastid, seq);
318 alignPanel.paintAlignment(true);
327 void selectSeq(int seq)
331 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
332 av.getSelectionGroup().addOrRemove(pickedSeq, true);
343 void selectSeqs(int start, int end)
345 if (av.getSelectionGroup() == null)
350 if (end >= av.getAlignment().getHeight())
352 end = av.getAlignment().getHeight() - 1;
365 for (int i = start; i <= end; i++)
367 av.getSelectionGroup().addSequence(
368 av.getAlignment().getSequenceAt(i), true);
379 public void mouseReleased(MouseEvent e)
381 if (scrollThread != null)
383 scrollThread.running = false;
386 mouseDragging = false;
387 PaintRefresher.Refresh(this, av.getSequenceSetId());
388 // always send selection message when mouse is released
398 public void highlightSearchResults(List<SequenceI> list)
400 idCanvas.setHighlighted(list);
407 int index = av.getAlignment().findIndex(list.get(0));
409 // do we need to scroll the panel?
410 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
412 alignPanel.setScrollValues(av.getStartRes(), index);
416 // this class allows scrolling off the bottom of the visible alignment
417 class ScrollThread extends Thread
419 boolean running = false;
423 public ScrollThread(boolean up)
429 public void stopScrolling()
441 if (alignPanel.scrollUp(up))
443 // scroll was ok, so add new sequence to selection
444 int seq = av.getStartSeq();
448 seq = av.getEndSeq();
453 selectSeqs(lastid - 1, seq);
455 else if (seq > lastid)
457 selectSeqs(lastid + 1, seq);
467 alignPanel.paintAlignment(false);
472 } catch (Exception ex)