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.SearchResults;
31 import jalview.datamodel.SearchResults.Match;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceGroup;
35 import jalview.datamodel.SequenceI;
36 import jalview.io.SequenceAnnotationReport;
37 import jalview.schemes.ResidueProperties;
38 import jalview.structure.SelectionListener;
39 import jalview.structure.SelectionSource;
40 import jalview.structure.SequenceListener;
41 import jalview.structure.StructureSelectionManager;
42 import jalview.structure.VamsasSource;
43 import jalview.util.Comparison;
44 import jalview.util.MappingUtils;
45 import jalview.util.MessageManager;
46 import jalview.viewmodel.AlignmentViewport;
48 import java.awt.BorderLayout;
49 import java.awt.Color;
51 import java.awt.FontMetrics;
52 import java.awt.Point;
53 import java.awt.event.MouseEvent;
54 import java.awt.event.MouseListener;
55 import java.awt.event.MouseMotionListener;
56 import java.awt.event.MouseWheelEvent;
57 import java.awt.event.MouseWheelListener;
58 import java.util.ArrayList;
59 import java.util.List;
61 import javax.swing.JOptionPane;
62 import javax.swing.JPanel;
63 import javax.swing.SwingUtilities;
64 import javax.swing.ToolTipManager;
70 * @version $Revision: 1.130 $
72 public class SeqPanel extends JPanel implements MouseListener,
73 MouseMotionListener, MouseWheelListener, SequenceListener,
78 public SeqCanvas seqCanvas;
81 public AlignmentPanel ap;
83 protected int lastres;
85 protected int startseq;
87 protected AlignViewport av;
89 ScrollThread scrollThread = null;
91 boolean mouseDragging = false;
93 boolean editingSeqs = false;
95 boolean groupEditing = false;
97 // ////////////////////////////////////////
98 // ///Everything below this is for defining the boundary of the rubberband
99 // ////////////////////////////////////////
102 boolean changeEndSeq = false;
104 boolean changeStartSeq = false;
106 boolean changeEndRes = false;
108 boolean changeStartRes = false;
110 SequenceGroup stretchGroup = null;
112 boolean remove = false;
114 Point lastMousePress;
116 boolean mouseWheelPressed = false;
118 StringBuffer keyboardNo1;
120 StringBuffer keyboardNo2;
122 java.net.URL linkImageURL;
124 private final SequenceAnnotationReport seqARep;
126 StringBuffer tooltipText = new StringBuffer();
130 EditCommand editCommand;
132 StructureSelectionManager ssm;
134 SearchResults lastSearchResults;
137 * Creates a new SeqPanel object.
144 public SeqPanel(AlignViewport av, AlignmentPanel ap)
146 linkImageURL = getClass().getResource("/images/link.gif");
147 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
148 ToolTipManager.sharedInstance().registerComponent(this);
149 ToolTipManager.sharedInstance().setInitialDelay(0);
150 ToolTipManager.sharedInstance().setDismissDelay(10000);
152 setBackground(Color.white);
154 seqCanvas = new SeqCanvas(ap);
155 setLayout(new BorderLayout());
156 add(seqCanvas, BorderLayout.CENTER);
162 addMouseMotionListener(this);
163 addMouseListener(this);
164 addMouseWheelListener(this);
165 ssm = av.getStructureSelectionManager();
166 ssm.addStructureViewerListener(this);
167 ssm.addSelectionListener(this);
171 int startWrapBlock = -1;
173 int wrappedBlock = -1;
176 * Returns the aligned sequence position (base 0) at the mouse position, or
177 * the closest visible one
182 int findRes(MouseEvent evt)
187 if (av.getWrapAlignment())
190 int hgap = av.getCharHeight();
191 if (av.getScaleAboveWrapped())
193 hgap += av.getCharHeight();
196 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
197 + hgap + seqCanvas.getAnnotationHeight();
201 x -= seqCanvas.LABEL_WEST;
203 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
209 wrappedBlock = y / cHeight;
210 wrappedBlock += av.getStartRes() / cwidth;
212 res = wrappedBlock * cwidth + x / av.getCharWidth();
217 if (x > seqCanvas.getX() + seqCanvas.getWidth())
219 // make sure we calculate relative to visible alignment, rather than
221 x = seqCanvas.getX() + seqCanvas.getWidth();
223 res = (x / av.getCharWidth()) + av.getStartRes();
224 if (res > av.getEndRes())
227 res = av.getEndRes();
231 if (av.hasHiddenColumns())
233 res = av.getColumnSelection().adjustForHiddenColumns(res);
240 int findSeq(MouseEvent evt)
245 if (av.getWrapAlignment())
247 int hgap = av.getCharHeight();
248 if (av.getScaleAboveWrapped())
250 hgap += av.getCharHeight();
253 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
254 + hgap + seqCanvas.getAnnotationHeight();
258 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
263 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
264 .getAlignment().getHeight() - 1);
271 * When all of a sequence of edits are complete, put the resulting edit list
272 * on the history stack (undo list), and reset flags for editing in progress.
278 if (editCommand != null && editCommand.getSize() > 0)
280 ap.alignFrame.addHistoryItem(editCommand);
281 av.firePropertyChange("alignment", null, av.getAlignment()
287 * Tidy up come what may...
292 groupEditing = false;
301 seqCanvas.cursorY = getKeyboardNo1() - 1;
305 void setCursorColumn()
307 seqCanvas.cursorX = getKeyboardNo1() - 1;
311 void setCursorRowAndColumn()
313 if (keyboardNo2 == null)
315 keyboardNo2 = new StringBuffer();
319 seqCanvas.cursorX = getKeyboardNo1() - 1;
320 seqCanvas.cursorY = getKeyboardNo2() - 1;
325 void setCursorPosition()
327 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
329 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
333 void moveCursor(int dx, int dy)
335 seqCanvas.cursorX += dx;
336 seqCanvas.cursorY += dy;
337 if (av.hasHiddenColumns()
338 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
340 int original = seqCanvas.cursorX - dx;
341 int maxWidth = av.getAlignment().getWidth();
343 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
344 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
346 seqCanvas.cursorX += dx;
349 if (seqCanvas.cursorX >= maxWidth
350 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
352 seqCanvas.cursorX = original;
359 void scrollToVisible()
361 if (seqCanvas.cursorX < 0)
363 seqCanvas.cursorX = 0;
365 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
367 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
370 if (seqCanvas.cursorY < 0)
372 seqCanvas.cursorY = 0;
374 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
376 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
380 if (av.getWrapAlignment())
382 ap.scrollToWrappedVisible(seqCanvas.cursorX);
386 while (seqCanvas.cursorY < av.startSeq)
390 while (seqCanvas.cursorY + 1 > av.endSeq)
394 if (!av.getWrapAlignment())
396 while (seqCanvas.cursorX < av.getColumnSelection()
397 .adjustForHiddenColumns(av.startRes))
399 if (!ap.scrollRight(false))
404 while (seqCanvas.cursorX > av.getColumnSelection()
405 .adjustForHiddenColumns(av.endRes))
407 if (!ap.scrollRight(true))
414 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
415 seqCanvas.cursorX, seqCanvas.cursorY);
420 void setSelectionAreaAtCursor(boolean topLeft)
422 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
424 if (av.getSelectionGroup() != null)
426 SequenceGroup sg = av.getSelectionGroup();
427 // Find the top and bottom of this group
428 int min = av.getAlignment().getHeight(), max = 0;
429 for (int i = 0; i < sg.getSize(); i++)
431 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
446 sg.setStartRes(seqCanvas.cursorX);
447 if (sg.getEndRes() < seqCanvas.cursorX)
449 sg.setEndRes(seqCanvas.cursorX);
452 min = seqCanvas.cursorY;
456 sg.setEndRes(seqCanvas.cursorX);
457 if (sg.getStartRes() > seqCanvas.cursorX)
459 sg.setStartRes(seqCanvas.cursorX);
462 max = seqCanvas.cursorY + 1;
467 // Only the user can do this
468 av.setSelectionGroup(null);
472 // Now add any sequences between min and max
473 sg.getSequences(null).clear();
474 for (int i = min; i < max; i++)
476 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
481 if (av.getSelectionGroup() == null)
483 SequenceGroup sg = new SequenceGroup();
484 sg.setStartRes(seqCanvas.cursorX);
485 sg.setEndRes(seqCanvas.cursorX);
486 sg.addSequence(sequence, false);
487 av.setSelectionGroup(sg);
490 ap.paintAlignment(false);
494 void insertGapAtCursor(boolean group)
496 groupEditing = group;
497 startseq = seqCanvas.cursorY;
498 lastres = seqCanvas.cursorX;
499 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
503 void deleteGapAtCursor(boolean group)
505 groupEditing = group;
506 startseq = seqCanvas.cursorY;
507 lastres = seqCanvas.cursorX + getKeyboardNo1();
508 editSequence(false, false, seqCanvas.cursorX);
512 void insertNucAtCursor(boolean group, String nuc)
514 // TODO not called - delete?
515 groupEditing = group;
516 startseq = seqCanvas.cursorY;
517 lastres = seqCanvas.cursorX;
518 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
522 void numberPressed(char value)
524 if (keyboardNo1 == null)
526 keyboardNo1 = new StringBuffer();
529 if (keyboardNo2 != null)
531 keyboardNo2.append(value);
535 keyboardNo1.append(value);
543 if (keyboardNo1 != null)
545 int value = Integer.parseInt(keyboardNo1.toString());
549 } catch (Exception x)
560 if (keyboardNo2 != null)
562 int value = Integer.parseInt(keyboardNo2.toString());
566 } catch (Exception x)
580 public void mouseReleased(MouseEvent evt)
582 mouseDragging = false;
583 mouseWheelPressed = false;
585 if (evt.isPopupTrigger()) // Windows: mouseReleased
594 doMouseReleasedDefineMode(evt);
608 public void mousePressed(MouseEvent evt)
610 lastMousePress = evt.getPoint();
612 if (SwingUtilities.isMiddleMouseButton(evt))
614 mouseWheelPressed = true;
618 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
620 if (evt.isAltDown() || evt.isControlDown())
628 doMousePressedDefineMode(evt);
632 int seq = findSeq(evt);
633 int res = findRes(evt);
635 if (seq < 0 || res < 0)
640 if ((seq < av.getAlignment().getHeight())
641 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
658 public void mouseOverSequence(SequenceI sequence, int index, int pos)
660 String tmp = sequence.hashCode() + " " + index + " " + pos;
662 if (lastMessage == null || !lastMessage.equals(tmp))
664 // System.err.println("mouseOver Sequence: "+tmp);
665 ssm.mouseOverSequence(sequence, index, pos, av);
671 * Highlight the mapped region described by the search results object (unless
672 * unchanged). This supports highlight of protein while mousing over linked
673 * cDNA and vice versa. The status bar is also updated to show the location of
674 * the start of the highlighted region.
677 public void highlightSequence(SearchResults results)
679 if (results == null || results.equals(lastSearchResults))
683 lastSearchResults = results;
685 if (av.isFollowHighlight())
688 * if scrollToPosition requires a scroll adjustment, this flag prevents
689 * another scroll event being propagated back to the originator
691 * @see AlignmentPanel#adjustmentValueChanged
693 ap.setDontScrollComplement(true);
694 if (ap.scrollToPosition(results, false))
696 seqCanvas.revalidate();
699 setStatusMessage(results);
700 seqCanvas.highlightSearchResults(results);
704 public VamsasSource getVamsasSource()
706 return this.ap == null ? null : this.ap.av;
710 public void updateColours(SequenceI seq, int index)
712 System.out.println("update the seqPanel colours");
723 public void mouseMoved(MouseEvent evt)
727 // This is because MacOSX creates a mouseMoved
728 // If control is down, other platforms will not.
732 int res = findRes(evt);
733 int seq = findSeq(evt);
735 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
740 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
742 if (res >= sequence.getLength())
747 pos = setStatusMessage(sequence, res, seq);
748 if (ssm != null && pos > -1)
750 mouseOverSequence(sequence, res, pos);
753 tooltipText.setLength(6); // Cuts the buffer back to <html>
755 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
758 for (int g = 0; g < groups.length; g++)
760 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
762 if (!groups[g].getName().startsWith("JTreeGroup")
763 && !groups[g].getName().startsWith("JGroup"))
765 tooltipText.append(groups[g].getName());
768 if (groups[g].getDescription() != null)
770 tooltipText.append(": " + groups[g].getDescription());
776 // use aa to see if the mouse pointer is on a
777 if (av.isShowSequenceFeatures())
780 List<SequenceFeature> features = ap.getFeatureRenderer()
781 .findFeaturesAtRes(sequence.getDatasetSequence(),
782 rpos = sequence.findPosition(res));
783 seqARep.appendFeatures(tooltipText, rpos, features,
784 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
786 if (tooltipText.length() == 6) // <html></html>
788 setToolTipText(null);
793 if (lastTooltip == null
794 || !lastTooltip.equals(tooltipText.toString()))
796 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
797 tooltipText.toString());
798 // String formatedTooltipText = tooltipText.toString();
799 setToolTipText(formatedTooltipText);
800 lastTooltip = tooltipText.toString();
807 private Point lastp = null;
812 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
815 public Point getToolTipLocation(MouseEvent event)
817 int x = event.getX(), w = getWidth();
818 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
821 if (!event.isShiftDown() || p == null)
823 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
824 event.getX() + wdth, event.getY() - 20) : null;
827 * TODO: try to modify position region is not obcured by tooltip
835 * set when the current UI interaction has resulted in a change that requires
836 * overview shading to be recalculated. this could be changed to something
837 * more expressive that indicates what actually has changed, so selective
838 * redraws can be applied
840 private boolean needOverviewUpdate = false; // TODO: refactor to avcontroller
843 * set if av.getSelectionGroup() refers to a group that is defined on the
844 * alignment view, rather than a transient selection
846 private boolean editingDefinedGroup = false; // TODO: refactor to avcontroller or viewModel
849 * Set status message in alignment panel
852 * aligned sequence object
856 * index of sequence in alignment
857 * @return position of res in sequence
859 int setStatusMessage(SequenceI sequence, int res, int seq)
861 StringBuilder text = new StringBuilder(32);
864 * Sequence number (if known), and sequence name.
866 String seqno = seq == -1 ? "" : " " + (seq + 1);
867 text.append("Sequence").append(seqno).append(" ID: ")
868 .append(sequence.getName());
870 String residue = null;
872 * Try to translate the display character to residue name (null for gap).
874 final String displayChar = String.valueOf(sequence.getCharAt(res));
875 if (av.getAlignment().isNucleotide())
877 residue = ResidueProperties.nucleotideName.get(displayChar);
880 text.append(" Nucleotide: ").append(residue);
885 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
886 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
890 text.append(" Residue: ").append(residue);
897 pos = sequence.findPosition(res);
898 text.append(" (").append(Integer.toString(pos)).append(")");
900 ap.alignFrame.statusBar.setText(text.toString());
905 * Set the status bar message to highlight the first matched position in
910 private void setStatusMessage(SearchResults results)
912 AlignmentI al = this.av.getAlignment();
913 int sequenceIndex = al.findIndex(results);
914 if (sequenceIndex == -1)
918 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
919 for (Match m : results.getResults())
921 SequenceI seq = m.getSequence();
922 if (seq.getDatasetSequence() != null)
924 seq = seq.getDatasetSequence();
930 * Convert position in sequence (base 1) to sequence character array
933 int start = m.getStart() - m.getSequence().getStart();
934 setStatusMessage(seq, start, sequenceIndex);
947 public void mouseDragged(MouseEvent evt)
949 if (mouseWheelPressed)
951 int oldWidth = av.getCharWidth();
953 // Which is bigger, left-right or up-down?
954 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
955 .getX() - lastMousePress.getX()))
957 int fontSize = av.font.getSize();
959 if (evt.getY() < lastMousePress.getY())
963 else if (evt.getY() > lastMousePress.getY())
974 new Font(av.font.getName(), av.font.getStyle(), fontSize),
976 av.setCharWidth(oldWidth);
981 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
983 av.setCharWidth(av.getCharWidth() - 1);
985 else if (evt.getX() > lastMousePress.getX())
987 av.setCharWidth(av.getCharWidth() + 1);
990 ap.paintAlignment(false);
993 FontMetrics fm = getFontMetrics(av.getFont());
994 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
996 lastMousePress = evt.getPoint();
1003 doMouseDraggedDefineMode(evt);
1007 int res = findRes(evt);
1014 if ((lastres == -1) || (lastres == res))
1019 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1021 // dragLeft, delete gap
1022 editSequence(false, false, res);
1026 editSequence(true, false, res);
1029 mouseDragging = true;
1030 if (scrollThread != null)
1032 scrollThread.setEvent(evt);
1036 // TODO: Make it more clever than many booleans
1037 synchronized void editSequence(boolean insertGap, boolean editSeq,
1041 int fixedRight = -1;
1042 boolean fixedColumns = false;
1043 SequenceGroup sg = av.getSelectionGroup();
1045 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1047 // No group, but the sequence may represent a group
1048 if (!groupEditing && av.hasHiddenRows())
1050 if (av.isHiddenRepSequence(seq))
1052 sg = av.getRepresentedSequences(seq);
1053 groupEditing = true;
1057 StringBuilder message = new StringBuilder(64);
1060 message.append("Edit group:");
1061 if (editCommand == null)
1063 editCommand = new EditCommand(
1064 MessageManager.getString("action.edit_group"));
1069 message.append("Edit sequence: " + seq.getName());
1070 String label = seq.getName();
1071 if (label.length() > 10)
1073 label = label.substring(0, 10);
1075 if (editCommand == null)
1077 editCommand = new EditCommand(MessageManager.formatMessage(
1078 "label.edit_params", new String[] { label }));
1084 message.append(" insert ");
1088 message.append(" delete ");
1091 message.append(Math.abs(startres - lastres) + " gaps.");
1092 ap.alignFrame.statusBar.setText(message.toString());
1094 // Are we editing within a selection group?
1096 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1099 fixedColumns = true;
1101 // sg might be null as the user may only see 1 sequence,
1102 // but the sequence represents a group
1105 if (!av.isHiddenRepSequence(seq))
1110 sg = av.getRepresentedSequences(seq);
1113 fixedLeft = sg.getStartRes();
1114 fixedRight = sg.getEndRes();
1116 if ((startres < fixedLeft && lastres >= fixedLeft)
1117 || (startres >= fixedLeft && lastres < fixedLeft)
1118 || (startres > fixedRight && lastres <= fixedRight)
1119 || (startres <= fixedRight && lastres > fixedRight))
1125 if (fixedLeft > startres)
1127 fixedRight = fixedLeft - 1;
1130 else if (fixedRight < startres)
1132 fixedLeft = fixedRight;
1137 if (av.hasHiddenColumns())
1139 fixedColumns = true;
1140 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1141 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1143 if ((insertGap && startres > y1 && lastres < y1)
1144 || (!insertGap && startres < y2 && lastres > y2))
1150 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1151 // Selection spans a hidden region
1152 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1160 fixedRight = y2 - 1;
1167 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1168 int g, groupSize = vseqs.size();
1169 SequenceI[] groupSeqs = new SequenceI[groupSize];
1170 for (g = 0; g < groupSeqs.length; g++)
1172 groupSeqs[g] = vseqs.get(g);
1178 // If the user has selected the whole sequence, and is dragging to
1179 // the right, we can still extend the alignment and selectionGroup
1180 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1181 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1183 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1184 fixedRight = sg.getEndRes();
1187 // Is it valid with fixed columns??
1188 // Find the next gap before the end
1189 // of the visible region boundary
1190 boolean blank = false;
1191 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1195 for (g = 0; g < groupSize; g++)
1197 for (int j = 0; j < startres - lastres; j++)
1199 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1214 if (sg.getSize() == av.getAlignment().getHeight())
1216 if ((av.hasHiddenColumns() && startres < av
1217 .getColumnSelection().getHiddenBoundaryRight(startres)))
1223 int alWidth = av.getAlignment().getWidth();
1224 if (av.hasHiddenRows())
1226 int hwidth = av.getAlignment().getHiddenSequences()
1228 if (hwidth > alWidth)
1233 // We can still insert gaps if the selectionGroup
1234 // contains all the sequences
1235 sg.setEndRes(sg.getEndRes() + startres - lastres);
1236 fixedRight = alWidth + startres - lastres;
1247 else if (!insertGap)
1249 // / Are we able to delete?
1250 // ie are all columns blank?
1252 for (g = 0; g < groupSize; g++)
1254 for (int j = startres; j < lastres; j++)
1256 if (groupSeqs[g].getLength() <= j)
1261 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1263 // Not a gap, block edit not valid
1273 // dragging to the right
1274 if (fixedColumns && fixedRight != -1)
1276 for (int j = lastres; j < startres; j++)
1278 insertChar(j, groupSeqs, fixedRight);
1283 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1289 // dragging to the left
1290 if (fixedColumns && fixedRight != -1)
1292 for (int j = lastres; j > startres; j--)
1294 deleteChar(startres, groupSeqs, fixedRight);
1299 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1306 // ///Editing a single sequence///////////
1310 // dragging to the right
1311 if (fixedColumns && fixedRight != -1)
1313 for (int j = lastres; j < startres; j++)
1315 insertChar(j, new SequenceI[] { seq }, fixedRight);
1320 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1321 startres - lastres);
1328 // dragging to the left
1329 if (fixedColumns && fixedRight != -1)
1331 for (int j = lastres; j > startres; j--)
1333 if (!Comparison.isGap(seq.getCharAt(startres)))
1338 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1343 // could be a keyboard edit trying to delete none gaps
1345 for (int m = startres; m < lastres; m++)
1347 if (!Comparison.isGap(seq.getCharAt(m)))
1356 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1362 {// insertGap==false AND editSeq==TRUE;
1363 if (fixedColumns && fixedRight != -1)
1365 for (int j = lastres; j < startres; j++)
1367 insertChar(j, new SequenceI[] { seq }, fixedRight);
1372 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1373 startres - lastres);
1380 seqCanvas.repaint();
1383 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1385 int blankColumn = fixedColumn;
1386 for (int s = 0; s < seq.length; s++)
1388 // Find the next gap before the end of the visible region boundary
1389 // If lastCol > j, theres a boundary after the gap insertion
1391 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1393 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1395 // Theres a space, so break and insert the gap
1400 if (blankColumn <= j)
1402 blankColumn = fixedColumn;
1408 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1410 appendEdit(Action.INSERT_GAP, seq, j, 1);
1415 * Helper method to add and perform one edit action.
1422 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1426 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1427 av.getAlignment().getGapCharacter());
1429 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1432 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1435 appendEdit(Action.DELETE_GAP, seq, j, 1);
1437 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1447 public void mouseEntered(MouseEvent e)
1454 if (scrollThread != null)
1456 scrollThread.running = false;
1457 scrollThread = null;
1468 public void mouseExited(MouseEvent e)
1470 if (av.getWrapAlignment())
1477 scrollThread = new ScrollThread();
1482 public void mouseClicked(MouseEvent evt)
1484 SequenceGroup sg = null;
1485 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1486 if (evt.getClickCount() > 1)
1488 sg = av.getSelectionGroup();
1489 if (sg != null && sg.getSize() == 1
1490 && sg.getEndRes() - sg.getStartRes() < 2)
1492 av.setSelectionGroup(null);
1495 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1496 .findFeaturesAtRes(sequence.getDatasetSequence(),
1497 sequence.findPosition(findRes(evt)));
1499 if (features != null && features.size() > 0)
1501 SearchResults highlight = new SearchResults();
1502 highlight.addResult(sequence, features.get(0).getBegin(), features
1504 seqCanvas.highlightSearchResults(highlight);
1506 if (features != null && features.size() > 0)
1508 seqCanvas.getFeatureRenderer().amendFeatures(
1509 new SequenceI[] { sequence },
1510 features.toArray(new SequenceFeature[features.size()]),
1513 seqCanvas.highlightSearchResults(null);
1519 public void mouseWheelMoved(MouseWheelEvent e)
1522 if (e.getWheelRotation() > 0)
1524 if (e.isShiftDown())
1526 ap.scrollRight(true);
1536 if (e.isShiftDown())
1538 ap.scrollRight(false);
1545 // TODO Update tooltip for new position.
1554 public void doMousePressedDefineMode(MouseEvent evt)
1556 final int res = findRes(evt);
1557 final int seq = findSeq(evt);
1560 startWrapBlock = wrappedBlock;
1562 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1564 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1565 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1566 MessageManager.getString("label.wrapped_view_no_edit"),
1567 JOptionPane.WARNING_MESSAGE);
1571 if (seq < 0 || res < 0)
1576 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1578 if ((sequence == null) || (res > sequence.getLength()))
1583 stretchGroup = av.getSelectionGroup();
1585 if (stretchGroup == null)
1587 stretchGroup = av.getAlignment().findGroup(sequence);
1589 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1590 && (res < stretchGroup.getEndRes()))
1592 av.setSelectionGroup(stretchGroup);
1593 editingDefinedGroup = true;
1597 stretchGroup = null;
1598 editingDefinedGroup = false;
1601 else if (!stretchGroup.getSequences(null).contains(sequence)
1602 || (stretchGroup.getStartRes() > res)
1603 || (stretchGroup.getEndRes() < res))
1605 stretchGroup = null;
1607 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1609 if (allGroups != null)
1611 for (int i = 0; i < allGroups.length; i++)
1613 if ((allGroups[i].getStartRes() <= res)
1614 && (allGroups[i].getEndRes() >= res))
1616 stretchGroup = allGroups[i];
1617 editingDefinedGroup = true;
1623 av.setSelectionGroup(stretchGroup);
1627 if (evt.isPopupTrigger()) // Mac: mousePressed
1635 seqCanvas.cursorX = findRes(evt);
1636 seqCanvas.cursorY = findSeq(evt);
1637 seqCanvas.repaint();
1641 if (stretchGroup == null)
1643 // Only if left mouse button do we want to change group sizes
1645 // define a new group here
1646 SequenceGroup sg = new SequenceGroup();
1647 sg.setStartRes(res);
1649 sg.addSequence(sequence, false);
1650 av.setSelectionGroup(sg);
1651 editingDefinedGroup = false;
1654 if (av.getConservationSelected())
1656 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1660 if (av.getAbovePIDThreshold())
1662 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1665 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1667 // Edit end res position of selected group
1668 changeEndRes = true;
1670 else if ((stretchGroup != null)
1671 && (stretchGroup.getStartRes() == res))
1673 // Edit end res position of selected group
1674 changeStartRes = true;
1676 stretchGroup.getWidth();
1679 seqCanvas.repaint();
1683 * Build and show a pop-up menu at the right-click mouse position
1689 void showPopupMenu(MouseEvent evt)
1691 final int res = findRes(evt);
1692 final int seq = findSeq(evt);
1693 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1694 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1695 .findFeaturesAtRes(sequence.getDatasetSequence(),
1696 sequence.findPosition(res));
1697 List<String> links = new ArrayList<String>();
1698 for (SequenceFeature sf : allFeatures)
1700 if (sf.links != null)
1702 for (String link : sf.links)
1709 PopupMenu pop = new PopupMenu(ap, null, links);
1710 pop.show(this, evt.getX(), evt.getY());
1719 public void doMouseReleasedDefineMode(MouseEvent evt)
1721 if (stretchGroup == null)
1725 // always do this - annotation has own state
1726 // but defer colourscheme update until hidden sequences are passed in
1727 boolean vischange = stretchGroup.recalcConservation(true);
1728 needOverviewUpdate |= vischange && editingDefinedGroup;
1729 if (stretchGroup.cs != null)
1731 stretchGroup.cs.alignmentChanged(stretchGroup,
1732 av.getHiddenRepSequences());
1734 if (stretchGroup.cs.conservationApplied())
1736 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1737 stretchGroup.getName());
1741 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1742 stretchGroup.getName());
1745 PaintRefresher.Refresh(this, av.getSequenceSetId());
1746 ap.paintAlignment(needOverviewUpdate);
1747 needOverviewUpdate =false;
1748 editingDefinedGroup = false;
1749 changeEndRes = false;
1750 changeStartRes = false;
1751 stretchGroup = null;
1761 public void doMouseDraggedDefineMode(MouseEvent evt)
1763 int res = findRes(evt);
1764 int y = findSeq(evt);
1766 if (wrappedBlock != startWrapBlock)
1771 if (stretchGroup == null)
1776 if (res >= av.getAlignment().getWidth())
1778 res = av.getAlignment().getWidth() - 1;
1781 if (stretchGroup.getEndRes() == res)
1783 // Edit end res position of selected group
1784 changeEndRes = true;
1786 else if (stretchGroup.getStartRes() == res)
1788 // Edit start res position of selected group
1789 changeStartRes = true;
1792 if (res < av.getStartRes())
1794 res = av.getStartRes();
1799 if (res > (stretchGroup.getStartRes() - 1))
1801 stretchGroup.setEndRes(res);
1802 needOverviewUpdate |= editingDefinedGroup;
1805 else if (changeStartRes)
1807 if (res < (stretchGroup.getEndRes() + 1))
1809 stretchGroup.setStartRes(res);
1810 needOverviewUpdate |= editingDefinedGroup;
1814 int dragDirection = 0;
1820 else if (y < oldSeq)
1825 while ((y != oldSeq) && (oldSeq > -1)
1826 && (y < av.getAlignment().getHeight()))
1828 // This routine ensures we don't skip any sequences, as the
1829 // selection is quite slow.
1830 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1832 oldSeq += dragDirection;
1839 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1841 if (stretchGroup.getSequences(null).contains(nextSeq))
1843 stretchGroup.deleteSequence(seq, false);
1844 needOverviewUpdate |= editingDefinedGroup;
1850 stretchGroup.addSequence(seq, false);
1853 stretchGroup.addSequence(nextSeq, false);
1854 needOverviewUpdate |= editingDefinedGroup;
1863 mouseDragging = true;
1865 if (scrollThread != null)
1867 scrollThread.setEvent(evt);
1870 seqCanvas.repaint();
1873 void scrollCanvas(MouseEvent evt)
1877 if (scrollThread != null)
1879 scrollThread.running = false;
1880 scrollThread = null;
1882 mouseDragging = false;
1886 if (scrollThread == null)
1888 scrollThread = new ScrollThread();
1891 mouseDragging = true;
1892 scrollThread.setEvent(evt);
1897 // this class allows scrolling off the bottom of the visible alignment
1898 class ScrollThread extends Thread
1902 boolean running = false;
1904 public ScrollThread()
1909 public void setEvent(MouseEvent e)
1914 public void stopScrolling()
1928 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1930 running = ap.scrollUp(true);
1933 if (mouseDragging && (evt.getY() >= getHeight())
1934 && (av.getAlignment().getHeight() > av.getEndSeq()))
1936 running = ap.scrollUp(false);
1939 if (mouseDragging && (evt.getX() < 0))
1941 running = ap.scrollRight(false);
1943 else if (mouseDragging && (evt.getX() >= getWidth()))
1945 running = ap.scrollRight(true);
1952 } catch (Exception ex)
1960 * modify current selection according to a received message.
1963 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1964 SelectionSource source)
1966 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1967 // handles selection messages...
1968 // TODO: extend config options to allow user to control if selections may be
1969 // shared between viewports.
1970 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1971 .getSequenceSetId().equals(av.getSequenceSetId())));
1972 if (iSentTheSelection || !av.followSelection)
1978 * Ignore the selection if there is one of our own pending.
1980 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1986 * Check for selection in a view of which this one is a dna/protein
1989 if (selectionFromTranslation(seqsel, colsel, source))
1994 // do we want to thread this ? (contention with seqsel and colsel locks, I
1997 * only copy colsel if there is a real intersection between
1998 * sequence selection and this panel's alignment
2000 boolean repaint = false;
2001 boolean copycolsel = false;
2003 SequenceGroup sgroup = null;
2004 if (seqsel != null && seqsel.getSize() > 0)
2006 if (av.getAlignment() == null)
2008 Cache.log.warn("alignviewport av SeqSetId="
2009 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
2010 + " 's alignment is NULL! returning immediately.");
2013 sgroup = seqsel.intersect(av.getAlignment(),
2014 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
2015 if ((sgroup != null && sgroup.getSize() > 0))
2020 if (sgroup != null && sgroup.getSize() > 0)
2022 av.setSelectionGroup(sgroup);
2026 av.setSelectionGroup(null);
2028 av.isSelectionGroupChanged(true);
2033 // the current selection is unset or from a previous message
2034 // so import the new colsel.
2035 if (colsel == null || colsel.isEmpty())
2037 if (av.getColumnSelection() != null)
2039 av.getColumnSelection().clear();
2045 // TODO: shift colSel according to the intersecting sequences
2046 if (av.getColumnSelection() == null)
2048 av.setColumnSelection(new ColumnSelection(colsel));
2052 av.getColumnSelection().setElementsFrom(colsel);
2055 av.isColSelChanged(true);
2060 && av.hasHiddenColumns()
2061 && (av.getColumnSelection() == null || av.getColumnSelection()
2062 .getHiddenColumns() == null))
2064 System.err.println("Bad things");
2066 if (repaint) // always true!
2068 // probably finessing with multiple redraws here
2069 PaintRefresher.Refresh(this, av.getSequenceSetId());
2070 // ap.paintAlignment(false);
2075 * If this panel is a cdna/protein translation view of the selection source,
2076 * tries to map the source selection to a local one, and returns true. Else
2083 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2084 ColumnSelection colsel, SelectionSource source)
2086 if (!(source instanceof AlignViewportI))
2090 final AlignViewportI sourceAv = (AlignViewportI) source;
2091 if (sourceAv.getCodingComplement() != av
2092 && av.getCodingComplement() != sourceAv)
2098 * Map sequence selection
2100 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2101 av.setSelectionGroup(sg);
2102 av.isSelectionGroupChanged(true);
2105 * Map column selection
2107 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2109 av.setColumnSelection(cs);
2111 PaintRefresher.Refresh(this, av.getSequenceSetId());