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.
23 import java.awt.BorderLayout;
24 import java.awt.Color;
26 import java.awt.FontMetrics;
27 import java.awt.Point;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.MouseListener;
32 import java.awt.event.MouseMotionListener;
33 import java.awt.event.MouseWheelEvent;
34 import java.awt.event.MouseWheelListener;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.JToolTip;
42 import javax.swing.SwingUtilities;
43 import javax.swing.Timer;
44 import javax.swing.ToolTipManager;
46 import jalview.api.AlignViewportI;
47 import jalview.bin.Console;
48 import jalview.commands.EditCommand;
49 import jalview.commands.EditCommand.Action;
50 import jalview.commands.EditCommand.Edit;
51 import jalview.datamodel.AlignmentAnnotation;
52 import jalview.datamodel.AlignmentI;
53 import jalview.datamodel.ColumnSelection;
54 import jalview.datamodel.HiddenColumns;
55 import jalview.datamodel.MappedFeatures;
56 import jalview.datamodel.SearchResultMatchI;
57 import jalview.datamodel.SearchResults;
58 import jalview.datamodel.SearchResultsI;
59 import jalview.datamodel.Sequence;
60 import jalview.datamodel.SequenceFeature;
61 import jalview.datamodel.SequenceGroup;
62 import jalview.datamodel.SequenceI;
63 import jalview.io.SequenceAnnotationReport;
64 import jalview.renderer.ResidueShaderI;
65 import jalview.schemes.ResidueProperties;
66 import jalview.structure.SelectionListener;
67 import jalview.structure.SelectionSource;
68 import jalview.structure.SequenceListener;
69 import jalview.structure.StructureSelectionManager;
70 import jalview.structure.VamsasSource;
71 import jalview.util.Comparison;
72 import jalview.util.MappingUtils;
73 import jalview.util.MessageManager;
74 import jalview.util.Platform;
75 import jalview.viewmodel.AlignmentViewport;
76 import jalview.viewmodel.ViewportRanges;
77 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
83 * @version $Revision: 1.130 $
85 public class SeqPanel extends JPanel
86 implements MouseListener, MouseMotionListener, MouseWheelListener,
87 SequenceListener, SelectionListener
90 * a class that holds computed mouse position
91 * - column of the alignment (0...)
92 * - sequence offset (0...)
93 * - annotation row offset (0...)
94 * where annotation offset is -1 unless the alignment is shown
95 * in wrapped mode, annotations are shown, and the mouse is
96 * over an annnotation row
101 * alignment column position of cursor (0...)
106 * index in alignment of sequence under cursor,
107 * or nearest above if cursor is not over a sequence
112 * index in annotations array of annotation under the cursor
113 * (only possible in wrapped mode with annotations shown),
114 * or -1 if cursor is not over an annotation row
116 final int annotationIndex;
118 MousePos(int col, int seq, int ann)
122 annotationIndex = ann;
125 boolean isOverAnnotation()
127 return annotationIndex != -1;
131 public boolean equals(Object obj)
133 if (obj == null || !(obj instanceof MousePos))
137 MousePos o = (MousePos) obj;
138 boolean b = (column == o.column && seqIndex == o.seqIndex
139 && annotationIndex == o.annotationIndex);
140 // System.out.println(obj + (b ? "= " : "!= ") + this);
145 * A simple hashCode that ensures that instances that satisfy equals() have
149 public int hashCode()
151 return column + seqIndex + annotationIndex;
155 * toString method for debug output purposes only
158 public String toString()
160 return String.format("c%d:s%d:a%d", column, seqIndex,
165 private static final int MAX_TOOLTIP_LENGTH = 300;
167 public SeqCanvas seqCanvas;
169 public AlignmentPanel ap;
172 * last position for mouseMoved event
174 private MousePos lastMousePosition;
176 protected int editLastRes;
178 protected int editStartSeq;
180 protected AlignViewport av;
182 ScrollThread scrollThread = null;
184 boolean mouseDragging = false;
186 boolean editingSeqs = false;
188 boolean groupEditing = false;
190 // ////////////////////////////////////////
191 // ///Everything below this is for defining the boundary of the rubberband
192 // ////////////////////////////////////////
195 boolean changeEndSeq = false;
197 boolean changeStartSeq = false;
199 boolean changeEndRes = false;
201 boolean changeStartRes = false;
203 SequenceGroup stretchGroup = null;
205 boolean remove = false;
207 Point lastMousePress;
209 boolean mouseWheelPressed = false;
211 StringBuffer keyboardNo1;
213 StringBuffer keyboardNo2;
215 private final SequenceAnnotationReport seqARep;
218 * the last tooltip on mousing over the alignment (or annotation in wrapped mode)
219 * - the tooltip is not set again if unchanged
220 * - this is the tooltip text _before_ formatting as html
222 private String lastTooltip;
225 * the last tooltip on mousing over the alignment (or annotation in wrapped mode)
226 * - used to decide where to place the tooltip in getTooltipLocation()
227 * - this is the tooltip text _after_ formatting as html
229 private String lastFormattedTooltip;
231 EditCommand editCommand;
233 StructureSelectionManager ssm;
235 SearchResultsI lastSearchResults;
238 * Creates a new SeqPanel object
243 public SeqPanel(AlignViewport viewport, AlignmentPanel alignPanel)
245 seqARep = new SequenceAnnotationReport(true);
246 ToolTipManager.sharedInstance().registerComponent(this);
247 ToolTipManager.sharedInstance().setInitialDelay(0);
248 ToolTipManager.sharedInstance().setDismissDelay(10000);
251 setBackground(Color.white);
253 seqCanvas = new SeqCanvas(alignPanel);
254 setLayout(new BorderLayout());
255 add(seqCanvas, BorderLayout.CENTER);
257 this.ap = alignPanel;
259 if (!viewport.isDataset())
261 addMouseMotionListener(this);
262 addMouseListener(this);
263 addMouseWheelListener(this);
264 ssm = viewport.getStructureSelectionManager();
265 ssm.addStructureViewerListener(this);
266 ssm.addSelectionListener(this);
270 int startWrapBlock = -1;
272 int wrappedBlock = -1;
275 * Computes the column and sequence row (and possibly annotation row when in
276 * wrapped mode) for the given mouse position
278 * Mouse position is not set if in wrapped mode with the cursor either between
279 * sequences, or over the left or right vertical scale.
284 MousePos findMousePosition(MouseEvent evt)
286 int col = findColumn(evt);
291 int charHeight = av.getCharHeight();
292 int alignmentHeight = av.getAlignment().getHeight();
293 if (av.getWrapAlignment())
295 seqCanvas.calculateWrappedGeometry(seqCanvas.getWidth(),
296 seqCanvas.getHeight());
299 * yPos modulo height of repeating width
301 int yOffsetPx = y % seqCanvas.wrappedRepeatHeightPx;
304 * height of sequences plus space / scale above,
305 * plus gap between sequences and annotations
307 int alignmentHeightPixels = seqCanvas.wrappedSpaceAboveAlignment
308 + alignmentHeight * charHeight
309 + SeqCanvas.SEQS_ANNOTATION_GAP;
310 if (yOffsetPx >= alignmentHeightPixels)
313 * mouse is over annotations; find annotation index, also set
314 * last sequence above (for backwards compatible behaviour)
316 AlignmentAnnotation[] anns = av.getAlignment()
317 .getAlignmentAnnotation();
318 int rowOffsetPx = yOffsetPx - alignmentHeightPixels;
319 annIndex = AnnotationPanel.getRowIndex(rowOffsetPx, anns);
320 seqIndex = alignmentHeight - 1;
325 * mouse is over sequence (or the space above sequences)
327 yOffsetPx -= seqCanvas.wrappedSpaceAboveAlignment;
330 seqIndex = Math.min(yOffsetPx / charHeight, alignmentHeight - 1);
336 ViewportRanges ranges = av.getRanges();
337 seqIndex = Math.min((y / charHeight) + ranges.getStartSeq(),
338 alignmentHeight - 1);
339 seqIndex = Math.min(seqIndex, ranges.getEndSeq());
342 return new MousePos(col, seqIndex, annIndex);
346 * Returns the aligned sequence position (base 0) at the mouse position, or
347 * the closest visible one
349 * Returns -1 if in wrapped mode with the mouse over either left or right
355 int findColumn(MouseEvent evt)
360 final int startRes = av.getRanges().getStartRes();
361 final int charWidth = av.getCharWidth();
363 if (av.getWrapAlignment())
365 int hgap = av.getCharHeight();
366 if (av.getScaleAboveWrapped())
368 hgap += av.getCharHeight();
371 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
372 + hgap + seqCanvas.getAnnotationHeight();
375 y = Math.max(0, y - hgap);
376 x -= seqCanvas.getLabelWidthWest();
379 // mouse is over left scale
383 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
388 if (x >= cwidth * charWidth)
390 // mouse is over right scale
394 wrappedBlock = y / cHeight;
395 wrappedBlock += startRes / cwidth;
396 // allow for wrapped view scrolled right (possible from Overview)
397 int startOffset = startRes % cwidth;
398 res = wrappedBlock * cwidth + startOffset
399 + Math.min(cwidth - 1, x / charWidth);
404 * make sure we calculate relative to visible alignment,
405 * rather than right-hand gutter
407 x = Math.min(x, seqCanvas.getX() + seqCanvas.getWidth());
408 res = (x / charWidth) + startRes;
409 res = Math.min(res, av.getRanges().getEndRes());
412 if (av.hasHiddenColumns())
414 res = av.getAlignment().getHiddenColumns()
415 .visibleToAbsoluteColumn(res);
422 * When all of a sequence of edits are complete, put the resulting edit list
423 * on the history stack (undo list), and reset flags for editing in progress.
429 if (editCommand != null && editCommand.getSize() > 0)
431 ap.alignFrame.addHistoryItem(editCommand);
432 av.firePropertyChange("alignment", null,
433 av.getAlignment().getSequences());
438 * Tidy up come what may...
443 groupEditing = false;
452 seqCanvas.cursorY = getKeyboardNo1() - 1;
453 scrollToVisible(true);
456 void setCursorColumn()
458 seqCanvas.cursorX = getKeyboardNo1() - 1;
459 scrollToVisible(true);
462 void setCursorRowAndColumn()
464 if (keyboardNo2 == null)
466 keyboardNo2 = new StringBuffer();
470 seqCanvas.cursorX = getKeyboardNo1() - 1;
471 seqCanvas.cursorY = getKeyboardNo2() - 1;
472 scrollToVisible(true);
476 void setCursorPosition()
478 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
480 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
481 scrollToVisible(true);
484 void moveCursor(int dx, int dy)
486 moveCursor(dx, dy, false);
489 void moveCursor(int dx, int dy, boolean nextWord)
491 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
495 int maxWidth = av.getAlignment().getWidth();
496 int maxHeight = av.getAlignment().getHeight();
497 SequenceI seqAtRow = av.getAlignment()
498 .getSequenceAt(seqCanvas.cursorY);
499 // look for next gap or residue
500 boolean isGap = Comparison
501 .isGap(seqAtRow.getCharAt(seqCanvas.cursorX));
502 int p = seqCanvas.cursorX, lastP, r = seqCanvas.cursorY, lastR;
518 seqAtRow = av.getAlignment().getSequenceAt(r);
520 p = nextVisible(hidden, maxWidth, p, dx);
521 } while ((dx != 0 ? p != lastP : r != lastR)
522 && isGap == Comparison.isGap(seqAtRow.getCharAt(p)));
523 seqCanvas.cursorX = p;
524 seqCanvas.cursorY = r;
528 int maxWidth = av.getAlignment().getWidth();
529 seqCanvas.cursorX = nextVisible(hidden, maxWidth, seqCanvas.cursorX,
531 seqCanvas.cursorY += dy;
533 scrollToVisible(false);
536 private int nextVisible(HiddenColumns hidden, int maxWidth, int original,
539 int newCursorX = original + dx;
540 if (av.hasHiddenColumns() && !hidden.isVisible(newCursorX))
542 int visx = hidden.absoluteToVisibleColumn(newCursorX - dx);
543 int[] region = hidden.getRegionWithEdgeAtRes(visx);
545 if (region != null) // just in case
550 newCursorX = region[1] + 1;
555 newCursorX = region[0] - 1;
559 newCursorX = (newCursorX < 0) ? 0 : newCursorX;
560 if (newCursorX >= maxWidth || !hidden.isVisible(newCursorX))
562 newCursorX = original;
568 * Scroll to make the cursor visible in the viewport.
571 * just jump to the location rather than scrolling
573 void scrollToVisible(boolean jump)
575 if (seqCanvas.cursorX < 0)
577 seqCanvas.cursorX = 0;
579 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
581 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
584 if (seqCanvas.cursorY < 0)
586 seqCanvas.cursorY = 0;
588 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
590 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
595 boolean repaintNeeded = true;
598 // only need to repaint if the viewport did not move, as otherwise it will
600 repaintNeeded = !av.getRanges().setViewportLocation(seqCanvas.cursorX,
605 if (av.getWrapAlignment())
607 // scrollToWrappedVisible expects x-value to have hidden cols subtracted
608 int x = av.getAlignment().getHiddenColumns()
609 .absoluteToVisibleColumn(seqCanvas.cursorX);
610 av.getRanges().scrollToWrappedVisible(x);
614 av.getRanges().scrollToVisible(seqCanvas.cursorX,
619 if (av.getAlignment().getHiddenColumns().isVisible(seqCanvas.cursorX))
621 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
622 seqCanvas.cursorX, seqCanvas.cursorY);
631 void setSelectionAreaAtCursor(boolean topLeft)
633 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
635 if (av.getSelectionGroup() != null)
637 SequenceGroup sg = av.getSelectionGroup();
638 // Find the top and bottom of this group
639 int min = av.getAlignment().getHeight(), max = 0;
640 for (int i = 0; i < sg.getSize(); i++)
642 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
657 sg.setStartRes(seqCanvas.cursorX);
658 if (sg.getEndRes() < seqCanvas.cursorX)
660 sg.setEndRes(seqCanvas.cursorX);
663 min = seqCanvas.cursorY;
667 sg.setEndRes(seqCanvas.cursorX);
668 if (sg.getStartRes() > seqCanvas.cursorX)
670 sg.setStartRes(seqCanvas.cursorX);
673 max = seqCanvas.cursorY + 1;
678 // Only the user can do this
679 av.setSelectionGroup(null);
683 // Now add any sequences between min and max
684 sg.getSequences(null).clear();
685 for (int i = min; i < max; i++)
687 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
692 if (av.getSelectionGroup() == null)
694 SequenceGroup sg = new SequenceGroup();
695 sg.setStartRes(seqCanvas.cursorX);
696 sg.setEndRes(seqCanvas.cursorX);
697 sg.addSequence(sequence, false);
698 av.setSelectionGroup(sg);
701 ap.paintAlignment(false, false);
705 void insertGapAtCursor(boolean group)
707 groupEditing = group;
708 editStartSeq = seqCanvas.cursorY;
709 editLastRes = seqCanvas.cursorX;
710 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
714 void deleteGapAtCursor(boolean group)
716 groupEditing = group;
717 editStartSeq = seqCanvas.cursorY;
718 editLastRes = seqCanvas.cursorX + getKeyboardNo1();
719 editSequence(false, false, seqCanvas.cursorX);
723 void insertNucAtCursor(boolean group, String nuc)
725 // TODO not called - delete?
726 groupEditing = group;
727 editStartSeq = seqCanvas.cursorY;
728 editLastRes = seqCanvas.cursorX;
729 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
733 void numberPressed(char value)
735 if (keyboardNo1 == null)
737 keyboardNo1 = new StringBuffer();
740 if (keyboardNo2 != null)
742 keyboardNo2.append(value);
746 keyboardNo1.append(value);
754 if (keyboardNo1 != null)
756 int value = Integer.parseInt(keyboardNo1.toString());
760 } catch (Exception x)
771 if (keyboardNo2 != null)
773 int value = Integer.parseInt(keyboardNo2.toString());
777 } catch (Exception x)
791 public void mouseReleased(MouseEvent evt)
793 MousePos pos = findMousePosition(evt);
794 if (pos.isOverAnnotation() || pos.seqIndex == -1 || pos.column == -1)
799 boolean didDrag = mouseDragging; // did we come here after a drag
800 mouseDragging = false;
801 mouseWheelPressed = false;
803 if (evt.isPopupTrigger()) // Windows: mouseReleased
805 showPopupMenu(evt, pos);
816 doMouseReleasedDefineMode(evt, didDrag);
827 public void mousePressed(MouseEvent evt)
829 lastMousePress = evt.getPoint();
830 MousePos pos = findMousePosition(evt);
831 if (pos.isOverAnnotation() || pos.seqIndex == -1 || pos.column == -1)
836 if (SwingUtilities.isMiddleMouseButton(evt))
838 mouseWheelPressed = true;
842 boolean isControlDown = Platform.isControlDown(evt);
843 if (evt.isShiftDown() || isControlDown)
853 doMousePressedDefineMode(evt, pos);
857 int seq = pos.seqIndex;
858 int res = pos.column;
860 if ((seq < av.getAlignment().getHeight())
861 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
878 public void mouseOverSequence(SequenceI sequence, int index, int pos)
880 String tmp = sequence.hashCode() + " " + index + " " + pos;
882 if (lastMessage == null || !lastMessage.equals(tmp))
884 // System.err.println("mouseOver Sequence: "+tmp);
885 ssm.mouseOverSequence(sequence, index, pos, av);
891 * Highlight the mapped region described by the search results object (unless
892 * unchanged). This supports highlight of protein while mousing over linked
893 * cDNA and vice versa. The status bar is also updated to show the location of
894 * the start of the highlighted region.
897 public String highlightSequence(SearchResultsI results)
899 if (results == null || results.equals(lastSearchResults))
903 lastSearchResults = results;
905 boolean wasScrolled = false;
907 if (av.isFollowHighlight())
909 // don't allow highlight of protein/cDNA to also scroll a complementary
910 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
911 // over residue to change abruptly, causing highlighted residue in panel 2
912 // to change, causing a scroll in panel 1 etc)
913 ap.setToScrollComplementPanel(false);
914 wasScrolled = ap.scrollToPosition(results);
917 seqCanvas.revalidate();
919 ap.setToScrollComplementPanel(true);
922 boolean fastPaint = !(wasScrolled && av.getWrapAlignment());
923 if (seqCanvas.highlightSearchResults(results, fastPaint))
925 setStatusMessage(results);
927 return results.isEmpty() ? null : getHighlightInfo(results);
931 * temporary hack: answers a message suitable to show on structure hover
932 * label. This is normally null. It is a peptide variation description if
934 * <li>results are a single residue in a protein alignment</li>
935 * <li>there is a mapping to a coding sequence (codon)</li>
936 * <li>there are one or more SNP variant features on the codon</li>
938 * in which case the answer is of the format (e.g.) "p.Glu388Asp"
943 private String getHighlightInfo(SearchResultsI results)
946 * ideally, just find mapped CDS (as we don't care about render style here);
947 * for now, go via split frame complement's FeatureRenderer
949 AlignViewportI complement = ap.getAlignViewport().getCodingComplement();
950 if (complement == null)
954 AlignFrame af = Desktop.getAlignFrameFor(complement);
955 FeatureRendererModel fr2 = af.getFeatureRenderer();
957 List<SearchResultMatchI> matches = results.getResults();
958 int j = matches.size();
959 List<String> infos = new ArrayList<>();
960 for (int i = 0; i < j; i++)
962 SearchResultMatchI match = matches.get(i);
963 int pos = match.getStart();
964 if (pos == match.getEnd())
966 SequenceI seq = match.getSequence();
967 SequenceI ds = seq.getDatasetSequence() == null ? seq
968 : seq.getDatasetSequence();
969 MappedFeatures mf = fr2.findComplementFeaturesAtResidue(ds, pos);
972 for (SequenceFeature sf : mf.features)
974 String pv = mf.findProteinVariants(sf);
975 if (pv.length() > 0 && !infos.contains(pv))
988 StringBuilder sb = new StringBuilder();
989 for (String info : infos)
997 return sb.toString();
1001 public VamsasSource getVamsasSource()
1003 return this.ap == null ? null : this.ap.av;
1007 public void updateColours(SequenceI seq, int index)
1009 System.out.println("update the seqPanel colours");
1014 * Action on mouse movement is to update the status bar to show the current
1015 * sequence position, and (if features are shown) to show any features at the
1016 * position in a tooltip. Does nothing if the mouse move does not change
1022 public void mouseMoved(MouseEvent evt)
1026 // This is because MacOSX creates a mouseMoved
1027 // If control is down, other platforms will not.
1031 final MousePos mousePos = findMousePosition(evt);
1032 if (mousePos.equals(lastMousePosition))
1035 * just a pixel move without change of 'cell'
1037 moveTooltip = false;
1041 lastMousePosition = mousePos;
1043 if (mousePos.isOverAnnotation())
1045 mouseMovedOverAnnotation(mousePos);
1048 final int seq = mousePos.seqIndex;
1050 final int column = mousePos.column;
1051 if (column < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
1053 lastMousePosition = null;
1054 setToolTipText(null);
1056 lastFormattedTooltip = null;
1057 ap.alignFrame.setStatus("");
1061 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1063 if (column >= sequence.getLength())
1069 * set status bar message, returning residue position in sequence
1071 boolean isGapped = Comparison.isGap(sequence.getCharAt(column));
1072 final int pos = setStatusMessage(sequence, column, seq);
1073 if (ssm != null && !isGapped)
1075 mouseOverSequence(sequence, column, pos);
1078 StringBuilder tooltipText = new StringBuilder(64);
1080 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
1083 for (int g = 0; g < groups.length; g++)
1085 if (groups[g].getStartRes() <= column
1086 && groups[g].getEndRes() >= column)
1088 if (!groups[g].getName().startsWith("JTreeGroup")
1089 && !groups[g].getName().startsWith("JGroup"))
1091 tooltipText.append(groups[g].getName());
1094 if (groups[g].getDescription() != null)
1096 tooltipText.append(": " + groups[g].getDescription());
1103 * add any features at the position to the tooltip; if over a gap, only
1104 * add features that straddle the gap (pos may be the residue before or
1107 int unshownFeatures = 0;
1108 if (av.isShowSequenceFeatures())
1110 List<SequenceFeature> features = ap.getFeatureRenderer()
1111 .findFeaturesAtColumn(sequence, column + 1);
1112 unshownFeatures = seqARep.appendFeatures(tooltipText, pos, features,
1113 this.ap.getSeqPanel().seqCanvas.fr, MAX_TOOLTIP_LENGTH);
1116 * add features in CDS/protein complement at the corresponding
1117 * position if configured to do so
1119 if (av.isShowComplementFeatures())
1121 if (!Comparison.isGap(sequence.getCharAt(column)))
1123 AlignViewportI complement = ap.getAlignViewport()
1124 .getCodingComplement();
1125 AlignFrame af = Desktop.getAlignFrameFor(complement);
1126 FeatureRendererModel fr2 = af.getFeatureRenderer();
1127 MappedFeatures mf = fr2.findComplementFeaturesAtResidue(sequence,
1131 unshownFeatures += seqARep.appendFeatures(tooltipText, pos, mf,
1132 fr2, MAX_TOOLTIP_LENGTH);
1137 if (tooltipText.length() == 0) // nothing added
1139 setToolTipText(null);
1144 if (tooltipText.length() > MAX_TOOLTIP_LENGTH)
1146 tooltipText.setLength(MAX_TOOLTIP_LENGTH);
1147 tooltipText.append("...");
1149 if (unshownFeatures > 0)
1151 tooltipText.append("<br/>").append("... ").append("<i>")
1152 .append(MessageManager.formatMessage(
1153 "label.features_not_shown", unshownFeatures))
1156 String textString = tooltipText.toString();
1157 if (!textString.equals(lastTooltip))
1159 lastTooltip = textString;
1160 lastFormattedTooltip = JvSwingUtils.wrapTooltip(true, textString);
1161 setToolTipText(lastFormattedTooltip);
1167 * When the view is in wrapped mode, and the mouse is over an annotation row,
1168 * shows the corresponding tooltip and status message (if any)
1173 protected void mouseMovedOverAnnotation(MousePos pos)
1175 final int column = pos.column;
1176 final int rowIndex = pos.annotationIndex;
1178 if (column < 0 || !av.getWrapAlignment() || !av.isShowAnnotation()
1183 AlignmentAnnotation[] anns = av.getAlignment().getAlignmentAnnotation();
1185 String tooltip = AnnotationPanel.buildToolTip(anns[rowIndex], column,
1187 if (tooltip == null ? tooltip != lastTooltip
1188 : !tooltip.equals(lastTooltip))
1190 lastTooltip = tooltip;
1191 lastFormattedTooltip = tooltip == null ? null
1192 : JvSwingUtils.wrapTooltip(true, tooltip);
1193 setToolTipText(lastFormattedTooltip);
1196 String msg = AnnotationPanel.getStatusMessage(av.getAlignment(), column,
1198 ap.alignFrame.setStatus(msg);
1202 * if Shift key is held down while moving the mouse,
1203 * the tooltip location is not changed once shown
1205 private Point lastTooltipLocation = null;
1208 * this flag is false for pixel moves within a residue,
1209 * to reduce tooltip flicker
1211 private boolean moveTooltip = true;
1214 * a dummy tooltip used to estimate where to position tooltips
1216 private JToolTip tempTip = new JLabel().createToolTip();
1221 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
1224 public Point getToolTipLocation(MouseEvent event)
1228 if (lastTooltip == null || !moveTooltip)
1233 if (lastTooltipLocation != null && event.isShiftDown())
1235 return lastTooltipLocation;
1238 int x = event.getX();
1239 int y = event.getY();
1242 tempTip.setTipText(lastFormattedTooltip);
1243 int tipWidth = (int) tempTip.getPreferredSize().getWidth();
1245 // was x += (w - x < 200) ? -(w / 2) : 5;
1246 x = (x + tipWidth < w ? x + 10 : w - tipWidth);
1247 Point p = new Point(x, y + av.getCharHeight()); // BH 2018 was - 20?
1249 return lastTooltipLocation = p;
1253 * set when the current UI interaction has resulted in a change that requires
1254 * shading in overviews and structures to be recalculated. this could be
1255 * changed to a something more expressive that indicates what actually has
1256 * changed, so selective redraws can be applied (ie. only structures, only
1259 private boolean updateOverviewAndStructs = false; // TODO: refactor to
1263 * set if av.getSelectionGroup() refers to a group that is defined on the
1264 * alignment view, rather than a transient selection
1266 // private boolean editingDefinedGroup = false; // TODO: refactor to
1267 // avcontroller or viewModel
1270 * Sets the status message in alignment panel, showing the sequence number
1271 * (index) and id, and residue and residue position if not at a gap, for the
1272 * given sequence and column position. Returns the residue position returned
1273 * by Sequence.findPosition. Note this may be for the nearest adjacent residue
1274 * if at a gapped position.
1277 * aligned sequence object
1281 * index of sequence in alignment
1282 * @return sequence position of residue at column, or adjacent residue if at a
1285 int setStatusMessage(SequenceI sequence, final int column, int seqIndex)
1287 char sequenceChar = sequence.getCharAt(column);
1288 int pos = sequence.findPosition(column);
1289 setStatusMessage(sequence.getName(), seqIndex, sequenceChar, pos);
1295 * Builds the status message for the current cursor location and writes it to
1296 * the status bar, for example
1299 * Sequence 3 ID: FER1_SOLLC
1300 * Sequence 5 ID: FER1_PEA Residue: THR (4)
1301 * Sequence 5 ID: FER1_PEA Residue: B (3)
1302 * Sequence 6 ID: O.niloticus.3 Nucleotide: Uracil (2)
1307 * sequence position in the alignment (1..)
1308 * @param sequenceChar
1309 * the character under the cursor
1311 * the sequence residue position (if not over a gap)
1313 protected void setStatusMessage(String seqName, int seqIndex,
1314 char sequenceChar, int residuePos)
1316 StringBuilder text = new StringBuilder(32);
1319 * Sequence number (if known), and sequence name.
1321 String seqno = seqIndex == -1 ? "" : " " + (seqIndex + 1);
1322 text.append("Sequence").append(seqno).append(" ID: ").append(seqName);
1324 String residue = null;
1327 * Try to translate the display character to residue name (null for gap).
1329 boolean isGapped = Comparison.isGap(sequenceChar);
1333 boolean nucleotide = av.getAlignment().isNucleotide();
1334 String displayChar = String.valueOf(sequenceChar);
1337 residue = ResidueProperties.nucleotideName.get(displayChar);
1341 residue = "X".equalsIgnoreCase(displayChar) ? "X"
1342 : ("*".equals(displayChar) ? "STOP"
1343 : ResidueProperties.aa2Triplet.get(displayChar));
1345 text.append(" ").append(nucleotide ? "Nucleotide" : "Residue")
1346 .append(": ").append(residue == null ? displayChar : residue);
1348 text.append(" (").append(Integer.toString(residuePos)).append(")");
1350 ap.alignFrame.setStatus(text.toString());
1354 * Set the status bar message to highlight the first matched position in
1359 private void setStatusMessage(SearchResultsI results)
1361 AlignmentI al = this.av.getAlignment();
1362 int sequenceIndex = al.findIndex(results);
1363 if (sequenceIndex == -1)
1367 SequenceI alignedSeq = al.getSequenceAt(sequenceIndex);
1368 SequenceI ds = alignedSeq.getDatasetSequence();
1369 for (SearchResultMatchI m : results.getResults())
1371 SequenceI seq = m.getSequence();
1372 if (seq.getDatasetSequence() != null)
1374 seq = seq.getDatasetSequence();
1379 int start = m.getStart();
1380 setStatusMessage(alignedSeq.getName(), sequenceIndex,
1381 seq.getCharAt(start - 1), start);
1391 public void mouseDragged(MouseEvent evt)
1393 MousePos pos = findMousePosition(evt);
1394 if (pos.isOverAnnotation() || pos.column == -1)
1399 if (mouseWheelPressed)
1401 boolean inSplitFrame = ap.av.getCodingComplement() != null;
1402 boolean copyChanges = inSplitFrame && av.isProteinFontAsCdna();
1404 int oldWidth = av.getCharWidth();
1406 // Which is bigger, left-right or up-down?
1407 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math
1408 .abs(evt.getX() - lastMousePress.getX()))
1411 * on drag up or down, decrement or increment font size
1413 int fontSize = av.font.getSize();
1414 boolean fontChanged = false;
1416 if (evt.getY() < lastMousePress.getY())
1421 else if (evt.getY() > lastMousePress.getY())
1434 Font newFont = new Font(av.font.getName(), av.font.getStyle(),
1436 av.setFont(newFont, true);
1437 av.setCharWidth(oldWidth);
1441 ap.av.getCodingComplement().setFont(newFont, true);
1442 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
1443 .getSplitViewContainer();
1444 splitFrame.adjustLayout();
1445 splitFrame.repaint();
1452 * on drag left or right, decrement or increment character width
1455 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
1457 newWidth = av.getCharWidth() - 1;
1458 av.setCharWidth(newWidth);
1460 else if (evt.getX() > lastMousePress.getX())
1462 newWidth = av.getCharWidth() + 1;
1463 av.setCharWidth(newWidth);
1467 ap.paintAlignment(false, false);
1471 * need to ensure newWidth is set on cdna, regardless of which
1472 * panel the mouse drag happened in; protein will compute its
1473 * character width as 1:1 or 3:1
1475 av.getCodingComplement().setCharWidth(newWidth);
1476 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
1477 .getSplitViewContainer();
1478 splitFrame.adjustLayout();
1479 splitFrame.repaint();
1484 FontMetrics fm = getFontMetrics(av.getFont());
1485 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
1487 lastMousePress = evt.getPoint();
1494 dragStretchGroup(evt);
1498 int res = pos.column;
1505 if ((editLastRes == -1) || (editLastRes == res))
1510 if ((res < av.getAlignment().getWidth()) && (res < editLastRes))
1512 // dragLeft, delete gap
1513 editSequence(false, false, res);
1517 editSequence(true, false, res);
1520 mouseDragging = true;
1521 if (scrollThread != null)
1523 scrollThread.setMousePosition(evt.getPoint());
1528 * Edits the sequence to insert or delete one or more gaps, in response to a
1529 * mouse drag or cursor mode command. The number of inserts/deletes may be
1530 * specified with the cursor command, or else depends on the mouse event
1531 * (normally one column, but potentially more for a fast mouse drag).
1533 * Delete gaps is limited to the number of gaps left of the cursor position
1534 * (mouse drag), or at or right of the cursor position (cursor mode).
1536 * In group editing mode (Ctrl or Cmd down), the edit acts on all sequences in
1537 * the current selection group.
1539 * In locked editing mode (with a selection group present), inserts/deletions
1540 * within the selection group are limited to its boundaries (and edits outside
1541 * the group stop at its border).
1544 * true to insert gaps, false to delete gaps
1546 * (unused parameter)
1548 * the column at which to perform the action; the number of columns
1549 * affected depends on <code>this.editLastRes</code> (cursor column
1552 synchronized void editSequence(boolean insertGap, boolean editSeq,
1556 int fixedRight = -1;
1557 boolean fixedColumns = false;
1558 SequenceGroup sg = av.getSelectionGroup();
1560 final SequenceI seq = av.getAlignment().getSequenceAt(editStartSeq);
1562 // No group, but the sequence may represent a group
1563 if (!groupEditing && av.hasHiddenRows())
1565 if (av.isHiddenRepSequence(seq))
1567 sg = av.getRepresentedSequences(seq);
1568 groupEditing = true;
1572 StringBuilder message = new StringBuilder(64); // for status bar
1575 * make a name for the edit action, for
1576 * status bar message and Undo/Redo menu
1578 String label = null;
1581 message.append("Edit group:");
1582 label = MessageManager.getString("action.edit_group");
1586 message.append("Edit sequence: " + seq.getName());
1587 label = seq.getName();
1588 if (label.length() > 10)
1590 label = label.substring(0, 10);
1592 label = MessageManager.formatMessage("label.edit_params",
1598 * initialise the edit command if there is not
1599 * already one being extended
1601 if (editCommand == null)
1603 editCommand = new EditCommand(label);
1608 message.append(" insert ");
1612 message.append(" delete ");
1615 message.append(Math.abs(startres - editLastRes) + " gaps.");
1616 ap.alignFrame.setStatus(message.toString());
1619 * is there a selection group containing the sequence being edited?
1620 * if so the boundary of the group is the limit of the edit
1621 * (but the edit may be inside or outside the selection group)
1623 boolean inSelectionGroup = sg != null
1624 && sg.getSequences(av.getHiddenRepSequences()).contains(seq);
1625 if (groupEditing || inSelectionGroup)
1627 fixedColumns = true;
1629 // sg might be null as the user may only see 1 sequence,
1630 // but the sequence represents a group
1633 if (!av.isHiddenRepSequence(seq))
1638 sg = av.getRepresentedSequences(seq);
1641 fixedLeft = sg.getStartRes();
1642 fixedRight = sg.getEndRes();
1644 if ((startres < fixedLeft && editLastRes >= fixedLeft)
1645 || (startres >= fixedLeft && editLastRes < fixedLeft)
1646 || (startres > fixedRight && editLastRes <= fixedRight)
1647 || (startres <= fixedRight && editLastRes > fixedRight))
1653 if (fixedLeft > startres)
1655 fixedRight = fixedLeft - 1;
1658 else if (fixedRight < startres)
1660 fixedLeft = fixedRight;
1665 if (av.hasHiddenColumns())
1667 fixedColumns = true;
1668 int y1 = av.getAlignment().getHiddenColumns()
1669 .getNextHiddenBoundary(true, startres);
1670 int y2 = av.getAlignment().getHiddenColumns()
1671 .getNextHiddenBoundary(false, startres);
1673 if ((insertGap && startres > y1 && editLastRes < y1)
1674 || (!insertGap && startres < y2 && editLastRes > y2))
1680 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1681 // Selection spans a hidden region
1682 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1690 fixedRight = y2 - 1;
1695 boolean success = doEditSequence(insertGap, editSeq, startres,
1696 fixedRight, fixedColumns, sg);
1699 * report what actually happened (might be less than
1700 * what was requested), by inspecting the edit commands added
1702 String msg = getEditStatusMessage(editCommand);
1703 ap.alignFrame.setStatus(msg == null ? " " : msg);
1709 editLastRes = startres;
1710 seqCanvas.repaint();
1714 * A helper method that performs the requested editing to insert or delete
1715 * gaps (if possible). Answers true if the edit was successful, false if could
1716 * only be performed in part or not at all. Failure may occur in 'locked edit'
1717 * mode, when an insertion requires a matching gapped position (or column) to
1718 * delete, and deletion requires an adjacent gapped position (or column) to
1722 * true if inserting gap(s), false if deleting
1724 * (unused parameter, currently always false)
1726 * the column at which to perform the edit
1728 * fixed right boundary column of a locked edit (within or to the
1729 * left of a selection group)
1730 * @param fixedColumns
1731 * true if this is a locked edit
1733 * the sequence group (if group edit is being performed)
1736 protected boolean doEditSequence(final boolean insertGap,
1737 final boolean editSeq, final int startres, int fixedRight,
1738 final boolean fixedColumns, final SequenceGroup sg)
1740 final SequenceI seq = av.getAlignment().getSequenceAt(editStartSeq);
1741 SequenceI[] seqs = new SequenceI[] { seq };
1745 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1746 int g, groupSize = vseqs.size();
1747 SequenceI[] groupSeqs = new SequenceI[groupSize];
1748 for (g = 0; g < groupSeqs.length; g++)
1750 groupSeqs[g] = vseqs.get(g);
1756 // If the user has selected the whole sequence, and is dragging to
1757 // the right, we can still extend the alignment and selectionGroup
1758 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1759 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1762 av.getAlignment().getWidth() + startres - editLastRes);
1763 fixedRight = sg.getEndRes();
1766 // Is it valid with fixed columns??
1767 // Find the next gap before the end
1768 // of the visible region boundary
1769 boolean blank = false;
1770 for (; fixedRight > editLastRes; fixedRight--)
1774 for (g = 0; g < groupSize; g++)
1776 for (int j = 0; j < startres - editLastRes; j++)
1778 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1793 if (sg.getSize() == av.getAlignment().getHeight())
1795 if ((av.hasHiddenColumns()
1796 && startres < av.getAlignment().getHiddenColumns()
1797 .getNextHiddenBoundary(false, startres)))
1802 int alWidth = av.getAlignment().getWidth();
1803 if (av.hasHiddenRows())
1805 int hwidth = av.getAlignment().getHiddenSequences()
1807 if (hwidth > alWidth)
1812 // We can still insert gaps if the selectionGroup
1813 // contains all the sequences
1814 sg.setEndRes(sg.getEndRes() + startres - editLastRes);
1815 fixedRight = alWidth + startres - editLastRes;
1825 else if (!insertGap)
1827 // / Are we able to delete?
1828 // ie are all columns blank?
1830 for (g = 0; g < groupSize; g++)
1832 for (int j = startres; j < editLastRes; j++)
1834 if (groupSeqs[g].getLength() <= j)
1839 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1841 // Not a gap, block edit not valid
1850 // dragging to the right
1851 if (fixedColumns && fixedRight != -1)
1853 for (int j = editLastRes; j < startres; j++)
1855 insertGap(j, groupSeqs, fixedRight);
1860 appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1861 startres - editLastRes, false);
1866 // dragging to the left
1867 if (fixedColumns && fixedRight != -1)
1869 for (int j = editLastRes; j > startres; j--)
1871 deleteChar(startres, groupSeqs, fixedRight);
1876 appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1877 editLastRes - startres, false);
1884 * editing a single sequence
1888 // dragging to the right
1889 if (fixedColumns && fixedRight != -1)
1891 for (int j = editLastRes; j < startres; j++)
1893 if (!insertGap(j, seqs, fixedRight))
1896 * e.g. cursor mode command specified
1897 * more inserts than are possible
1905 appendEdit(Action.INSERT_GAP, seqs, editLastRes,
1906 startres - editLastRes, false);
1913 // dragging to the left
1914 if (fixedColumns && fixedRight != -1)
1916 for (int j = editLastRes; j > startres; j--)
1918 if (!Comparison.isGap(seq.getCharAt(startres)))
1922 deleteChar(startres, seqs, fixedRight);
1927 // could be a keyboard edit trying to delete none gaps
1929 for (int m = startres; m < editLastRes; m++)
1931 if (!Comparison.isGap(seq.getCharAt(m)))
1939 appendEdit(Action.DELETE_GAP, seqs, startres, max, false);
1944 {// insertGap==false AND editSeq==TRUE;
1945 if (fixedColumns && fixedRight != -1)
1947 for (int j = editLastRes; j < startres; j++)
1949 insertGap(j, seqs, fixedRight);
1954 appendEdit(Action.INSERT_NUC, seqs, editLastRes,
1955 startres - editLastRes, false);
1965 * Constructs an informative status bar message while dragging to insert or
1966 * delete gaps. Answers null if inserts and deletes cancel out.
1968 * @param editCommand
1969 * a command containing the list of individual edits
1972 protected static String getEditStatusMessage(EditCommand editCommand)
1974 if (editCommand == null)
1980 * add any inserts, and subtract any deletes,
1981 * not counting those auto-inserted when doing a 'locked edit'
1982 * (so only counting edits 'under the cursor')
1985 for (Edit cmd : editCommand.getEdits())
1987 if (!cmd.isSystemGenerated())
1989 count += cmd.getAction() == Action.INSERT_GAP ? cmd.getNumber()
1997 * inserts and deletes cancel out
2002 String msgKey = count > 1 ? "label.insert_gaps"
2003 : (count == 1 ? "label.insert_gap"
2004 : (count == -1 ? "label.delete_gap"
2005 : "label.delete_gaps"));
2006 count = Math.abs(count);
2008 return MessageManager.formatMessage(msgKey, String.valueOf(count));
2012 * Inserts one gap at column j, deleting the right-most gapped column up to
2013 * (and including) fixedColumn. Returns true if the edit is successful, false
2014 * if no blank column is available to allow the insertion to be balanced by a
2019 * @param fixedColumn
2022 boolean insertGap(int j, SequenceI[] seq, int fixedColumn)
2024 int blankColumn = fixedColumn;
2025 for (int s = 0; s < seq.length; s++)
2027 // Find the next gap before the end of the visible region boundary
2028 // If lastCol > j, theres a boundary after the gap insertion
2030 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
2032 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
2034 // Theres a space, so break and insert the gap
2039 if (blankColumn <= j)
2041 blankColumn = fixedColumn;
2047 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1, true);
2049 appendEdit(Action.INSERT_GAP, seq, j, 1, false);
2055 * Helper method to add and perform one edit action
2061 * @param systemGenerated
2062 * true if the edit is a 'balancing' delete (or insert) to match a
2063 * user's insert (or delete) in a locked editing region
2065 protected void appendEdit(Action action, SequenceI[] seq, int pos,
2066 int count, boolean systemGenerated)
2069 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
2070 av.getAlignment().getGapCharacter());
2071 edit.setSystemGenerated(systemGenerated);
2073 editCommand.appendEdit(edit, av.getAlignment(), true, null);
2077 * Deletes the character at column j, and inserts a gap at fixedColumn, in
2078 * each of the given sequences. The caller should ensure that all sequences
2079 * are gapped in column j.
2083 * @param fixedColumn
2085 void deleteChar(int j, SequenceI[] seqs, int fixedColumn)
2087 appendEdit(Action.DELETE_GAP, seqs, j, 1, false);
2089 appendEdit(Action.INSERT_GAP, seqs, fixedColumn, 1, true);
2093 * On reentering the panel, stops any scrolling that was started on dragging
2099 public void mouseEntered(MouseEvent e)
2109 * On leaving the panel, if the mouse is being dragged, starts a thread to
2110 * scroll it until the mouse is released (in unwrapped mode only)
2115 public void mouseExited(MouseEvent e)
2117 lastMousePosition = null;
2118 ap.alignFrame.setStatus(" ");
2119 if (av.getWrapAlignment())
2124 if (mouseDragging && scrollThread == null)
2126 startScrolling(e.getPoint());
2131 * Handler for double-click on a position with one or more sequence features.
2132 * Opens the Amend Features dialog to allow feature details to be amended, or
2133 * the feature deleted.
2136 public void mouseClicked(MouseEvent evt)
2138 SequenceGroup sg = null;
2139 MousePos pos = findMousePosition(evt);
2140 if (pos.isOverAnnotation() || pos.seqIndex == -1 || pos.column == -1)
2145 if (evt.getClickCount() > 1 && av.isShowSequenceFeatures())
2147 sg = av.getSelectionGroup();
2148 if (sg != null && sg.getSize() == 1
2149 && sg.getEndRes() - sg.getStartRes() < 2)
2151 av.setSelectionGroup(null);
2154 int column = pos.column;
2157 * find features at the position (if not gapped), or straddling
2158 * the position (if at a gap)
2160 SequenceI sequence = av.getAlignment().getSequenceAt(pos.seqIndex);
2161 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
2162 .findFeaturesAtColumn(sequence, column + 1);
2164 if (!features.isEmpty())
2167 * highlight the first feature at the position on the alignment
2169 SearchResultsI highlight = new SearchResults();
2170 highlight.addResult(sequence, features.get(0).getBegin(),
2171 features.get(0).getEnd());
2172 seqCanvas.highlightSearchResults(highlight, true);
2175 * open the Amend Features dialog
2177 new FeatureEditor(ap, Collections.singletonList(sequence), features,
2178 false).showDialog();
2184 public void mouseWheelMoved(MouseWheelEvent e)
2187 double wheelRotation = e.getPreciseWheelRotation();
2188 if (wheelRotation > 0)
2190 if (e.isShiftDown())
2192 av.getRanges().scrollRight(true);
2197 av.getRanges().scrollUp(false);
2200 else if (wheelRotation < 0)
2202 if (e.isShiftDown())
2204 av.getRanges().scrollRight(false);
2208 av.getRanges().scrollUp(true);
2213 * update status bar and tooltip for new position
2214 * (need to synthesize a mouse movement to refresh tooltip)
2217 ToolTipManager.sharedInstance().mouseMoved(e);
2226 protected void doMousePressedDefineMode(MouseEvent evt, MousePos pos)
2228 if (pos.isOverAnnotation() || pos.seqIndex == -1 || pos.column == -1)
2233 final int res = pos.column;
2234 final int seq = pos.seqIndex;
2236 updateOverviewAndStructs = false;
2238 startWrapBlock = wrappedBlock;
2240 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
2242 if ((sequence == null) || (res > sequence.getLength()))
2247 stretchGroup = av.getSelectionGroup();
2249 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
2251 stretchGroup = av.getAlignment().findGroup(sequence, res);
2252 if (stretchGroup != null)
2254 // only update the current selection if the popup menu has a group to
2256 av.setSelectionGroup(stretchGroup);
2261 * defer right-mouse click handling to mouseReleased on Windows
2262 * (where isPopupTrigger() will answer true)
2263 * NB isRightMouseButton is also true for Cmd-click on Mac
2265 if (Platform.isWinRightButton(evt))
2270 if (evt.isPopupTrigger()) // Mac: mousePressed
2272 showPopupMenu(evt, pos);
2278 seqCanvas.cursorX = res;
2279 seqCanvas.cursorY = seq;
2280 seqCanvas.repaint();
2284 if (stretchGroup == null)
2286 createStretchGroup(res, sequence);
2289 if (stretchGroup != null)
2291 stretchGroup.addPropertyChangeListener(seqCanvas);
2294 seqCanvas.repaint();
2297 private void createStretchGroup(int res, SequenceI sequence)
2299 // Only if left mouse button do we want to change group sizes
2300 // define a new group here
2301 SequenceGroup sg = new SequenceGroup();
2302 sg.setStartRes(res);
2304 sg.addSequence(sequence, false);
2305 av.setSelectionGroup(sg);
2308 if (av.getConservationSelected())
2310 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
2314 if (av.getAbovePIDThreshold())
2316 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
2319 // TODO: stretchGroup will always be not null. Is this a merge error ?
2320 // or is there a threading issue here?
2321 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
2323 // Edit end res position of selected group
2324 changeEndRes = true;
2326 else if ((stretchGroup != null) && (stretchGroup.getStartRes() == res))
2328 // Edit end res position of selected group
2329 changeStartRes = true;
2331 stretchGroup.getWidth();
2336 * Build and show a pop-up menu at the right-click mouse position
2341 void showPopupMenu(MouseEvent evt, MousePos pos)
2343 final int column = pos.column;
2344 final int seq = pos.seqIndex;
2345 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
2346 if (sequence != null)
2348 PopupMenu pop = new PopupMenu(ap, sequence, column);
2349 pop.show(this, evt.getX(), evt.getY());
2354 * Update the display after mouse up on a selection or group
2357 * mouse released event details
2359 * true if this event is happening after a mouse drag (rather than a
2362 protected void doMouseReleasedDefineMode(MouseEvent evt,
2365 if (stretchGroup == null)
2370 stretchGroup.removePropertyChangeListener(seqCanvas);
2372 // always do this - annotation has own state
2373 // but defer colourscheme update until hidden sequences are passed in
2374 boolean vischange = stretchGroup.recalcConservation(true);
2375 updateOverviewAndStructs |= vischange && av.isSelectionDefinedGroup()
2377 if (stretchGroup.cs != null)
2381 stretchGroup.cs.alignmentChanged(stretchGroup,
2382 av.getHiddenRepSequences());
2385 ResidueShaderI groupColourScheme = stretchGroup
2386 .getGroupColourScheme();
2387 String name = stretchGroup.getName();
2388 if (stretchGroup.cs.conservationApplied())
2390 SliderPanel.setConservationSlider(ap, groupColourScheme, name);
2392 if (stretchGroup.cs.getThreshold() > 0)
2394 SliderPanel.setPIDSliderSource(ap, groupColourScheme, name);
2397 PaintRefresher.Refresh(this, av.getSequenceSetId());
2398 // TODO: structure colours only need updating if stretchGroup used to or now
2399 // does contain sequences with structure views
2400 ap.paintAlignment(updateOverviewAndStructs, updateOverviewAndStructs);
2401 updateOverviewAndStructs = false;
2402 changeEndRes = false;
2403 changeStartRes = false;
2404 stretchGroup = null;
2409 * Resizes the borders of a selection group depending on the direction of
2414 protected void dragStretchGroup(MouseEvent evt)
2416 if (stretchGroup == null)
2421 MousePos pos = findMousePosition(evt);
2422 if (pos.isOverAnnotation() || pos.column == -1 || pos.seqIndex == -1)
2427 int res = pos.column;
2428 int y = pos.seqIndex;
2430 if (wrappedBlock != startWrapBlock)
2435 res = Math.min(res, av.getAlignment().getWidth() - 1);
2437 if (stretchGroup.getEndRes() == res)
2439 // Edit end res position of selected group
2440 changeEndRes = true;
2442 else if (stretchGroup.getStartRes() == res)
2444 // Edit start res position of selected group
2445 changeStartRes = true;
2448 if (res < av.getRanges().getStartRes())
2450 res = av.getRanges().getStartRes();
2455 if (res > (stretchGroup.getStartRes() - 1))
2457 stretchGroup.setEndRes(res);
2458 updateOverviewAndStructs |= av.isSelectionDefinedGroup();
2461 else if (changeStartRes)
2463 if (res < (stretchGroup.getEndRes() + 1))
2465 stretchGroup.setStartRes(res);
2466 updateOverviewAndStructs |= av.isSelectionDefinedGroup();
2470 int dragDirection = 0;
2476 else if (y < oldSeq)
2481 while ((y != oldSeq) && (oldSeq > -1)
2482 && (y < av.getAlignment().getHeight()))
2484 // This routine ensures we don't skip any sequences, as the
2485 // selection is quite slow.
2486 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
2488 oldSeq += dragDirection;
2495 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
2497 if (stretchGroup.getSequences(null).contains(nextSeq))
2499 stretchGroup.deleteSequence(seq, false);
2500 updateOverviewAndStructs |= av.isSelectionDefinedGroup();
2506 stretchGroup.addSequence(seq, false);
2509 stretchGroup.addSequence(nextSeq, false);
2510 updateOverviewAndStructs |= av.isSelectionDefinedGroup();
2519 mouseDragging = true;
2521 if (scrollThread != null)
2523 scrollThread.setMousePosition(evt.getPoint());
2527 * construct a status message showing the range of the selection
2529 StringBuilder status = new StringBuilder(64);
2530 List<SequenceI> seqs = stretchGroup.getSequences();
2531 String name = seqs.get(0).getName();
2532 if (name.length() > 20)
2534 name = name.substring(0, 20);
2536 status.append(name).append(" - ");
2537 name = seqs.get(seqs.size() - 1).getName();
2538 if (name.length() > 20)
2540 name = name.substring(0, 20);
2542 status.append(name).append(" ");
2543 int startRes = stretchGroup.getStartRes();
2544 status.append(" cols ").append(String.valueOf(startRes + 1))
2546 int endRes = stretchGroup.getEndRes();
2547 status.append(String.valueOf(endRes + 1));
2548 status.append(" (").append(String.valueOf(seqs.size())).append(" x ")
2549 .append(String.valueOf(endRes - startRes + 1)).append(")");
2550 ap.alignFrame.setStatus(status.toString());
2554 * Stops the scroll thread if it is running
2556 void stopScrolling()
2558 if (scrollThread != null)
2560 scrollThread.stopScrolling();
2561 scrollThread = null;
2563 mouseDragging = false;
2567 * Starts a thread to scroll the alignment, towards a given mouse position
2568 * outside the panel bounds, unless the alignment is in wrapped mode
2572 void startScrolling(Point mousePos)
2575 * set this.mouseDragging in case this was called from
2576 * a drag in ScalePanel or AnnotationPanel
2578 mouseDragging = true;
2579 if (!av.getWrapAlignment() && scrollThread == null)
2581 scrollThread = new ScrollThread();
2582 scrollThread.setMousePosition(mousePos);
2583 if (Platform.isJS())
2586 * Javascript - run every 20ms until scrolling stopped
2587 * or reaches the limit of scrollable alignment
2589 Timer t = new Timer(20, new ActionListener()
2592 public void actionPerformed(ActionEvent e)
2594 if (scrollThread != null)
2596 // if (!scrollOnce() {t.stop();}) gives compiler error :-(
2597 scrollThread.scrollOnce();
2601 t.addActionListener(new ActionListener()
2604 public void actionPerformed(ActionEvent e)
2606 if (scrollThread == null)
2608 // SeqPanel.stopScrolling called
2618 * Java - run in a new thread
2620 scrollThread.start();
2626 * Performs scrolling of the visible alignment left, right, up or down, until
2627 * scrolling is stopped by calling stopScrolling, mouse drag is ended, or the
2628 * limit of the alignment is reached
2630 class ScrollThread extends Thread
2632 private Point mousePos;
2634 private volatile boolean keepRunning = true;
2639 public ScrollThread()
2641 setName("SeqPanel$ScrollThread");
2645 * Sets the position of the mouse that determines the direction of the
2646 * scroll to perform. If this is called as the mouse moves, scrolling should
2647 * respond accordingly. For example, if the mouse is dragged right, scroll
2648 * right should start; if the drag continues down, scroll down should also
2653 public void setMousePosition(Point p)
2659 * Sets a flag that will cause the thread to exit
2661 public void stopScrolling()
2663 keepRunning = false;
2667 * Scrolls the alignment left or right, and/or up or down, depending on the
2668 * last notified mouse position, until the limit of the alignment is
2669 * reached, or a flag is set to stop the scroll
2676 if (mousePos != null)
2678 keepRunning = scrollOnce();
2683 } catch (Exception ex)
2687 SeqPanel.this.scrollThread = null;
2693 * <li>one row up, if the mouse is above the panel</li>
2694 * <li>one row down, if the mouse is below the panel</li>
2695 * <li>one column left, if the mouse is left of the panel</li>
2696 * <li>one column right, if the mouse is right of the panel</li>
2698 * Answers true if a scroll was performed, false if not - meaning either
2699 * that the mouse position is within the panel, or the edge of the alignment
2702 boolean scrollOnce()
2705 * quit after mouseUp ensures interrupt in JalviewJS
2712 boolean scrolled = false;
2713 ViewportRanges ranges = SeqPanel.this.av.getRanges();
2720 // mouse is above this panel - try scroll up
2721 scrolled = ranges.scrollUp(true);
2723 else if (mousePos.y >= getHeight())
2725 // mouse is below this panel - try scroll down
2726 scrolled = ranges.scrollUp(false);
2730 * scroll left or right
2734 scrolled |= ranges.scrollRight(false);
2736 else if (mousePos.x >= getWidth())
2738 scrolled |= ranges.scrollRight(true);
2745 * modify current selection according to a received message.
2748 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
2749 HiddenColumns hidden, SelectionSource source)
2751 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
2752 // handles selection messages...
2753 // TODO: extend config options to allow user to control if selections may be
2754 // shared between viewports.
2755 boolean iSentTheSelection = (av == source
2756 || (source instanceof AlignViewport
2757 && ((AlignmentViewport) source).getSequenceSetId()
2758 .equals(av.getSequenceSetId())));
2760 if (iSentTheSelection)
2762 // respond to our own event by updating dependent dialogs
2763 if (ap.getCalculationDialog() != null)
2765 ap.getCalculationDialog().validateCalcTypes();
2771 // process further ?
2772 if (!av.followSelection)
2778 * Ignore the selection if there is one of our own pending.
2780 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
2786 * Check for selection in a view of which this one is a dna/protein
2789 if (selectionFromTranslation(seqsel, colsel, hidden, source))
2794 // do we want to thread this ? (contention with seqsel and colsel locks, I
2797 * only copy colsel if there is a real intersection between
2798 * sequence selection and this panel's alignment
2800 boolean repaint = false;
2801 boolean copycolsel = false;
2803 SequenceGroup sgroup = null;
2804 if (seqsel != null && seqsel.getSize() > 0)
2806 if (av.getAlignment() == null)
2808 Console.warn("alignviewport av SeqSetId=" + av.getSequenceSetId()
2809 + " ViewId=" + av.getViewId()
2810 + " 's alignment is NULL! returning immediately.");
2813 sgroup = seqsel.intersect(av.getAlignment(),
2814 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
2815 if ((sgroup != null && sgroup.getSize() > 0))
2820 if (sgroup != null && sgroup.getSize() > 0)
2822 av.setSelectionGroup(sgroup);
2826 av.setSelectionGroup(null);
2828 av.isSelectionGroupChanged(true);
2833 // the current selection is unset or from a previous message
2834 // so import the new colsel.
2835 if (colsel == null || colsel.isEmpty())
2837 if (av.getColumnSelection() != null)
2839 av.getColumnSelection().clear();
2845 // TODO: shift colSel according to the intersecting sequences
2846 if (av.getColumnSelection() == null)
2848 av.setColumnSelection(new ColumnSelection(colsel));
2852 av.getColumnSelection().setElementsFrom(colsel,
2853 av.getAlignment().getHiddenColumns());
2856 av.isColSelChanged(true);
2860 if (copycolsel && av.hasHiddenColumns()
2861 && (av.getAlignment().getHiddenColumns() == null))
2863 System.err.println("Bad things");
2865 if (repaint) // always true!
2867 // probably finessing with multiple redraws here
2868 PaintRefresher.Refresh(this, av.getSequenceSetId());
2869 // ap.paintAlignment(false);
2872 // lastly, update dependent dialogs
2873 if (ap.getCalculationDialog() != null)
2875 ap.getCalculationDialog().validateCalcTypes();
2881 * If this panel is a cdna/protein translation view of the selection source,
2882 * tries to map the source selection to a local one, and returns true. Else
2889 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2890 ColumnSelection colsel, HiddenColumns hidden,
2891 SelectionSource source)
2893 if (!(source instanceof AlignViewportI))
2897 final AlignViewportI sourceAv = (AlignViewportI) source;
2898 if (sourceAv.getCodingComplement() != av
2899 && av.getCodingComplement() != sourceAv)
2905 * Map sequence selection
2907 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2908 av.setSelectionGroup(sg != null && sg.getSize() > 0 ? sg : null);
2909 av.isSelectionGroupChanged(true);
2912 * Map column selection
2914 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2916 ColumnSelection cs = new ColumnSelection();
2917 HiddenColumns hs = new HiddenColumns();
2918 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
2919 av.setColumnSelection(cs);
2920 boolean hiddenChanged = av.getAlignment().setHiddenColumns(hs);
2922 // lastly, update any dependent dialogs
2923 if (ap.getCalculationDialog() != null)
2925 ap.getCalculationDialog().validateCalcTypes();
2929 * repaint alignment, and also Overview or Structure
2930 * if hidden column selection has changed
2932 ap.paintAlignment(hiddenChanged, hiddenChanged);
2939 * @return null or last search results handled by this panel
2941 public SearchResultsI getLastSearchResults()
2943 return lastSearchResults;