2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, 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.Vector;
26 import jalview.datamodel.*;
27 import jalview.util.UrlLink;
35 public class IdPanel extends JPanel implements MouseListener,
36 MouseMotionListener, MouseWheelListener
38 protected IdCanvas idCanvas;
40 protected AlignViewport av;
42 protected AlignmentPanel alignPanel;
44 ScrollThread scrollThread = null;
53 boolean mouseDragging = false;
56 * Creates a new IdPanel object.
63 public IdPanel(AlignViewport av, AlignmentPanel parent)
67 idCanvas = new IdCanvas(av);
68 linkImageURL = getClass().getResource("/images/link.gif").toString();
69 setLayout(new BorderLayout());
70 add(idCanvas, BorderLayout.CENTER);
71 addMouseListener(this);
72 addMouseMotionListener(this);
73 addMouseWheelListener(this);
74 ToolTipManager.sharedInstance().registerComponent(this);
83 public void mouseMoved(MouseEvent e)
85 SeqPanel sp = alignPanel.seqPanel;
86 int seq = Math.max(0, sp.findSeq(e));
88 if (seq > -1 && seq < av.alignment.getHeight())
90 SequenceI sequence = av.alignment.getSequenceAt(seq);
91 StringBuffer tip = new StringBuffer();
95 if (sequence.getDescription() != null)
97 tmp = sequence.getDescription();
98 tip.append("<br>" + tmp);
99 maxWidth = Math.max(maxWidth, tmp.length());
102 DBRefEntry[] dbrefs = sequence.getDatasetSequence().getDBRef();
103 if (av.isShowDbRefs() && dbrefs != null)
105 for (int i = 0; i < dbrefs.length; i++)
108 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
110 maxWidth = Math.max(maxWidth, tmp.length());
114 // ADD NON POSITIONAL SEQUENCE INFO
115 SequenceFeature[] features = sequence.getDatasetSequence()
116 .getSequenceFeatures();
117 SequenceFeature[] tfeat = new SequenceFeature[1];
118 if (av.isShowNpFeats() && features != null)
120 for (int i = 0; i < features.length; i++)
122 if (features[i].begin == 0 && features[i].end == 0)
124 int sz = -tip.length();
125 tfeat[0] = features[i];
126 sp.appendFeatures(tip, linkImageURL, 0, tfeat,
127 sp.seqCanvas.fr.minmax);
129 maxWidth = Math.max(maxWidth, sz);
136 tip.insert(0, "<table width=350 border=0><tr><td><i>");
137 tip.append("</i></td></tr></table>");
140 tip.append("</html>");
142 setToolTipText("<html>" + sequence.getDisplayId(true) + " "
153 public void mouseDragged(MouseEvent e)
155 mouseDragging = true;
157 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
161 selectSeqs(lastid - 1, seq);
163 else if (seq > lastid)
165 selectSeqs(lastid + 1, seq);
169 alignPanel.paintAlignment(true);
172 public void mouseWheelMoved(MouseWheelEvent e)
175 if (e.getWheelRotation() > 0)
177 alignPanel.scrollUp(false);
181 alignPanel.scrollUp(true);
191 public void mouseClicked(MouseEvent e)
193 if (e.getClickCount() < 2)
198 java.util.Vector links = Preferences.sequenceURLLinks;
199 if (links == null || links.size() < 1)
204 int seq = alignPanel.seqPanel.findSeq(e);
207 String id = av.getAlignment().getSequenceAt(seq).getName();
208 while (url == null && i < links.size())
210 // DEFAULT LINK IS FIRST IN THE LINK LIST
211 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
212 url = links.elementAt(i++).toString();
213 jalview.util.UrlLink urlLink = null;
216 urlLink = new UrlLink(url);
217 } catch (Exception foo)
219 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
225 if (!urlLink.isValid())
227 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
232 String urls[] = urlLink.makeUrls(id, true);
233 if (urls == null || urls[0] == null || urls[0].length() < 4)
238 // just take first URL made from regex
243 jalview.util.BrowserLauncher.openURL(url);
244 } catch (Exception ex)
247 .showInternalMessageDialog(
249 "Unixers: Couldn't find default web browser."
250 + "\nAdd the full path to your browser in Preferences.",
251 "Web browser not found", JOptionPane.WARNING_MESSAGE);
252 ex.printStackTrace();
263 public void mouseEntered(MouseEvent e)
265 if (scrollThread != null)
267 scrollThread.running = false;
277 public void mouseExited(MouseEvent e)
279 if (av.getWrapAlignment())
284 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
286 scrollThread = new ScrollThread(true);
289 if (mouseDragging && (e.getY() >= getHeight())
290 && (av.alignment.getHeight() > av.getEndSeq()))
292 scrollThread = new ScrollThread(false);
302 public void mousePressed(MouseEvent e)
304 if (e.getClickCount() == 2)
309 int seq = alignPanel.seqPanel.findSeq(e);
311 if (javax.swing.SwingUtilities.isRightMouseButton(e))
313 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
314 // build a new links menu based on the current links + any non-positional
316 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
317 SequenceFeature sf[] = sq==null ? null : sq.getDatasetSequence().getSequenceFeatures();
318 for (int sl = 0; sf != null && sl < sf.length; sl++)
320 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
322 if (sf[sl].links != null && sf[sl].links.size() > 0)
324 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
326 nlinks.addElement(sf[sl].links.elementAt(l));
332 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
333 nlinks, new Vector(Preferences.getGroupURLLinks()));
334 pop.show(this, e.getX(), e.getY());
339 if ((av.getSelectionGroup() == null)
340 || ((!e.isControlDown() && !e.isShiftDown()) && av
341 .getSelectionGroup() != null))
343 av.setSelectionGroup(new SequenceGroup());
344 av.getSelectionGroup().setStartRes(0);
345 av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
348 if (e.isShiftDown() && (lastid != -1))
350 selectSeqs(lastid, seq);
356 alignPanel.paintAlignment(true);
365 void selectSeq(int seq)
369 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
370 av.getSelectionGroup().addOrRemove(pickedSeq, true);
381 void selectSeqs(int start, int end)
383 if (av.getSelectionGroup() == null)
388 if (end >= av.getAlignment().getHeight())
390 end = av.getAlignment().getHeight() - 1;
403 for (int i = start; i <= end; i++)
405 av.getSelectionGroup().addSequence(
406 av.getAlignment().getSequenceAt(i), true);
416 public void mouseReleased(MouseEvent e)
418 if (scrollThread != null)
420 scrollThread.running = false;
423 mouseDragging = false;
424 PaintRefresher.Refresh(this, av.getSequenceSetId());
425 // always send selection message when mouse is released
435 public void highlightSearchResults(java.util.Vector found)
437 idCanvas.setHighlighted(found);
444 int index = av.alignment.findIndex((SequenceI) found.get(0));
446 // do we need to scroll the panel?
447 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
449 alignPanel.setScrollValues(av.getStartRes(), index);
453 // this class allows scrolling off the bottom of the visible alignment
454 class ScrollThread extends Thread
456 boolean running = false;
460 public ScrollThread(boolean up)
466 public void stopScrolling()
477 if (alignPanel.scrollUp(up))
479 // scroll was ok, so add new sequence to selection
480 int seq = av.getStartSeq();
484 seq = av.getEndSeq();
489 selectSeqs(lastid - 1, seq);
491 else if (seq > lastid)
493 selectSeqs(lastid + 1, seq);
503 alignPanel.paintAlignment(false);
508 } catch (Exception ex)