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.io.SequenceAnnotationReport;
29 import jalview.util.UrlLink;
37 public class IdPanel extends JPanel implements MouseListener,
38 MouseMotionListener, MouseWheelListener
40 protected IdCanvas idCanvas;
42 protected AlignViewport av;
44 protected AlignmentPanel alignPanel;
46 ScrollThread scrollThread = null;
55 boolean mouseDragging = false;
56 private final SequenceAnnotationReport seqAnnotReport;
59 * Creates a new IdPanel object.
66 public IdPanel(AlignViewport av, AlignmentPanel parent)
70 idCanvas = new IdCanvas(av);
71 linkImageURL = getClass().getResource("/images/link.gif").toString();
72 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
73 setLayout(new BorderLayout());
74 add(idCanvas, BorderLayout.CENTER);
75 addMouseListener(this);
76 addMouseMotionListener(this);
77 addMouseWheelListener(this);
78 ToolTipManager.sharedInstance().registerComponent(this);
87 public void mouseMoved(MouseEvent e)
89 SeqPanel sp = alignPanel.seqPanel;
90 int seq = Math.max(0, sp.findSeq(e));
91 if (seq > -1 && seq < av.getAlignment().getHeight())
93 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
94 StringBuffer tip = new StringBuffer();
95 seqAnnotReport.createSequenceAnnotationReport(tip, sequence,
96 av.isShowDbRefs(), av.isShowNpFeats(),
97 sp.seqCanvas.fr.minmax);
98 setToolTipText("<html>" + sequence.getDisplayId(true) + " "
99 + tip.toString()+"</html>");
110 public void mouseDragged(MouseEvent e)
112 mouseDragging = true;
114 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
118 selectSeqs(lastid - 1, seq);
120 else if (seq > lastid)
122 selectSeqs(lastid + 1, seq);
126 alignPanel.paintAlignment(true);
130 public void mouseWheelMoved(MouseWheelEvent e)
133 if (e.getWheelRotation() > 0)
137 alignPanel.scrollRight(true);
140 alignPanel.scrollUp(false);
147 alignPanel.scrollRight(false);
149 alignPanel.scrollUp(true);
161 public void mouseClicked(MouseEvent e)
163 if (e.getClickCount() < 2)
168 java.util.Vector links = Preferences.sequenceURLLinks;
169 if (links == null || links.size() < 1)
174 int seq = alignPanel.seqPanel.findSeq(e);
177 String id = av.getAlignment().getSequenceAt(seq).getName();
178 while (url == null && i < links.size())
180 // DEFAULT LINK IS FIRST IN THE LINK LIST
181 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
182 url = links.elementAt(i++).toString();
183 jalview.util.UrlLink urlLink = null;
186 urlLink = new UrlLink(url);
187 } catch (Exception foo)
189 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
195 if (!urlLink.isValid())
197 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
202 String urls[] = urlLink.makeUrls(id, true);
203 if (urls == null || urls[0] == null || urls[0].length() < 4)
208 // just take first URL made from regex
213 jalview.util.BrowserLauncher.openURL(url);
214 } catch (Exception ex)
217 .showInternalMessageDialog(
219 "Unixers: Couldn't find default web browser."
220 + "\nAdd the full path to your browser in Preferences.",
221 "Web browser not found", JOptionPane.WARNING_MESSAGE);
222 ex.printStackTrace();
234 public void mouseEntered(MouseEvent e)
236 if (scrollThread != null)
238 scrollThread.running = false;
249 public void mouseExited(MouseEvent e)
251 if (av.getWrapAlignment())
256 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
258 scrollThread = new ScrollThread(true);
261 if (mouseDragging && (e.getY() >= getHeight())
262 && (av.getAlignment().getHeight() > av.getEndSeq()))
264 scrollThread = new ScrollThread(false);
275 public void mousePressed(MouseEvent e)
277 if (e.getClickCount() == 2)
282 int seq = alignPanel.seqPanel.findSeq(e);
284 if (javax.swing.SwingUtilities.isRightMouseButton(e))
286 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
287 // build a new links menu based on the current links + any non-positional
289 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
290 SequenceFeature sf[] = sq==null ? null : sq.getDatasetSequence().getSequenceFeatures();
291 for (int sl = 0; sf != null && sl < sf.length; sl++)
293 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
295 if (sf[sl].links != null && sf[sl].links.size() > 0)
297 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
299 nlinks.addElement(sf[sl].links.elementAt(l));
305 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
306 nlinks, new Vector(Preferences.getGroupURLLinks()));
307 pop.show(this, e.getX(), e.getY());
312 if ((av.getSelectionGroup() == null)
313 || ((!e.isControlDown() && !e.isShiftDown()) && av
314 .getSelectionGroup() != null))
316 av.setSelectionGroup(new SequenceGroup());
317 av.getSelectionGroup().setStartRes(0);
318 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
321 if (e.isShiftDown() && (lastid != -1))
323 selectSeqs(lastid, seq);
329 alignPanel.paintAlignment(true);
338 void selectSeq(int seq)
342 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
343 av.getSelectionGroup().addOrRemove(pickedSeq, true);
354 void selectSeqs(int start, int end)
356 if (av.getSelectionGroup() == null)
361 if (end >= av.getAlignment().getHeight())
363 end = av.getAlignment().getHeight() - 1;
376 for (int i = start; i <= end; i++)
378 av.getSelectionGroup().addSequence(
379 av.getAlignment().getSequenceAt(i), true);
390 public void mouseReleased(MouseEvent e)
392 if (scrollThread != null)
394 scrollThread.running = false;
397 mouseDragging = false;
398 PaintRefresher.Refresh(this, av.getSequenceSetId());
399 // always send selection message when mouse is released
409 public void highlightSearchResults(List<SequenceI> list)
411 idCanvas.setHighlighted(list);
418 int index = av.getAlignment().findIndex(list.get(0));
420 // do we need to scroll the panel?
421 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
423 alignPanel.setScrollValues(av.getStartRes(), index);
427 // this class allows scrolling off the bottom of the visible alignment
428 class ScrollThread extends Thread
430 boolean running = false;
434 public ScrollThread(boolean up)
440 public void stopScrolling()
452 if (alignPanel.scrollUp(up))
454 // scroll was ok, so add new sequence to selection
455 int seq = av.getStartSeq();
459 seq = av.getEndSeq();
464 selectSeqs(lastid - 1, seq);
466 else if (seq > lastid)
468 selectSeqs(lastid + 1, seq);
478 alignPanel.paintAlignment(false);
483 } catch (Exception ex)