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.HashMap;
38 import java.util.List;
40 public class IdPanel extends Panel implements MouseListener,
44 protected IdCanvas idCanvas;
46 protected AlignmentViewport av;
48 protected AlignmentPanel alignPanel;
50 ScrollThread scrollThread = null;
54 boolean mouseDragging = false;
56 UrlProviderI urlProvider = null;
58 public IdPanel(AlignViewport av, AlignmentPanel parent)
62 idCanvas = new IdCanvas(av);
63 setLayout(new BorderLayout());
64 add(idCanvas, BorderLayout.CENTER);
65 idCanvas.addMouseListener(this);
66 idCanvas.addMouseMotionListener(this);
69 // TODO: add in group link parameter
71 // make a list of label,url pairs
72 HashMap<String, String> urlList = new HashMap<String, String>();
73 if (av.applet != null)
75 for (int i = 1; i < 10; i++)
77 label = av.applet.getParameter("linkLabel_" + i);
78 url = av.applet.getParameter("linkURL_" + i);
80 // only add non-null parameters
83 urlList.put(label, url);
87 if (!urlList.isEmpty())
89 // set default as first entry in list
90 String defaultUrl = av.applet.getParameter("linkLabel_1");
91 UrlProviderFactoryI factory = new AppletUrlProviderFactory(
93 urlProvider = factory.createUrlProvider();
101 public void mouseMoved(MouseEvent e)
103 int seq = alignPanel.seqPanel.findSeq(e);
105 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
107 StringBuffer tooltiptext = new StringBuffer();
108 if (sequence == null)
112 if (sequence.getDescription() != null)
114 tooltiptext.append(sequence.getDescription());
115 tooltiptext.append("\n");
118 for (SequenceFeature sf : sequence.getFeatures()
119 .getNonPositionalFeatures())
122 if (sf.getFeatureGroup() != null)
124 tooltiptext.append(sf.getFeatureGroup());
127 if (sf.getType() != null)
129 tooltiptext.append(" ");
130 tooltiptext.append(sf.getType());
133 if (sf.getDescription() != null)
135 tooltiptext.append(" ");
136 tooltiptext.append(sf.getDescription());
139 if (!Float.isNaN(sf.getScore()) && sf.getScore() != 0f)
141 tooltiptext.append(" Score = ");
142 tooltiptext.append(sf.getScore());
145 if (sf.getStatus() != null && sf.getStatus().length() > 0)
147 tooltiptext.append(" (");
148 tooltiptext.append(sf.getStatus());
149 tooltiptext.append(")");
154 tooltiptext.append("\n");
158 if (tooltiptext.length() == 0)
160 // nothing to display - so clear tooltip if one is visible
163 tooltip.setVisible(false);
171 tooltip = new Tooltip(sequence.getDisplayId(true) + "\n"
172 + tooltiptext.toString(), idCanvas);
176 tooltip.setTip(sequence.getDisplayId(true) + "\n"
177 + tooltiptext.toString());
183 public void mouseDragged(MouseEvent e)
185 mouseDragging = true;
187 int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
191 selectSeqs(lastid - 1, seq);
193 else if (seq > lastid)
195 selectSeqs(lastid + 1, seq);
199 alignPanel.paintAlignment(false);
203 public void mouseClicked(MouseEvent e)
205 if (e.getClickCount() < 2)
210 // get the sequence details
211 int seq = alignPanel.seqPanel.findSeq(e);
212 SequenceI sq = av.getAlignment().getSequenceAt(seq);
217 String id = sq.getName();
219 // get the default url with the sequence details filled in
220 String url = urlProvider.getPrimaryUrl(id);
221 String target = urlProvider.getPrimaryTarget(id);
224 alignPanel.alignFrame.showURL(url, target);
225 } catch (Exception ex)
227 ex.printStackTrace();
232 public void mouseEntered(MouseEvent e)
234 if (scrollThread != null)
236 scrollThread.running = false;
241 public void mouseExited(MouseEvent e)
243 if (av.getWrapAlignment())
248 if (mouseDragging && e.getY() < 0 && av.getRanges().getStartSeq() > 0)
250 scrollThread = new ScrollThread(true);
253 if (mouseDragging && e.getY() >= getSize().height
254 && av.getAlignment().getHeight() > av.getRanges().getEndSeq())
256 scrollThread = new ScrollThread(false);
261 public void mousePressed(MouseEvent e)
263 if (e.getClickCount() > 1)
269 if (av.getWrapAlignment())
271 y -= 2 * av.getCharHeight();
274 int seq = alignPanel.seqPanel.findSeq(e);
276 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
278 SequenceI sq = av.getAlignment().getSequenceAt(seq);
281 * build a new links menu based on the current links
282 * and any non-positional features
284 List<String> nlinks = urlProvider.getLinksForMenu();
286 for (SequenceFeature sf : sq.getFeatures().getNonPositionalFeatures())
288 if (sf.links != null)
290 for (String link : sf.links)
297 APopupMenu popup = new APopupMenu(alignPanel, sq, nlinks);
299 popup.show(this, e.getX(), e.getY());
303 if ((av.getSelectionGroup() == null)
304 || ((!jalview.util.Platform.isControlDown(e) && !e
305 .isShiftDown()) && av.getSelectionGroup() != null))
307 av.setSelectionGroup(new SequenceGroup());
308 av.getSelectionGroup().setStartRes(0);
309 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
312 if (e.isShiftDown() && lastid != -1)
314 selectSeqs(lastid, seq);
321 alignPanel.paintAlignment(false);
324 void selectSeq(int seq)
327 SequenceI pickedSeq = av.getAlignment().getSequenceAt(seq);
328 av.getSelectionGroup().addOrRemove(pickedSeq, true);
331 void selectSeqs(int start, int end)
336 if (end >= av.getAlignment().getHeight())
338 end = av.getAlignment().getHeight() - 1;
348 if (av.getSelectionGroup() == null)
350 av.setSelectionGroup(new SequenceGroup());
352 for (int i = start; i <= end; i++)
354 av.getSelectionGroup().addSequence(
355 av.getAlignment().getSequenceAt(i), i == end);
361 public void mouseReleased(MouseEvent e)
363 if (scrollThread != null)
365 scrollThread.running = false;
368 if (av.getSelectionGroup() != null)
370 av.getSelectionGroup().recalcConservation();
373 mouseDragging = false;
374 PaintRefresher.Refresh(this, av.getSequenceSetId());
375 // always send selection message when mouse is released
379 public void highlightSearchResults(List<SequenceI> list)
381 idCanvas.setHighlighted(list);
388 int index = av.getAlignment().findIndex(list.get(0));
390 // do we need to scroll the panel?
391 if (av.getRanges().getStartSeq() > index
392 || av.getRanges().getEndSeq() < index)
394 alignPanel.setScrollValues(av.getRanges().getStartRes(), index);
398 // this class allows scrolling off the bottom of the visible alignment
399 class ScrollThread extends Thread
401 boolean running = false;
405 public ScrollThread(boolean up)
411 public void stopScrolling()
422 if (alignPanel.scrollUp(up))
424 // scroll was ok, so add new sequence to selection
425 int seq = av.getRanges().getStartSeq();
428 seq = av.getRanges().getEndSeq();
433 selectSeqs(lastid - 1, seq);
435 else if (seq > lastid && seq < av.getAlignment().getHeight())
437 selectSeqs(lastid + 1, seq);
447 alignPanel.paintAlignment(true);
451 } catch (Exception ex)