2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
22 import java.awt.event.*;
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();
80 .addElement("SRS|http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?-newId+(([uniprot-all:$SEQUENCE_ID$]))+-view+SwissEntry");
86 public void mouseMoved(MouseEvent e)
88 int seq = alignPanel.seqPanel.findSeq(e);
90 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
92 // look for non-pos features
93 StringBuffer tooltiptext = new StringBuffer();
96 if (sequence.getDescription() != null)
98 tooltiptext.append(sequence.getDescription());
99 tooltiptext.append("\n");
102 SequenceFeature sf[] = sequence.getSequenceFeatures();
103 for (int sl=0;sf!=null && sl<sf.length;sl++)
105 if (sf[sl].begin==sf[sl].end && sf[sl].begin==0)
108 if (sf[sl].getFeatureGroup()!=null) { tooltiptext.append(sf[sl].getFeatureGroup()); nl=true;};
109 if (sf[sl].getType()!=null) { tooltiptext.append(" "); tooltiptext.append(sf[sl].getType()); nl=true;};
110 if (sf[sl].getDescription()!=null) { tooltiptext.append(" "); tooltiptext.append(sf[sl].getDescription()); nl=true;};
111 if (sf[sl].getScore()!=Float.NaN && sf[sl].getScore()!=0f) { tooltiptext.append(" Score = "); tooltiptext.append(sf[sl].getScore()); nl=true;};
112 if (sf[sl].getStatus()!=null && sf[sl].getStatus().length()>0) { tooltiptext.append(" ("); tooltiptext.append(sf[sl].getStatus()); tooltiptext.append(")");nl=true;};
113 if (nl) {tooltiptext.append("\n"); }
117 if (tooltiptext.length()==0)
119 // nothing to display - so clear tooltip if one is visible
122 tooltip.setVisible(false);
130 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
131 + tooltiptext.toString(), idCanvas);
135 tooltip.setTip(sequence.getDisplayId(true) + "\n"
136 + tooltiptext.toString());
141 public void mouseDragged(MouseEvent e)
143 mouseDragging = true;
145 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
149 selectSeqs(lastid - 1, seq);
151 else if (seq > lastid)
153 selectSeqs(lastid + 1, seq);
157 alignPanel.paintAlignment(false);
160 public void mouseClicked(MouseEvent e)
162 if (e.getClickCount() < 2)
167 // DEFAULT LINK IS FIRST IN THE LINK LIST
168 int seq = alignPanel.seqPanel.findSeq(e);
169 String id = av.getAlignment().getSequenceAt(seq).getName();
171 String target = null;
174 while (url == null && i < links.size())
176 // DEFAULT LINK IS FIRST IN THE LINK LIST
177 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
178 url = links.elementAt(i++).toString();
179 jalview.util.UrlLink urlLink = null;
182 urlLink = new UrlLink(url);
183 target = urlLink.getTarget();
184 } catch (Exception foo)
186 System.err.println("Exception for URLLink '" + url + "'");
187 foo.printStackTrace();
192 if (!urlLink.isValid())
194 System.err.println(urlLink.getInvalidMessage());
199 String urls[] = urlLink.makeUrls(id, true);
200 if (urls == null || urls[0] == null || urls[0].length() < 1)
205 // just take first URL made from regex
211 alignPanel.alignFrame.showURL(url, target);
212 } catch (Exception ex)
214 ex.printStackTrace();
218 public void mouseEntered(MouseEvent e)
220 if (scrollThread != null)
222 scrollThread.running = false;
226 public void mouseExited(MouseEvent e)
228 if (av.getWrapAlignment())
233 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
235 scrollThread = new ScrollThread(true);
238 if (mouseDragging && e.getY() >= getSize().height
239 && av.alignment.getHeight() > av.getEndSeq())
241 scrollThread = new ScrollThread(false);
245 public void mousePressed(MouseEvent e)
247 if (e.getClickCount() > 1)
253 if (av.getWrapAlignment())
255 y -= 2 * av.charHeight;
258 int seq = alignPanel.seqPanel.findSeq(e);
260 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
262 Sequence sq = (Sequence) av
263 .getAlignment().getSequenceAt(seq);
264 // build a new links menu based on the current links + any non-positional features
265 Vector nlinks = new Vector();
266 for (int l=0,lSize=links.size();l<lSize; l++)
268 nlinks.addElement(links.elementAt(l));
270 SequenceFeature sf[] = sq.getSequenceFeatures();
271 for (int sl=0;sf!=null && sl<sf.length;sl++)
273 if (sf[sl].begin==sf[sl].end && sf[sl].begin==0)
275 if (sf[sl].links!=null && sf[sl].links.size()>0)
277 for (int l=0, lSize=sf[sl].links.size(); l<lSize; l++)
279 nlinks.addElement(sf[sl].links.elementAt(l));
285 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
287 popup.show(this, e.getX(), e.getY());
291 if ((av.getSelectionGroup() == null)
292 || ((!e.isControlDown() && !e.isShiftDown()) && av
293 .getSelectionGroup() != null))
295 av.setSelectionGroup(new SequenceGroup());
296 av.getSelectionGroup().setStartRes(0);
297 av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
300 if (e.isShiftDown() && lastid != -1)
302 selectSeqs(lastid, seq);
309 alignPanel.paintAlignment(false);
312 void selectSeq(int seq)
315 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
316 av.getSelectionGroup().addOrRemove(pickedSeq, false);
319 void selectSeqs(int start, int end)
324 if (end >= av.getAlignment().getHeight())
326 end = av.getAlignment().getHeight() - 1;
337 for (int i = start; i <= end; i++)
339 av.getSelectionGroup().addSequence(
340 av.getAlignment().getSequenceAt(i), false);
345 public void mouseReleased(MouseEvent e)
347 if (scrollThread != null)
349 scrollThread.running = false;
352 if (av.getSelectionGroup() != null)
354 av.getSelectionGroup().recalcConservation();
357 mouseDragging = false;
358 PaintRefresher.Refresh(this, av.getSequenceSetId());
361 public void highlightSearchResults(java.util.Vector found)
363 idCanvas.setHighlighted(found);
370 int index = av.alignment.findIndex((SequenceI) found.elementAt(0));
372 // do we need to scroll the panel?
373 if (av.getStartSeq() > index || av.getEndSeq() < index)
375 alignPanel.setScrollValues(av.getStartRes(), index);
379 // this class allows scrolling off the bottom of the visible alignment
380 class ScrollThread extends Thread
382 boolean running = false;
386 public ScrollThread(boolean up)
392 public void stopScrolling()
402 if (alignPanel.scrollUp(up))
404 // scroll was ok, so add new sequence to selection
405 int seq = av.getStartSeq();
408 seq = av.getEndSeq();
413 selectSeqs(lastid - 1, seq);
415 else if (seq > lastid && seq < av.alignment.getHeight())
417 selectSeqs(lastid + 1, seq);
427 alignPanel.paintAlignment(true);
431 } catch (Exception ex)