2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.awt.event.*;
25 import java.util.List;
26 import java.util.Vector;
30 import jalview.datamodel.*;
31 import jalview.io.SequenceAnnotationReport;
32 import jalview.util.UrlLink;
40 public class IdPanel extends JPanel implements MouseListener,
41 MouseMotionListener, MouseWheelListener
43 protected IdCanvas idCanvas;
45 protected AlignViewport av;
47 protected AlignmentPanel alignPanel;
49 ScrollThread scrollThread = null;
58 boolean mouseDragging = false;
60 private final SequenceAnnotationReport seqAnnotReport;
63 * Creates a new IdPanel object.
70 public IdPanel(AlignViewport av, AlignmentPanel parent)
74 idCanvas = new IdCanvas(av);
75 linkImageURL = getClass().getResource("/images/link.gif").toString();
76 seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
77 setLayout(new BorderLayout());
78 add(idCanvas, BorderLayout.CENTER);
79 addMouseListener(this);
80 addMouseMotionListener(this);
81 addMouseWheelListener(this);
82 ToolTipManager.sharedInstance().registerComponent(this);
92 public void mouseMoved(MouseEvent e)
94 SeqPanel sp = alignPanel.seqPanel;
95 int seq = Math.max(0, sp.findSeq(e));
96 if (seq > -1 && seq < av.getAlignment().getHeight())
98 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
99 StringBuffer tip = new StringBuffer();
101 .createSequenceAnnotationReport(tip, sequence,
102 av.isShowDbRefs(), av.isShowNpFeats(),
103 sp.seqCanvas.fr.minmax);
104 setToolTipText("<html>" + sequence.getDisplayId(true) + " "
105 + tip.toString() + "</html>");
116 public void mouseDragged(MouseEvent e)
118 mouseDragging = true;
120 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
124 selectSeqs(lastid - 1, seq);
126 else if (seq > lastid)
128 selectSeqs(lastid + 1, seq);
132 alignPanel.paintAlignment(true);
136 public void mouseWheelMoved(MouseWheelEvent e)
139 if (e.getWheelRotation() > 0)
143 alignPanel.scrollRight(true);
148 alignPanel.scrollUp(false);
155 alignPanel.scrollRight(false);
159 alignPanel.scrollUp(true);
171 public void mouseClicked(MouseEvent e)
173 if (e.getClickCount() < 2)
178 java.util.Vector links = Preferences.sequenceURLLinks;
179 if (links == null || links.size() < 1)
184 int seq = alignPanel.seqPanel.findSeq(e);
187 String id = av.getAlignment().getSequenceAt(seq).getName();
188 while (url == null && i < links.size())
190 // DEFAULT LINK IS FIRST IN THE LINK LIST
191 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
192 url = links.elementAt(i++).toString();
193 jalview.util.UrlLink urlLink = null;
196 urlLink = new UrlLink(url);
197 } catch (Exception foo)
199 jalview.bin.Cache.log.error("Exception for URLLink '" + url + "'",
205 if (!urlLink.isValid())
207 jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
212 String urls[] = urlLink.makeUrls(id, true);
213 if (urls == null || urls[0] == null || urls[0].length() < 4)
218 // just take first URL made from regex
223 jalview.util.BrowserLauncher.openURL(url);
224 } catch (Exception ex)
227 .showInternalMessageDialog(
229 "Unixers: Couldn't find default web browser."
230 + "\nAdd the full path to your browser in Preferences.",
231 "Web browser not found", JOptionPane.WARNING_MESSAGE);
232 ex.printStackTrace();
244 public void mouseEntered(MouseEvent e)
246 if (scrollThread != null)
248 scrollThread.running = false;
259 public void mouseExited(MouseEvent e)
261 if (av.getWrapAlignment())
266 if (mouseDragging && (e.getY() < 0) && (av.getStartSeq() > 0))
268 scrollThread = new ScrollThread(true);
271 if (mouseDragging && (e.getY() >= getHeight())
272 && (av.getAlignment().getHeight() > av.getEndSeq()))
274 scrollThread = new ScrollThread(false);
285 public void mousePressed(MouseEvent e)
287 if (e.getClickCount() == 2)
292 int seq = alignPanel.seqPanel.findSeq(e);
294 if (javax.swing.SwingUtilities.isRightMouseButton(e))
296 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
297 // build a new links menu based on the current links + any non-positional
299 Vector nlinks = new Vector(Preferences.sequenceURLLinks);
300 SequenceFeature sf[] = sq == null ? null : sq.getDatasetSequence()
301 .getSequenceFeatures();
302 for (int sl = 0; sf != null && sl < sf.length; sl++)
304 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
306 if (sf[sl].links != null && sf[sl].links.size() > 0)
308 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
310 nlinks.addElement(sf[sl].links.elementAt(l));
316 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
317 nlinks, new Vector(Preferences.getGroupURLLinks()));
318 pop.show(this, e.getX(), e.getY());
323 if ((av.getSelectionGroup() == null)
324 || ((!e.isControlDown() && !e.isShiftDown()) && av
325 .getSelectionGroup() != null))
327 av.setSelectionGroup(new SequenceGroup());
328 av.getSelectionGroup().setStartRes(0);
329 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
332 if (e.isShiftDown() && (lastid != -1))
334 selectSeqs(lastid, seq);
340 alignPanel.paintAlignment(true);
349 void selectSeq(int seq)
353 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
354 av.getSelectionGroup().addOrRemove(pickedSeq, true);
365 void selectSeqs(int start, int end)
367 if (av.getSelectionGroup() == null)
372 if (end >= av.getAlignment().getHeight())
374 end = av.getAlignment().getHeight() - 1;
387 for (int i = start; i <= end; i++)
389 av.getSelectionGroup().addSequence(
390 av.getAlignment().getSequenceAt(i), i == end);
401 public void mouseReleased(MouseEvent e)
403 if (scrollThread != null)
405 scrollThread.running = false;
408 mouseDragging = false;
409 PaintRefresher.Refresh(this, av.getSequenceSetId());
410 // always send selection message when mouse is released
420 public void highlightSearchResults(List<SequenceI> list)
422 idCanvas.setHighlighted(list);
429 int index = av.getAlignment().findIndex(list.get(0));
431 // do we need to scroll the panel?
432 if ((av.getStartSeq() > index) || (av.getEndSeq() < index))
434 alignPanel.setScrollValues(av.getStartRes(), index);
438 // this class allows scrolling off the bottom of the visible alignment
439 class ScrollThread extends Thread
441 boolean running = false;
445 public ScrollThread(boolean up)
451 public void stopScrolling()
463 if (alignPanel.scrollUp(up))
465 // scroll was ok, so add new sequence to selection
466 int seq = av.getStartSeq();
470 seq = av.getEndSeq();
475 selectSeqs(lastid - 1, seq);
477 else if (seq > lastid)
479 selectSeqs(lastid + 1, seq);
489 alignPanel.paintAlignment(false);
494 } catch (Exception ex)