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 jalview.api.AlignViewportI;
24 import jalview.bin.Cache;
25 import jalview.commands.EditCommand;
26 import jalview.commands.EditCommand.Action;
27 import jalview.commands.EditCommand.Edit;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.HiddenColumns;
31 import jalview.datamodel.SearchResultMatchI;
32 import jalview.datamodel.SearchResults;
33 import jalview.datamodel.SearchResultsI;
34 import jalview.datamodel.Sequence;
35 import jalview.datamodel.SequenceFeature;
36 import jalview.datamodel.SequenceGroup;
37 import jalview.datamodel.SequenceI;
38 import jalview.io.SequenceAnnotationReport;
39 import jalview.renderer.ResidueShaderI;
40 import jalview.schemes.ResidueProperties;
41 import jalview.structure.SelectionListener;
42 import jalview.structure.SelectionSource;
43 import jalview.structure.SequenceListener;
44 import jalview.structure.StructureSelectionManager;
45 import jalview.structure.VamsasSource;
46 import jalview.util.Comparison;
47 import jalview.util.MappingUtils;
48 import jalview.util.MessageManager;
49 import jalview.util.Platform;
50 import jalview.viewmodel.AlignmentViewport;
52 import java.awt.BorderLayout;
53 import java.awt.Color;
55 import java.awt.FontMetrics;
56 import java.awt.Point;
57 import java.awt.event.MouseEvent;
58 import java.awt.event.MouseListener;
59 import java.awt.event.MouseMotionListener;
60 import java.awt.event.MouseWheelEvent;
61 import java.awt.event.MouseWheelListener;
62 import java.util.ArrayList;
63 import java.util.Collections;
64 import java.util.List;
66 import javax.swing.JPanel;
67 import javax.swing.SwingUtilities;
68 import javax.swing.ToolTipManager;
74 * @version $Revision: 1.130 $
76 public class SeqPanel extends JPanel implements MouseListener,
77 MouseMotionListener, MouseWheelListener, SequenceListener,
82 public SeqCanvas seqCanvas;
85 public AlignmentPanel ap;
87 protected int lastres;
89 protected int startseq;
91 protected AlignViewport av;
93 ScrollThread scrollThread = null;
95 boolean mouseDragging = false;
97 boolean editingSeqs = false;
99 boolean groupEditing = false;
101 // ////////////////////////////////////////
102 // ///Everything below this is for defining the boundary of the rubberband
103 // ////////////////////////////////////////
106 boolean changeEndSeq = false;
108 boolean changeStartSeq = false;
110 boolean changeEndRes = false;
112 boolean changeStartRes = false;
114 SequenceGroup stretchGroup = null;
116 boolean remove = false;
118 Point lastMousePress;
120 boolean mouseWheelPressed = false;
122 StringBuffer keyboardNo1;
124 StringBuffer keyboardNo2;
126 java.net.URL linkImageURL;
128 private final SequenceAnnotationReport seqARep;
130 StringBuilder tooltipText = new StringBuilder();
134 EditCommand editCommand;
136 StructureSelectionManager ssm;
138 SearchResultsI lastSearchResults;
141 * Creates a new SeqPanel object.
148 public SeqPanel(AlignViewport av, AlignmentPanel ap)
150 linkImageURL = getClass().getResource("/images/link.gif");
151 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
152 ToolTipManager.sharedInstance().registerComponent(this);
153 ToolTipManager.sharedInstance().setInitialDelay(0);
154 ToolTipManager.sharedInstance().setDismissDelay(10000);
156 setBackground(Color.white);
158 seqCanvas = new SeqCanvas(ap);
159 setLayout(new BorderLayout());
160 add(seqCanvas, BorderLayout.CENTER);
166 addMouseMotionListener(this);
167 addMouseListener(this);
168 addMouseWheelListener(this);
169 ssm = av.getStructureSelectionManager();
170 ssm.addStructureViewerListener(this);
171 ssm.addSelectionListener(this);
175 int startWrapBlock = -1;
177 int wrappedBlock = -1;
180 * Returns the aligned sequence position (base 0) at the mouse position, or
181 * the closest visible one
186 int findColumn(MouseEvent evt)
191 if (av.getWrapAlignment())
194 int hgap = av.getCharHeight();
195 if (av.getScaleAboveWrapped())
197 hgap += av.getCharHeight();
200 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
201 + hgap + seqCanvas.getAnnotationHeight();
205 x -= seqCanvas.LABEL_WEST;
207 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
213 wrappedBlock = y / cHeight;
214 wrappedBlock += av.getRanges().getStartRes() / cwidth;
216 res = wrappedBlock * cwidth + x / av.getCharWidth();
221 if (x > seqCanvas.getX() + seqCanvas.getWidth())
223 // make sure we calculate relative to visible alignment, rather than
225 x = seqCanvas.getX() + seqCanvas.getWidth();
227 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
228 if (res > av.getRanges().getEndRes())
231 res = av.getRanges().getEndRes();
235 if (av.hasHiddenColumns())
237 res = av.getAlignment().getHiddenColumns()
238 .adjustForHiddenColumns(res);
245 int findSeq(MouseEvent evt)
250 if (av.getWrapAlignment())
252 int hgap = av.getCharHeight();
253 if (av.getScaleAboveWrapped())
255 hgap += av.getCharHeight();
258 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
259 + hgap + seqCanvas.getAnnotationHeight();
263 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
268 seq = Math.min((y / av.getCharHeight())
269 + av.getRanges().getStartSeq(),
271 .getAlignment().getHeight() - 1);
278 * When all of a sequence of edits are complete, put the resulting edit list
279 * on the history stack (undo list), and reset flags for editing in progress.
285 if (editCommand != null && editCommand.getSize() > 0)
287 ap.alignFrame.addHistoryItem(editCommand);
288 av.firePropertyChange("alignment", null, av.getAlignment()
294 * Tidy up come what may...
299 groupEditing = false;
308 seqCanvas.cursorY = getKeyboardNo1() - 1;
312 void setCursorColumn()
314 seqCanvas.cursorX = getKeyboardNo1() - 1;
318 void setCursorRowAndColumn()
320 if (keyboardNo2 == null)
322 keyboardNo2 = new StringBuffer();
326 seqCanvas.cursorX = getKeyboardNo1() - 1;
327 seqCanvas.cursorY = getKeyboardNo2() - 1;
332 void setCursorPosition()
334 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
336 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
340 void moveCursor(int dx, int dy)
342 seqCanvas.cursorX += dx;
343 seqCanvas.cursorY += dy;
345 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
347 if (av.hasHiddenColumns()
348 && !hidden.isVisible(seqCanvas.cursorX))
350 int original = seqCanvas.cursorX - dx;
351 int maxWidth = av.getAlignment().getWidth();
353 while (!hidden.isVisible(seqCanvas.cursorX)
354 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
356 seqCanvas.cursorX += dx;
359 if (seqCanvas.cursorX >= maxWidth
360 || !hidden.isVisible(seqCanvas.cursorX))
362 seqCanvas.cursorX = original;
369 void scrollToVisible()
371 if (seqCanvas.cursorX < 0)
373 seqCanvas.cursorX = 0;
375 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
377 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
380 if (seqCanvas.cursorY < 0)
382 seqCanvas.cursorY = 0;
384 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
386 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
390 if (av.getWrapAlignment())
392 av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX);
396 av.getRanges().scrollToVisible(seqCanvas.cursorX, seqCanvas.cursorY,
399 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
400 seqCanvas.cursorX, seqCanvas.cursorY);
405 void setSelectionAreaAtCursor(boolean topLeft)
407 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
409 if (av.getSelectionGroup() != null)
411 SequenceGroup sg = av.getSelectionGroup();
412 // Find the top and bottom of this group
413 int min = av.getAlignment().getHeight(), max = 0;
414 for (int i = 0; i < sg.getSize(); i++)
416 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
431 sg.setStartRes(seqCanvas.cursorX);
432 if (sg.getEndRes() < seqCanvas.cursorX)
434 sg.setEndRes(seqCanvas.cursorX);
437 min = seqCanvas.cursorY;
441 sg.setEndRes(seqCanvas.cursorX);
442 if (sg.getStartRes() > seqCanvas.cursorX)
444 sg.setStartRes(seqCanvas.cursorX);
447 max = seqCanvas.cursorY + 1;
452 // Only the user can do this
453 av.setSelectionGroup(null);
457 // Now add any sequences between min and max
458 sg.getSequences(null).clear();
459 for (int i = min; i < max; i++)
461 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
466 if (av.getSelectionGroup() == null)
468 SequenceGroup sg = new SequenceGroup();
469 sg.setStartRes(seqCanvas.cursorX);
470 sg.setEndRes(seqCanvas.cursorX);
471 sg.addSequence(sequence, false);
472 av.setSelectionGroup(sg);
475 ap.paintAlignment(false);
479 void insertGapAtCursor(boolean group)
481 groupEditing = group;
482 startseq = seqCanvas.cursorY;
483 lastres = seqCanvas.cursorX;
484 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
488 void deleteGapAtCursor(boolean group)
490 groupEditing = group;
491 startseq = seqCanvas.cursorY;
492 lastres = seqCanvas.cursorX + getKeyboardNo1();
493 editSequence(false, false, seqCanvas.cursorX);
497 void insertNucAtCursor(boolean group, String nuc)
499 // TODO not called - delete?
500 groupEditing = group;
501 startseq = seqCanvas.cursorY;
502 lastres = seqCanvas.cursorX;
503 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
507 void numberPressed(char value)
509 if (keyboardNo1 == null)
511 keyboardNo1 = new StringBuffer();
514 if (keyboardNo2 != null)
516 keyboardNo2.append(value);
520 keyboardNo1.append(value);
528 if (keyboardNo1 != null)
530 int value = Integer.parseInt(keyboardNo1.toString());
534 } catch (Exception x)
545 if (keyboardNo2 != null)
547 int value = Integer.parseInt(keyboardNo2.toString());
551 } catch (Exception x)
565 public void mouseReleased(MouseEvent evt)
567 mouseDragging = false;
568 mouseWheelPressed = false;
570 if (evt.isPopupTrigger()) // Windows: mouseReleased
579 doMouseReleasedDefineMode(evt);
593 public void mousePressed(MouseEvent evt)
595 lastMousePress = evt.getPoint();
597 if (SwingUtilities.isMiddleMouseButton(evt))
599 mouseWheelPressed = true;
603 boolean isControlDown = Platform.isControlDown(evt);
604 if (evt.isShiftDown() || isControlDown)
614 doMousePressedDefineMode(evt);
618 int seq = findSeq(evt);
619 int res = findColumn(evt);
621 if (seq < 0 || res < 0)
626 if ((seq < av.getAlignment().getHeight())
627 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
644 public void mouseOverSequence(SequenceI sequence, int index, int pos)
646 String tmp = sequence.hashCode() + " " + index + " " + pos;
648 if (lastMessage == null || !lastMessage.equals(tmp))
650 // System.err.println("mouseOver Sequence: "+tmp);
651 ssm.mouseOverSequence(sequence, index, pos, av);
657 * Highlight the mapped region described by the search results object (unless
658 * unchanged). This supports highlight of protein while mousing over linked
659 * cDNA and vice versa. The status bar is also updated to show the location of
660 * the start of the highlighted region.
663 public void highlightSequence(SearchResultsI results)
665 if (results == null || results.equals(lastSearchResults))
669 lastSearchResults = results;
671 if (av.isFollowHighlight())
673 // don't allow highlight of protein/cDNA to also scroll a complementary
674 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
675 // over residue to change abruptly, causing highlighted residue in panel 2
676 // to change, causing a scroll in panel 1 etc)
677 ap.setToScrollComplementPanel(false);
678 if (ap.scrollToPosition(results, false))
680 seqCanvas.revalidate();
682 ap.setToScrollComplementPanel(true);
684 setStatusMessage(results);
685 seqCanvas.highlightSearchResults(results);
689 public VamsasSource getVamsasSource()
691 return this.ap == null ? null : this.ap.av;
695 public void updateColours(SequenceI seq, int index)
697 System.out.println("update the seqPanel colours");
708 public void mouseMoved(MouseEvent evt)
712 // This is because MacOSX creates a mouseMoved
713 // If control is down, other platforms will not.
717 final int column = findColumn(evt);
718 int seq = findSeq(evt);
719 if (column < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
724 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
726 if (column >= sequence.getLength())
732 * set status bar message, returning residue position in sequence
734 final int pos = setStatusMessage(sequence, column, seq);
735 if (ssm != null && pos > -1)
737 mouseOverSequence(sequence, column, pos);
740 tooltipText.setLength(6); // Cuts the buffer back to <html>
742 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
745 for (int g = 0; g < groups.length; g++)
747 if (groups[g].getStartRes() <= column
748 && groups[g].getEndRes() >= column)
750 if (!groups[g].getName().startsWith("JTreeGroup")
751 && !groups[g].getName().startsWith("JGroup"))
753 tooltipText.append(groups[g].getName());
756 if (groups[g].getDescription() != null)
758 tooltipText.append(": " + groups[g].getDescription());
764 if (av.isShowSequenceFeatures() && pos != -1)
766 List<SequenceFeature> features = ap.getFeatureRenderer()
767 .findFeaturesAtRes(sequence.getDatasetSequence(), pos);
768 seqARep.appendFeatures(tooltipText, pos, features,
769 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
771 if (tooltipText.length() == 6) // <html>
773 setToolTipText(null);
778 if (lastTooltip == null
779 || !lastTooltip.equals(tooltipText.toString()))
781 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
782 tooltipText.toString());
783 // String formatedTooltipText = tooltipText.toString();
784 setToolTipText(formatedTooltipText);
785 lastTooltip = tooltipText.toString();
792 private Point lastp = null;
797 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
800 public Point getToolTipLocation(MouseEvent event)
802 int x = event.getX(), w = getWidth();
803 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
806 if (!event.isShiftDown() || p == null)
808 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
809 event.getX() + wdth, event.getY() - 20) : null;
812 * TODO: try to modify position region is not obcured by tooltip
820 * set when the current UI interaction has resulted in a change that requires
821 * overview shading to be recalculated. this could be changed to something
822 * more expressive that indicates what actually has changed, so selective
823 * redraws can be applied
825 private boolean needOverviewUpdate = false; // TODO: refactor to avcontroller
828 * set if av.getSelectionGroup() refers to a group that is defined on the
829 * alignment view, rather than a transient selection
831 // private boolean editingDefinedGroup = false; // TODO: refactor to
832 // avcontroller or viewModel
835 * Sets the status message in alignment panel, showing the sequence number
836 * (index) and id, residue and residue position for the given sequence and
837 * column position. Returns the calculated residue position in the sequence,
838 * or -1 for a gapped column position.
841 * aligned sequence object
845 * index of sequence in alignment
846 * @return position of column in sequence or -1 if at a gap
848 int setStatusMessage(SequenceI sequence, final int column, int seq)
850 StringBuilder text = new StringBuilder(32);
853 * Sequence number (if known), and sequence name.
855 String seqno = seq == -1 ? "" : " " + (seq + 1);
856 text.append("Sequence").append(seqno).append(" ID: ")
857 .append(sequence.getName());
859 String residue = null;
861 * Try to translate the display character to residue name (null for gap).
863 final String displayChar = String.valueOf(sequence.getCharAt(column));
864 if (av.getAlignment().isNucleotide())
866 residue = ResidueProperties.nucleotideName.get(displayChar);
869 text.append(" Nucleotide: ").append(residue);
874 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
875 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
879 text.append(" Residue: ").append(residue);
886 pos = sequence.findPosition(column);
887 text.append(" (").append(Integer.toString(pos)).append(")");
889 ap.alignFrame.statusBar.setText(text.toString());
894 * Set the status bar message to highlight the first matched position in
899 private void setStatusMessage(SearchResultsI results)
901 AlignmentI al = this.av.getAlignment();
902 int sequenceIndex = al.findIndex(results);
903 if (sequenceIndex == -1)
907 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
908 for (SearchResultMatchI m : results.getResults())
910 SequenceI seq = m.getSequence();
911 if (seq.getDatasetSequence() != null)
913 seq = seq.getDatasetSequence();
919 * Convert position in sequence (base 1) to sequence character array
922 int start = m.getStart() - m.getSequence().getStart();
923 setStatusMessage(seq, start, sequenceIndex);
933 public void mouseDragged(MouseEvent evt)
935 if (mouseWheelPressed)
937 boolean inSplitFrame = ap.av.getCodingComplement() != null;
938 boolean copyChanges = inSplitFrame && av.isProteinFontAsCdna();
940 int oldWidth = av.getCharWidth();
942 // Which is bigger, left-right or up-down?
943 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
944 .getX() - lastMousePress.getX()))
947 * on drag up or down, decrement or increment font size
949 int fontSize = av.font.getSize();
950 boolean fontChanged = false;
952 if (evt.getY() < lastMousePress.getY())
957 else if (evt.getY() > lastMousePress.getY())
970 Font newFont = new Font(av.font.getName(), av.font.getStyle(),
972 av.setFont(newFont, true);
973 av.setCharWidth(oldWidth);
977 ap.av.getCodingComplement().setFont(newFont, true);
978 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
979 .getSplitViewContainer();
980 splitFrame.adjustLayout();
981 splitFrame.repaint();
988 * on drag left or right, decrement or increment character width
991 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
993 newWidth = av.getCharWidth() - 1;
994 av.setCharWidth(newWidth);
996 else if (evt.getX() > lastMousePress.getX())
998 newWidth = av.getCharWidth() + 1;
999 av.setCharWidth(newWidth);
1003 ap.paintAlignment(false);
1007 * need to ensure newWidth is set on cdna, regardless of which
1008 * panel the mouse drag happened in; protein will compute its
1009 * character width as 1:1 or 3:1
1011 av.getCodingComplement().setCharWidth(newWidth);
1012 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
1013 .getSplitViewContainer();
1014 splitFrame.adjustLayout();
1015 splitFrame.repaint();
1020 FontMetrics fm = getFontMetrics(av.getFont());
1021 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
1023 lastMousePress = evt.getPoint();
1030 doMouseDraggedDefineMode(evt);
1034 int res = findColumn(evt);
1041 if ((lastres == -1) || (lastres == res))
1046 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1048 // dragLeft, delete gap
1049 editSequence(false, false, res);
1053 editSequence(true, false, res);
1056 mouseDragging = true;
1057 if (scrollThread != null)
1059 scrollThread.setEvent(evt);
1063 // TODO: Make it more clever than many booleans
1064 synchronized void editSequence(boolean insertGap, boolean editSeq,
1068 int fixedRight = -1;
1069 boolean fixedColumns = false;
1070 SequenceGroup sg = av.getSelectionGroup();
1072 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1074 // No group, but the sequence may represent a group
1075 if (!groupEditing && av.hasHiddenRows())
1077 if (av.isHiddenRepSequence(seq))
1079 sg = av.getRepresentedSequences(seq);
1080 groupEditing = true;
1084 StringBuilder message = new StringBuilder(64);
1087 message.append("Edit group:");
1088 if (editCommand == null)
1090 editCommand = new EditCommand(
1091 MessageManager.getString("action.edit_group"));
1096 message.append("Edit sequence: " + seq.getName());
1097 String label = seq.getName();
1098 if (label.length() > 10)
1100 label = label.substring(0, 10);
1102 if (editCommand == null)
1104 editCommand = new EditCommand(MessageManager.formatMessage(
1105 "label.edit_params", new String[] { label }));
1111 message.append(" insert ");
1115 message.append(" delete ");
1118 message.append(Math.abs(startres - lastres) + " gaps.");
1119 ap.alignFrame.statusBar.setText(message.toString());
1121 // Are we editing within a selection group?
1123 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1126 fixedColumns = true;
1128 // sg might be null as the user may only see 1 sequence,
1129 // but the sequence represents a group
1132 if (!av.isHiddenRepSequence(seq))
1137 sg = av.getRepresentedSequences(seq);
1140 fixedLeft = sg.getStartRes();
1141 fixedRight = sg.getEndRes();
1143 if ((startres < fixedLeft && lastres >= fixedLeft)
1144 || (startres >= fixedLeft && lastres < fixedLeft)
1145 || (startres > fixedRight && lastres <= fixedRight)
1146 || (startres <= fixedRight && lastres > fixedRight))
1152 if (fixedLeft > startres)
1154 fixedRight = fixedLeft - 1;
1157 else if (fixedRight < startres)
1159 fixedLeft = fixedRight;
1164 if (av.hasHiddenColumns())
1166 fixedColumns = true;
1167 int y1 = av.getAlignment().getHiddenColumns()
1168 .getHiddenBoundaryLeft(startres);
1169 int y2 = av.getAlignment().getHiddenColumns()
1170 .getHiddenBoundaryRight(startres);
1172 if ((insertGap && startres > y1 && lastres < y1)
1173 || (!insertGap && startres < y2 && lastres > y2))
1179 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1180 // Selection spans a hidden region
1181 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1189 fixedRight = y2 - 1;
1196 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1197 int g, groupSize = vseqs.size();
1198 SequenceI[] groupSeqs = new SequenceI[groupSize];
1199 for (g = 0; g < groupSeqs.length; g++)
1201 groupSeqs[g] = vseqs.get(g);
1207 // If the user has selected the whole sequence, and is dragging to
1208 // the right, we can still extend the alignment and selectionGroup
1209 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1210 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1212 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1213 fixedRight = sg.getEndRes();
1216 // Is it valid with fixed columns??
1217 // Find the next gap before the end
1218 // of the visible region boundary
1219 boolean blank = false;
1220 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1224 for (g = 0; g < groupSize; g++)
1226 for (int j = 0; j < startres - lastres; j++)
1228 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1243 if (sg.getSize() == av.getAlignment().getHeight())
1245 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1246 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1252 int alWidth = av.getAlignment().getWidth();
1253 if (av.hasHiddenRows())
1255 int hwidth = av.getAlignment().getHiddenSequences()
1257 if (hwidth > alWidth)
1262 // We can still insert gaps if the selectionGroup
1263 // contains all the sequences
1264 sg.setEndRes(sg.getEndRes() + startres - lastres);
1265 fixedRight = alWidth + startres - lastres;
1276 else if (!insertGap)
1278 // / Are we able to delete?
1279 // ie are all columns blank?
1281 for (g = 0; g < groupSize; g++)
1283 for (int j = startres; j < lastres; j++)
1285 if (groupSeqs[g].getLength() <= j)
1290 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1292 // Not a gap, block edit not valid
1302 // dragging to the right
1303 if (fixedColumns && fixedRight != -1)
1305 for (int j = lastres; j < startres; j++)
1307 insertChar(j, groupSeqs, fixedRight);
1312 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1318 // dragging to the left
1319 if (fixedColumns && fixedRight != -1)
1321 for (int j = lastres; j > startres; j--)
1323 deleteChar(startres, groupSeqs, fixedRight);
1328 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1335 // ///Editing a single sequence///////////
1339 // dragging to the right
1340 if (fixedColumns && fixedRight != -1)
1342 for (int j = lastres; j < startres; j++)
1344 insertChar(j, new SequenceI[] { seq }, fixedRight);
1349 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1350 startres - lastres);
1357 // dragging to the left
1358 if (fixedColumns && fixedRight != -1)
1360 for (int j = lastres; j > startres; j--)
1362 if (!Comparison.isGap(seq.getCharAt(startres)))
1367 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1372 // could be a keyboard edit trying to delete none gaps
1374 for (int m = startres; m < lastres; m++)
1376 if (!Comparison.isGap(seq.getCharAt(m)))
1385 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1391 {// insertGap==false AND editSeq==TRUE;
1392 if (fixedColumns && fixedRight != -1)
1394 for (int j = lastres; j < startres; j++)
1396 insertChar(j, new SequenceI[] { seq }, fixedRight);
1401 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1402 startres - lastres);
1409 seqCanvas.repaint();
1412 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1414 int blankColumn = fixedColumn;
1415 for (int s = 0; s < seq.length; s++)
1417 // Find the next gap before the end of the visible region boundary
1418 // If lastCol > j, theres a boundary after the gap insertion
1420 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1422 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1424 // Theres a space, so break and insert the gap
1429 if (blankColumn <= j)
1431 blankColumn = fixedColumn;
1437 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1439 appendEdit(Action.INSERT_GAP, seq, j, 1);
1444 * Helper method to add and perform one edit action.
1451 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1455 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1456 av.getAlignment().getGapCharacter());
1458 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1461 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1464 appendEdit(Action.DELETE_GAP, seq, j, 1);
1466 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1476 public void mouseEntered(MouseEvent e)
1483 if (scrollThread != null)
1485 scrollThread.running = false;
1486 scrollThread = null;
1497 public void mouseExited(MouseEvent e)
1499 if (av.getWrapAlignment())
1506 scrollThread = new ScrollThread();
1511 * Handler for double-click on a position with one or more sequence features.
1512 * Opens the Amend Features dialog to allow feature details to be amended, or
1513 * the feature deleted.
1516 public void mouseClicked(MouseEvent evt)
1518 SequenceGroup sg = null;
1519 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1520 if (evt.getClickCount() > 1)
1522 sg = av.getSelectionGroup();
1523 if (sg != null && sg.getSize() == 1
1524 && sg.getEndRes() - sg.getStartRes() < 2)
1526 av.setSelectionGroup(null);
1529 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1530 .findFeaturesAtRes(sequence.getDatasetSequence(),
1531 sequence.findPosition(findColumn(evt)));
1533 if (!features.isEmpty())
1536 * highlight the first feature at the position on the alignment
1538 SearchResultsI highlight = new SearchResults();
1539 highlight.addResult(sequence, features.get(0).getBegin(), features
1541 seqCanvas.highlightSearchResults(highlight);
1544 * open the Amend Features dialog; clear highlighting afterwards,
1545 * whether changes were made or not
1547 List<SequenceI> seqs = Collections.singletonList(sequence);
1548 seqCanvas.getFeatureRenderer().amendFeatures(seqs, features, false,
1550 seqCanvas.highlightSearchResults(null);
1556 public void mouseWheelMoved(MouseWheelEvent e)
1559 if (e.getWheelRotation() > 0)
1561 if (e.isShiftDown())
1563 av.getRanges().scrollRight(true);
1568 av.getRanges().scrollUp(false);
1573 if (e.isShiftDown())
1575 av.getRanges().scrollRight(false);
1579 av.getRanges().scrollUp(true);
1582 // TODO Update tooltip for new position.
1591 public void doMousePressedDefineMode(MouseEvent evt)
1593 final int res = findColumn(evt);
1594 final int seq = findSeq(evt);
1596 needOverviewUpdate = false;
1598 startWrapBlock = wrappedBlock;
1600 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1602 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1603 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1604 MessageManager.getString("label.wrapped_view_no_edit"),
1605 JvOptionPane.WARNING_MESSAGE);
1609 if (seq < 0 || res < 0)
1614 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1616 if ((sequence == null) || (res > sequence.getLength()))
1621 stretchGroup = av.getSelectionGroup();
1623 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1625 stretchGroup = av.getAlignment().findGroup(sequence, res);
1626 if (stretchGroup != null)
1628 // only update the current selection if the popup menu has a group to
1630 av.setSelectionGroup(stretchGroup);
1634 if (evt.isPopupTrigger()) // Mac: mousePressed
1641 * defer right-mouse click handling to mouseReleased on Windows
1642 * (where isPopupTrigger() will answer true)
1643 * NB isRightMouseButton is also true for Cmd-click on Mac
1645 if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
1652 seqCanvas.cursorX = findColumn(evt);
1653 seqCanvas.cursorY = findSeq(evt);
1654 seqCanvas.repaint();
1658 if (stretchGroup == null)
1660 // Only if left mouse button do we want to change group sizes
1662 // define a new group here
1663 SequenceGroup sg = new SequenceGroup();
1664 sg.setStartRes(res);
1666 sg.addSequence(sequence, false);
1667 av.setSelectionGroup(sg);
1670 if (av.getConservationSelected())
1672 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1676 if (av.getAbovePIDThreshold())
1678 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1681 // TODO: stretchGroup will always be not null. Is this a merge error ?
1682 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1684 // Edit end res position of selected group
1685 changeEndRes = true;
1687 else if ((stretchGroup != null)
1688 && (stretchGroup.getStartRes() == res))
1690 // Edit end res position of selected group
1691 changeStartRes = true;
1693 stretchGroup.getWidth();
1696 seqCanvas.repaint();
1700 * Build and show a pop-up menu at the right-click mouse position
1706 void showPopupMenu(MouseEvent evt)
1708 final int res = findColumn(evt);
1709 final int seq = findSeq(evt);
1710 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1711 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1712 .findFeaturesAtRes(sequence.getDatasetSequence(),
1713 sequence.findPosition(res));
1714 List<String> links = new ArrayList<String>();
1715 for (SequenceFeature sf : allFeatures)
1717 if (sf.links != null)
1719 for (String link : sf.links)
1726 PopupMenu pop = new PopupMenu(ap, null, links);
1727 pop.show(this, evt.getX(), evt.getY());
1736 public void doMouseReleasedDefineMode(MouseEvent evt)
1738 if (stretchGroup == null)
1742 // always do this - annotation has own state
1743 // but defer colourscheme update until hidden sequences are passed in
1744 boolean vischange = stretchGroup.recalcConservation(true);
1745 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1746 if (stretchGroup.cs != null)
1748 stretchGroup.cs.alignmentChanged(stretchGroup,
1749 av.getHiddenRepSequences());
1751 ResidueShaderI groupColourScheme = stretchGroup.getGroupColourScheme();
1752 String name = stretchGroup.getName();
1753 if (stretchGroup.cs.conservationApplied())
1755 SliderPanel.setConservationSlider(ap, groupColourScheme, name);
1757 if (stretchGroup.cs.getThreshold() > 0)
1759 SliderPanel.setPIDSliderSource(ap, groupColourScheme, name);
1762 PaintRefresher.Refresh(this, av.getSequenceSetId());
1763 ap.paintAlignment(needOverviewUpdate);
1764 needOverviewUpdate = false;
1765 changeEndRes = false;
1766 changeStartRes = false;
1767 stretchGroup = null;
1777 public void doMouseDraggedDefineMode(MouseEvent evt)
1779 int res = findColumn(evt);
1780 int y = findSeq(evt);
1782 if (wrappedBlock != startWrapBlock)
1787 if (stretchGroup == null)
1792 if (res >= av.getAlignment().getWidth())
1794 res = av.getAlignment().getWidth() - 1;
1797 if (stretchGroup.getEndRes() == res)
1799 // Edit end res position of selected group
1800 changeEndRes = true;
1802 else if (stretchGroup.getStartRes() == res)
1804 // Edit start res position of selected group
1805 changeStartRes = true;
1808 if (res < av.getRanges().getStartRes())
1810 res = av.getRanges().getStartRes();
1815 if (res > (stretchGroup.getStartRes() - 1))
1817 stretchGroup.setEndRes(res);
1818 needOverviewUpdate |= av.isSelectionDefinedGroup();
1821 else if (changeStartRes)
1823 if (res < (stretchGroup.getEndRes() + 1))
1825 stretchGroup.setStartRes(res);
1826 needOverviewUpdate |= av.isSelectionDefinedGroup();
1830 int dragDirection = 0;
1836 else if (y < oldSeq)
1841 while ((y != oldSeq) && (oldSeq > -1)
1842 && (y < av.getAlignment().getHeight()))
1844 // This routine ensures we don't skip any sequences, as the
1845 // selection is quite slow.
1846 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1848 oldSeq += dragDirection;
1855 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1857 if (stretchGroup.getSequences(null).contains(nextSeq))
1859 stretchGroup.deleteSequence(seq, false);
1860 needOverviewUpdate |= av.isSelectionDefinedGroup();
1866 stretchGroup.addSequence(seq, false);
1869 stretchGroup.addSequence(nextSeq, false);
1870 needOverviewUpdate |= av.isSelectionDefinedGroup();
1879 mouseDragging = true;
1881 if (scrollThread != null)
1883 scrollThread.setEvent(evt);
1886 seqCanvas.repaint();
1889 void scrollCanvas(MouseEvent evt)
1893 if (scrollThread != null)
1895 scrollThread.running = false;
1896 scrollThread = null;
1898 mouseDragging = false;
1902 if (scrollThread == null)
1904 scrollThread = new ScrollThread();
1907 mouseDragging = true;
1908 scrollThread.setEvent(evt);
1913 // this class allows scrolling off the bottom of the visible alignment
1914 class ScrollThread extends Thread
1918 boolean running = false;
1920 public ScrollThread()
1925 public void setEvent(MouseEvent e)
1930 public void stopScrolling()
1944 if (mouseDragging && (evt.getY() < 0)
1945 && (av.getRanges().getStartSeq() > 0))
1947 running = av.getRanges().scrollUp(true);
1950 if (mouseDragging && (evt.getY() >= getHeight())
1951 && (av.getAlignment().getHeight() > av.getRanges()
1954 running = av.getRanges().scrollUp(false);
1957 if (mouseDragging && (evt.getX() < 0))
1959 running = av.getRanges().scrollRight(false);
1961 else if (mouseDragging && (evt.getX() >= getWidth()))
1963 running = av.getRanges().scrollRight(true);
1970 } catch (Exception ex)
1978 * modify current selection according to a received message.
1981 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1982 HiddenColumns hidden, SelectionSource source)
1984 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1985 // handles selection messages...
1986 // TODO: extend config options to allow user to control if selections may be
1987 // shared between viewports.
1988 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1989 .getSequenceSetId().equals(av.getSequenceSetId())));
1991 if (iSentTheSelection)
1993 // respond to our own event by updating dependent dialogs
1994 if (ap.getCalculationDialog() != null)
1996 ap.getCalculationDialog().validateCalcTypes();
1999 // process further ?
2000 if (!av.followSelection)
2007 * Ignore the selection if there is one of our own pending.
2009 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
2015 * Check for selection in a view of which this one is a dna/protein
2018 if (selectionFromTranslation(seqsel, colsel, hidden, source))
2023 // do we want to thread this ? (contention with seqsel and colsel locks, I
2026 * only copy colsel if there is a real intersection between
2027 * sequence selection and this panel's alignment
2029 boolean repaint = false;
2030 boolean copycolsel = false;
2032 SequenceGroup sgroup = null;
2033 if (seqsel != null && seqsel.getSize() > 0)
2035 if (av.getAlignment() == null)
2037 Cache.log.warn("alignviewport av SeqSetId=" + av.getSequenceSetId()
2038 + " ViewId=" + av.getViewId()
2039 + " 's alignment is NULL! returning immediately.");
2042 sgroup = seqsel.intersect(av.getAlignment(),
2043 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
2044 if ((sgroup != null && sgroup.getSize() > 0))
2049 if (sgroup != null && sgroup.getSize() > 0)
2051 av.setSelectionGroup(sgroup);
2055 av.setSelectionGroup(null);
2057 av.isSelectionGroupChanged(true);
2062 // the current selection is unset or from a previous message
2063 // so import the new colsel.
2064 if (colsel == null || colsel.isEmpty())
2066 if (av.getColumnSelection() != null)
2068 av.getColumnSelection().clear();
2074 // TODO: shift colSel according to the intersecting sequences
2075 if (av.getColumnSelection() == null)
2077 av.setColumnSelection(new ColumnSelection(colsel));
2081 av.getColumnSelection().setElementsFrom(colsel,
2082 av.getAlignment().getHiddenColumns());
2085 av.isColSelChanged(true);
2090 && av.hasHiddenColumns()
2091 && (av.getAlignment().getHiddenColumns() == null || av
2092 .getAlignment().getHiddenColumns().getHiddenRegions() == null))
2094 System.err.println("Bad things");
2096 if (repaint) // always true!
2098 // probably finessing with multiple redraws here
2099 PaintRefresher.Refresh(this, av.getSequenceSetId());
2100 // ap.paintAlignment(false);
2103 // lastly, update dependent dialogs
2104 if (ap.getCalculationDialog() != null)
2106 ap.getCalculationDialog().validateCalcTypes();
2112 * If this panel is a cdna/protein translation view of the selection source,
2113 * tries to map the source selection to a local one, and returns true. Else
2120 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2121 ColumnSelection colsel, HiddenColumns hidden,
2122 SelectionSource source)
2124 if (!(source instanceof AlignViewportI))
2128 final AlignViewportI sourceAv = (AlignViewportI) source;
2129 if (sourceAv.getCodingComplement() != av
2130 && av.getCodingComplement() != sourceAv)
2136 * Map sequence selection
2138 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2139 av.setSelectionGroup(sg);
2140 av.isSelectionGroupChanged(true);
2143 * Map column selection
2145 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2147 ColumnSelection cs = new ColumnSelection();
2148 HiddenColumns hs = new HiddenColumns();
2149 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
2150 av.setColumnSelection(cs);
2151 av.getAlignment().setHiddenColumns(hs);
2153 // lastly, update any dependent dialogs
2154 if (ap.getCalculationDialog() != null)
2156 ap.getCalculationDialog().validateCalcTypes();
2159 PaintRefresher.Refresh(this, av.getSequenceSetId());