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 urlList.put(label, url);
84 if (!urlList.isEmpty())
86 // set default as first entry in list
87 String defaultUrl = av.applet.getParameter("linkLabel_1");
88 UrlProviderFactoryI factory = new AppletUrlProviderFactory(
90 urlProvider = factory.createUrlProvider();
98 public void mouseMoved(MouseEvent e)
100 int seq = alignPanel.seqPanel.findSeq(e);
102 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
104 // look for non-pos features
105 StringBuffer tooltiptext = new StringBuffer();
106 if (sequence != null)
108 if (sequence.getDescription() != null)
110 tooltiptext.append(sequence.getDescription());
111 tooltiptext.append("\n");
114 SequenceFeature sf[] = sequence.getSequenceFeatures();
115 for (int sl = 0; sf != null && sl < sf.length; sl++)
117 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
120 if (sf[sl].getFeatureGroup() != null)
122 tooltiptext.append(sf[sl].getFeatureGroup());
126 if (sf[sl].getType() != null)
128 tooltiptext.append(" ");
129 tooltiptext.append(sf[sl].getType());
133 if (sf[sl].getDescription() != null)
135 tooltiptext.append(" ");
136 tooltiptext.append(sf[sl].getDescription());
140 if (!Float.isNaN(sf[sl].getScore()) && sf[sl].getScore() != 0f)
142 tooltiptext.append(" Score = ");
143 tooltiptext.append(sf[sl].getScore());
147 if (sf[sl].getStatus() != null && sf[sl].getStatus().length() > 0)
149 tooltiptext.append(" (");
150 tooltiptext.append(sf[sl].getStatus());
151 tooltiptext.append(")");
157 tooltiptext.append("\n");
162 if (tooltiptext.length() == 0)
164 // nothing to display - so clear tooltip if one is visible
167 tooltip.setVisible(false);
175 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
176 + tooltiptext.toString(), idCanvas);
180 tooltip.setTip(sequence.getDisplayId(true) + "\n"
181 + tooltiptext.toString());
187 public void mouseDragged(MouseEvent e)
189 mouseDragging = true;
191 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
195 selectSeqs(lastid - 1, seq);
197 else if (seq > lastid)
199 selectSeqs(lastid + 1, seq);
203 alignPanel.paintAlignment(false);
207 public void mouseClicked(MouseEvent e)
209 if (e.getClickCount() < 2)
214 // get the sequence details
215 int seq = alignPanel.seqPanel.findSeq(e);
216 SequenceI sq = av.getAlignment().getSequenceAt(seq);
221 String id = sq.getName();
223 // get the default url with the sequence details filled in
224 String url = urlProvider.getPrimaryUrl(id);
225 String target = urlProvider.getPrimaryTarget(id);
228 alignPanel.alignFrame.showURL(url, target);
229 } catch (Exception ex)
231 ex.printStackTrace();
236 public void mouseEntered(MouseEvent e)
238 if (scrollThread != null)
240 scrollThread.running = false;
245 public void mouseExited(MouseEvent e)
247 if (av.getWrapAlignment())
252 if (mouseDragging && e.getY() < 0 && av.getStartSeq() > 0)
254 scrollThread = new ScrollThread(true);
257 if (mouseDragging && e.getY() >= getSize().height
258 && av.getAlignment().getHeight() > av.getEndSeq())
260 scrollThread = new ScrollThread(false);
265 public void mousePressed(MouseEvent e)
267 if (e.getClickCount() > 1)
273 if (av.getWrapAlignment())
275 y -= 2 * av.getCharHeight();
278 int seq = alignPanel.seqPanel.findSeq(e);
280 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
282 Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
284 // build a new links menu based on the current links + any non-positional
286 List<String> nlinks = urlProvider.getLinksForMenu();
288 SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
289 for (int sl = 0; sf != null && sl < sf.length; sl++)
291 if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
293 if (sf[sl].links != null && sf[sl].links.size() > 0)
295 for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
297 nlinks.add(sf[sl].links.elementAt(l));
303 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
305 popup.show(this, e.getX(), e.getY());
309 if ((av.getSelectionGroup() == null)
310 || ((!jalview.util.Platform.isControlDown(e) && !e
311 .isShiftDown()) && av.getSelectionGroup() != null))
313 av.setSelectionGroup(new SequenceGroup());
314 av.getSelectionGroup().setStartRes(0);
315 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
318 if (e.isShiftDown() && lastid != -1)
320 selectSeqs(lastid, seq);
327 alignPanel.paintAlignment(false);
330 void selectSeq(int seq)
333 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
334 av.getSelectionGroup().addOrRemove(pickedSeq, true);
337 void selectSeqs(int start, int end)
342 if (end >= av.getAlignment().getHeight())
344 end = av.getAlignment().getHeight() - 1;
354 if (av.getSelectionGroup() == null)
356 av.setSelectionGroup(new SequenceGroup());
358 for (int i = start; i <= end; i++)
360 av.getSelectionGroup().addSequence(
361 av.getAlignment().getSequenceAt(i), i == end);
367 public void mouseReleased(MouseEvent e)
369 if (scrollThread != null)
371 scrollThread.running = false;
374 if (av.getSelectionGroup() != null)
376 av.getSelectionGroup().recalcConservation();
379 mouseDragging = false;
380 PaintRefresher.Refresh(this, av.getSequenceSetId());
381 // always send selection message when mouse is released
385 public void highlightSearchResults(List<SequenceI> list)
387 idCanvas.setHighlighted(list);
394 int index = av.getAlignment().findIndex(list.get(0));
396 // do we need to scroll the panel?
397 if (av.getStartSeq() > index || av.getEndSeq() < index)
399 alignPanel.setScrollValues(av.getStartRes(), index);
403 // this class allows scrolling off the bottom of the visible alignment
404 class ScrollThread extends Thread
406 boolean running = false;
410 public ScrollThread(boolean up)
416 public void stopScrolling()
427 if (alignPanel.scrollUp(up))
429 // scroll was ok, so add new sequence to selection
430 int seq = av.getStartSeq();
433 seq = av.getEndSeq();
438 selectSeqs(lastid - 1, seq);
440 else if (seq > lastid && seq < av.getAlignment().getHeight())
442 selectSeqs(lastid + 1, seq);
452 alignPanel.paintAlignment(true);
456 } catch (Exception ex)