2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
18 package jalview.appletgui;
21 import java.awt.event.*;
22 import java.util.List;
23 import java.util.Vector;
25 import jalview.datamodel.*;
26 import jalview.util.UrlLink;
28 public class IdPanel extends Panel implements MouseListener,
32 protected IdCanvas idCanvas;
34 protected AlignViewport av;
36 protected AlignmentPanel alignPanel;
38 ScrollThread scrollThread = null;
46 boolean mouseDragging = false;
48 java.util.Vector links = new java.util.Vector();
50 public IdPanel(AlignViewport av, AlignmentPanel parent)
54 idCanvas = new IdCanvas(av);
55 setLayout(new BorderLayout());
56 add(idCanvas, BorderLayout.CENTER);
57 idCanvas.addMouseListener(this);
58 idCanvas.addMouseMotionListener(this);
61 // TODO: add in group link parameter
62 if (av.applet != null)
64 for (int i = 1; i < 10; i++)
66 label = av.applet.getParameter("linkLabel_" + i);
67 url = av.applet.getParameter("linkURL_" + i);
69 if (label != null && url != null)
71 links.addElement(label + "|" + url);
77 // upgrade old SRS link
79 .indexOf("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
83 "EMBL-EBI Search|http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$",
89 links = new java.util.Vector();
90 links.addElement("EMBL-EBI Search|http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$");
96 public void mouseMoved(MouseEvent e)
98 int seq = alignPanel.seqPanel.findSeq(e);
100 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
102 // look for non-pos features
103 StringBuffer tooltiptext = new StringBuffer();
104 if (sequence != null)
106 if (sequence.getDescription() != null)
108 tooltiptext.append(sequence.getDescription());
109 tooltiptext.append("\n");
112 SequenceFeature sf[] = sequence.getSequenceFeatures();
113 for (int sl = 0; sf != null && sl < sf.length; sl++)
115 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
118 if (sf[sl].getFeatureGroup() != null)
120 tooltiptext.append(sf[sl].getFeatureGroup());
124 if (sf[sl].getType() != null)
126 tooltiptext.append(" ");
127 tooltiptext.append(sf[sl].getType());
131 if (sf[sl].getDescription() != null)
133 tooltiptext.append(" ");
134 tooltiptext.append(sf[sl].getDescription());
138 if (sf[sl].getScore() != Float.NaN && sf[sl].getScore() != 0f)
140 tooltiptext.append(" Score = ");
141 tooltiptext.append(sf[sl].getScore());
145 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
147 tooltiptext.append(" (");
148 tooltiptext.append(sf[sl].getStatus());
149 tooltiptext.append(")");
155 tooltiptext.append("\n");
160 if (tooltiptext.length() == 0)
162 // nothing to display - so clear tooltip if one is visible
165 tooltip.setVisible(false);
173 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
174 + tooltiptext.toString(), idCanvas);
178 tooltip.setTip(sequence.getDisplayId(true) + "\n"
179 + tooltiptext.toString());
184 public void mouseDragged(MouseEvent e)
186 mouseDragging = true;
188 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
192 selectSeqs(lastid - 1, seq);
194 else if (seq > lastid)
196 selectSeqs(lastid + 1, seq);
200 alignPanel.paintAlignment(false);
203 public void mouseClicked(MouseEvent e)
205 if (e.getClickCount() < 2)
210 // DEFAULT LINK IS FIRST IN THE LINK LIST
211 int seq = alignPanel.seqPanel.findSeq(e);
212 SequenceI sq = av.getAlignment().getSequenceAt(seq);
217 String id = sq.getName();
219 String target = null;
222 while (url == null && i < links.size())
224 // DEFAULT LINK IS FIRST IN THE LINK LIST
225 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
226 url = links.elementAt(i++).toString();
227 jalview.util.UrlLink urlLink = null;
230 urlLink = new UrlLink(url);
231 target = urlLink.getTarget();
232 } catch (Exception foo)
234 System.err.println("Exception for URLLink '" + url + "'");
235 foo.printStackTrace();
240 if (!urlLink.isValid())
242 System.err.println(urlLink.getInvalidMessage());
247 String urls[] = urlLink.makeUrls(id, true);
248 if (urls == null || urls[0] == null || urls[0].length() < 1)
253 // just take first URL made from regex
259 alignPanel.alignFrame.showURL(url, target);
260 } catch (Exception ex)
262 ex.printStackTrace();
266 public void mouseEntered(MouseEvent e)
268 if (scrollThread != null)
270 scrollThread.running = false;
274 public void mouseExited(MouseEvent e)
276 if (av.getWrapAlignment())
281 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
283 scrollThread = new ScrollThread(true);
286 if (mouseDragging && e.getY() >= getSize().height
287 && av.getAlignment().getHeight() > av.getEndSeq())
289 scrollThread = new ScrollThread(false);
293 public void mousePressed(MouseEvent e)
295 if (e.getClickCount() > 1)
301 if (av.getWrapAlignment())
303 y -= 2 * av.charHeight;
306 int seq = alignPanel.seqPanel.findSeq(e);
308 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
310 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
312 // build a new links menu based on the current links + any non-positional
314 Vector nlinks = new Vector();
315 for (int l = 0, lSize = links.size(); l < lSize; l++)
317 nlinks.addElement(links.elementAt(l));
319 SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
320 for (int sl = 0; sf != null && sl < sf.length; sl++)
322 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
324 if (sf[sl].links != null && sf[sl].links.size() > 0)
326 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
328 nlinks.addElement(sf[sl].links.elementAt(l));
334 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
336 popup.show(this, e.getX(), e.getY());
340 if ((av.getSelectionGroup() == null)
341 || ((!e.isControlDown() && !e.isShiftDown()) && av
342 .getSelectionGroup() != null))
344 av.setSelectionGroup(new SequenceGroup());
345 av.getSelectionGroup().setStartRes(0);
346 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
349 if (e.isShiftDown() && lastid != -1)
351 selectSeqs(lastid, seq);
358 alignPanel.paintAlignment(false);
361 void selectSeq(int seq)
364 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
365 av.getSelectionGroup().addOrRemove(pickedSeq, true);
368 void selectSeqs(int start, int end)
373 if (end >= av.getAlignment().getHeight())
375 end = av.getAlignment().getHeight() - 1;
385 if (av.getSelectionGroup() == null)
387 av.setSelectionGroup(new SequenceGroup());
389 for (int i = start; i <= end; i++)
391 av.getSelectionGroup().addSequence(
392 av.getAlignment().getSequenceAt(i), i == end);
397 public void mouseReleased(MouseEvent e)
399 if (scrollThread != null)
401 scrollThread.running = false;
404 if (av.getSelectionGroup() != null)
406 av.getSelectionGroup().recalcConservation();
409 mouseDragging = false;
410 PaintRefresher.Refresh(this, av.getSequenceSetId());
411 // always send selection message when mouse is released
415 public void highlightSearchResults(List<SequenceI> list)
417 idCanvas.setHighlighted(list);
424 int index = av.getAlignment().findIndex(list.get(0));
426 // do we need to scroll the panel?
427 if (av.getStartSeq() > index || av.getEndSeq() < index)
429 alignPanel.setScrollValues(av.getStartRes(), index);
433 // this class allows scrolling off the bottom of the visible alignment
434 class ScrollThread extends Thread
436 boolean running = false;
440 public ScrollThread(boolean up)
446 public void stopScrolling()
456 if (alignPanel.scrollUp(up))
458 // scroll was ok, so add new sequence to selection
459 int seq = av.getStartSeq();
462 seq = av.getEndSeq();
467 selectSeqs(lastid - 1, seq);
469 else if (seq > lastid && seq < av.getAlignment().getHeight())
471 selectSeqs(lastid + 1, seq);
481 alignPanel.paintAlignment(true);
485 } catch (Exception ex)