2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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 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,sp.seqCanvas.fr.minmax);
128 maxWidth = Math.max(maxWidth, sz);
135 tip.insert(0, "<table width=350 border=0><tr><td><i>");
136 tip.append("</i></td></tr></table>");
139 tip.append("</html>");
141 setToolTipText("<html>" + sequence.getDisplayId(true)
152 public void mouseDragged(MouseEvent e)
154 mouseDragging = true;
156 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
160 selectSeqs(lastid - 1, seq);
162 else if (seq > lastid)
164 selectSeqs(lastid + 1, seq);
168 alignPanel.paintAlignment(true);
171 public void mouseWheelMoved(MouseWheelEvent e)
174 if (e.getWheelRotation() > 0)
176 alignPanel.scrollUp(false);
180 alignPanel.scrollUp(true);
190 public void mouseClicked(MouseEvent e)
192 if (e.getClickCount() < 2)
197 java.util.Vector links = Preferences.sequenceURLLinks;
198 if (links == null || links.size() < 1)
203 int seq = alignPanel.seqPanel.findSeq(e);
206 String id = av.getAlignment().getSequenceAt(seq).getName();
207 while (url == null && i < links.size())
209 // DEFAULT LINK IS FIRST IN THE LINK LIST
210 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
211 url = links.elementAt(i++).toString();
212 jalview.util.UrlLink urlLink = null;
215 urlLink = new UrlLink(url);
216 } catch (Exception foo)
218 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
224 if (!urlLink.isValid())
226 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
231 String urls[] = urlLink.makeUrls(id, true);
232 if (urls == null || urls[0] == null || urls[0].length() < 4)
237 // just take first URL made from regex
242 jalview.util.BrowserLauncher.openURL(url);
243 } catch (Exception ex)
246 .showInternalMessageDialog(
248 "Unixers: Couldn't find default web browser."
249 + "\nAdd the full path to your browser in Preferences.",
250 "Web browser not found", JOptionPane.WARNING_MESSAGE);
251 ex.printStackTrace();
262 public void mouseEntered(MouseEvent e)
264 if (scrollThread != null)
266 scrollThread.running = false;
276 public void mouseExited(MouseEvent e)
278 if (av.getWrapAlignment())
283 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
285 scrollThread = new ScrollThread(true);
288 if (mouseDragging && (e.getY() >= getHeight())
289 && (av.alignment.getHeight() > av.getEndSeq()))
291 scrollThread = new ScrollThread(false);
301 public void mousePressed(MouseEvent e)
303 if (e.getClickCount() == 2)
308 int seq = alignPanel.seqPanel.findSeq(e);
310 if (javax.swing.SwingUtilities.isRightMouseButton(e))
312 Sequence sq = (Sequence) av
313 .getAlignment().getSequenceAt(seq);
314 // build a new links menu based on the current links + any non-positional features
315 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
316 SequenceFeature sf[] = sq.getDatasetSequence().getSequenceFeatures();
317 for (int sl=0;sf!=null && sl<sf.length;sl++)
319 if (sf[sl].begin==sf[sl].end && sf[sl].begin==0)
321 if (sf[sl].links!=null && sf[sl].links.size()>0)
323 for (int l=0, lSize=sf[sl].links.size(); l<lSize; l++)
325 nlinks.addElement(sf[sl].links.elementAt(l));
331 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel,
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);
415 public void mouseReleased(MouseEvent e)
417 if (scrollThread != null)
419 scrollThread.running = false;
421 // if (mouseDragging)
423 // always send selection message when mouse is released
428 mouseDragging = false;
429 PaintRefresher.Refresh(this, av.getSequenceSetId());
438 public void highlightSearchResults(java.util.Vector found)
440 idCanvas.setHighlighted(found);
447 int index = av.alignment.findIndex((SequenceI) found.get(0));
449 // do we need to scroll the panel?
450 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
452 alignPanel.setScrollValues(av.getStartRes(), index);
456 // this class allows scrolling off the bottom of the visible alignment
457 class ScrollThread extends Thread
459 boolean running = false;
463 public ScrollThread(boolean up)
469 public void stopScrolling()
480 if (alignPanel.scrollUp(up))
482 // scroll was ok, so add new sequence to selection
483 int seq = av.getStartSeq();
487 seq = av.getEndSeq();
492 selectSeqs(lastid - 1, seq);
494 else if (seq > lastid)
496 selectSeqs(lastid + 1, seq);
506 alignPanel.paintAlignment(false);
511 } catch (Exception ex)