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);
85 public void mouseMoved(MouseEvent e)
87 SeqPanel sp = alignPanel.seqPanel;
88 int seq = Math.max(0, sp.findSeq(e));
90 if (seq > -1 && seq < av.getAlignment().getHeight())
92 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
93 StringBuffer tip = new StringBuffer();
97 if (sequence.getDescription() != null)
99 tmp = sequence.getDescription();
100 tip.append("<br>" + tmp);
101 maxWidth = Math.max(maxWidth, tmp.length());
104 DBRefEntry[] dbrefs = sequence.getDatasetSequence().getDBRef();
105 if (av.isShowDbRefs() && dbrefs != null)
107 for (int i = 0; i < dbrefs.length; i++)
110 tmp = dbrefs[i].getSource() + " " + dbrefs[i].getAccessionId();
112 maxWidth = Math.max(maxWidth, tmp.length());
116 // ADD NON POSITIONAL SEQUENCE INFO
117 SequenceFeature[] features = sequence.getDatasetSequence()
118 .getSequenceFeatures();
119 SequenceFeature[] tfeat = new SequenceFeature[1];
120 if (av.isShowNpFeats() && features != null)
122 for (int i = 0; i < features.length; i++)
124 if (features[i].begin == 0 && features[i].end == 0)
126 int sz = -tip.length();
127 tfeat[0] = features[i];
128 sp.appendFeatures(tip, linkImageURL, 0, tfeat,
129 sp.seqCanvas.fr.minmax);
131 maxWidth = Math.max(maxWidth, sz);
138 tip.insert(0, "<table width=350 border=0><tr><td><i>");
139 tip.append("</i></td></tr></table>");
142 tip.append("</html>");
144 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);
176 public void mouseWheelMoved(MouseWheelEvent e)
179 if (e.getWheelRotation() > 0)
181 alignPanel.scrollUp(false);
185 alignPanel.scrollUp(true);
196 public void mouseClicked(MouseEvent e)
198 if (e.getClickCount() < 2)
203 java.util.Vector links = Preferences.sequenceURLLinks;
204 if (links == null || links.size() < 1)
209 int seq = alignPanel.seqPanel.findSeq(e);
212 String id = av.getAlignment().getSequenceAt(seq).getName();
213 while (url == null && i < links.size())
215 // DEFAULT LINK IS FIRST IN THE LINK LIST
216 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
217 url = links.elementAt(i++).toString();
218 jalview.util.UrlLink urlLink = null;
221 urlLink = new UrlLink(url);
222 } catch (Exception foo)
224 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
230 if (!urlLink.isValid())
232 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
237 String urls[] = urlLink.makeUrls(id, true);
238 if (urls == null || urls[0] == null || urls[0].length() < 4)
243 // just take first URL made from regex
248 jalview.util.BrowserLauncher.openURL(url);
249 } catch (Exception ex)
252 .showInternalMessageDialog(
254 "Unixers: Couldn't find default web browser."
255 + "\nAdd the full path to your browser in Preferences.",
256 "Web browser not found", JOptionPane.WARNING_MESSAGE);
257 ex.printStackTrace();
269 public void mouseEntered(MouseEvent e)
271 if (scrollThread != null)
273 scrollThread.running = false;
284 public void mouseExited(MouseEvent e)
286 if (av.getWrapAlignment())
291 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
293 scrollThread = new ScrollThread(true);
296 if (mouseDragging && (e.getY() >= getHeight())
297 && (av.getAlignment().getHeight() > av.getEndSeq()))
299 scrollThread = new ScrollThread(false);
310 public void mousePressed(MouseEvent e)
312 if (e.getClickCount() == 2)
317 int seq = alignPanel.seqPanel.findSeq(e);
319 if (javax.swing.SwingUtilities.isRightMouseButton(e))
321 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
322 // build a new links menu based on the current links + any non-positional
324 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
325 SequenceFeature sf[] = sq==null ? null : sq.getDatasetSequence().getSequenceFeatures();
326 for (int sl = 0; sf != null && sl < sf.length; sl++)
328 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
330 if (sf[sl].links != null && sf[sl].links.size() > 0)
332 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
334 nlinks.addElement(sf[sl].links.elementAt(l));
340 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
341 nlinks, new Vector(Preferences.getGroupURLLinks()));
342 pop.show(this, e.getX(), e.getY());
347 if ((av.getSelectionGroup() == null)
348 || ((!e.isControlDown() && !e.isShiftDown()) && av
349 .getSelectionGroup() != null))
351 av.setSelectionGroup(new SequenceGroup());
352 av.getSelectionGroup().setStartRes(0);
353 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
356 if (e.isShiftDown() && (lastid != -1))
358 selectSeqs(lastid, seq);
364 alignPanel.paintAlignment(true);
373 void selectSeq(int seq)
377 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
378 av.getSelectionGroup().addOrRemove(pickedSeq, true);
389 void selectSeqs(int start, int end)
391 if (av.getSelectionGroup() == null)
396 if (end >= av.getAlignment().getHeight())
398 end = av.getAlignment().getHeight() - 1;
411 for (int i = start; i <= end; i++)
413 av.getSelectionGroup().addSequence(
414 av.getAlignment().getSequenceAt(i), true);
425 public void mouseReleased(MouseEvent e)
427 if (scrollThread != null)
429 scrollThread.running = false;
432 mouseDragging = false;
433 PaintRefresher.Refresh(this, av.getSequenceSetId());
434 // always send selection message when mouse is released
444 public void highlightSearchResults(List<SequenceI> list)
446 idCanvas.setHighlighted(list);
453 int index = av.getAlignment().findIndex(list.get(0));
455 // do we need to scroll the panel?
456 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
458 alignPanel.setScrollValues(av.getStartRes(), index);
462 // this class allows scrolling off the bottom of the visible alignment
463 class ScrollThread extends Thread
465 boolean running = false;
469 public ScrollThread(boolean up)
475 public void stopScrolling()
487 if (alignPanel.scrollUp(up))
489 // scroll was ok, so add new sequence to selection
490 int seq = av.getStartSeq();
494 seq = av.getEndSeq();
499 selectSeqs(lastid - 1, seq);
501 else if (seq > lastid)
503 selectSeqs(lastid + 1, seq);
513 alignPanel.paintAlignment(false);
518 } catch (Exception ex)