2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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();
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 SequenceI sq = av.getAlignment().getSequenceAt(seq);
205 String id = sq.getName();
207 String target = null;
210 while (url == null && i < links.size())
212 // DEFAULT LINK IS FIRST IN THE LINK LIST
213 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
214 url = links.elementAt(i++).toString();
215 jalview.util.UrlLink urlLink = null;
218 urlLink = new UrlLink(url);
219 target = urlLink.getTarget();
220 } catch (Exception foo)
222 System.err.println("Exception for URLLink '" + url + "'");
223 foo.printStackTrace();
228 if (!urlLink.isValid())
230 System.err.println(urlLink.getInvalidMessage());
235 String urls[] = urlLink.makeUrls(id, true);
236 if (urls == null || urls[0] == null || urls[0].length() < 1)
241 // just take first URL made from regex
247 alignPanel.alignFrame.showURL(url, target);
248 } catch (Exception ex)
250 ex.printStackTrace();
254 public void mouseEntered(MouseEvent e)
256 if (scrollThread != null)
258 scrollThread.running = false;
262 public void mouseExited(MouseEvent e)
264 if (av.getWrapAlignment())
269 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
271 scrollThread = new ScrollThread(true);
274 if (mouseDragging && e.getY() >= getSize().height
275 && av.getAlignment().getHeight() > av.getEndSeq())
277 scrollThread = new ScrollThread(false);
281 public void mousePressed(MouseEvent e)
283 if (e.getClickCount() > 1)
289 if (av.getWrapAlignment())
291 y -= 2 * av.charHeight;
294 int seq = alignPanel.seqPanel.findSeq(e);
296 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
298 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
300 // build a new links menu based on the current links + any non-positional
302 Vector nlinks = new Vector();
303 for (int l = 0, lSize = links.size(); l < lSize; l++)
305 nlinks.addElement(links.elementAt(l));
307 SequenceFeature sf[] = sq==null ? null:sq.getSequenceFeatures();
308 for (int sl = 0; sf != null && sl < sf.length; sl++)
310 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
312 if (sf[sl].links != null && sf[sl].links.size() > 0)
314 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
316 nlinks.addElement(sf[sl].links.elementAt(l));
322 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
324 popup.show(this, e.getX(), e.getY());
328 if ((av.getSelectionGroup() == null)
329 || ((!e.isControlDown() && !e.isShiftDown()) && av
330 .getSelectionGroup() != null))
332 av.setSelectionGroup(new SequenceGroup());
333 av.getSelectionGroup().setStartRes(0);
334 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
337 if (e.isShiftDown() && lastid != -1)
339 selectSeqs(lastid, seq);
346 alignPanel.paintAlignment(false);
349 void selectSeq(int seq)
352 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
353 av.getSelectionGroup().addOrRemove(pickedSeq, false);
356 void selectSeqs(int start, int end)
361 if (end >= av.getAlignment().getHeight())
363 end = av.getAlignment().getHeight() - 1;
373 if (av.getSelectionGroup() == null)
375 av.setSelectionGroup(new SequenceGroup());
377 for (int i = start; i <= end; i++)
379 av.getSelectionGroup().addSequence(
380 av.getAlignment().getSequenceAt(i), false);
385 public void mouseReleased(MouseEvent e)
387 if (scrollThread != null)
389 scrollThread.running = false;
392 if (av.getSelectionGroup() != null)
394 av.getSelectionGroup().recalcConservation();
397 mouseDragging = false;
398 PaintRefresher.Refresh(this, av.getSequenceSetId());
399 // always send selection message when mouse is released
403 public void highlightSearchResults(java.util.Vector found)
405 idCanvas.setHighlighted(found);
412 int index = av.getAlignment().findIndex((SequenceI) found.elementAt(0));
414 // do we need to scroll the panel?
415 if (av.getStartSeq() > index || av.getEndSeq() < index)
417 alignPanel.setScrollValues(av.getStartRes(), index);
421 // this class allows scrolling off the bottom of the visible alignment
422 class ScrollThread extends Thread
424 boolean running = false;
428 public ScrollThread(boolean up)
434 public void stopScrolling()
444 if (alignPanel.scrollUp(up))
446 // scroll was ok, so add new sequence to selection
447 int seq = av.getStartSeq();
450 seq = av.getEndSeq();
455 selectSeqs(lastid - 1, seq);
457 else if (seq > lastid && seq < av.getAlignment().getHeight())
459 selectSeqs(lastid + 1, seq);
469 alignPanel.paintAlignment(true);
473 } catch (Exception ex)