2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
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();
78 links.addElement("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
84 public void mouseMoved(MouseEvent e)
86 int seq = alignPanel.seqPanel.findSeq(e);
88 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
90 // look for non-pos features
91 StringBuffer tooltiptext = new StringBuffer();
93 if (sequence.getDescription() != null)
95 tooltiptext.append(sequence.getDescription());
96 tooltiptext.append("\n");
99 SequenceFeature sf[] = sequence.getSequenceFeatures();
100 for (int sl = 0; sf != null && sl < sf.length; sl++)
102 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
105 if (sf[sl].getFeatureGroup() != null)
107 tooltiptext.append(sf[sl].getFeatureGroup());
111 if (sf[sl].getType() != null)
113 tooltiptext.append(" ");
114 tooltiptext.append(sf[sl].getType());
118 if (sf[sl].getDescription() != null)
120 tooltiptext.append(" ");
121 tooltiptext.append(sf[sl].getDescription());
125 if (sf[sl].getScore() != Float.NaN && sf[sl].getScore() != 0f)
127 tooltiptext.append(" Score = ");
128 tooltiptext.append(sf[sl].getScore());
132 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
134 tooltiptext.append(" (");
135 tooltiptext.append(sf[sl].getStatus());
136 tooltiptext.append(")");
142 tooltiptext.append("\n");
147 if (tooltiptext.length() == 0)
149 // nothing to display - so clear tooltip if one is visible
152 tooltip.setVisible(false);
160 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
161 + tooltiptext.toString(), idCanvas);
165 tooltip.setTip(sequence.getDisplayId(true) + "\n"
166 + tooltiptext.toString());
171 public void mouseDragged(MouseEvent e)
173 mouseDragging = true;
175 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
179 selectSeqs(lastid - 1, seq);
181 else if (seq > lastid)
183 selectSeqs(lastid + 1, seq);
187 alignPanel.paintAlignment(false);
190 public void mouseClicked(MouseEvent e)
192 if (e.getClickCount() < 2)
197 // DEFAULT LINK IS FIRST IN THE LINK LIST
198 int seq = alignPanel.seqPanel.findSeq(e);
199 String id = av.getAlignment().getSequenceAt(seq).getName();
201 String target = null;
204 while (url == null && i < links.size())
206 // DEFAULT LINK IS FIRST IN THE LINK LIST
207 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
208 url = links.elementAt(i++).toString();
209 jalview.util.UrlLink urlLink = null;
212 urlLink = new UrlLink(url);
213 target = urlLink.getTarget();
214 } catch (Exception foo)
216 System.err.println("Exception for URLLink '" + url + "'");
217 foo.printStackTrace();
222 if (!urlLink.isValid())
224 System.err.println(urlLink.getInvalidMessage());
229 String urls[] = urlLink.makeUrls(id, true);
230 if (urls == null || urls[0] == null || urls[0].length() < 1)
235 // just take first URL made from regex
241 alignPanel.alignFrame.showURL(url, target);
242 } catch (Exception ex)
244 ex.printStackTrace();
248 public void mouseEntered(MouseEvent e)
250 if (scrollThread != null)
252 scrollThread.running = false;
256 public void mouseExited(MouseEvent e)
258 if (av.getWrapAlignment())
263 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
265 scrollThread = new ScrollThread(true);
268 if (mouseDragging && e.getY() >= getSize().height
269 && av.alignment.getHeight() > av.getEndSeq())
271 scrollThread = new ScrollThread(false);
275 public void mousePressed(MouseEvent e)
277 if (e.getClickCount() > 1)
283 if (av.getWrapAlignment())
285 y -= 2 * av.charHeight;
288 int seq = alignPanel.seqPanel.findSeq(e);
290 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
292 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
293 // build a new links menu based on the current links + any non-positional
295 Vector nlinks = new Vector();
296 for (int l = 0, lSize = links.size(); l < lSize; l++)
298 nlinks.addElement(links.elementAt(l));
300 SequenceFeature sf[] = sq.getSequenceFeatures();
301 for (int sl = 0; sf != null && sl < sf.length; sl++)
303 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
305 if (sf[sl].links != null && sf[sl].links.size() > 0)
307 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
309 nlinks.addElement(sf[sl].links.elementAt(l));
315 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
317 popup.show(this, e.getX(), e.getY());
321 if ((av.getSelectionGroup() == null)
322 || ((!e.isControlDown() && !e.isShiftDown()) && av
323 .getSelectionGroup() != null))
325 av.setSelectionGroup(new SequenceGroup());
326 av.getSelectionGroup().setStartRes(0);
327 av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
330 if (e.isShiftDown() && lastid != -1)
332 selectSeqs(lastid, seq);
339 alignPanel.paintAlignment(false);
342 void selectSeq(int seq)
345 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
346 av.getSelectionGroup().addOrRemove(pickedSeq, false);
349 void selectSeqs(int start, int end)
354 if (end >= av.getAlignment().getHeight())
356 end = av.getAlignment().getHeight() - 1;
367 for (int i = start; i <= end; i++)
369 av.getSelectionGroup().addSequence(
370 av.getAlignment().getSequenceAt(i), false);
375 public void mouseReleased(MouseEvent e)
377 if (scrollThread != null)
379 scrollThread.running = false;
382 if (av.getSelectionGroup() != null)
384 av.getSelectionGroup().recalcConservation();
387 mouseDragging = false;
388 PaintRefresher.Refresh(this, av.getSequenceSetId());
391 public void highlightSearchResults(java.util.Vector found)
393 idCanvas.setHighlighted(found);
400 int index = av.alignment.findIndex((SequenceI) found.elementAt(0));
402 // do we need to scroll the panel?
403 if (av.getStartSeq() > index || av.getEndSeq() < index)
405 alignPanel.setScrollValues(av.getStartRes(), index);
409 // this class allows scrolling off the bottom of the visible alignment
410 class ScrollThread extends Thread
412 boolean running = false;
416 public ScrollThread(boolean up)
422 public void stopScrolling()
432 if (alignPanel.scrollUp(up))
434 // scroll was ok, so add new sequence to selection
435 int seq = av.getStartSeq();
438 seq = av.getEndSeq();
443 selectSeqs(lastid - 1, seq);
445 else if (seq > lastid && seq < av.alignment.getHeight())
447 selectSeqs(lastid + 1, seq);
457 alignPanel.paintAlignment(true);
461 } catch (Exception ex)