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.List;
64 import javax.swing.JPanel;
65 import javax.swing.SwingUtilities;
66 import javax.swing.ToolTipManager;
72 * @version $Revision: 1.130 $
74 public class SeqPanel extends JPanel implements MouseListener,
75 MouseMotionListener, MouseWheelListener, SequenceListener,
80 public SeqCanvas seqCanvas;
83 public AlignmentPanel ap;
85 protected int lastres;
87 protected int startseq;
89 protected AlignViewport av;
91 ScrollThread scrollThread = null;
93 boolean mouseDragging = false;
95 boolean editingSeqs = false;
97 boolean groupEditing = false;
99 // ////////////////////////////////////////
100 // ///Everything below this is for defining the boundary of the rubberband
101 // ////////////////////////////////////////
104 boolean changeEndSeq = false;
106 boolean changeStartSeq = false;
108 boolean changeEndRes = false;
110 boolean changeStartRes = false;
112 SequenceGroup stretchGroup = null;
114 boolean remove = false;
116 Point lastMousePress;
118 boolean mouseWheelPressed = false;
120 StringBuffer keyboardNo1;
122 StringBuffer keyboardNo2;
124 java.net.URL linkImageURL;
126 private final SequenceAnnotationReport seqARep;
128 StringBuilder tooltipText = new StringBuilder();
132 EditCommand editCommand;
134 StructureSelectionManager ssm;
136 SearchResultsI lastSearchResults;
139 * Creates a new SeqPanel object.
146 public SeqPanel(AlignViewport av, AlignmentPanel ap)
148 linkImageURL = getClass().getResource("/images/link.gif");
149 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
150 ToolTipManager.sharedInstance().registerComponent(this);
151 ToolTipManager.sharedInstance().setInitialDelay(0);
152 ToolTipManager.sharedInstance().setDismissDelay(10000);
154 setBackground(Color.white);
156 seqCanvas = new SeqCanvas(ap);
157 setLayout(new BorderLayout());
158 add(seqCanvas, BorderLayout.CENTER);
164 addMouseMotionListener(this);
165 addMouseListener(this);
166 addMouseWheelListener(this);
167 ssm = av.getStructureSelectionManager();
168 ssm.addStructureViewerListener(this);
169 ssm.addSelectionListener(this);
173 int startWrapBlock = -1;
175 int wrappedBlock = -1;
178 * Returns the aligned sequence position (base 0) at the mouse position, or
179 * the closest visible one
184 int findRes(MouseEvent evt)
189 if (av.getWrapAlignment())
192 int hgap = av.getCharHeight();
193 if (av.getScaleAboveWrapped())
195 hgap += av.getCharHeight();
198 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
199 + hgap + seqCanvas.getAnnotationHeight();
203 x -= seqCanvas.LABEL_WEST;
205 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
211 wrappedBlock = y / cHeight;
212 wrappedBlock += av.getRanges().getStartRes() / cwidth;
214 res = wrappedBlock * cwidth + x / av.getCharWidth();
219 if (x > seqCanvas.getX() + seqCanvas.getWidth())
221 // make sure we calculate relative to visible alignment, rather than
223 x = seqCanvas.getX() + seqCanvas.getWidth();
225 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
226 if (res > av.getRanges().getEndRes())
229 res = av.getRanges().getEndRes();
233 if (av.hasHiddenColumns())
235 res = av.getColumnSelection().adjustForHiddenColumns(res);
242 int findSeq(MouseEvent evt)
247 if (av.getWrapAlignment())
249 int hgap = av.getCharHeight();
250 if (av.getScaleAboveWrapped())
252 hgap += av.getCharHeight();
255 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
256 + hgap + seqCanvas.getAnnotationHeight();
260 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
265 seq = Math.min((y / av.getCharHeight())
266 + av.getRanges().getStartSeq(),
268 .getAlignment().getHeight() - 1);
275 * When all of a sequence of edits are complete, put the resulting edit list
276 * on the history stack (undo list), and reset flags for editing in progress.
282 if (editCommand != null && editCommand.getSize() > 0)
284 ap.alignFrame.addHistoryItem(editCommand);
285 av.firePropertyChange("alignment", null, av.getAlignment()
291 * Tidy up come what may...
296 groupEditing = false;
305 seqCanvas.cursorY = getKeyboardNo1() - 1;
309 void setCursorColumn()
311 seqCanvas.cursorX = getKeyboardNo1() - 1;
315 void setCursorRowAndColumn()
317 if (keyboardNo2 == null)
319 keyboardNo2 = new StringBuffer();
323 seqCanvas.cursorX = getKeyboardNo1() - 1;
324 seqCanvas.cursorY = getKeyboardNo2() - 1;
329 void setCursorPosition()
331 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
333 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
337 void moveCursor(int dx, int dy)
339 seqCanvas.cursorX += dx;
340 seqCanvas.cursorY += dy;
341 if (av.hasHiddenColumns()
342 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
344 int original = seqCanvas.cursorX - dx;
345 int maxWidth = av.getAlignment().getWidth();
347 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
348 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
350 seqCanvas.cursorX += dx;
353 if (seqCanvas.cursorX >= maxWidth
354 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
356 seqCanvas.cursorX = original;
363 void scrollToVisible()
365 if (seqCanvas.cursorX < 0)
367 seqCanvas.cursorX = 0;
369 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
371 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
374 if (seqCanvas.cursorY < 0)
376 seqCanvas.cursorY = 0;
378 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
380 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
384 if (av.getWrapAlignment())
386 av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX);
390 av.getRanges().scrollToVisible(seqCanvas.cursorX, seqCanvas.cursorY,
393 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
394 seqCanvas.cursorX, seqCanvas.cursorY);
399 void setSelectionAreaAtCursor(boolean topLeft)
401 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
403 if (av.getSelectionGroup() != null)
405 SequenceGroup sg = av.getSelectionGroup();
406 // Find the top and bottom of this group
407 int min = av.getAlignment().getHeight(), max = 0;
408 for (int i = 0; i < sg.getSize(); i++)
410 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
425 sg.setStartRes(seqCanvas.cursorX);
426 if (sg.getEndRes() < seqCanvas.cursorX)
428 sg.setEndRes(seqCanvas.cursorX);
431 min = seqCanvas.cursorY;
435 sg.setEndRes(seqCanvas.cursorX);
436 if (sg.getStartRes() > seqCanvas.cursorX)
438 sg.setStartRes(seqCanvas.cursorX);
441 max = seqCanvas.cursorY + 1;
446 // Only the user can do this
447 av.setSelectionGroup(null);
451 // Now add any sequences between min and max
452 sg.getSequences(null).clear();
453 for (int i = min; i < max; i++)
455 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
460 if (av.getSelectionGroup() == null)
462 SequenceGroup sg = new SequenceGroup();
463 sg.setStartRes(seqCanvas.cursorX);
464 sg.setEndRes(seqCanvas.cursorX);
465 sg.addSequence(sequence, false);
466 av.setSelectionGroup(sg);
469 ap.paintAlignment(false);
473 void insertGapAtCursor(boolean group)
475 groupEditing = group;
476 startseq = seqCanvas.cursorY;
477 lastres = seqCanvas.cursorX;
478 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
482 void deleteGapAtCursor(boolean group)
484 groupEditing = group;
485 startseq = seqCanvas.cursorY;
486 lastres = seqCanvas.cursorX + getKeyboardNo1();
487 editSequence(false, false, seqCanvas.cursorX);
491 void insertNucAtCursor(boolean group, String nuc)
493 // TODO not called - delete?
494 groupEditing = group;
495 startseq = seqCanvas.cursorY;
496 lastres = seqCanvas.cursorX;
497 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
501 void numberPressed(char value)
503 if (keyboardNo1 == null)
505 keyboardNo1 = new StringBuffer();
508 if (keyboardNo2 != null)
510 keyboardNo2.append(value);
514 keyboardNo1.append(value);
522 if (keyboardNo1 != null)
524 int value = Integer.parseInt(keyboardNo1.toString());
528 } catch (Exception x)
539 if (keyboardNo2 != null)
541 int value = Integer.parseInt(keyboardNo2.toString());
545 } catch (Exception x)
559 public void mouseReleased(MouseEvent evt)
561 mouseDragging = false;
562 mouseWheelPressed = false;
564 if (evt.isPopupTrigger()) // Windows: mouseReleased
573 doMouseReleasedDefineMode(evt);
587 public void mousePressed(MouseEvent evt)
589 lastMousePress = evt.getPoint();
591 if (SwingUtilities.isMiddleMouseButton(evt))
593 mouseWheelPressed = true;
597 boolean isControlDown = Platform.isControlDown(evt);
598 if (evt.isShiftDown() || isControlDown)
608 doMousePressedDefineMode(evt);
612 int seq = findSeq(evt);
613 int res = findRes(evt);
615 if (seq < 0 || res < 0)
620 if ((seq < av.getAlignment().getHeight())
621 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
638 public void mouseOverSequence(SequenceI sequence, int index, int pos)
640 String tmp = sequence.hashCode() + " " + index + " " + pos;
642 if (lastMessage == null || !lastMessage.equals(tmp))
644 // System.err.println("mouseOver Sequence: "+tmp);
645 ssm.mouseOverSequence(sequence, index, pos, av);
651 * Highlight the mapped region described by the search results object (unless
652 * unchanged). This supports highlight of protein while mousing over linked
653 * cDNA and vice versa. The status bar is also updated to show the location of
654 * the start of the highlighted region.
657 public void highlightSequence(SearchResultsI results)
659 if (results == null || results.equals(lastSearchResults))
663 lastSearchResults = results;
665 if (av.isFollowHighlight())
667 // don't allow highlight of protein/cDNA to also scroll a complementary
668 // panel,as this sets up a feedback loop (scrolling panel 1 causes moused
669 // over residue to change abruptly, causing highlighted residue in panel 2
670 // to change, causing a scroll in panel 1 etc)
671 ap.setDontScrollComplement(true);
672 if (ap.scrollToPosition(results, false))
674 seqCanvas.revalidate();
676 ap.setDontScrollComplement(false);
678 setStatusMessage(results);
679 seqCanvas.highlightSearchResults(results);
683 public VamsasSource getVamsasSource()
685 return this.ap == null ? null : this.ap.av;
689 public void updateColours(SequenceI seq, int index)
691 System.out.println("update the seqPanel colours");
702 public void mouseMoved(MouseEvent evt)
706 // This is because MacOSX creates a mouseMoved
707 // If control is down, other platforms will not.
711 int res = findRes(evt);
712 int seq = findSeq(evt);
714 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
719 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
721 if (res >= sequence.getLength())
726 pos = setStatusMessage(sequence, res, seq);
727 if (ssm != null && pos > -1)
729 mouseOverSequence(sequence, res, pos);
732 tooltipText.setLength(6); // Cuts the buffer back to <html>
734 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
737 for (int g = 0; g < groups.length; g++)
739 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
741 if (!groups[g].getName().startsWith("JTreeGroup")
742 && !groups[g].getName().startsWith("JGroup"))
744 tooltipText.append(groups[g].getName());
747 if (groups[g].getDescription() != null)
749 tooltipText.append(": " + groups[g].getDescription());
755 // use aa to see if the mouse pointer is on a
756 if (av.isShowSequenceFeatures())
759 List<SequenceFeature> features = ap.getFeatureRenderer()
760 .findFeaturesAtRes(sequence.getDatasetSequence(),
761 rpos = sequence.findPosition(res));
762 seqARep.appendFeatures(tooltipText, rpos, features,
763 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
765 if (tooltipText.length() == 6) // <html>
767 setToolTipText(null);
772 if (lastTooltip == null
773 || !lastTooltip.equals(tooltipText.toString()))
775 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
776 tooltipText.toString());
777 // String formatedTooltipText = tooltipText.toString();
778 setToolTipText(formatedTooltipText);
779 lastTooltip = tooltipText.toString();
786 private Point lastp = null;
791 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
794 public Point getToolTipLocation(MouseEvent event)
796 int x = event.getX(), w = getWidth();
797 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
800 if (!event.isShiftDown() || p == null)
802 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
803 event.getX() + wdth, event.getY() - 20) : null;
806 * TODO: try to modify position region is not obcured by tooltip
814 * set when the current UI interaction has resulted in a change that requires
815 * overview shading to be recalculated. this could be changed to something
816 * more expressive that indicates what actually has changed, so selective
817 * redraws can be applied
819 private boolean needOverviewUpdate = false; // TODO: refactor to avcontroller
822 * set if av.getSelectionGroup() refers to a group that is defined on the
823 * alignment view, rather than a transient selection
825 // private boolean editingDefinedGroup = false; // TODO: refactor to
826 // avcontroller or viewModel
829 * Set status message in alignment panel
832 * aligned sequence object
836 * index of sequence in alignment
837 * @return position of res in sequence
839 int setStatusMessage(SequenceI sequence, int res, int seq)
841 StringBuilder text = new StringBuilder(32);
844 * Sequence number (if known), and sequence name.
846 String seqno = seq == -1 ? "" : " " + (seq + 1);
847 text.append("Sequence").append(seqno).append(" ID: ")
848 .append(sequence.getName());
850 String residue = null;
852 * Try to translate the display character to residue name (null for gap).
854 final String displayChar = String.valueOf(sequence.getCharAt(res));
855 if (av.getAlignment().isNucleotide())
857 residue = ResidueProperties.nucleotideName.get(displayChar);
860 text.append(" Nucleotide: ").append(residue);
865 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
866 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
870 text.append(" Residue: ").append(residue);
877 pos = sequence.findPosition(res);
878 text.append(" (").append(Integer.toString(pos)).append(")");
880 ap.alignFrame.statusBar.setText(text.toString());
885 * Set the status bar message to highlight the first matched position in
890 private void setStatusMessage(SearchResultsI results)
892 AlignmentI al = this.av.getAlignment();
893 int sequenceIndex = al.findIndex(results);
894 if (sequenceIndex == -1)
898 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
899 for (SearchResultMatchI m : results.getResults())
901 SequenceI seq = m.getSequence();
902 if (seq.getDatasetSequence() != null)
904 seq = seq.getDatasetSequence();
910 * Convert position in sequence (base 1) to sequence character array
913 int start = m.getStart() - m.getSequence().getStart();
914 setStatusMessage(seq, start, sequenceIndex);
927 public void mouseDragged(MouseEvent evt)
929 if (mouseWheelPressed)
931 int oldWidth = av.getCharWidth();
933 // Which is bigger, left-right or up-down?
934 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
935 .getX() - lastMousePress.getX()))
937 int fontSize = av.font.getSize();
939 if (evt.getY() < lastMousePress.getY())
943 else if (evt.getY() > lastMousePress.getY())
954 new Font(av.font.getName(), av.font.getStyle(), fontSize),
956 av.setCharWidth(oldWidth);
961 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
963 av.setCharWidth(av.getCharWidth() - 1);
965 else if (evt.getX() > lastMousePress.getX())
967 av.setCharWidth(av.getCharWidth() + 1);
970 ap.paintAlignment(false);
973 FontMetrics fm = getFontMetrics(av.getFont());
974 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
976 lastMousePress = evt.getPoint();
983 doMouseDraggedDefineMode(evt);
987 int res = findRes(evt);
994 if ((lastres == -1) || (lastres == res))
999 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1001 // dragLeft, delete gap
1002 editSequence(false, false, res);
1006 editSequence(true, false, res);
1009 mouseDragging = true;
1010 if (scrollThread != null)
1012 scrollThread.setEvent(evt);
1016 // TODO: Make it more clever than many booleans
1017 synchronized void editSequence(boolean insertGap, boolean editSeq,
1021 int fixedRight = -1;
1022 boolean fixedColumns = false;
1023 SequenceGroup sg = av.getSelectionGroup();
1025 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1027 // No group, but the sequence may represent a group
1028 if (!groupEditing && av.hasHiddenRows())
1030 if (av.isHiddenRepSequence(seq))
1032 sg = av.getRepresentedSequences(seq);
1033 groupEditing = true;
1037 StringBuilder message = new StringBuilder(64);
1040 message.append("Edit group:");
1041 if (editCommand == null)
1043 editCommand = new EditCommand(
1044 MessageManager.getString("action.edit_group"));
1049 message.append("Edit sequence: " + seq.getName());
1050 String label = seq.getName();
1051 if (label.length() > 10)
1053 label = label.substring(0, 10);
1055 if (editCommand == null)
1057 editCommand = new EditCommand(MessageManager.formatMessage(
1058 "label.edit_params", new String[] { label }));
1064 message.append(" insert ");
1068 message.append(" delete ");
1071 message.append(Math.abs(startres - lastres) + " gaps.");
1072 ap.alignFrame.statusBar.setText(message.toString());
1074 // Are we editing within a selection group?
1076 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1079 fixedColumns = true;
1081 // sg might be null as the user may only see 1 sequence,
1082 // but the sequence represents a group
1085 if (!av.isHiddenRepSequence(seq))
1090 sg = av.getRepresentedSequences(seq);
1093 fixedLeft = sg.getStartRes();
1094 fixedRight = sg.getEndRes();
1096 if ((startres < fixedLeft && lastres >= fixedLeft)
1097 || (startres >= fixedLeft && lastres < fixedLeft)
1098 || (startres > fixedRight && lastres <= fixedRight)
1099 || (startres <= fixedRight && lastres > fixedRight))
1105 if (fixedLeft > startres)
1107 fixedRight = fixedLeft - 1;
1110 else if (fixedRight < startres)
1112 fixedLeft = fixedRight;
1117 if (av.hasHiddenColumns())
1119 fixedColumns = true;
1120 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1121 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1123 if ((insertGap && startres > y1 && lastres < y1)
1124 || (!insertGap && startres < y2 && lastres > y2))
1130 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1131 // Selection spans a hidden region
1132 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1140 fixedRight = y2 - 1;
1147 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1148 int g, groupSize = vseqs.size();
1149 SequenceI[] groupSeqs = new SequenceI[groupSize];
1150 for (g = 0; g < groupSeqs.length; g++)
1152 groupSeqs[g] = vseqs.get(g);
1158 // If the user has selected the whole sequence, and is dragging to
1159 // the right, we can still extend the alignment and selectionGroup
1160 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1161 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1163 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1164 fixedRight = sg.getEndRes();
1167 // Is it valid with fixed columns??
1168 // Find the next gap before the end
1169 // of the visible region boundary
1170 boolean blank = false;
1171 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1175 for (g = 0; g < groupSize; g++)
1177 for (int j = 0; j < startres - lastres; j++)
1179 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1194 if (sg.getSize() == av.getAlignment().getHeight())
1196 if ((av.hasHiddenColumns() && startres < av
1197 .getColumnSelection().getHiddenBoundaryRight(startres)))
1203 int alWidth = av.getAlignment().getWidth();
1204 if (av.hasHiddenRows())
1206 int hwidth = av.getAlignment().getHiddenSequences()
1208 if (hwidth > alWidth)
1213 // We can still insert gaps if the selectionGroup
1214 // contains all the sequences
1215 sg.setEndRes(sg.getEndRes() + startres - lastres);
1216 fixedRight = alWidth + startres - lastres;
1227 else if (!insertGap)
1229 // / Are we able to delete?
1230 // ie are all columns blank?
1232 for (g = 0; g < groupSize; g++)
1234 for (int j = startres; j < lastres; j++)
1236 if (groupSeqs[g].getLength() <= j)
1241 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1243 // Not a gap, block edit not valid
1253 // dragging to the right
1254 if (fixedColumns && fixedRight != -1)
1256 for (int j = lastres; j < startres; j++)
1258 insertChar(j, groupSeqs, fixedRight);
1263 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1269 // dragging to the left
1270 if (fixedColumns && fixedRight != -1)
1272 for (int j = lastres; j > startres; j--)
1274 deleteChar(startres, groupSeqs, fixedRight);
1279 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1286 // ///Editing a single sequence///////////
1290 // dragging to the right
1291 if (fixedColumns && fixedRight != -1)
1293 for (int j = lastres; j < startres; j++)
1295 insertChar(j, new SequenceI[] { seq }, fixedRight);
1300 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1301 startres - lastres);
1308 // dragging to the left
1309 if (fixedColumns && fixedRight != -1)
1311 for (int j = lastres; j > startres; j--)
1313 if (!Comparison.isGap(seq.getCharAt(startres)))
1318 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1323 // could be a keyboard edit trying to delete none gaps
1325 for (int m = startres; m < lastres; m++)
1327 if (!Comparison.isGap(seq.getCharAt(m)))
1336 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1342 {// insertGap==false AND editSeq==TRUE;
1343 if (fixedColumns && fixedRight != -1)
1345 for (int j = lastres; j < startres; j++)
1347 insertChar(j, new SequenceI[] { seq }, fixedRight);
1352 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1353 startres - lastres);
1360 seqCanvas.repaint();
1363 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1365 int blankColumn = fixedColumn;
1366 for (int s = 0; s < seq.length; s++)
1368 // Find the next gap before the end of the visible region boundary
1369 // If lastCol > j, theres a boundary after the gap insertion
1371 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1373 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1375 // Theres a space, so break and insert the gap
1380 if (blankColumn <= j)
1382 blankColumn = fixedColumn;
1388 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1390 appendEdit(Action.INSERT_GAP, seq, j, 1);
1395 * Helper method to add and perform one edit action.
1402 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1406 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1407 av.getAlignment().getGapCharacter());
1409 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1412 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1415 appendEdit(Action.DELETE_GAP, seq, j, 1);
1417 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1427 public void mouseEntered(MouseEvent e)
1434 if (scrollThread != null)
1436 scrollThread.running = false;
1437 scrollThread = null;
1448 public void mouseExited(MouseEvent e)
1450 if (av.getWrapAlignment())
1457 scrollThread = new ScrollThread();
1462 public void mouseClicked(MouseEvent evt)
1464 SequenceGroup sg = null;
1465 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1466 if (evt.getClickCount() > 1)
1468 sg = av.getSelectionGroup();
1469 if (sg != null && sg.getSize() == 1
1470 && sg.getEndRes() - sg.getStartRes() < 2)
1472 av.setSelectionGroup(null);
1475 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1476 .findFeaturesAtRes(sequence.getDatasetSequence(),
1477 sequence.findPosition(findRes(evt)));
1479 if (features != null && features.size() > 0)
1481 SearchResultsI highlight = new SearchResults();
1482 highlight.addResult(sequence, features.get(0).getBegin(), features
1484 seqCanvas.highlightSearchResults(highlight);
1486 if (features != null && features.size() > 0)
1488 seqCanvas.getFeatureRenderer().amendFeatures(
1489 new SequenceI[] { sequence },
1490 features.toArray(new SequenceFeature[features.size()]),
1493 seqCanvas.highlightSearchResults(null);
1499 public void mouseWheelMoved(MouseWheelEvent e)
1502 if (e.getWheelRotation() > 0)
1504 if (e.isShiftDown())
1506 av.getRanges().scrollRight(true);
1511 av.getRanges().scrollUp(false);
1516 if (e.isShiftDown())
1518 av.getRanges().scrollRight(false);
1522 av.getRanges().scrollUp(true);
1525 // TODO Update tooltip for new position.
1534 public void doMousePressedDefineMode(MouseEvent evt)
1536 final int res = findRes(evt);
1537 final int seq = findSeq(evt);
1539 needOverviewUpdate = false;
1541 startWrapBlock = wrappedBlock;
1543 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1545 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1546 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1547 MessageManager.getString("label.wrapped_view_no_edit"),
1548 JvOptionPane.WARNING_MESSAGE);
1552 if (seq < 0 || res < 0)
1557 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1559 if ((sequence == null) || (res > sequence.getLength()))
1564 stretchGroup = av.getSelectionGroup();
1566 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1568 stretchGroup = av.getAlignment().findGroup(sequence, res);
1569 if (stretchGroup != null)
1571 // only update the current selection if the popup menu has a group to
1573 av.setSelectionGroup(stretchGroup);
1577 if (evt.isPopupTrigger()) // Mac: mousePressed
1584 * defer right-mouse click handling to mouseReleased on Windows
1585 * (where isPopupTrigger() will answer true)
1586 * NB isRightMouseButton is also true for Cmd-click on Mac
1588 if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
1595 seqCanvas.cursorX = findRes(evt);
1596 seqCanvas.cursorY = findSeq(evt);
1597 seqCanvas.repaint();
1601 if (stretchGroup == null)
1603 // Only if left mouse button do we want to change group sizes
1605 // define a new group here
1606 SequenceGroup sg = new SequenceGroup();
1607 sg.setStartRes(res);
1609 sg.addSequence(sequence, false);
1610 av.setSelectionGroup(sg);
1613 if (av.getConservationSelected())
1615 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1619 if (av.getAbovePIDThreshold())
1621 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1624 // TODO: stretchGroup will always be not null. Is this a merge error ?
1625 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1627 // Edit end res position of selected group
1628 changeEndRes = true;
1630 else if ((stretchGroup != null)
1631 && (stretchGroup.getStartRes() == res))
1633 // Edit end res position of selected group
1634 changeStartRes = true;
1636 stretchGroup.getWidth();
1639 seqCanvas.repaint();
1643 * Build and show a pop-up menu at the right-click mouse position
1649 void showPopupMenu(MouseEvent evt)
1651 final int res = findRes(evt);
1652 final int seq = findSeq(evt);
1653 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1654 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1655 .findFeaturesAtRes(sequence.getDatasetSequence(),
1656 sequence.findPosition(res));
1657 List<String> links = new ArrayList<String>();
1658 for (SequenceFeature sf : allFeatures)
1660 if (sf.links != null)
1662 for (String link : sf.links)
1669 PopupMenu pop = new PopupMenu(ap, null, links);
1670 pop.show(this, evt.getX(), evt.getY());
1679 public void doMouseReleasedDefineMode(MouseEvent evt)
1681 if (stretchGroup == null)
1685 // always do this - annotation has own state
1686 // but defer colourscheme update until hidden sequences are passed in
1687 boolean vischange = stretchGroup.recalcConservation(true);
1688 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1689 if (stretchGroup.cs != null)
1691 stretchGroup.cs.alignmentChanged(stretchGroup,
1692 av.getHiddenRepSequences());
1694 ResidueShaderI groupColourScheme = stretchGroup.getGroupColourScheme();
1695 String name = stretchGroup.getName();
1696 if (stretchGroup.cs.conservationApplied())
1698 SliderPanel.setConservationSlider(ap, groupColourScheme, name);
1700 if (stretchGroup.cs.getThreshold() > 0)
1702 SliderPanel.setPIDSliderSource(ap, groupColourScheme, name);
1705 PaintRefresher.Refresh(this, av.getSequenceSetId());
1706 ap.paintAlignment(needOverviewUpdate);
1707 needOverviewUpdate = false;
1708 changeEndRes = false;
1709 changeStartRes = false;
1710 stretchGroup = null;
1720 public void doMouseDraggedDefineMode(MouseEvent evt)
1722 int res = findRes(evt);
1723 int y = findSeq(evt);
1725 if (wrappedBlock != startWrapBlock)
1730 if (stretchGroup == null)
1735 if (res >= av.getAlignment().getWidth())
1737 res = av.getAlignment().getWidth() - 1;
1740 if (stretchGroup.getEndRes() == res)
1742 // Edit end res position of selected group
1743 changeEndRes = true;
1745 else if (stretchGroup.getStartRes() == res)
1747 // Edit start res position of selected group
1748 changeStartRes = true;
1751 if (res < av.getRanges().getStartRes())
1753 res = av.getRanges().getStartRes();
1758 if (res > (stretchGroup.getStartRes() - 1))
1760 stretchGroup.setEndRes(res);
1761 needOverviewUpdate |= av.isSelectionDefinedGroup();
1764 else if (changeStartRes)
1766 if (res < (stretchGroup.getEndRes() + 1))
1768 stretchGroup.setStartRes(res);
1769 needOverviewUpdate |= av.isSelectionDefinedGroup();
1773 int dragDirection = 0;
1779 else if (y < oldSeq)
1784 while ((y != oldSeq) && (oldSeq > -1)
1785 && (y < av.getAlignment().getHeight()))
1787 // This routine ensures we don't skip any sequences, as the
1788 // selection is quite slow.
1789 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1791 oldSeq += dragDirection;
1798 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1800 if (stretchGroup.getSequences(null).contains(nextSeq))
1802 stretchGroup.deleteSequence(seq, false);
1803 needOverviewUpdate |= av.isSelectionDefinedGroup();
1809 stretchGroup.addSequence(seq, false);
1812 stretchGroup.addSequence(nextSeq, false);
1813 needOverviewUpdate |= av.isSelectionDefinedGroup();
1822 mouseDragging = true;
1824 if (scrollThread != null)
1826 scrollThread.setEvent(evt);
1829 seqCanvas.repaint();
1832 void scrollCanvas(MouseEvent evt)
1836 if (scrollThread != null)
1838 scrollThread.running = false;
1839 scrollThread = null;
1841 mouseDragging = false;
1845 if (scrollThread == null)
1847 scrollThread = new ScrollThread();
1850 mouseDragging = true;
1851 scrollThread.setEvent(evt);
1856 // this class allows scrolling off the bottom of the visible alignment
1857 class ScrollThread extends Thread
1861 boolean running = false;
1863 public ScrollThread()
1868 public void setEvent(MouseEvent e)
1873 public void stopScrolling()
1887 if (mouseDragging && (evt.getY() < 0)
1888 && (av.getRanges().getStartSeq() > 0))
1890 running = av.getRanges().scrollUp(true);
1893 if (mouseDragging && (evt.getY() >= getHeight())
1894 && (av.getAlignment().getHeight() > av.getRanges()
1897 running = av.getRanges().scrollUp(false);
1900 if (mouseDragging && (evt.getX() < 0))
1902 running = av.getRanges().scrollRight(false);
1904 else if (mouseDragging && (evt.getX() >= getWidth()))
1906 running = av.getRanges().scrollRight(true);
1913 } catch (Exception ex)
1921 * modify current selection according to a received message.
1924 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1925 SelectionSource source)
1927 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1928 // handles selection messages...
1929 // TODO: extend config options to allow user to control if selections may be
1930 // shared between viewports.
1931 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1932 .getSequenceSetId().equals(av.getSequenceSetId())));
1933 if (iSentTheSelection || !av.followSelection)
1939 * Ignore the selection if there is one of our own pending.
1941 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1947 * Check for selection in a view of which this one is a dna/protein
1950 if (selectionFromTranslation(seqsel, colsel, source))
1955 // do we want to thread this ? (contention with seqsel and colsel locks, I
1958 * only copy colsel if there is a real intersection between
1959 * sequence selection and this panel's alignment
1961 boolean repaint = false;
1962 boolean copycolsel = false;
1964 SequenceGroup sgroup = null;
1965 if (seqsel != null && seqsel.getSize() > 0)
1967 if (av.getAlignment() == null)
1969 Cache.log.warn("alignviewport av SeqSetId=" + av.getSequenceSetId()
1970 + " ViewId=" + av.getViewId()
1971 + " 's alignment is NULL! returning immediately.");
1974 sgroup = seqsel.intersect(av.getAlignment(),
1975 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1976 if ((sgroup != null && sgroup.getSize() > 0))
1981 if (sgroup != null && sgroup.getSize() > 0)
1983 av.setSelectionGroup(sgroup);
1987 av.setSelectionGroup(null);
1989 av.isSelectionGroupChanged(true);
1994 // the current selection is unset or from a previous message
1995 // so import the new colsel.
1996 if (colsel == null || colsel.isEmpty())
1998 if (av.getColumnSelection() != null)
2000 av.getColumnSelection().clear();
2006 // TODO: shift colSel according to the intersecting sequences
2007 if (av.getColumnSelection() == null)
2009 av.setColumnSelection(new ColumnSelection(colsel));
2013 av.getColumnSelection().setElementsFrom(colsel);
2016 av.isColSelChanged(true);
2021 && av.hasHiddenColumns()
2022 && (av.getColumnSelection() == null || av.getColumnSelection()
2023 .getHiddenColumns() == null))
2025 System.err.println("Bad things");
2027 if (repaint) // always true!
2029 // probably finessing with multiple redraws here
2030 PaintRefresher.Refresh(this, av.getSequenceSetId());
2031 // ap.paintAlignment(false);
2036 * If this panel is a cdna/protein translation view of the selection source,
2037 * tries to map the source selection to a local one, and returns true. Else
2044 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2045 ColumnSelection colsel, SelectionSource source)
2047 if (!(source instanceof AlignViewportI))
2051 final AlignViewportI sourceAv = (AlignViewportI) source;
2052 if (sourceAv.getCodingComplement() != av
2053 && av.getCodingComplement() != sourceAv)
2059 * Map sequence selection
2061 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2062 av.setSelectionGroup(sg);
2063 av.isSelectionGroupChanged(true);
2066 * Map column selection
2068 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2070 av.setColumnSelection(cs);
2072 PaintRefresher.Refresh(this, av.getSequenceSetId());