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.api.UrlProviderFactoryI;
28 import jalview.urls.api.UrlProviderI;
29 import jalview.urls.applet.AppletUrlProviderFactory;
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.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
42 public class IdPanel extends Panel
43 implements MouseListener, MouseMotionListener
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 // only add non-null parameters
85 urlList.put(label, url);
89 if (!urlList.isEmpty())
91 // set default as first entry in list
92 String defaultUrl = av.applet.getParameter("linkLabel_1");
93 UrlProviderFactoryI factory = new AppletUrlProviderFactory(
95 urlProvider = factory.createUrlProvider();
103 public void mouseMoved(MouseEvent e)
105 int seq = alignPanel.seqPanel.findSeq(e);
107 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
109 // look for non-pos features
110 StringBuffer tooltiptext = new StringBuffer();
111 if (sequence != null)
113 if (sequence.getDescription() != null)
115 tooltiptext.append(sequence.getDescription());
116 tooltiptext.append("\n");
119 SequenceFeature sf[] = sequence.getSequenceFeatures();
120 for (int sl = 0; sf != null && sl < sf.length; sl++)
122 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
125 if (sf[sl].getFeatureGroup() != null)
127 tooltiptext.append(sf[sl].getFeatureGroup());
131 if (sf[sl].getType() != null)
133 tooltiptext.append(" ");
134 tooltiptext.append(sf[sl].getType());
138 if (sf[sl].getDescription() != null)
140 tooltiptext.append(" ");
141 tooltiptext.append(sf[sl].getDescription());
145 if (!Float.isNaN(sf[sl].getScore()) && sf[sl].getScore() != 0f)
147 tooltiptext.append(" Score = ");
148 tooltiptext.append(sf[sl].getScore());
152 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
154 tooltiptext.append(" (");
155 tooltiptext.append(sf[sl].getStatus());
156 tooltiptext.append(")");
162 tooltiptext.append("\n");
167 if (tooltiptext.length() == 0)
169 // nothing to display - so clear tooltip if one is visible
172 tooltip.setVisible(false);
180 tooltip = new Tooltip(
181 sequence.getDisplayId(true) + "\n" + tooltiptext.toString(),
187 sequence.getDisplayId(true) + "\n" + tooltiptext.toString());
193 public void mouseDragged(MouseEvent e)
195 mouseDragging = true;
197 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
201 selectSeqs(lastid - 1, seq);
203 else if (seq > lastid)
205 selectSeqs(lastid + 1, seq);
209 alignPanel.paintAlignment(false);
213 public void mouseClicked(MouseEvent e)
215 if (e.getClickCount() < 2)
220 // get the sequence details
221 int seq = alignPanel.seqPanel.findSeq(e);
222 SequenceI sq = av.getAlignment().getSequenceAt(seq);
227 String id = sq.getName();
229 // get the default url with the sequence details filled in
230 if (urlProvider == null)
234 String url = urlProvider.getPrimaryUrl(id);
235 String target = urlProvider.getPrimaryTarget(id);
238 alignPanel.alignFrame.showURL(url, target);
239 } catch (Exception ex)
241 ex.printStackTrace();
246 public void mouseEntered(MouseEvent e)
248 if (scrollThread != null)
250 scrollThread.running = false;
255 public void mouseExited(MouseEvent e)
257 if (av.getWrapAlignment())
262 if (mouseDragging && e.getY() < 0 && av.getRanges().getStartSeq() > 0)
264 scrollThread = new ScrollThread(true);
267 if (mouseDragging && e.getY() >= getSize().height
268 && av.getAlignment().getHeight() > av.getRanges().getEndSeq())
270 scrollThread = new ScrollThread(false);
275 public void mousePressed(MouseEvent e)
277 if (e.getClickCount() > 1)
283 if (av.getWrapAlignment())
285 y -= 2 * av.getCharHeight();
288 int seq = alignPanel.seqPanel.findSeq(e);
290 if ((e.getModifiers()
291 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
293 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
295 // build a new links menu based on the current links + any non-positional
298 if (urlProvider != null)
300 nlinks = urlProvider.getLinksForMenu();
304 nlinks = new ArrayList<String>();
306 SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
307 for (int sl = 0; sf != null && sl < sf.length; sl++)
309 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
311 if (sf[sl].links != null && sf[sl].links.size() > 0)
313 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
315 nlinks.add(sf[sl].links.elementAt(l));
321 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
323 popup.show(this, e.getX(), e.getY());
327 if ((av.getSelectionGroup() == null)
328 || ((!jalview.util.Platform.isControlDown(e)
329 && !e.isShiftDown()) && av.getSelectionGroup() != null))
331 av.setSelectionGroup(new SequenceGroup());
332 av.getSelectionGroup().setStartRes(0);
333 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
336 if (e.isShiftDown() && lastid != -1)
338 selectSeqs(lastid, seq);
345 alignPanel.paintAlignment(false);
348 void selectSeq(int seq)
351 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
352 av.getSelectionGroup().addOrRemove(pickedSeq, true);
355 void selectSeqs(int start, int end)
360 if (end >= av.getAlignment().getHeight())
362 end = av.getAlignment().getHeight() - 1;
372 if (av.getSelectionGroup() == null)
374 av.setSelectionGroup(new SequenceGroup());
376 for (int i = start; i <= end; i++)
378 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
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(List<SequenceI> list)
405 idCanvas.setHighlighted(list);
412 int index = av.getAlignment().findIndex(list.get(0));
414 // do we need to scroll the panel?
415 if (av.getRanges().getStartSeq() > index
416 || av.getRanges().getEndSeq() < index)
418 av.getRanges().setStartSeq(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()
446 if (av.getRanges().scrollUp(up))
448 // scroll was ok, so add new sequence to selection
449 int seq = av.getRanges().getStartSeq();
452 seq = av.getRanges().getEndSeq();
457 selectSeqs(lastid - 1, seq);
459 else if (seq > lastid && seq < av.getAlignment().getHeight())
461 selectSeqs(lastid + 1, seq);
471 alignPanel.paintAlignment(true);
475 } catch (Exception ex)