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.SearchResultMatchI;
31 import jalview.datamodel.SearchResults;
32 import jalview.datamodel.SearchResultsI;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceGroup;
36 import jalview.datamodel.SequenceI;
37 import jalview.io.SequenceAnnotationReport;
38 import jalview.renderer.ResidueShaderI;
39 import jalview.schemes.ResidueProperties;
40 import jalview.structure.SelectionListener;
41 import jalview.structure.SelectionSource;
42 import jalview.structure.SequenceListener;
43 import jalview.structure.StructureSelectionManager;
44 import jalview.structure.VamsasSource;
45 import jalview.util.Comparison;
46 import jalview.util.MappingUtils;
47 import jalview.util.MessageManager;
48 import jalview.util.Platform;
49 import jalview.viewmodel.AlignmentViewport;
51 import java.awt.BorderLayout;
52 import java.awt.Color;
54 import java.awt.FontMetrics;
55 import java.awt.Point;
56 import java.awt.event.MouseEvent;
57 import java.awt.event.MouseListener;
58 import java.awt.event.MouseMotionListener;
59 import java.awt.event.MouseWheelEvent;
60 import java.awt.event.MouseWheelListener;
61 import java.util.ArrayList;
62 import java.util.Collections;
63 import java.util.List;
65 import javax.swing.JPanel;
66 import javax.swing.SwingUtilities;
67 import javax.swing.ToolTipManager;
73 * @version $Revision: 1.130 $
75 public class SeqPanel extends JPanel implements MouseListener,
76 MouseMotionListener, MouseWheelListener, SequenceListener,
81 public SeqCanvas seqCanvas;
84 public AlignmentPanel ap;
86 protected int lastres;
88 protected int startseq;
90 protected AlignViewport av;
92 ScrollThread scrollThread = null;
94 boolean mouseDragging = false;
96 boolean editingSeqs = false;
98 boolean groupEditing = false;
100 // ////////////////////////////////////////
101 // ///Everything below this is for defining the boundary of the rubberband
102 // ////////////////////////////////////////
105 boolean changeEndSeq = false;
107 boolean changeStartSeq = false;
109 boolean changeEndRes = false;
111 boolean changeStartRes = false;
113 SequenceGroup stretchGroup = null;
115 boolean remove = false;
117 Point lastMousePress;
119 boolean mouseWheelPressed = false;
121 StringBuffer keyboardNo1;
123 StringBuffer keyboardNo2;
125 java.net.URL linkImageURL;
127 private final SequenceAnnotationReport seqARep;
129 StringBuilder tooltipText = new StringBuilder();
133 EditCommand editCommand;
135 StructureSelectionManager ssm;
137 SearchResultsI lastSearchResults;
140 * Creates a new SeqPanel object.
147 public SeqPanel(AlignViewport av, AlignmentPanel ap)
149 linkImageURL = getClass().getResource("/images/link.gif");
150 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
151 ToolTipManager.sharedInstance().registerComponent(this);
152 ToolTipManager.sharedInstance().setInitialDelay(0);
153 ToolTipManager.sharedInstance().setDismissDelay(10000);
155 setBackground(Color.white);
157 seqCanvas = new SeqCanvas(ap);
158 setLayout(new BorderLayout());
159 add(seqCanvas, BorderLayout.CENTER);
165 addMouseMotionListener(this);
166 addMouseListener(this);
167 addMouseWheelListener(this);
168 ssm = av.getStructureSelectionManager();
169 ssm.addStructureViewerListener(this);
170 ssm.addSelectionListener(this);
174 int startWrapBlock = -1;
176 int wrappedBlock = -1;
179 * Returns the aligned sequence position (base 0) at the mouse position, or
180 * the closest visible one
185 int findRes(MouseEvent evt)
190 if (av.getWrapAlignment())
193 int hgap = av.getCharHeight();
194 if (av.getScaleAboveWrapped())
196 hgap += av.getCharHeight();
199 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
200 + hgap + seqCanvas.getAnnotationHeight();
204 x -= seqCanvas.LABEL_WEST;
206 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
212 wrappedBlock = y / cHeight;
213 wrappedBlock += av.getRanges().getStartRes() / cwidth;
215 res = wrappedBlock * cwidth + x / av.getCharWidth();
220 if (x > seqCanvas.getX() + seqCanvas.getWidth())
222 // make sure we calculate relative to visible alignment, rather than
224 x = seqCanvas.getX() + seqCanvas.getWidth();
226 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
227 if (res > av.getRanges().getEndRes())
230 res = av.getRanges().getEndRes();
234 if (av.hasHiddenColumns())
236 res = av.getColumnSelection().adjustForHiddenColumns(res);
243 int findSeq(MouseEvent evt)
248 if (av.getWrapAlignment())
250 int hgap = av.getCharHeight();
251 if (av.getScaleAboveWrapped())
253 hgap += av.getCharHeight();
256 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
257 + hgap + seqCanvas.getAnnotationHeight();
261 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
266 seq = Math.min((y / av.getCharHeight())
267 + av.getRanges().getStartSeq(),
269 .getAlignment().getHeight() - 1);
276 * When all of a sequence of edits are complete, put the resulting edit list
277 * on the history stack (undo list), and reset flags for editing in progress.
283 if (editCommand != null && editCommand.getSize() > 0)
285 ap.alignFrame.addHistoryItem(editCommand);
286 av.firePropertyChange("alignment", null, av.getAlignment()
292 * Tidy up come what may...
297 groupEditing = false;
306 seqCanvas.cursorY = getKeyboardNo1() - 1;
310 void setCursorColumn()
312 seqCanvas.cursorX = getKeyboardNo1() - 1;
316 void setCursorRowAndColumn()
318 if (keyboardNo2 == null)
320 keyboardNo2 = new StringBuffer();
324 seqCanvas.cursorX = getKeyboardNo1() - 1;
325 seqCanvas.cursorY = getKeyboardNo2() - 1;
330 void setCursorPosition()
332 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
334 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
338 void moveCursor(int dx, int dy)
340 seqCanvas.cursorX += dx;
341 seqCanvas.cursorY += dy;
342 if (av.hasHiddenColumns()
343 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
345 int original = seqCanvas.cursorX - dx;
346 int maxWidth = av.getAlignment().getWidth();
348 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
349 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
351 seqCanvas.cursorX += dx;
354 if (seqCanvas.cursorX >= maxWidth
355 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
357 seqCanvas.cursorX = original;
364 void scrollToVisible()
366 if (seqCanvas.cursorX < 0)
368 seqCanvas.cursorX = 0;
370 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
372 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
375 if (seqCanvas.cursorY < 0)
377 seqCanvas.cursorY = 0;
379 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
381 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
385 if (av.getWrapAlignment())
387 ap.scrollToWrappedVisible(seqCanvas.cursorX);
391 while (seqCanvas.cursorY < av.getRanges().getStartSeq())
395 while (seqCanvas.cursorY + 1 > av.getRanges().getEndSeq())
399 if (!av.getWrapAlignment())
401 while (seqCanvas.cursorX < av.getColumnSelection()
402 .adjustForHiddenColumns(av.getRanges().getStartRes()))
404 if (!ap.scrollRight(false))
409 while (seqCanvas.cursorX > av.getColumnSelection()
410 .adjustForHiddenColumns(av.getRanges().getEndRes()))
412 if (!ap.scrollRight(true))
419 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
420 seqCanvas.cursorX, seqCanvas.cursorY);
425 void setSelectionAreaAtCursor(boolean topLeft)
427 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
429 if (av.getSelectionGroup() != null)
431 SequenceGroup sg = av.getSelectionGroup();
432 // Find the top and bottom of this group
433 int min = av.getAlignment().getHeight(), max = 0;
434 for (int i = 0; i < sg.getSize(); i++)
436 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
451 sg.setStartRes(seqCanvas.cursorX);
452 if (sg.getEndRes() < seqCanvas.cursorX)
454 sg.setEndRes(seqCanvas.cursorX);
457 min = seqCanvas.cursorY;
461 sg.setEndRes(seqCanvas.cursorX);
462 if (sg.getStartRes() > seqCanvas.cursorX)
464 sg.setStartRes(seqCanvas.cursorX);
467 max = seqCanvas.cursorY + 1;
472 // Only the user can do this
473 av.setSelectionGroup(null);
477 // Now add any sequences between min and max
478 sg.getSequences(null).clear();
479 for (int i = min; i < max; i++)
481 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
486 if (av.getSelectionGroup() == null)
488 SequenceGroup sg = new SequenceGroup();
489 sg.setStartRes(seqCanvas.cursorX);
490 sg.setEndRes(seqCanvas.cursorX);
491 sg.addSequence(sequence, false);
492 av.setSelectionGroup(sg);
495 ap.paintAlignment(false);
499 void insertGapAtCursor(boolean group)
501 groupEditing = group;
502 startseq = seqCanvas.cursorY;
503 lastres = seqCanvas.cursorX;
504 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
508 void deleteGapAtCursor(boolean group)
510 groupEditing = group;
511 startseq = seqCanvas.cursorY;
512 lastres = seqCanvas.cursorX + getKeyboardNo1();
513 editSequence(false, false, seqCanvas.cursorX);
517 void insertNucAtCursor(boolean group, String nuc)
519 // TODO not called - delete?
520 groupEditing = group;
521 startseq = seqCanvas.cursorY;
522 lastres = seqCanvas.cursorX;
523 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
527 void numberPressed(char value)
529 if (keyboardNo1 == null)
531 keyboardNo1 = new StringBuffer();
534 if (keyboardNo2 != null)
536 keyboardNo2.append(value);
540 keyboardNo1.append(value);
548 if (keyboardNo1 != null)
550 int value = Integer.parseInt(keyboardNo1.toString());
554 } catch (Exception x)
565 if (keyboardNo2 != null)
567 int value = Integer.parseInt(keyboardNo2.toString());
571 } catch (Exception x)
585 public void mouseReleased(MouseEvent evt)
587 mouseDragging = false;
588 mouseWheelPressed = false;
590 if (evt.isPopupTrigger()) // Windows: mouseReleased
599 doMouseReleasedDefineMode(evt);
613 public void mousePressed(MouseEvent evt)
615 lastMousePress = evt.getPoint();
617 if (SwingUtilities.isMiddleMouseButton(evt))
619 mouseWheelPressed = true;
623 boolean isControlDown = Platform.isControlDown(evt);
624 if (evt.isShiftDown() || isControlDown)
634 doMousePressedDefineMode(evt);
638 int seq = findSeq(evt);
639 int res = findRes(evt);
641 if (seq < 0 || res < 0)
646 if ((seq < av.getAlignment().getHeight())
647 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
664 public void mouseOverSequence(SequenceI sequence, int index, int pos)
666 String tmp = sequence.hashCode() + " " + index + " " + pos;
668 if (lastMessage == null || !lastMessage.equals(tmp))
670 // System.err.println("mouseOver Sequence: "+tmp);
671 ssm.mouseOverSequence(sequence, index, pos, av);
677 * Highlight the mapped region described by the search results object (unless
678 * unchanged). This supports highlight of protein while mousing over linked
679 * cDNA and vice versa. The status bar is also updated to show the location of
680 * the start of the highlighted region.
683 public void highlightSequence(SearchResultsI results)
685 if (results == null || results.equals(lastSearchResults))
689 lastSearchResults = results;
691 if (av.isFollowHighlight())
694 * if scrollToPosition requires a scroll adjustment, this flag prevents
695 * another scroll event being propagated back to the originator
697 * @see AlignmentPanel#adjustmentValueChanged
699 ap.setDontScrollComplement(true);
700 if (ap.scrollToPosition(results, false))
702 seqCanvas.revalidate();
705 setStatusMessage(results);
706 seqCanvas.highlightSearchResults(results);
710 public VamsasSource getVamsasSource()
712 return this.ap == null ? null : this.ap.av;
716 public void updateColours(SequenceI seq, int index)
718 System.out.println("update the seqPanel colours");
729 public void mouseMoved(MouseEvent evt)
733 // This is because MacOSX creates a mouseMoved
734 // If control is down, other platforms will not.
738 int res = findRes(evt);
739 int seq = findSeq(evt);
741 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
746 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
748 if (res >= sequence.getLength())
753 pos = setStatusMessage(sequence, res, seq);
754 if (ssm != null && pos > -1)
756 mouseOverSequence(sequence, res, pos);
759 tooltipText.setLength(6); // Cuts the buffer back to <html>
761 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
764 for (int g = 0; g < groups.length; g++)
766 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
768 if (!groups[g].getName().startsWith("JTreeGroup")
769 && !groups[g].getName().startsWith("JGroup"))
771 tooltipText.append(groups[g].getName());
774 if (groups[g].getDescription() != null)
776 tooltipText.append(": " + groups[g].getDescription());
782 // use aa to see if the mouse pointer is on a
783 if (av.isShowSequenceFeatures())
786 List<SequenceFeature> features = ap.getFeatureRenderer()
787 .findFeaturesAtRes(sequence.getDatasetSequence(),
788 rpos = sequence.findPosition(res));
789 seqARep.appendFeatures(tooltipText, rpos, features,
790 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
792 if (tooltipText.length() == 6) // <html>
794 setToolTipText(null);
799 if (lastTooltip == null
800 || !lastTooltip.equals(tooltipText.toString()))
802 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
803 tooltipText.toString());
804 // String formatedTooltipText = tooltipText.toString();
805 setToolTipText(formatedTooltipText);
806 lastTooltip = tooltipText.toString();
813 private Point lastp = null;
818 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
821 public Point getToolTipLocation(MouseEvent event)
823 int x = event.getX(), w = getWidth();
824 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
827 if (!event.isShiftDown() || p == null)
829 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
830 event.getX() + wdth, event.getY() - 20) : null;
833 * TODO: try to modify position region is not obcured by tooltip
841 * set when the current UI interaction has resulted in a change that requires
842 * overview shading to be recalculated. this could be changed to something
843 * more expressive that indicates what actually has changed, so selective
844 * redraws can be applied
846 private boolean needOverviewUpdate = false; // TODO: refactor to avcontroller
849 * set if av.getSelectionGroup() refers to a group that is defined on the
850 * alignment view, rather than a transient selection
852 // private boolean editingDefinedGroup = false; // TODO: refactor to
853 // avcontroller or viewModel
856 * Set status message in alignment panel
859 * aligned sequence object
863 * index of sequence in alignment
864 * @return position of res in sequence
866 int setStatusMessage(SequenceI sequence, int res, int seq)
868 StringBuilder text = new StringBuilder(32);
871 * Sequence number (if known), and sequence name.
873 String seqno = seq == -1 ? "" : " " + (seq + 1);
874 text.append("Sequence").append(seqno).append(" ID: ")
875 .append(sequence.getName());
877 String residue = null;
879 * Try to translate the display character to residue name (null for gap).
881 final String displayChar = String.valueOf(sequence.getCharAt(res));
882 if (av.getAlignment().isNucleotide())
884 residue = ResidueProperties.nucleotideName.get(displayChar);
887 text.append(" Nucleotide: ").append(residue);
892 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
893 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
897 text.append(" Residue: ").append(residue);
904 pos = sequence.findPosition(res);
905 text.append(" (").append(Integer.toString(pos)).append(")");
907 ap.alignFrame.statusBar.setText(text.toString());
912 * Set the status bar message to highlight the first matched position in
917 private void setStatusMessage(SearchResultsI results)
919 AlignmentI al = this.av.getAlignment();
920 int sequenceIndex = al.findIndex(results);
921 if (sequenceIndex == -1)
925 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
926 for (SearchResultMatchI m : results.getResults())
928 SequenceI seq = m.getSequence();
929 if (seq.getDatasetSequence() != null)
931 seq = seq.getDatasetSequence();
937 * Convert position in sequence (base 1) to sequence character array
940 int start = m.getStart() - m.getSequence().getStart();
941 setStatusMessage(seq, start, sequenceIndex);
951 public void mouseDragged(MouseEvent evt)
953 if (mouseWheelPressed)
955 boolean inSplitFrame = ap.av.getCodingComplement() != null;
956 boolean copyChanges = inSplitFrame && av.isProteinFontAsCdna();
958 int oldWidth = av.getCharWidth();
960 // Which is bigger, left-right or up-down?
961 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
962 .getX() - lastMousePress.getX()))
965 * on drag up or down, decrement or increment font size
967 int fontSize = av.font.getSize();
968 boolean fontChanged = false;
970 if (evt.getY() < lastMousePress.getY())
975 else if (evt.getY() > lastMousePress.getY())
988 Font newFont = new Font(av.font.getName(), av.font.getStyle(),
990 av.setFont(newFont, true);
991 av.setCharWidth(oldWidth);
995 ap.av.getCodingComplement().setFont(newFont, true);
996 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
997 .getSplitViewContainer();
998 splitFrame.adjustLayout();
999 splitFrame.repaint();
1006 * on drag left or right, decrement or increment character width
1009 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
1011 newWidth = av.getCharWidth() - 1;
1012 av.setCharWidth(newWidth);
1014 else if (evt.getX() > lastMousePress.getX())
1016 newWidth = av.getCharWidth() + 1;
1017 av.setCharWidth(newWidth);
1021 ap.paintAlignment(false);
1025 * need to ensure newWidth is set on cdna, regardless of which
1026 * panel the mouse drag happened in; protein will compute its
1027 * character width as 1:1 or 3:1
1029 av.getCodingComplement().setCharWidth(newWidth);
1030 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
1031 .getSplitViewContainer();
1032 splitFrame.adjustLayout();
1033 splitFrame.repaint();
1038 FontMetrics fm = getFontMetrics(av.getFont());
1039 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
1041 lastMousePress = evt.getPoint();
1048 doMouseDraggedDefineMode(evt);
1052 int res = findRes(evt);
1059 if ((lastres == -1) || (lastres == res))
1064 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1066 // dragLeft, delete gap
1067 editSequence(false, false, res);
1071 editSequence(true, false, res);
1074 mouseDragging = true;
1075 if (scrollThread != null)
1077 scrollThread.setEvent(evt);
1081 // TODO: Make it more clever than many booleans
1082 synchronized void editSequence(boolean insertGap, boolean editSeq,
1086 int fixedRight = -1;
1087 boolean fixedColumns = false;
1088 SequenceGroup sg = av.getSelectionGroup();
1090 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1092 // No group, but the sequence may represent a group
1093 if (!groupEditing && av.hasHiddenRows())
1095 if (av.isHiddenRepSequence(seq))
1097 sg = av.getRepresentedSequences(seq);
1098 groupEditing = true;
1102 StringBuilder message = new StringBuilder(64);
1105 message.append("Edit group:");
1106 if (editCommand == null)
1108 editCommand = new EditCommand(
1109 MessageManager.getString("action.edit_group"));
1114 message.append("Edit sequence: " + seq.getName());
1115 String label = seq.getName();
1116 if (label.length() > 10)
1118 label = label.substring(0, 10);
1120 if (editCommand == null)
1122 editCommand = new EditCommand(MessageManager.formatMessage(
1123 "label.edit_params", new String[] { label }));
1129 message.append(" insert ");
1133 message.append(" delete ");
1136 message.append(Math.abs(startres - lastres) + " gaps.");
1137 ap.alignFrame.statusBar.setText(message.toString());
1139 // Are we editing within a selection group?
1141 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1144 fixedColumns = true;
1146 // sg might be null as the user may only see 1 sequence,
1147 // but the sequence represents a group
1150 if (!av.isHiddenRepSequence(seq))
1155 sg = av.getRepresentedSequences(seq);
1158 fixedLeft = sg.getStartRes();
1159 fixedRight = sg.getEndRes();
1161 if ((startres < fixedLeft && lastres >= fixedLeft)
1162 || (startres >= fixedLeft && lastres < fixedLeft)
1163 || (startres > fixedRight && lastres <= fixedRight)
1164 || (startres <= fixedRight && lastres > fixedRight))
1170 if (fixedLeft > startres)
1172 fixedRight = fixedLeft - 1;
1175 else if (fixedRight < startres)
1177 fixedLeft = fixedRight;
1182 if (av.hasHiddenColumns())
1184 fixedColumns = true;
1185 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1186 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1188 if ((insertGap && startres > y1 && lastres < y1)
1189 || (!insertGap && startres < y2 && lastres > y2))
1195 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1196 // Selection spans a hidden region
1197 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1205 fixedRight = y2 - 1;
1212 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1213 int g, groupSize = vseqs.size();
1214 SequenceI[] groupSeqs = new SequenceI[groupSize];
1215 for (g = 0; g < groupSeqs.length; g++)
1217 groupSeqs[g] = vseqs.get(g);
1223 // If the user has selected the whole sequence, and is dragging to
1224 // the right, we can still extend the alignment and selectionGroup
1225 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1226 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1228 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1229 fixedRight = sg.getEndRes();
1232 // Is it valid with fixed columns??
1233 // Find the next gap before the end
1234 // of the visible region boundary
1235 boolean blank = false;
1236 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1240 for (g = 0; g < groupSize; g++)
1242 for (int j = 0; j < startres - lastres; j++)
1244 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1259 if (sg.getSize() == av.getAlignment().getHeight())
1261 if ((av.hasHiddenColumns() && startres < av
1262 .getColumnSelection().getHiddenBoundaryRight(startres)))
1268 int alWidth = av.getAlignment().getWidth();
1269 if (av.hasHiddenRows())
1271 int hwidth = av.getAlignment().getHiddenSequences()
1273 if (hwidth > alWidth)
1278 // We can still insert gaps if the selectionGroup
1279 // contains all the sequences
1280 sg.setEndRes(sg.getEndRes() + startres - lastres);
1281 fixedRight = alWidth + startres - lastres;
1292 else if (!insertGap)
1294 // / Are we able to delete?
1295 // ie are all columns blank?
1297 for (g = 0; g < groupSize; g++)
1299 for (int j = startres; j < lastres; j++)
1301 if (groupSeqs[g].getLength() <= j)
1306 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1308 // Not a gap, block edit not valid
1318 // dragging to the right
1319 if (fixedColumns && fixedRight != -1)
1321 for (int j = lastres; j < startres; j++)
1323 insertChar(j, groupSeqs, fixedRight);
1328 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1334 // dragging to the left
1335 if (fixedColumns && fixedRight != -1)
1337 for (int j = lastres; j > startres; j--)
1339 deleteChar(startres, groupSeqs, fixedRight);
1344 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1351 // ///Editing a single sequence///////////
1355 // dragging to the right
1356 if (fixedColumns && fixedRight != -1)
1358 for (int j = lastres; j < startres; j++)
1360 insertChar(j, new SequenceI[] { seq }, fixedRight);
1365 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1366 startres - lastres);
1373 // dragging to the left
1374 if (fixedColumns && fixedRight != -1)
1376 for (int j = lastres; j > startres; j--)
1378 if (!Comparison.isGap(seq.getCharAt(startres)))
1383 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1388 // could be a keyboard edit trying to delete none gaps
1390 for (int m = startres; m < lastres; m++)
1392 if (!Comparison.isGap(seq.getCharAt(m)))
1401 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1407 {// insertGap==false AND editSeq==TRUE;
1408 if (fixedColumns && fixedRight != -1)
1410 for (int j = lastres; j < startres; j++)
1412 insertChar(j, new SequenceI[] { seq }, fixedRight);
1417 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1418 startres - lastres);
1425 seqCanvas.repaint();
1428 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1430 int blankColumn = fixedColumn;
1431 for (int s = 0; s < seq.length; s++)
1433 // Find the next gap before the end of the visible region boundary
1434 // If lastCol > j, theres a boundary after the gap insertion
1436 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1438 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1440 // Theres a space, so break and insert the gap
1445 if (blankColumn <= j)
1447 blankColumn = fixedColumn;
1453 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1455 appendEdit(Action.INSERT_GAP, seq, j, 1);
1460 * Helper method to add and perform one edit action.
1467 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1471 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1472 av.getAlignment().getGapCharacter());
1474 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1477 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1480 appendEdit(Action.DELETE_GAP, seq, j, 1);
1482 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1492 public void mouseEntered(MouseEvent e)
1499 if (scrollThread != null)
1501 scrollThread.running = false;
1502 scrollThread = null;
1513 public void mouseExited(MouseEvent e)
1515 if (av.getWrapAlignment())
1522 scrollThread = new ScrollThread();
1527 * Handler for double-click on a position with one or more sequence features.
1528 * Opens the Amend Features dialog to allow feature details to be amended, or
1529 * the feature deleted.
1532 public void mouseClicked(MouseEvent evt)
1534 SequenceGroup sg = null;
1535 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1536 if (evt.getClickCount() > 1)
1538 sg = av.getSelectionGroup();
1539 if (sg != null && sg.getSize() == 1
1540 && sg.getEndRes() - sg.getStartRes() < 2)
1542 av.setSelectionGroup(null);
1545 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1546 .findFeaturesAtRes(sequence.getDatasetSequence(),
1547 sequence.findPosition(findRes(evt)));
1549 if (!features.isEmpty())
1552 * highlight the first feature at the position on the alignment
1554 SearchResultsI highlight = new SearchResults();
1555 highlight.addResult(sequence, features.get(0).getBegin(), features
1557 seqCanvas.highlightSearchResults(highlight);
1560 * open the Amend Features dialog; clear highlighting afterwards,
1561 * whether changes were made or not
1563 List<SequenceI> seqs = Collections.singletonList(sequence);
1564 seqCanvas.getFeatureRenderer().amendFeatures(seqs, features, false,
1566 seqCanvas.highlightSearchResults(null);
1572 public void mouseWheelMoved(MouseWheelEvent e)
1575 if (e.getWheelRotation() > 0)
1577 if (e.isShiftDown())
1579 ap.scrollRight(true);
1589 if (e.isShiftDown())
1591 ap.scrollRight(false);
1598 // TODO Update tooltip for new position.
1607 public void doMousePressedDefineMode(MouseEvent evt)
1609 final int res = findRes(evt);
1610 final int seq = findSeq(evt);
1612 needOverviewUpdate = false;
1614 startWrapBlock = wrappedBlock;
1616 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1618 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1619 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1620 MessageManager.getString("label.wrapped_view_no_edit"),
1621 JvOptionPane.WARNING_MESSAGE);
1625 if (seq < 0 || res < 0)
1630 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1632 if ((sequence == null) || (res > sequence.getLength()))
1637 stretchGroup = av.getSelectionGroup();
1639 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1641 stretchGroup = av.getAlignment().findGroup(sequence, res);
1642 if (stretchGroup != null)
1644 // only update the current selection if the popup menu has a group to
1646 av.setSelectionGroup(stretchGroup);
1650 if (evt.isPopupTrigger()) // Mac: mousePressed
1657 * defer right-mouse click handling to mouseReleased on Windows
1658 * (where isPopupTrigger() will answer true)
1659 * NB isRightMouseButton is also true for Cmd-click on Mac
1661 if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
1668 seqCanvas.cursorX = findRes(evt);
1669 seqCanvas.cursorY = findSeq(evt);
1670 seqCanvas.repaint();
1674 if (stretchGroup == null)
1676 // Only if left mouse button do we want to change group sizes
1678 // define a new group here
1679 SequenceGroup sg = new SequenceGroup();
1680 sg.setStartRes(res);
1682 sg.addSequence(sequence, false);
1683 av.setSelectionGroup(sg);
1686 if (av.getConservationSelected())
1688 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1692 if (av.getAbovePIDThreshold())
1694 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1697 // TODO: stretchGroup will always be not null. Is this a merge error ?
1698 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1700 // Edit end res position of selected group
1701 changeEndRes = true;
1703 else if ((stretchGroup != null)
1704 && (stretchGroup.getStartRes() == res))
1706 // Edit end res position of selected group
1707 changeStartRes = true;
1709 stretchGroup.getWidth();
1712 seqCanvas.repaint();
1716 * Build and show a pop-up menu at the right-click mouse position
1722 void showPopupMenu(MouseEvent evt)
1724 final int res = findRes(evt);
1725 final int seq = findSeq(evt);
1726 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1727 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1728 .findFeaturesAtRes(sequence.getDatasetSequence(),
1729 sequence.findPosition(res));
1730 List<String> links = new ArrayList<String>();
1731 for (SequenceFeature sf : allFeatures)
1733 if (sf.links != null)
1735 for (String link : sf.links)
1742 PopupMenu pop = new PopupMenu(ap, null, links);
1743 pop.show(this, evt.getX(), evt.getY());
1752 public void doMouseReleasedDefineMode(MouseEvent evt)
1754 if (stretchGroup == null)
1758 // always do this - annotation has own state
1759 // but defer colourscheme update until hidden sequences are passed in
1760 boolean vischange = stretchGroup.recalcConservation(true);
1761 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1762 if (stretchGroup.cs != null)
1764 stretchGroup.cs.alignmentChanged(stretchGroup,
1765 av.getHiddenRepSequences());
1767 ResidueShaderI groupColourScheme = stretchGroup.getGroupColourScheme();
1768 String name = stretchGroup.getName();
1769 if (stretchGroup.cs.conservationApplied())
1771 SliderPanel.setConservationSlider(ap, groupColourScheme, name);
1773 if (stretchGroup.cs.getThreshold() > 0)
1775 SliderPanel.setPIDSliderSource(ap, groupColourScheme, name);
1778 PaintRefresher.Refresh(this, av.getSequenceSetId());
1779 ap.paintAlignment(needOverviewUpdate);
1780 needOverviewUpdate = false;
1781 changeEndRes = false;
1782 changeStartRes = false;
1783 stretchGroup = null;
1793 public void doMouseDraggedDefineMode(MouseEvent evt)
1795 int res = findRes(evt);
1796 int y = findSeq(evt);
1798 if (wrappedBlock != startWrapBlock)
1803 if (stretchGroup == null)
1808 if (res >= av.getAlignment().getWidth())
1810 res = av.getAlignment().getWidth() - 1;
1813 if (stretchGroup.getEndRes() == res)
1815 // Edit end res position of selected group
1816 changeEndRes = true;
1818 else if (stretchGroup.getStartRes() == res)
1820 // Edit start res position of selected group
1821 changeStartRes = true;
1824 if (res < av.getRanges().getStartRes())
1826 res = av.getRanges().getStartRes();
1831 if (res > (stretchGroup.getStartRes() - 1))
1833 stretchGroup.setEndRes(res);
1834 needOverviewUpdate |= av.isSelectionDefinedGroup();
1837 else if (changeStartRes)
1839 if (res < (stretchGroup.getEndRes() + 1))
1841 stretchGroup.setStartRes(res);
1842 needOverviewUpdate |= av.isSelectionDefinedGroup();
1846 int dragDirection = 0;
1852 else if (y < oldSeq)
1857 while ((y != oldSeq) && (oldSeq > -1)
1858 && (y < av.getAlignment().getHeight()))
1860 // This routine ensures we don't skip any sequences, as the
1861 // selection is quite slow.
1862 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1864 oldSeq += dragDirection;
1871 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1873 if (stretchGroup.getSequences(null).contains(nextSeq))
1875 stretchGroup.deleteSequence(seq, false);
1876 needOverviewUpdate |= av.isSelectionDefinedGroup();
1882 stretchGroup.addSequence(seq, false);
1885 stretchGroup.addSequence(nextSeq, false);
1886 needOverviewUpdate |= av.isSelectionDefinedGroup();
1895 mouseDragging = true;
1897 if (scrollThread != null)
1899 scrollThread.setEvent(evt);
1902 seqCanvas.repaint();
1905 void scrollCanvas(MouseEvent evt)
1909 if (scrollThread != null)
1911 scrollThread.running = false;
1912 scrollThread = null;
1914 mouseDragging = false;
1918 if (scrollThread == null)
1920 scrollThread = new ScrollThread();
1923 mouseDragging = true;
1924 scrollThread.setEvent(evt);
1929 // this class allows scrolling off the bottom of the visible alignment
1930 class ScrollThread extends Thread
1934 boolean running = false;
1936 public ScrollThread()
1941 public void setEvent(MouseEvent e)
1946 public void stopScrolling()
1960 if (mouseDragging && (evt.getY() < 0)
1961 && (av.getRanges().getStartSeq() > 0))
1963 running = ap.scrollUp(true);
1966 if (mouseDragging && (evt.getY() >= getHeight())
1967 && (av.getAlignment().getHeight() > av.getRanges()
1970 running = ap.scrollUp(false);
1973 if (mouseDragging && (evt.getX() < 0))
1975 running = ap.scrollRight(false);
1977 else if (mouseDragging && (evt.getX() >= getWidth()))
1979 running = ap.scrollRight(true);
1986 } catch (Exception ex)
1994 * modify current selection according to a received message.
1997 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1998 SelectionSource source)
2000 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
2001 // handles selection messages...
2002 // TODO: extend config options to allow user to control if selections may be
2003 // shared between viewports.
2004 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
2005 .getSequenceSetId().equals(av.getSequenceSetId())));
2007 if (iSentTheSelection)
2009 // respond to our own event by updating dependent dialogs
2010 if (ap.getCalculationDialog() != null)
2012 ap.getCalculationDialog().validateCalcTypes();
2015 // process further ?
2016 if (!av.followSelection)
2023 * Ignore the selection if there is one of our own pending.
2025 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
2031 * Check for selection in a view of which this one is a dna/protein
2034 if (selectionFromTranslation(seqsel, colsel, source))
2039 // do we want to thread this ? (contention with seqsel and colsel locks, I
2042 * only copy colsel if there is a real intersection between
2043 * sequence selection and this panel's alignment
2045 boolean repaint = false;
2046 boolean copycolsel = false;
2048 SequenceGroup sgroup = null;
2049 if (seqsel != null && seqsel.getSize() > 0)
2051 if (av.getAlignment() == null)
2053 Cache.log.warn("alignviewport av SeqSetId=" + av.getSequenceSetId()
2054 + " ViewId=" + av.getViewId()
2055 + " 's alignment is NULL! returning immediately.");
2058 sgroup = seqsel.intersect(av.getAlignment(),
2059 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
2060 if ((sgroup != null && sgroup.getSize() > 0))
2065 if (sgroup != null && sgroup.getSize() > 0)
2067 av.setSelectionGroup(sgroup);
2071 av.setSelectionGroup(null);
2073 av.isSelectionGroupChanged(true);
2078 // the current selection is unset or from a previous message
2079 // so import the new colsel.
2080 if (colsel == null || colsel.isEmpty())
2082 if (av.getColumnSelection() != null)
2084 av.getColumnSelection().clear();
2090 // TODO: shift colSel according to the intersecting sequences
2091 if (av.getColumnSelection() == null)
2093 av.setColumnSelection(new ColumnSelection(colsel));
2097 av.getColumnSelection().setElementsFrom(colsel);
2100 av.isColSelChanged(true);
2105 && av.hasHiddenColumns()
2106 && (av.getColumnSelection() == null || av.getColumnSelection()
2107 .getHiddenColumns() == null))
2109 System.err.println("Bad things");
2111 if (repaint) // always true!
2113 // probably finessing with multiple redraws here
2114 PaintRefresher.Refresh(this, av.getSequenceSetId());
2115 // ap.paintAlignment(false);
2118 // lastly, update dependent dialogs
2119 if (ap.getCalculationDialog() != null)
2121 ap.getCalculationDialog().validateCalcTypes();
2127 * If this panel is a cdna/protein translation view of the selection source,
2128 * tries to map the source selection to a local one, and returns true. Else
2135 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2136 ColumnSelection colsel, SelectionSource source)
2138 if (!(source instanceof AlignViewportI))
2142 final AlignViewportI sourceAv = (AlignViewportI) source;
2143 if (sourceAv.getCodingComplement() != av
2144 && av.getCodingComplement() != sourceAv)
2150 * Map sequence selection
2152 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2153 av.setSelectionGroup(sg);
2154 av.isSelectionGroupChanged(true);
2157 * Map column selection
2159 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2161 av.setColumnSelection(cs);
2163 // lastly, update any dependent dialogs
2164 if (ap.getCalculationDialog() != null)
2166 ap.getCalculationDialog().validateCalcTypes();
2169 PaintRefresher.Refresh(this, av.getSequenceSetId());