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.HashMap;
39 import java.util.List;
41 public class IdPanel extends Panel implements MouseListener,
45 protected IdCanvas idCanvas;
47 protected AlignmentViewport av;
49 protected AlignmentPanel alignPanel;
51 ScrollThread scrollThread = null;
55 boolean mouseDragging = false;
57 UrlProviderI urlProvider = null;
59 public IdPanel(AlignViewport av, AlignmentPanel parent)
63 idCanvas = new IdCanvas(av);
64 setLayout(new BorderLayout());
65 add(idCanvas, BorderLayout.CENTER);
66 idCanvas.addMouseListener(this);
67 idCanvas.addMouseMotionListener(this);
70 // TODO: add in group link parameter
72 // make a list of label,url pairs
73 HashMap<String, String> urlList = new HashMap<String, String>();
74 if (av.applet != null)
76 for (int i = 1; i < 10; i++)
78 label = av.applet.getParameter("linkLabel_" + i);
79 url = av.applet.getParameter("linkURL_" + i);
81 // only add non-null parameters
84 urlList.put(label, url);
88 if (!urlList.isEmpty())
90 // set default as first entry in list
91 String defaultUrl = av.applet.getParameter("linkLabel_1");
92 UrlProviderFactoryI factory = new AppletUrlProviderFactory(
94 urlProvider = factory.createUrlProvider();
102 public void mouseMoved(MouseEvent e)
104 int seq = alignPanel.seqPanel.findSeq(e);
106 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
108 // look for non-pos features
109 StringBuffer tooltiptext = new StringBuffer();
110 if (sequence != null)
112 if (sequence.getDescription() != null)
114 tooltiptext.append(sequence.getDescription());
115 tooltiptext.append("\n");
118 SequenceFeature sf[] = sequence.getSequenceFeatures();
119 for (int sl = 0; sf != null && sl < sf.length; sl++)
121 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
124 if (sf[sl].getFeatureGroup() != null)
126 tooltiptext.append(sf[sl].getFeatureGroup());
130 if (sf[sl].getType() != null)
132 tooltiptext.append(" ");
133 tooltiptext.append(sf[sl].getType());
137 if (sf[sl].getDescription() != null)
139 tooltiptext.append(" ");
140 tooltiptext.append(sf[sl].getDescription());
144 if (!Float.isNaN(sf[sl].getScore()) && sf[sl].getScore() != 0f)
146 tooltiptext.append(" Score = ");
147 tooltiptext.append(sf[sl].getScore());
151 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
153 tooltiptext.append(" (");
154 tooltiptext.append(sf[sl].getStatus());
155 tooltiptext.append(")");
161 tooltiptext.append("\n");
166 if (tooltiptext.length() == 0)
168 // nothing to display - so clear tooltip if one is visible
171 tooltip.setVisible(false);
179 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
180 + tooltiptext.toString(), idCanvas);
184 tooltip.setTip(sequence.getDisplayId(true) + "\n"
185 + tooltiptext.toString());
191 public void mouseDragged(MouseEvent e)
193 mouseDragging = true;
195 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
199 selectSeqs(lastid - 1, seq);
201 else if (seq > lastid)
203 selectSeqs(lastid + 1, seq);
207 alignPanel.paintAlignment(false);
211 public void mouseClicked(MouseEvent e)
213 if (e.getClickCount() < 2)
218 // get the sequence details
219 int seq = alignPanel.seqPanel.findSeq(e);
220 SequenceI sq = av.getAlignment().getSequenceAt(seq);
225 String id = sq.getName();
227 // get the default url with the sequence details filled in
228 String url = urlProvider.getPrimaryUrl(id);
229 String target = urlProvider.getPrimaryTarget(id);
232 alignPanel.alignFrame.showURL(url, target);
233 } catch (Exception ex)
235 ex.printStackTrace();
240 public void mouseEntered(MouseEvent e)
242 if (scrollThread != null)
244 scrollThread.running = false;
249 public void mouseExited(MouseEvent e)
251 if (av.getWrapAlignment())
256 if (mouseDragging && e.getY() < 0 && av.getRanges().getStartSeq() > 0)
258 scrollThread = new ScrollThread(true);
261 if (mouseDragging && e.getY() >= getSize().height
262 && av.getAlignment().getHeight() > av.getRanges().getEndSeq())
264 scrollThread = new ScrollThread(false);
269 public void mousePressed(MouseEvent e)
271 if (e.getClickCount() > 1)
277 if (av.getWrapAlignment())
279 y -= 2 * av.getCharHeight();
282 int seq = alignPanel.seqPanel.findSeq(e);
284 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
286 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
288 // build a new links menu based on the current links + any non-positional
290 List<String> nlinks = urlProvider.getLinksForMenu();
292 SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
293 for (int sl = 0; sf != null && sl < sf.length; sl++)
295 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
297 if (sf[sl].links != null && sf[sl].links.size() > 0)
299 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
301 nlinks.add(sf[sl].links.elementAt(l));
307 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
309 popup.show(this, e.getX(), e.getY());
313 if ((av.getSelectionGroup() == null)
314 || ((!jalview.util.Platform.isControlDown(e) && !e
315 .isShiftDown()) && av.getSelectionGroup() != null))
317 av.setSelectionGroup(new SequenceGroup());
318 av.getSelectionGroup().setStartRes(0);
319 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
322 if (e.isShiftDown() && lastid != -1)
324 selectSeqs(lastid, seq);
331 alignPanel.paintAlignment(false);
334 void selectSeq(int seq)
337 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
338 av.getSelectionGroup().addOrRemove(pickedSeq, true);
341 void selectSeqs(int start, int end)
346 if (end >= av.getAlignment().getHeight())
348 end = av.getAlignment().getHeight() - 1;
358 if (av.getSelectionGroup() == null)
360 av.setSelectionGroup(new SequenceGroup());
362 for (int i = start; i <= end; i++)
364 av.getSelectionGroup().addSequence(
365 av.getAlignment().getSequenceAt(i), i == end);
371 public void mouseReleased(MouseEvent e)
373 if (scrollThread != null)
375 scrollThread.running = false;
378 if (av.getSelectionGroup() != null)
380 av.getSelectionGroup().recalcConservation();
383 mouseDragging = false;
384 PaintRefresher.Refresh(this, av.getSequenceSetId());
385 // always send selection message when mouse is released
389 public void highlightSearchResults(List<SequenceI> list)
391 idCanvas.setHighlighted(list);
398 int index = av.getAlignment().findIndex(list.get(0));
400 // do we need to scroll the panel?
401 if (av.getRanges().getStartSeq() > index
402 || av.getRanges().getEndSeq() < index)
404 alignPanel.setScrollValues(av.getRanges().getStartRes(), index);
408 // this class allows scrolling off the bottom of the visible alignment
409 class ScrollThread extends Thread
411 boolean running = false;
415 public ScrollThread(boolean up)
421 public void stopScrolling()
432 if (alignPanel.scrollUp(up))
434 // scroll was ok, so add new sequence to selection
435 int seq = av.getRanges().getStartSeq();
438 seq = av.getRanges().getEndSeq();
443 selectSeqs(lastid - 1, seq);
445 else if (seq > lastid && seq < av.getAlignment().getHeight())
447 selectSeqs(lastid + 1, seq);
457 alignPanel.paintAlignment(true);
461 } catch (Exception ex)