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);
78 links = new java.util.Vector();
79 links.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();
95 if (sequence.getDescription() != null)
97 tooltiptext.append(sequence.getDescription());
98 tooltiptext.append("\n");
101 SequenceFeature sf[] = sequence.getSequenceFeatures();
102 for (int sl = 0; sf != null && sl < sf.length; sl++)
104 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
107 if (sf[sl].getFeatureGroup() != null)
109 tooltiptext.append(sf[sl].getFeatureGroup());
113 if (sf[sl].getType() != null)
115 tooltiptext.append(" ");
116 tooltiptext.append(sf[sl].getType());
120 if (sf[sl].getDescription() != null)
122 tooltiptext.append(" ");
123 tooltiptext.append(sf[sl].getDescription());
127 if (sf[sl].getScore() != Float.NaN && sf[sl].getScore() != 0f)
129 tooltiptext.append(" Score = ");
130 tooltiptext.append(sf[sl].getScore());
134 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
136 tooltiptext.append(" (");
137 tooltiptext.append(sf[sl].getStatus());
138 tooltiptext.append(")");
144 tooltiptext.append("\n");
149 if (tooltiptext.length() == 0)
151 // nothing to display - so clear tooltip if one is visible
154 tooltip.setVisible(false);
162 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
163 + tooltiptext.toString(), idCanvas);
167 tooltip.setTip(sequence.getDisplayId(true) + "\n"
168 + tooltiptext.toString());
173 public void mouseDragged(MouseEvent e)
175 mouseDragging = true;
177 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
181 selectSeqs(lastid - 1, seq);
183 else if (seq > lastid)
185 selectSeqs(lastid + 1, seq);
189 alignPanel.paintAlignment(false);
192 public void mouseClicked(MouseEvent e)
194 if (e.getClickCount() < 2)
199 // DEFAULT LINK IS FIRST IN THE LINK LIST
200 int seq = alignPanel.seqPanel.findSeq(e);
201 SequenceI sq = av.getAlignment().getSequenceAt(seq);
206 String id = sq.getName();
208 String target = null;
211 while (url == null && i < links.size())
213 // DEFAULT LINK IS FIRST IN THE LINK LIST
214 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
215 url = links.elementAt(i++).toString();
216 jalview.util.UrlLink urlLink = null;
219 urlLink = new UrlLink(url);
220 target = urlLink.getTarget();
221 } catch (Exception foo)
223 System.err.println("Exception for URLLink '" + url + "'");
224 foo.printStackTrace();
229 if (!urlLink.isValid())
231 System.err.println(urlLink.getInvalidMessage());
236 String urls[] = urlLink.makeUrls(id, true);
237 if (urls == null || urls[0] == null || urls[0].length() < 1)
242 // just take first URL made from regex
248 alignPanel.alignFrame.showURL(url, target);
249 } catch (Exception ex)
251 ex.printStackTrace();
255 public void mouseEntered(MouseEvent e)
257 if (scrollThread != null)
259 scrollThread.running = false;
263 public void mouseExited(MouseEvent e)
265 if (av.getWrapAlignment())
270 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
272 scrollThread = new ScrollThread(true);
275 if (mouseDragging && e.getY() >= getSize().height
276 && av.getAlignment().getHeight() > av.getEndSeq())
278 scrollThread = new ScrollThread(false);
282 public void mousePressed(MouseEvent e)
284 if (e.getClickCount() > 1)
290 if (av.getWrapAlignment())
292 y -= 2 * av.charHeight;
295 int seq = alignPanel.seqPanel.findSeq(e);
297 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
299 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
301 // build a new links menu based on the current links + any non-positional
303 Vector nlinks = new Vector();
304 for (int l = 0, lSize = links.size(); l < lSize; l++)
306 nlinks.addElement(links.elementAt(l));
308 SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
309 for (int sl = 0; sf != null && sl < sf.length; sl++)
311 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
313 if (sf[sl].links != null && sf[sl].links.size() > 0)
315 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
317 nlinks.addElement(sf[sl].links.elementAt(l));
323 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
325 popup.show(this, e.getX(), e.getY());
329 if ((av.getSelectionGroup() == null)
330 || ((!e.isControlDown() && !e.isShiftDown()) && av
331 .getSelectionGroup() != null))
333 av.setSelectionGroup(new SequenceGroup());
334 av.getSelectionGroup().setStartRes(0);
335 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
338 if (e.isShiftDown() && lastid != -1)
340 selectSeqs(lastid, seq);
347 alignPanel.paintAlignment(false);
350 void selectSeq(int seq)
353 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
354 av.getSelectionGroup().addOrRemove(pickedSeq, true);
357 void selectSeqs(int start, int end)
362 if (end >= av.getAlignment().getHeight())
364 end = av.getAlignment().getHeight() - 1;
374 if (av.getSelectionGroup() == null)
376 av.setSelectionGroup(new SequenceGroup());
378 for (int i = start; i <= end; i++)
380 av.getSelectionGroup().addSequence(
381 av.getAlignment().getSequenceAt(i), i == end);
386 public void mouseReleased(MouseEvent e)
388 if (scrollThread != null)
390 scrollThread.running = false;
393 if (av.getSelectionGroup() != null)
395 av.getSelectionGroup().recalcConservation();
398 mouseDragging = false;
399 PaintRefresher.Refresh(this, av.getSequenceSetId());
400 // always send selection message when mouse is released
404 public void highlightSearchResults(List<SequenceI> list)
406 idCanvas.setHighlighted(list);
413 int index = av.getAlignment().findIndex(list.get(0));
415 // do we need to scroll the panel?
416 if (av.getStartSeq() > index || av.getEndSeq() < index)
418 alignPanel.setScrollValues(av.getStartRes(), index);
422 // this class allows scrolling off the bottom of the visible alignment
423 class ScrollThread extends Thread
425 boolean running = false;
429 public ScrollThread(boolean up)
435 public void stopScrolling()
445 if (alignPanel.scrollUp(up))
447 // scroll was ok, so add new sequence to selection
448 int seq = av.getStartSeq();
451 seq = av.getEndSeq();
456 selectSeqs(lastid - 1, seq);
458 else if (seq > lastid && seq < av.getAlignment().getHeight())
460 selectSeqs(lastid + 1, seq);
470 alignPanel.paintAlignment(true);
474 } catch (Exception ex)