2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
21 package jalview.appletgui;
23 import jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.UrlLink;
28 import jalview.viewmodel.AlignmentViewport;
30 import java.awt.BorderLayout;
31 import java.awt.Panel;
32 import java.awt.event.InputEvent;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.MouseListener;
35 import java.awt.event.MouseMotionListener;
36 import java.util.List;
37 import java.util.Vector;
39 public class IdPanel extends Panel implements MouseListener,
43 protected IdCanvas idCanvas;
45 protected AlignmentViewport av;
47 protected AlignmentPanel alignPanel;
49 ScrollThread scrollThread = null;
53 boolean mouseDragging = false;
55 java.util.Vector links = new java.util.Vector();
57 public IdPanel(AlignViewport av, AlignmentPanel parent)
61 idCanvas = new IdCanvas(av);
62 setLayout(new BorderLayout());
63 add(idCanvas, BorderLayout.CENTER);
64 idCanvas.addMouseListener(this);
65 idCanvas.addMouseMotionListener(this);
68 // TODO: add in group link parameter
69 if (av.applet != null)
71 for (int i = 1; i < 10; i++)
73 label = av.applet.getParameter("linkLabel_" + i);
74 url = av.applet.getParameter("linkURL_" + i);
76 if (label != null && url != null)
78 links.addElement(label + "|" + url);
84 // upgrade old SRS link
86 .indexOf("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
90 "EMBL-EBI Search|http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$",
96 links = new java.util.Vector();
97 links.addElement("EMBL-EBI Search|http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$");
103 public void mouseMoved(MouseEvent e)
105 int seq = alignPanel.seqPanel.findSeq(e);
107 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
109 // look for non-pos features
110 StringBuffer tooltiptext = new StringBuffer();
111 if (sequence != null)
113 if (sequence.getDescription() != null)
115 tooltiptext.append(sequence.getDescription());
116 tooltiptext.append("\n");
119 SequenceFeature sf[] = sequence.getSequenceFeatures();
120 for (int sl = 0; sf != null && sl < sf.length; sl++)
122 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
125 if (sf[sl].getFeatureGroup() != null)
127 tooltiptext.append(sf[sl].getFeatureGroup());
131 if (sf[sl].getType() != null)
133 tooltiptext.append(" ");
134 tooltiptext.append(sf[sl].getType());
138 if (sf[sl].getDescription() != null)
140 tooltiptext.append(" ");
141 tooltiptext.append(sf[sl].getDescription());
145 if (!Float.isNaN(sf[sl].getScore()) && sf[sl].getScore() != 0f)
147 tooltiptext.append(" Score = ");
148 tooltiptext.append(sf[sl].getScore());
152 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
154 tooltiptext.append(" (");
155 tooltiptext.append(sf[sl].getStatus());
156 tooltiptext.append(")");
162 tooltiptext.append("\n");
167 if (tooltiptext.length() == 0)
169 // nothing to display - so clear tooltip if one is visible
172 tooltip.setVisible(false);
180 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
181 + tooltiptext.toString(), idCanvas);
185 tooltip.setTip(sequence.getDisplayId(true) + "\n"
186 + tooltiptext.toString());
191 public void mouseDragged(MouseEvent e)
193 mouseDragging = true;
195 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
199 selectSeqs(lastid - 1, seq);
201 else if (seq > lastid)
203 selectSeqs(lastid + 1, seq);
207 alignPanel.paintAlignment(false);
210 public void mouseClicked(MouseEvent e)
212 if (e.getClickCount() < 2)
217 // DEFAULT LINK IS FIRST IN THE LINK LIST
218 int seq = alignPanel.seqPanel.findSeq(e);
219 SequenceI sq = av.getAlignment().getSequenceAt(seq);
224 String id = sq.getName();
226 String target = null;
229 while (url == null && i < links.size())
231 // DEFAULT LINK IS FIRST IN THE LINK LIST
232 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
233 url = links.elementAt(i++).toString();
234 jalview.util.UrlLink urlLink = null;
237 urlLink = new UrlLink(url);
238 target = urlLink.getTarget();
239 } catch (Exception foo)
241 System.err.println("Exception for URLLink '" + url + "'");
242 foo.printStackTrace();
247 if (!urlLink.isValid())
249 System.err.println(urlLink.getInvalidMessage());
254 String urls[] = urlLink.makeUrls(id, true);
255 if (urls == null || urls[0] == null || urls[0].length() < 1)
260 // just take first URL made from regex
266 alignPanel.alignFrame.showURL(url, target);
267 } catch (Exception ex)
269 ex.printStackTrace();
273 public void mouseEntered(MouseEvent e)
275 if (scrollThread != null)
277 scrollThread.running = false;
281 public void mouseExited(MouseEvent e)
283 if (av.getWrapAlignment())
288 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
290 scrollThread = new ScrollThread(true);
293 if (mouseDragging && e.getY() >= getSize().height
294 && av.getAlignment().getHeight() > av.getEndSeq())
296 scrollThread = new ScrollThread(false);
300 public void mousePressed(MouseEvent e)
302 if (e.getClickCount() > 1)
308 if (av.getWrapAlignment())
310 y -= 2 * av.getCharHeight();
313 int seq = alignPanel.seqPanel.findSeq(e);
315 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
317 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
319 // build a new links menu based on the current links + any non-positional
321 Vector nlinks = new Vector();
322 for (int l = 0, lSize = links.size(); l < lSize; l++)
324 nlinks.addElement(links.elementAt(l));
326 SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
327 for (int sl = 0; sf != null && sl < sf.length; sl++)
329 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
331 if (sf[sl].links != null && sf[sl].links.size() > 0)
333 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
335 nlinks.addElement(sf[sl].links.elementAt(l));
341 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
343 popup.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);
365 alignPanel.paintAlignment(false);
368 void selectSeq(int seq)
371 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
372 av.getSelectionGroup().addOrRemove(pickedSeq, true);
375 void selectSeqs(int start, int end)
380 if (end >= av.getAlignment().getHeight())
382 end = av.getAlignment().getHeight() - 1;
392 if (av.getSelectionGroup() == null)
394 av.setSelectionGroup(new SequenceGroup());
396 for (int i = start; i <= end; i++)
398 av.getSelectionGroup().addSequence(
399 av.getAlignment().getSequenceAt(i), i == end);
404 public void mouseReleased(MouseEvent e)
406 if (scrollThread != null)
408 scrollThread.running = false;
411 if (av.getSelectionGroup() != null)
413 av.getSelectionGroup().recalcConservation();
416 mouseDragging = false;
417 PaintRefresher.Refresh(this, av.getSequenceSetId());
418 // always send selection message when mouse is released
422 public void highlightSearchResults(List<SequenceI> list)
424 idCanvas.setHighlighted(list);
431 int index = av.getAlignment().findIndex(list.get(0));
433 // do we need to scroll the panel?
434 if (av.getStartSeq() > index || av.getEndSeq() < index)
436 alignPanel.setScrollValues(av.getStartRes(), index);
440 // this class allows scrolling off the bottom of the visible alignment
441 class ScrollThread extends Thread
443 boolean running = false;
447 public ScrollThread(boolean up)
453 public void stopScrolling()
463 if (alignPanel.scrollUp(up))
465 // scroll was ok, so add new sequence to selection
466 int seq = av.getStartSeq();
469 seq = av.getEndSeq();
474 selectSeqs(lastid - 1, seq);
476 else if (seq > lastid && seq < av.getAlignment().getHeight())
478 selectSeqs(lastid + 1, seq);
488 alignPanel.paintAlignment(true);
492 } catch (Exception ex)