2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 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();
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) { tooltiptext.append(sf[sl].getFeatureGroup()); nl=true;};
108 if (sf[sl].getType()!=null) { tooltiptext.append(" "); tooltiptext.append(sf[sl].getType()); nl=true;};
109 if (sf[sl].getDescription()!=null) { tooltiptext.append(" "); tooltiptext.append(sf[sl].getDescription()); nl=true;};
110 if (sf[sl].getScore()!=Float.NaN && sf[sl].getScore()!=0f) { tooltiptext.append(" Score = "); tooltiptext.append(sf[sl].getScore()); nl=true;};
111 if (sf[sl].getStatus()!=null && sf[sl].getStatus().length()>0) { tooltiptext.append(" ("); tooltiptext.append(sf[sl].getStatus()); tooltiptext.append(")");nl=true;};
112 if (nl) {tooltiptext.append("\n"); }
116 if (tooltiptext.length()==0)
118 // nothing to display - so clear tooltip if one is visible
121 tooltip.setVisible(false);
129 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
130 + tooltiptext.toString(), idCanvas);
134 tooltip.setTip(sequence.getDisplayId(true) + "\n"
135 + tooltiptext.toString());
140 public void mouseDragged(MouseEvent e)
142 mouseDragging = true;
144 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
148 selectSeqs(lastid - 1, seq);
150 else if (seq > lastid)
152 selectSeqs(lastid + 1, seq);
156 alignPanel.paintAlignment(false);
159 public void mouseClicked(MouseEvent e)
161 if (e.getClickCount() < 2)
166 // DEFAULT LINK IS FIRST IN THE LINK LIST
167 int seq = alignPanel.seqPanel.findSeq(e);
168 String id = av.getAlignment().getSequenceAt(seq).getName();
170 String target = null;
173 while (url == null && i < links.size())
175 // DEFAULT LINK IS FIRST IN THE LINK LIST
176 // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
177 url = links.elementAt(i++).toString();
178 jalview.util.UrlLink urlLink = null;
181 urlLink = new UrlLink(url);
182 target = urlLink.getTarget();
183 } catch (Exception foo)
185 System.err.println("Exception for URLLink '" + url + "'");
186 foo.printStackTrace();
191 if (!urlLink.isValid())
193 System.err.println(urlLink.getInvalidMessage());
198 String urls[] = urlLink.makeUrls(id, true);
199 if (urls == null || urls[0] == null || urls[0].length() < 1)
204 // just take first URL made from regex
210 alignPanel.alignFrame.showURL(url, target);
211 } catch (Exception ex)
213 ex.printStackTrace();
217 public void mouseEntered(MouseEvent e)
219 if (scrollThread != null)
221 scrollThread.running = false;
225 public void mouseExited(MouseEvent e)
227 if (av.getWrapAlignment())
232 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
234 scrollThread = new ScrollThread(true);
237 if (mouseDragging && e.getY() >= getSize().height
238 && av.alignment.getHeight() > av.getEndSeq())
240 scrollThread = new ScrollThread(false);
244 public void mousePressed(MouseEvent e)
246 if (e.getClickCount() > 1)
252 if (av.getWrapAlignment())
254 y -= 2 * av.charHeight;
257 int seq = alignPanel.seqPanel.findSeq(e);
259 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
261 Sequence sq = (Sequence) av
262 .getAlignment().getSequenceAt(seq);
263 // build a new links menu based on the current links + any non-positional features
264 Vector nlinks = new Vector();
265 for (int l=0,lSize=links.size();l<lSize; l++)
267 nlinks.addElement(links.elementAt(l));
269 SequenceFeature sf[] = sq.getSequenceFeatures();
270 for (int sl=0;sf!=null && sl<sf.length;sl++)
272 if (sf[sl].begin==sf[sl].end && sf[sl].begin==0)
274 if (sf[sl].links!=null && sf[sl].links.size()>0)
276 for (int l=0, lSize=sf[sl].links.size(); l<lSize; l++)
278 nlinks.addElement(sf[sl].links.elementAt(l));
284 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
286 popup.show(this, e.getX(), e.getY());
290 if ((av.getSelectionGroup() == null)
291 || ((!e.isControlDown() && !e.isShiftDown()) && av
292 .getSelectionGroup() != null))
294 av.setSelectionGroup(new SequenceGroup());
295 av.getSelectionGroup().setStartRes(0);
296 av.getSelectionGroup().setEndRes(av.alignment.getWidth() - 1);
299 if (e.isShiftDown() && lastid != -1)
301 selectSeqs(lastid, seq);
308 alignPanel.paintAlignment(false);
311 void selectSeq(int seq)
314 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
315 av.getSelectionGroup().addOrRemove(pickedSeq, false);
318 void selectSeqs(int start, int end)
323 if (end >= av.getAlignment().getHeight())
325 end = av.getAlignment().getHeight() - 1;
336 for (int i = start; i <= end; i++)
338 av.getSelectionGroup().addSequence(
339 av.getAlignment().getSequenceAt(i), false);
344 public void mouseReleased(MouseEvent e)
346 if (scrollThread != null)
348 scrollThread.running = false;
351 if (av.getSelectionGroup() != null)
353 av.getSelectionGroup().recalcConservation();
356 mouseDragging = false;
357 PaintRefresher.Refresh(this, av.getSequenceSetId());
360 public void highlightSearchResults(java.util.Vector found)
362 idCanvas.setHighlighted(found);
369 int index = av.alignment.findIndex((SequenceI) found.elementAt(0));
371 // do we need to scroll the panel?
372 if (av.getStartSeq() > index || av.getEndSeq() < index)
374 alignPanel.setScrollValues(av.getStartRes(), index);
378 // this class allows scrolling off the bottom of the visible alignment
379 class ScrollThread extends Thread
381 boolean running = false;
385 public ScrollThread(boolean up)
391 public void stopScrolling()
401 if (alignPanel.scrollUp(up))
403 // scroll was ok, so add new sequence to selection
404 int seq = av.getStartSeq();
407 seq = av.getEndSeq();
412 selectSeqs(lastid - 1, seq);
414 else if (seq > lastid && seq < av.alignment.getHeight())
416 selectSeqs(lastid + 1, seq);
426 alignPanel.paintAlignment(true);
430 } catch (Exception ex)