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.util.UrlLink;
36 public class IdPanel extends JPanel implements MouseListener,
37 MouseMotionListener, MouseWheelListener
39 protected IdCanvas idCanvas;
41 protected AlignViewport av;
43 protected AlignmentPanel alignPanel;
45 ScrollThread scrollThread = null;
54 boolean mouseDragging = false;
57 * Creates a new IdPanel object.
64 public IdPanel(AlignViewport av, AlignmentPanel parent)
68 idCanvas = new IdCanvas(av);
69 linkImageURL = getClass().getResource("/images/link.gif").toString();
70 setLayout(new BorderLayout());
71 add(idCanvas, BorderLayout.CENTER);
72 addMouseListener(this);
73 addMouseMotionListener(this);
74 addMouseWheelListener(this);
75 ToolTipManager.sharedInstance().registerComponent(this);
84 public void mouseMoved(MouseEvent e)
86 SeqPanel sp = alignPanel.seqPanel;
87 int seq = Math.max(0, sp.findSeq(e));
89 if (seq > -1 && seq < av.getAlignment().getHeight())
91 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
92 StringBuffer tip = new StringBuffer();
96 if (sequence.getDescription() != null)
98 tmp = sequence.getDescription();
99 tip.append("<br>" + tmp);
100 maxWidth = Math.max(maxWidth, tmp.length());
103 DBRefEntry[] dbrefs = sequence.getDatasetSequence().getDBRef();
104 if (av.isShowDbRefs() && dbrefs != null)
106 for (int i = 0; i < dbrefs.length; i++)
109 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
111 maxWidth = Math.max(maxWidth, tmp.length());
115 // ADD NON POSITIONAL SEQUENCE INFO
116 SequenceFeature[] features = sequence.getDatasetSequence()
117 .getSequenceFeatures();
118 SequenceFeature[] tfeat = new SequenceFeature[1];
119 if (av.isShowNpFeats() && features != null)
121 for (int i = 0; i < features.length; i++)
123 if (features[i].begin == 0 && features[i].end == 0)
125 int sz = -tip.length();
126 tfeat[0] = features[i];
127 sp.appendFeatures(tip, linkImageURL, 0, tfeat,
128 sp.seqCanvas.fr.minmax);
130 maxWidth = Math.max(maxWidth, sz);
137 tip.insert(0, "<table width=350 border=0><tr><td><i>");
138 tip.append("</i></td></tr></table>");
141 tip.append("</html>");
143 setToolTipText("<html>" + sequence.getDisplayId(true) + " "
154 public void mouseDragged(MouseEvent e)
156 mouseDragging = true;
158 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
162 selectSeqs(lastid - 1, seq);
164 else if (seq > lastid)
166 selectSeqs(lastid + 1, seq);
170 alignPanel.paintAlignment(true);
173 public void mouseWheelMoved(MouseWheelEvent e)
176 if (e.getWheelRotation() > 0)
178 alignPanel.scrollUp(false);
182 alignPanel.scrollUp(true);
192 public void mouseClicked(MouseEvent e)
194 if (e.getClickCount() < 2)
199 java.util.Vector links = Preferences.sequenceURLLinks;
200 if (links == null || links.size() < 1)
205 int seq = alignPanel.seqPanel.findSeq(e);
208 String id = av.getAlignment().getSequenceAt(seq).getName();
209 while (url == null && i < links.size())
211 // DEFAULT LINK IS FIRST IN THE LINK LIST
212 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
213 url = links.elementAt(i++).toString();
214 jalview.util.UrlLink urlLink = null;
217 urlLink = new UrlLink(url);
218 } catch (Exception foo)
220 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
226 if (!urlLink.isValid())
228 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
233 String urls[] = urlLink.makeUrls(id, true);
234 if (urls == null || urls[0] == null || urls[0].length() < 4)
239 // just take first URL made from regex
244 jalview.util.BrowserLauncher.openURL(url);
245 } catch (Exception ex)
248 .showInternalMessageDialog(
250 "Unixers: Couldn't find default web browser."
251 + "\nAdd the full path to your browser in Preferences.",
252 "Web browser not found", JOptionPane.WARNING_MESSAGE);
253 ex.printStackTrace();
264 public void mouseEntered(MouseEvent e)
266 if (scrollThread != null)
268 scrollThread.running = false;
278 public void mouseExited(MouseEvent e)
280 if (av.getWrapAlignment())
285 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
287 scrollThread = new ScrollThread(true);
290 if (mouseDragging && (e.getY() >= getHeight())
291 && (av.getAlignment().getHeight() > av.getEndSeq()))
293 scrollThread = new ScrollThread(false);
303 public void mousePressed(MouseEvent e)
305 if (e.getClickCount() == 2)
310 int seq = alignPanel.seqPanel.findSeq(e);
312 if (javax.swing.SwingUtilities.isRightMouseButton(e))
314 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
315 // build a new links menu based on the current links + any non-positional
317 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
318 SequenceFeature sf[] = sq==null ? null : sq.getDatasetSequence().getSequenceFeatures();
319 for (int sl = 0; sf != null && sl < sf.length; sl++)
321 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
323 if (sf[sl].links != null && sf[sl].links.size() > 0)
325 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
327 nlinks.addElement(sf[sl].links.elementAt(l));
333 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
334 nlinks, new Vector(Preferences.getGroupURLLinks()));
335 pop.show(this, e.getX(), e.getY());
340 if ((av.getSelectionGroup() == null)
341 || ((!e.isControlDown() && !e.isShiftDown()) && av
342 .getSelectionGroup() != null))
344 av.setSelectionGroup(new SequenceGroup());
345 av.getSelectionGroup().setStartRes(0);
346 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
349 if (e.isShiftDown() && (lastid != -1))
351 selectSeqs(lastid, seq);
357 alignPanel.paintAlignment(true);
366 void selectSeq(int seq)
370 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
371 av.getSelectionGroup().addOrRemove(pickedSeq, true);
382 void selectSeqs(int start, int end)
384 if (av.getSelectionGroup() == null)
389 if (end >= av.getAlignment().getHeight())
391 end = av.getAlignment().getHeight() - 1;
404 for (int i = start; i <= end; i++)
406 av.getSelectionGroup().addSequence(
407 av.getAlignment().getSequenceAt(i), true);
417 public void mouseReleased(MouseEvent e)
419 if (scrollThread != null)
421 scrollThread.running = false;
424 mouseDragging = false;
425 PaintRefresher.Refresh(this, av.getSequenceSetId());
426 // always send selection message when mouse is released
436 public void highlightSearchResults(List<SequenceI> list)
438 idCanvas.setHighlighted(list);
445 int index = av.getAlignment().findIndex((SequenceI) list.get(0));
447 // do we need to scroll the panel?
448 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
450 alignPanel.setScrollValues(av.getStartRes(), index);
454 // this class allows scrolling off the bottom of the visible alignment
455 class ScrollThread extends Thread
457 boolean running = false;
461 public ScrollThread(boolean up)
467 public void stopScrolling()
478 if (alignPanel.scrollUp(up))
480 // scroll was ok, so add new sequence to selection
481 int seq = av.getStartSeq();
485 seq = av.getEndSeq();
490 selectSeqs(lastid - 1, seq);
492 else if (seq > lastid)
494 selectSeqs(lastid + 1, seq);
504 alignPanel.paintAlignment(false);
509 } catch (Exception ex)