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.SequenceFeature;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.urls.api.UrlProviderFactoryI;
27 import jalview.urls.api.UrlProviderI;
28 import jalview.urls.applet.AppletUrlProviderFactory;
29 import jalview.viewmodel.AlignmentViewport;
31 import java.awt.BorderLayout;
32 import java.awt.Panel;
33 import java.awt.event.InputEvent;
34 import java.awt.event.MouseEvent;
35 import java.awt.event.MouseListener;
36 import java.awt.event.MouseMotionListener;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
41 public class IdPanel extends Panel
42 implements MouseListener, MouseMotionListener
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 viewport, AlignmentPanel parent)
63 idCanvas = new IdCanvas(viewport);
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<>();
74 if (viewport.applet != null)
76 for (int i = 1; i < 10; i++)
78 label = viewport.applet.getParameter("linkLabel_" + i);
79 url = viewport.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 = viewport.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 StringBuffer tooltiptext = new StringBuffer();
109 if (sequence == null)
113 if (sequence.getDescription() != null)
115 tooltiptext.append(sequence.getDescription());
116 tooltiptext.append("\n");
119 for (SequenceFeature sf : sequence.getFeatures()
120 .getNonPositionalFeatures())
123 if (sf.getFeatureGroup() != null)
125 tooltiptext.append(sf.getFeatureGroup());
128 if (sf.getType() != null)
130 tooltiptext.append(" ");
131 tooltiptext.append(sf.getType());
134 if (sf.getDescription() != null)
136 tooltiptext.append(" ");
137 tooltiptext.append(sf.getDescription());
140 if (!Float.isNaN(sf.getScore()) && sf.getScore() != 0f)
142 tooltiptext.append(" Score = ");
143 tooltiptext.append(sf.getScore());
146 if (sf.getStatus() != null && sf.getStatus().length() > 0)
148 tooltiptext.append(" (");
149 tooltiptext.append(sf.getStatus());
150 tooltiptext.append(")");
155 tooltiptext.append("\n");
159 if (tooltiptext.length() == 0)
161 // nothing to display - so clear tooltip if one is visible
164 tooltip.setVisible(false);
172 tooltip = new Tooltip(
173 sequence.getDisplayId(true) + "\n" + tooltiptext.toString(),
179 sequence.getDisplayId(true) + "\n" + tooltiptext.toString());
185 public void mouseDragged(MouseEvent e)
187 mouseDragging = true;
189 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
193 selectSeqs(lastid - 1, seq);
195 else if (seq > lastid)
197 selectSeqs(lastid + 1, seq);
201 alignPanel.paintAlignment(false, false);
205 public void mouseClicked(MouseEvent e)
207 if (e.getClickCount() < 2)
212 // get the sequence details
213 int seq = alignPanel.seqPanel.findSeq(e);
214 SequenceI sq = av.getAlignment().getSequenceAt(seq);
219 String id = sq.getName();
221 // get the default url with the sequence details filled in
222 if (urlProvider == null)
226 String url = urlProvider.getPrimaryUrl(id);
227 String target = urlProvider.getPrimaryTarget(id);
230 alignPanel.alignFrame.showURL(url, target);
231 } catch (Exception ex)
233 ex.printStackTrace();
238 public void mouseEntered(MouseEvent e)
240 if (scrollThread != null)
242 scrollThread.running = false;
247 public void mouseExited(MouseEvent e)
249 if (av.getWrapAlignment())
254 if (mouseDragging && e.getY() < 0 && av.getRanges().getStartSeq() > 0)
256 scrollThread = new ScrollThread(true);
259 if (mouseDragging && e.getY() >= getSize().height
260 && av.getAlignment().getHeight() > av.getRanges().getEndSeq())
262 scrollThread = new ScrollThread(false);
267 public void mousePressed(MouseEvent e)
269 if (e.getClickCount() > 1)
275 if (av.getWrapAlignment())
277 y -= 2 * av.getCharHeight();
280 int seq = alignPanel.seqPanel.findSeq(e);
282 if ((e.getModifiers()
283 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
285 SequenceI sq = av.getAlignment().getSequenceAt(seq);
288 * build a new links menu based on the current links
289 * and any non-positional features
292 if (urlProvider != null)
294 nlinks = urlProvider.getLinksForMenu();
298 nlinks = new ArrayList<>();
301 for (SequenceFeature sf : sq.getFeatures().getNonPositionalFeatures())
303 if (sf.links != null)
305 for (String link : sf.links)
312 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
314 popup.show(this, e.getX(), e.getY());
318 if ((av.getSelectionGroup() == null)
319 || ((!jalview.util.Platform.isControlDown(e)
320 && !e.isShiftDown()) && av.getSelectionGroup() != null))
322 av.setSelectionGroup(new SequenceGroup());
323 av.getSelectionGroup().setStartRes(0);
324 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
327 if (e.isShiftDown() && lastid != -1)
329 selectSeqs(lastid, seq);
336 alignPanel.paintAlignment(false, false);
339 void selectSeq(int seq)
342 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
343 av.getSelectionGroup().addOrRemove(pickedSeq, true);
346 void selectSeqs(int start, int end)
351 if (end >= av.getAlignment().getHeight())
353 end = av.getAlignment().getHeight() - 1;
363 if (av.getSelectionGroup() == null)
365 av.setSelectionGroup(new SequenceGroup());
367 for (int i = start; i <= end; i++)
369 av.getSelectionGroup().addSequence(av.getAlignment().getSequenceAt(i),
376 public void mouseReleased(MouseEvent e)
378 if (scrollThread != null)
380 scrollThread.running = false;
383 if (av.getSelectionGroup() != null)
385 av.getSelectionGroup().recalcConservation();
388 mouseDragging = false;
389 PaintRefresher.Refresh(this, av.getSequenceSetId());
390 // always send selection message when mouse is released
394 public void highlightSearchResults(List<SequenceI> list)
396 idCanvas.setHighlighted(list);
403 int index = av.getAlignment().findIndex(list.get(0));
405 // do we need to scroll the panel?
406 if (av.getRanges().getStartSeq() > index
407 || av.getRanges().getEndSeq() < index)
409 av.getRanges().setStartSeq(index);
413 // this class allows scrolling off the bottom of the visible alignment
414 class ScrollThread extends Thread
416 boolean running = false;
420 public ScrollThread(boolean isUp)
426 public void stopScrolling()
437 if (av.getRanges().scrollUp(up))
439 // scroll was ok, so add new sequence to selection
440 int seq = av.getRanges().getStartSeq();
443 seq = av.getRanges().getEndSeq();
448 selectSeqs(lastid - 1, seq);
450 else if (seq > lastid && seq < av.getAlignment().getHeight())
452 selectSeqs(lastid + 1, seq);
462 alignPanel.paintAlignment(true, false);
466 } catch (Exception ex)