2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
22 import java.awt.event.*;
23 import java.util.List;
24 import java.util.Vector;
26 import jalview.datamodel.*;
27 import jalview.util.UrlLink;
29 public class IdPanel extends Panel implements MouseListener,
33 protected IdCanvas idCanvas;
35 protected AlignViewport av;
37 protected AlignmentPanel alignPanel;
39 ScrollThread scrollThread = null;
47 boolean mouseDragging = false;
49 java.util.Vector links = new java.util.Vector();
51 public IdPanel(AlignViewport av, AlignmentPanel parent)
55 idCanvas = new IdCanvas(av);
56 setLayout(new BorderLayout());
57 add(idCanvas, BorderLayout.CENTER);
58 idCanvas.addMouseListener(this);
59 idCanvas.addMouseMotionListener(this);
62 // TODO: add in group link parameter
63 if (av.applet != null)
65 for (int i = 1; i < 10; i++)
67 label = av.applet.getParameter("linkLabel_" + i);
68 url = av.applet.getParameter("linkURL_" + i);
70 if (label != null && url != null)
72 links.addElement(label + "|" + url);
78 // upgrade old SRS link
80 .indexOf("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
84 "EMBL-EBI Search|http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$",
90 links = new java.util.Vector();
91 links.addElement("EMBL-EBI Search|http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$");
97 public void mouseMoved(MouseEvent e)
99 int seq = alignPanel.seqPanel.findSeq(e);
101 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
103 // look for non-pos features
104 StringBuffer tooltiptext = new StringBuffer();
105 if (sequence != null)
107 if (sequence.getDescription() != null)
109 tooltiptext.append(sequence.getDescription());
110 tooltiptext.append("\n");
113 SequenceFeature sf[] = sequence.getSequenceFeatures();
114 for (int sl = 0; sf != null && sl < sf.length; sl++)
116 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
119 if (sf[sl].getFeatureGroup() != null)
121 tooltiptext.append(sf[sl].getFeatureGroup());
125 if (sf[sl].getType() != null)
127 tooltiptext.append(" ");
128 tooltiptext.append(sf[sl].getType());
132 if (sf[sl].getDescription() != null)
134 tooltiptext.append(" ");
135 tooltiptext.append(sf[sl].getDescription());
139 if (sf[sl].getScore() != Float.NaN && sf[sl].getScore() != 0f)
141 tooltiptext.append(" Score = ");
142 tooltiptext.append(sf[sl].getScore());
146 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
148 tooltiptext.append(" (");
149 tooltiptext.append(sf[sl].getStatus());
150 tooltiptext.append(")");
156 tooltiptext.append("\n");
161 if (tooltiptext.length() == 0)
163 // nothing to display - so clear tooltip if one is visible
166 tooltip.setVisible(false);
174 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
175 + tooltiptext.toString(), idCanvas);
179 tooltip.setTip(sequence.getDisplayId(true) + "\n"
180 + tooltiptext.toString());
185 public void mouseDragged(MouseEvent e)
187 mouseDragging = true;
189 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
193 selectSeqs(lastid - 1, seq);
195 else if (seq > lastid)
197 selectSeqs(lastid + 1, seq);
201 alignPanel.paintAlignment(false);
204 public void mouseClicked(MouseEvent e)
206 if (e.getClickCount() < 2)
211 // DEFAULT LINK IS FIRST IN THE LINK LIST
212 int seq = alignPanel.seqPanel.findSeq(e);
213 SequenceI sq = av.getAlignment().getSequenceAt(seq);
218 String id = sq.getName();
220 String target = null;
223 while (url == null && i < links.size())
225 // DEFAULT LINK IS FIRST IN THE LINK LIST
226 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
227 url = links.elementAt(i++).toString();
228 jalview.util.UrlLink urlLink = null;
231 urlLink = new UrlLink(url);
232 target = urlLink.getTarget();
233 } catch (Exception foo)
235 System.err.println("Exception for URLLink '" + url + "'");
236 foo.printStackTrace();
241 if (!urlLink.isValid())
243 System.err.println(urlLink.getInvalidMessage());
248 String urls[] = urlLink.makeUrls(id, true);
249 if (urls == null || urls[0] == null || urls[0].length() < 1)
254 // just take first URL made from regex
260 alignPanel.alignFrame.showURL(url, target);
261 } catch (Exception ex)
263 ex.printStackTrace();
267 public void mouseEntered(MouseEvent e)
269 if (scrollThread != null)
271 scrollThread.running = false;
275 public void mouseExited(MouseEvent e)
277 if (av.getWrapAlignment())
282 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
284 scrollThread = new ScrollThread(true);
287 if (mouseDragging && e.getY() >= getSize().height
288 && av.getAlignment().getHeight() > av.getEndSeq())
290 scrollThread = new ScrollThread(false);
294 public void mousePressed(MouseEvent e)
296 if (e.getClickCount() > 1)
302 if (av.getWrapAlignment())
304 y -= 2 * av.charHeight;
307 int seq = alignPanel.seqPanel.findSeq(e);
309 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
311 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
313 // build a new links menu based on the current links + any non-positional
315 Vector nlinks = new Vector();
316 for (int l = 0, lSize = links.size(); l < lSize; l++)
318 nlinks.addElement(links.elementAt(l));
320 SequenceFeature sf[] = sq == null ? null : sq.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 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
337 popup.show(this, e.getX(), e.getY());
341 if ((av.getSelectionGroup() == null)
342 || ((!e.isControlDown() && !e.isShiftDown()) && av
343 .getSelectionGroup() != null))
345 av.setSelectionGroup(new SequenceGroup());
346 av.getSelectionGroup().setStartRes(0);
347 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
350 if (e.isShiftDown() && lastid != -1)
352 selectSeqs(lastid, seq);
359 alignPanel.paintAlignment(false);
362 void selectSeq(int seq)
365 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
366 av.getSelectionGroup().addOrRemove(pickedSeq, true);
369 void selectSeqs(int start, int end)
374 if (end >= av.getAlignment().getHeight())
376 end = av.getAlignment().getHeight() - 1;
386 if (av.getSelectionGroup() == null)
388 av.setSelectionGroup(new SequenceGroup());
390 for (int i = start; i <= end; i++)
392 av.getSelectionGroup().addSequence(
393 av.getAlignment().getSequenceAt(i), i == end);
398 public void mouseReleased(MouseEvent e)
400 if (scrollThread != null)
402 scrollThread.running = false;
405 if (av.getSelectionGroup() != null)
407 av.getSelectionGroup().recalcConservation();
410 mouseDragging = false;
411 PaintRefresher.Refresh(this, av.getSequenceSetId());
412 // always send selection message when mouse is released
416 public void highlightSearchResults(List<SequenceI> list)
418 idCanvas.setHighlighted(list);
425 int index = av.getAlignment().findIndex(list.get(0));
427 // do we need to scroll the panel?
428 if (av.getStartSeq() > index || av.getEndSeq() < index)
430 alignPanel.setScrollValues(av.getStartRes(), index);
434 // this class allows scrolling off the bottom of the visible alignment
435 class ScrollThread extends Thread
437 boolean running = false;
441 public ScrollThread(boolean up)
447 public void stopScrolling()
457 if (alignPanel.scrollUp(up))
459 // scroll was ok, so add new sequence to selection
460 int seq = av.getStartSeq();
463 seq = av.getEndSeq();
468 selectSeqs(lastid - 1, seq);
470 else if (seq > lastid && seq < av.getAlignment().getHeight())
472 selectSeqs(lastid + 1, seq);
482 alignPanel.paintAlignment(true);
486 } catch (Exception ex)