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;
52 boolean mouseDragging = false;
55 * Creates a new IdPanel object.
62 public IdPanel(AlignViewport av, AlignmentPanel parent)
66 idCanvas = new IdCanvas(av);
67 setLayout(new BorderLayout());
68 add(idCanvas, BorderLayout.CENTER);
69 addMouseListener(this);
70 addMouseMotionListener(this);
71 addMouseWheelListener(this);
72 ToolTipManager.sharedInstance().registerComponent(this);
81 public void mouseMoved(MouseEvent e)
83 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
85 if (seq > -1 && seq < av.alignment.getHeight())
87 SequenceI sequence = av.alignment.getSequenceAt(seq);
88 StringBuffer tip = new StringBuffer();
92 if (sequence.getDescription() != null)
94 tmp = sequence.getDescription();
95 tip.append("<br>" + tmp);
96 maxWidth = Math.max(maxWidth, tmp.length());
99 DBRefEntry[] dbrefs = sequence.getDatasetSequence().getDBRef();
100 if (av.isShowDbRefs() && dbrefs != null)
102 for (int i = 0; i < dbrefs.length; i++)
105 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
107 maxWidth = Math.max(maxWidth, tmp.length());
111 // ADD NON POSITIONAL SEQUENCE INFO
112 SequenceFeature[] features = sequence.getDatasetSequence()
113 .getSequenceFeatures();
114 if (av.isShowNpFeats() && features != null)
116 for (int i = 0; i < features.length; i++)
118 if (features[i].begin == 0 && features[i].end == 0)
121 int sz = -tip.length();
123 if (features[i].getFeatureGroup()!=null) { tip.append(features[i].getFeatureGroup()); nl=true;};
124 if (features[i].getType()!=null) { tip.append(" "); tip.append(features[i].getType()); nl=true;};
125 if (features[i].getDescription()!=null) { tip.append(" "); tip.append(features[i].getDescription()); nl=true;};
126 if (features[i].getScore()!=Float.NaN && features[i].getScore()!=0f) { tip.append(" Score = "); tip.append(features[i].getScore()); nl=true;};
127 if (features[i].getStatus()!=null && features[i].getStatus().length()>0) { tip.append(" ("); tip.append(features[i].getStatus()); tip.append(")");nl=true;};
131 maxWidth = Math.max(maxWidth, sz);
139 tip.insert(0, "<table width=350 border=0><tr><td><i>");
140 tip.append("</i></td></tr></table>");
143 tip.append("</html>");
145 setToolTipText("<html>" + sequence.getDisplayId(true)
156 public void mouseDragged(MouseEvent e)
158 mouseDragging = true;
160 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
164 selectSeqs(lastid - 1, seq);
166 else if (seq > lastid)
168 selectSeqs(lastid + 1, seq);
172 alignPanel.paintAlignment(true);
175 public void mouseWheelMoved(MouseWheelEvent e)
178 if (e.getWheelRotation() > 0)
180 alignPanel.scrollUp(false);
184 alignPanel.scrollUp(true);
194 public void mouseClicked(MouseEvent e)
196 if (e.getClickCount() < 2)
201 java.util.Vector links = Preferences.sequenceURLLinks;
202 if (links == null || links.size() < 1)
207 int seq = alignPanel.seqPanel.findSeq(e);
210 String id = av.getAlignment().getSequenceAt(seq).getName();
211 while (url == null && i < links.size())
213 // DEFAULT LINK IS FIRST IN THE LINK LIST
214 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
215 url = links.elementAt(i++).toString();
216 jalview.util.UrlLink urlLink = null;
219 urlLink = new UrlLink(url);
220 } catch (Exception foo)
222 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
228 if (!urlLink.isValid())
230 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
235 String urls[] = urlLink.makeUrls(id, true);
236 if (urls == null || urls[0] == null || urls[0].length() < 4)
241 // just take first URL made from regex
246 jalview.util.BrowserLauncher.openURL(url);
247 } catch (Exception ex)
250 .showInternalMessageDialog(
252 "Unixers: Couldn't find default web browser."
253 + "\nAdd the full path to your browser in Preferences.",
254 "Web browser not found", JOptionPane.WARNING_MESSAGE);
255 ex.printStackTrace();
266 public void mouseEntered(MouseEvent e)
268 if (scrollThread != null)
270 scrollThread.running = false;
280 public void mouseExited(MouseEvent e)
282 if (av.getWrapAlignment())
287 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
289 scrollThread = new ScrollThread(true);
292 if (mouseDragging && (e.getY() >= getHeight())
293 && (av.alignment.getHeight() > av.getEndSeq()))
295 scrollThread = new ScrollThread(false);
305 public void mousePressed(MouseEvent e)
307 if (e.getClickCount() == 2)
312 int seq = alignPanel.seqPanel.findSeq(e);
314 if (javax.swing.SwingUtilities.isRightMouseButton(e))
316 Sequence sq = (Sequence) av
317 .getAlignment().getSequenceAt(seq);
318 // build a new links menu based on the current links + any non-positional features
319 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
320 SequenceFeature sf[] = sq.getDatasetSequence().getSequenceFeatures();
321 for (int sl=0;sf!=null && sl<sf.length;sl++)
323 if (sf[sl].begin==sf[sl].end && sf[sl].begin==0)
325 if (sf[sl].links!=null && sf[sl].links.size()>0)
327 for (int l=0, lSize=sf[sl].links.size(); l<lSize; l++)
329 nlinks.addElement(sf[sl].links.elementAt(l));
335 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel,
338 pop.show(this, e.getX(), e.getY());
343 if ((av.getSelectionGroup() == null)
344 || ((!e.isControlDown() && !e.isShiftDown()) && av
345 .getSelectionGroup() != null))
347 av.setSelectionGroup(new SequenceGroup());
348 av.getSelectionGroup().setStartRes(0);
349 av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
352 if (e.isShiftDown() && (lastid != -1))
354 selectSeqs(lastid, seq);
361 alignPanel.paintAlignment(true);
370 void selectSeq(int seq)
374 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
375 av.getSelectionGroup().addOrRemove(pickedSeq, true);
386 void selectSeqs(int start, int end)
388 if (av.getSelectionGroup() == null)
393 if (end >= av.getAlignment().getHeight())
395 end = av.getAlignment().getHeight() - 1;
408 for (int i = start; i <= end; i++)
410 av.getSelectionGroup().addSequence(
411 av.getAlignment().getSequenceAt(i), true);
421 public void mouseReleased(MouseEvent e)
423 if (scrollThread != null)
425 scrollThread.running = false;
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)