2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5.1)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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.Vector;
24 import jalview.datamodel.*;
25 import jalview.util.UrlLink;
27 public class IdPanel extends Panel implements MouseListener,
31 protected IdCanvas idCanvas;
33 protected AlignViewport av;
35 protected AlignmentPanel alignPanel;
37 ScrollThread scrollThread = null;
45 boolean mouseDragging = false;
47 java.util.Vector links = new java.util.Vector();
49 public IdPanel(AlignViewport av, AlignmentPanel parent)
53 idCanvas = new IdCanvas(av);
54 setLayout(new BorderLayout());
55 add(idCanvas, BorderLayout.CENTER);
56 idCanvas.addMouseListener(this);
57 idCanvas.addMouseMotionListener(this);
60 // TODO: add in group link parameter
61 if (av.applet != null)
63 for (int i = 1; i < 10; i++)
65 label = av.applet.getParameter("linkLabel_" + i);
66 url = av.applet.getParameter("linkURL_" + i);
68 if (label != null && url != null)
70 links.addElement(label + "|" + url);
77 links = new java.util.Vector();
79 .addElement("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
85 public void mouseMoved(MouseEvent e)
87 int seq = alignPanel.seqPanel.findSeq(e);
89 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
91 // look for non-pos features
92 StringBuffer tooltiptext = new StringBuffer();
94 if (sequence.getDescription() != null)
96 tooltiptext.append(sequence.getDescription());
97 tooltiptext.append("\n");
100 SequenceFeature sf[] = sequence.getSequenceFeatures();
101 for (int sl = 0; sf != null && sl < sf.length; sl++)
103 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
106 if (sf[sl].getFeatureGroup() != null)
108 tooltiptext.append(sf[sl].getFeatureGroup());
112 if (sf[sl].getType() != null)
114 tooltiptext.append(" ");
115 tooltiptext.append(sf[sl].getType());
119 if (sf[sl].getDescription() != null)
121 tooltiptext.append(" ");
122 tooltiptext.append(sf[sl].getDescription());
126 if (sf[sl].getScore() != Float.NaN && sf[sl].getScore() != 0f)
128 tooltiptext.append(" Score = ");
129 tooltiptext.append(sf[sl].getScore());
133 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
135 tooltiptext.append(" (");
136 tooltiptext.append(sf[sl].getStatus());
137 tooltiptext.append(")");
143 tooltiptext.append("\n");
148 if (tooltiptext.length() == 0)
150 // nothing to display - so clear tooltip if one is visible
153 tooltip.setVisible(false);
161 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
162 + tooltiptext.toString(), idCanvas);
166 tooltip.setTip(sequence.getDisplayId(true) + "\n"
167 + tooltiptext.toString());
172 public void mouseDragged(MouseEvent e)
174 mouseDragging = true;
176 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
180 selectSeqs(lastid - 1, seq);
182 else if (seq > lastid)
184 selectSeqs(lastid + 1, seq);
188 alignPanel.paintAlignment(false);
191 public void mouseClicked(MouseEvent e)
193 if (e.getClickCount() < 2)
198 // DEFAULT LINK IS FIRST IN THE LINK LIST
199 int seq = alignPanel.seqPanel.findSeq(e);
200 String id = av.getAlignment().getSequenceAt(seq).getName();
202 String target = null;
205 while (url == null && i < links.size())
207 // DEFAULT LINK IS FIRST IN THE LINK LIST
208 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
209 url = links.elementAt(i++).toString();
210 jalview.util.UrlLink urlLink = null;
213 urlLink = new UrlLink(url);
214 target = urlLink.getTarget();
215 } catch (Exception foo)
217 System.err.println("Exception for URLLink '" + url + "'");
218 foo.printStackTrace();
223 if (!urlLink.isValid())
225 System.err.println(urlLink.getInvalidMessage());
230 String urls[] = urlLink.makeUrls(id, true);
231 if (urls == null || urls[0] == null || urls[0].length() < 1)
236 // just take first URL made from regex
242 alignPanel.alignFrame.showURL(url, target);
243 } catch (Exception ex)
245 ex.printStackTrace();
249 public void mouseEntered(MouseEvent e)
251 if (scrollThread != null)
253 scrollThread.running = false;
257 public void mouseExited(MouseEvent e)
259 if (av.getWrapAlignment())
264 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
266 scrollThread = new ScrollThread(true);
269 if (mouseDragging && e.getY() >= getSize().height
270 && av.alignment.getHeight() > av.getEndSeq())
272 scrollThread = new ScrollThread(false);
276 public void mousePressed(MouseEvent e)
278 if (e.getClickCount() > 1)
284 if (av.getWrapAlignment())
286 y -= 2 * av.charHeight;
289 int seq = alignPanel.seqPanel.findSeq(e);
291 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
293 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
294 // build a new links menu based on the current links + any non-positional
296 Vector nlinks = new Vector();
297 for (int l = 0, lSize = links.size(); l < lSize; l++)
299 nlinks.addElement(links.elementAt(l));
301 SequenceFeature sf[] = sq.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 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
318 popup.show(this, e.getX(), e.getY());
322 if ((av.getSelectionGroup() == null)
323 || ((!e.isControlDown() && !e.isShiftDown()) && av
324 .getSelectionGroup() != null))
326 av.setSelectionGroup(new SequenceGroup());
327 av.getSelectionGroup().setStartRes(0);
328 av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
331 if (e.isShiftDown() && lastid != -1)
333 selectSeqs(lastid, seq);
340 alignPanel.paintAlignment(false);
343 void selectSeq(int seq)
346 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
347 av.getSelectionGroup().addOrRemove(pickedSeq, false);
350 void selectSeqs(int start, int end)
355 if (end >= av.getAlignment().getHeight())
357 end = av.getAlignment().getHeight() - 1;
368 for (int i = start; i <= end; i++)
370 av.getSelectionGroup().addSequence(
371 av.getAlignment().getSequenceAt(i), false);
376 public void mouseReleased(MouseEvent e)
378 if (scrollThread != null)
380 scrollThread.running = false;
383 if (av.getSelectionGroup() != null)
385 av.getSelectionGroup().recalcConservation();
388 mouseDragging = false;
389 PaintRefresher.Refresh(this, av.getSequenceSetId());
392 public void highlightSearchResults(java.util.Vector found)
394 idCanvas.setHighlighted(found);
401 int index = av.alignment.findIndex((SequenceI) found.elementAt(0));
403 // do we need to scroll the panel?
404 if (av.getStartSeq() > index || av.getEndSeq() < index)
406 alignPanel.setScrollValues(av.getStartRes(), index);
410 // this class allows scrolling off the bottom of the visible alignment
411 class ScrollThread extends Thread
413 boolean running = false;
417 public ScrollThread(boolean up)
423 public void stopScrolling()
433 if (alignPanel.scrollUp(up))
435 // scroll was ok, so add new sequence to selection
436 int seq = av.getStartSeq();
439 seq = av.getEndSeq();
444 selectSeqs(lastid - 1, seq);
446 else if (seq > lastid && seq < av.alignment.getHeight())
448 selectSeqs(lastid + 1, seq);
458 alignPanel.paintAlignment(true);
462 } catch (Exception ex)