2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.urls.AppletUrlProviderFactory;
28 import jalview.urls.UrlProviderFactoryI;
29 import jalview.urls.UrlProviderI;
30 import jalview.viewmodel.AlignmentViewport;
32 import java.awt.BorderLayout;
33 import java.awt.Panel;
34 import java.awt.event.InputEvent;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Vector;
42 public class IdPanel extends Panel implements MouseListener,
46 protected IdCanvas idCanvas;
48 protected AlignmentViewport av;
50 protected AlignmentPanel alignPanel;
52 ScrollThread scrollThread = null;
56 boolean mouseDragging = false;
58 UrlProviderI urlProvider = null;
60 public IdPanel(AlignViewport av, AlignmentPanel parent)
64 idCanvas = new IdCanvas(av);
65 setLayout(new BorderLayout());
66 add(idCanvas, BorderLayout.CENTER);
67 idCanvas.addMouseListener(this);
68 idCanvas.addMouseMotionListener(this);
71 // TODO: add in group link parameter
73 // make a list of label,url pairs
74 HashMap<String, String> urlList = new HashMap<String, String>();
75 if (av.applet != null)
77 for (int i = 1; i < 10; i++)
79 label = av.applet.getParameter("linkLabel_" + i);
80 url = av.applet.getParameter("linkURL_" + i);
82 urlList.put(label, url);
85 if (!urlList.isEmpty())
87 // set default as first entry in list
88 String defaultUrl = av.applet.getParameter("linkLabel_1");
89 UrlProviderFactoryI factory = new AppletUrlProviderFactory(
91 urlProvider = factory.createUrlProvider();
99 public void mouseMoved(MouseEvent e)
101 int seq = alignPanel.seqPanel.findSeq(e);
103 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
105 // look for non-pos features
106 StringBuffer tooltiptext = new StringBuffer();
107 if (sequence != null)
109 if (sequence.getDescription() != null)
111 tooltiptext.append(sequence.getDescription());
112 tooltiptext.append("\n");
115 SequenceFeature sf[] = sequence.getSequenceFeatures();
116 for (int sl = 0; sf != null && sl < sf.length; sl++)
118 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
121 if (sf[sl].getFeatureGroup() != null)
123 tooltiptext.append(sf[sl].getFeatureGroup());
127 if (sf[sl].getType() != null)
129 tooltiptext.append(" ");
130 tooltiptext.append(sf[sl].getType());
134 if (sf[sl].getDescription() != null)
136 tooltiptext.append(" ");
137 tooltiptext.append(sf[sl].getDescription());
141 if (!Float.isNaN(sf[sl].getScore()) && sf[sl].getScore() != 0f)
143 tooltiptext.append(" Score = ");
144 tooltiptext.append(sf[sl].getScore());
148 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
150 tooltiptext.append(" (");
151 tooltiptext.append(sf[sl].getStatus());
152 tooltiptext.append(")");
158 tooltiptext.append("\n");
163 if (tooltiptext.length() == 0)
165 // nothing to display - so clear tooltip if one is visible
168 tooltip.setVisible(false);
176 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
177 + tooltiptext.toString(), idCanvas);
181 tooltip.setTip(sequence.getDisplayId(true) + "\n"
182 + tooltiptext.toString());
188 public void mouseDragged(MouseEvent e)
190 mouseDragging = true;
192 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
196 selectSeqs(lastid - 1, seq);
198 else if (seq > lastid)
200 selectSeqs(lastid + 1, seq);
204 alignPanel.paintAlignment(false);
208 public void mouseClicked(MouseEvent e)
210 if (e.getClickCount() < 2)
215 // get the sequence details
216 int seq = alignPanel.seqPanel.findSeq(e);
217 SequenceI sq = av.getAlignment().getSequenceAt(seq);
222 String id = sq.getName();
224 // get the default url with the sequence details filled in
225 String url = urlProvider.getDefaultUrl(id);
226 String target = urlProvider.getDefaultTarget(id);
229 alignPanel.alignFrame.showURL(url, target);
230 } catch (Exception ex)
232 ex.printStackTrace();
237 public void mouseEntered(MouseEvent e)
239 if (scrollThread != null)
241 scrollThread.running = false;
246 public void mouseExited(MouseEvent e)
248 if (av.getWrapAlignment())
253 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
255 scrollThread = new ScrollThread(true);
258 if (mouseDragging && e.getY() >= getSize().height
259 && av.getAlignment().getHeight() > av.getEndSeq())
261 scrollThread = new ScrollThread(false);
266 public void mousePressed(MouseEvent e)
268 if (e.getClickCount() > 1)
274 if (av.getWrapAlignment())
276 y -= 2 * av.getCharHeight();
279 int seq = alignPanel.seqPanel.findSeq(e);
281 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
283 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
285 // build a new links menu based on the current links + any non-positional
287 Vector<String> nlinks = urlProvider.getLinksForMenu();
289 SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
290 for (int sl = 0; sf != null && sl < sf.length; sl++)
292 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
294 if (sf[sl].links != null && sf[sl].links.size() > 0)
296 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
298 nlinks.addElement(sf[sl].links.elementAt(l));
304 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
306 popup.show(this, e.getX(), e.getY());
310 if ((av.getSelectionGroup() == null)
311 || ((!jalview.util.Platform.isControlDown(e) && !e
312 .isShiftDown()) && av.getSelectionGroup() != null))
314 av.setSelectionGroup(new SequenceGroup());
315 av.getSelectionGroup().setStartRes(0);
316 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
319 if (e.isShiftDown() && lastid != -1)
321 selectSeqs(lastid, seq);
328 alignPanel.paintAlignment(false);
331 void selectSeq(int seq)
334 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
335 av.getSelectionGroup().addOrRemove(pickedSeq, true);
338 void selectSeqs(int start, int end)
343 if (end >= av.getAlignment().getHeight())
345 end = av.getAlignment().getHeight() - 1;
355 if (av.getSelectionGroup() == null)
357 av.setSelectionGroup(new SequenceGroup());
359 for (int i = start; i <= end; i++)
361 av.getSelectionGroup().addSequence(
362 av.getAlignment().getSequenceAt(i), i == end);
368 public void mouseReleased(MouseEvent e)
370 if (scrollThread != null)
372 scrollThread.running = false;
375 if (av.getSelectionGroup() != null)
377 av.getSelectionGroup().recalcConservation();
380 mouseDragging = false;
381 PaintRefresher.Refresh(this, av.getSequenceSetId());
382 // always send selection message when mouse is released
386 public void highlightSearchResults(List<SequenceI> list)
388 idCanvas.setHighlighted(list);
395 int index = av.getAlignment().findIndex(list.get(0));
397 // do we need to scroll the panel?
398 if (av.getStartSeq() > index || av.getEndSeq() < index)
400 alignPanel.setScrollValues(av.getStartRes(), index);
404 // this class allows scrolling off the bottom of the visible alignment
405 class ScrollThread extends Thread
407 boolean running = false;
411 public ScrollThread(boolean up)
417 public void stopScrolling()
428 if (alignPanel.scrollUp(up))
430 // scroll was ok, so add new sequence to selection
431 int seq = av.getStartSeq();
434 seq = av.getEndSeq();
439 selectSeqs(lastid - 1, seq);
441 else if (seq > lastid && seq < av.getAlignment().getHeight())
443 selectSeqs(lastid + 1, seq);
453 alignPanel.paintAlignment(true);
457 } catch (Exception ex)