2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import java.awt.event.*;
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;
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 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
87 if (seq > -1 && seq < av.alignment.getHeight())
89 SequenceI sequence = av.alignment.getSequenceAt(seq);
90 StringBuffer tip = new StringBuffer();
94 if (sequence.getDescription() != null)
96 tmp = sequence.getDescription();
97 tip.append("<br>" + tmp);
98 maxWidth = Math.max(maxWidth, tmp.length());
101 DBRefEntry[] dbrefs = sequence.getDatasetSequence().getDBRef();
102 if (av.isShowDbRefs() && dbrefs != null)
104 for (int i = 0; i < dbrefs.length; i++)
107 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
109 maxWidth = Math.max(maxWidth, tmp.length());
113 // ADD NON POSITIONAL SEQUENCE INFO
114 SequenceFeature[] features = sequence.getDatasetSequence()
115 .getSequenceFeatures();
116 SequenceFeature[] tfeat = new SequenceFeature[1];
117 if (av.isShowNpFeats() && features != null)
119 for (int i = 0; i < features.length; i++)
121 if (features[i].begin == 0 && features[i].end == 0)
123 int sz = -tip.length();
124 tfeat[0] = features[i];
125 SeqPanel.appendFeatures(tip, linkImageURL, 0, tfeat);
127 maxWidth = Math.max(maxWidth, sz);
134 tip.insert(0, "<table width=350 border=0><tr><td><i>");
135 tip.append("</i></td></tr></table>");
138 tip.append("</html>");
140 setToolTipText("<html>" + sequence.getDisplayId(true)
151 public void mouseDragged(MouseEvent e)
153 mouseDragging = true;
155 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
159 selectSeqs(lastid - 1, seq);
161 else if (seq > lastid)
163 selectSeqs(lastid + 1, seq);
167 alignPanel.paintAlignment(true);
170 public void mouseWheelMoved(MouseWheelEvent e)
173 if (e.getWheelRotation() > 0)
175 alignPanel.scrollUp(false);
179 alignPanel.scrollUp(true);
189 public void mouseClicked(MouseEvent e)
191 if (e.getClickCount() < 2)
196 java.util.Vector links = Preferences.sequenceURLLinks;
197 if (links == null || links.size() < 1)
202 int seq = alignPanel.seqPanel.findSeq(e);
205 String id = av.getAlignment().getSequenceAt(seq).getName();
206 while (url == null && i < links.size())
208 // DEFAULT LINK IS FIRST IN THE LINK LIST
209 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
210 url = links.elementAt(i++).toString();
211 jalview.util.UrlLink urlLink = null;
214 urlLink = new UrlLink(url);
215 } catch (Exception foo)
217 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
223 if (!urlLink.isValid())
225 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
230 String urls[] = urlLink.makeUrls(id, true);
231 if (urls == null || urls[0] == null || urls[0].length() < 4)
236 // just take first URL made from regex
241 jalview.util.BrowserLauncher.openURL(url);
242 } catch (Exception ex)
245 .showInternalMessageDialog(
247 "Unixers: Couldn't find default web browser."
248 + "\nAdd the full path to your browser in Preferences.",
249 "Web browser not found", JOptionPane.WARNING_MESSAGE);
250 ex.printStackTrace();
261 public void mouseEntered(MouseEvent e)
263 if (scrollThread != null)
265 scrollThread.running = false;
275 public void mouseExited(MouseEvent e)
277 if (av.getWrapAlignment())
282 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
284 scrollThread = new ScrollThread(true);
287 if (mouseDragging && (e.getY() >= getHeight())
288 && (av.alignment.getHeight() > av.getEndSeq()))
290 scrollThread = new ScrollThread(false);
300 public void mousePressed(MouseEvent e)
302 if (e.getClickCount() == 2)
307 int seq = alignPanel.seqPanel.findSeq(e);
309 if (javax.swing.SwingUtilities.isRightMouseButton(e))
311 Sequence sq = (Sequence) av
312 .getAlignment().getSequenceAt(seq);
313 // build a new links menu based on the current links + any non-positional features
314 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
315 SequenceFeature sf[] = sq.getDatasetSequence().getSequenceFeatures();
316 for (int sl=0;sf!=null && sl<sf.length;sl++)
318 if (sf[sl].begin==sf[sl].end && sf[sl].begin==0)
320 if (sf[sl].links!=null && sf[sl].links.size()>0)
322 for (int l=0, lSize=sf[sl].links.size(); l<lSize; l++)
324 nlinks.addElement(sf[sl].links.elementAt(l));
330 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel,
333 pop.show(this, e.getX(), e.getY());
338 if ((av.getSelectionGroup() == null)
339 || ((!e.isControlDown() && !e.isShiftDown()) && av
340 .getSelectionGroup() != null))
342 av.setSelectionGroup(new SequenceGroup());
343 av.getSelectionGroup().setStartRes(0);
344 av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
347 if (e.isShiftDown() && (lastid != -1))
349 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());
433 public void highlightSearchResults(java.util.Vector found)
435 idCanvas.setHighlighted(found);
442 int index = av.alignment.findIndex((SequenceI) found.get(0));
444 // do we need to scroll the panel?
445 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
447 alignPanel.setScrollValues(av.getStartRes(), index);
451 // this class allows scrolling off the bottom of the visible alignment
452 class ScrollThread extends Thread
454 boolean running = false;
458 public ScrollThread(boolean up)
464 public void stopScrolling()
475 if (alignPanel.scrollUp(up))
477 // scroll was ok, so add new sequence to selection
478 int seq = av.getStartSeq();
482 seq = av.getEndSeq();
487 selectSeqs(lastid - 1, seq);
489 else if (seq > lastid)
491 selectSeqs(lastid + 1, seq);
501 alignPanel.paintAlignment(false);
506 } catch (Exception ex)