2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.BorderLayout;
24 import java.awt.Color;
26 import java.awt.FontMetrics;
27 import java.awt.Point;
28 import java.awt.event.MouseEvent;
29 import java.awt.event.MouseListener;
30 import java.awt.event.MouseMotionListener;
31 import java.awt.event.MouseWheelEvent;
32 import java.awt.event.MouseWheelListener;
33 import java.util.List;
34 import java.util.Vector;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.ToolTipManager;
40 import jalview.api.AlignViewportI;
41 import jalview.commands.EditCommand;
42 import jalview.commands.EditCommand.Action;
43 import jalview.commands.EditCommand.Edit;
44 import jalview.datamodel.ColumnSelection;
45 import jalview.datamodel.SearchResults;
46 import jalview.datamodel.SearchResults.Match;
47 import jalview.datamodel.Sequence;
48 import jalview.datamodel.SequenceFeature;
49 import jalview.datamodel.SequenceGroup;
50 import jalview.datamodel.SequenceI;
51 import jalview.io.SequenceAnnotationReport;
52 import jalview.schemes.ResidueProperties;
53 import jalview.structure.SelectionListener;
54 import jalview.structure.SelectionSource;
55 import jalview.structure.SequenceListener;
56 import jalview.structure.StructureSelectionManager;
57 import jalview.structure.VamsasSource;
58 import jalview.util.Comparison;
59 import jalview.util.MappingUtils;
60 import jalview.util.MessageManager;
61 import jalview.viewmodel.AlignmentViewport;
67 * @version $Revision: 1.130 $
69 public class SeqPanel extends JPanel implements MouseListener,
70 MouseMotionListener, MouseWheelListener, SequenceListener,
75 public SeqCanvas seqCanvas;
78 public AlignmentPanel ap;
80 protected int lastres;
82 protected int startseq;
84 protected AlignViewport av;
86 ScrollThread scrollThread = null;
88 boolean mouseDragging = false;
90 boolean editingSeqs = false;
92 boolean groupEditing = false;
94 // ////////////////////////////////////////
95 // ///Everything below this is for defining the boundary of the rubberband
96 // ////////////////////////////////////////
99 boolean changeEndSeq = false;
101 boolean changeStartSeq = false;
103 boolean changeEndRes = false;
105 boolean changeStartRes = false;
107 SequenceGroup stretchGroup = null;
109 boolean remove = false;
111 Point lastMousePress;
113 boolean mouseWheelPressed = false;
115 StringBuffer keyboardNo1;
117 StringBuffer keyboardNo2;
119 java.net.URL linkImageURL;
121 private final SequenceAnnotationReport seqARep;
123 StringBuffer tooltipText = new StringBuffer();
127 EditCommand editCommand;
129 StructureSelectionManager ssm;
132 * Creates a new SeqPanel object.
139 public SeqPanel(AlignViewport av, AlignmentPanel ap)
141 linkImageURL = getClass().getResource("/images/link.gif");
142 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
143 ToolTipManager.sharedInstance().registerComponent(this);
144 ToolTipManager.sharedInstance().setInitialDelay(0);
145 ToolTipManager.sharedInstance().setDismissDelay(10000);
147 setBackground(Color.white);
149 seqCanvas = new SeqCanvas(ap);
150 setLayout(new BorderLayout());
151 add(seqCanvas, BorderLayout.CENTER);
157 addMouseMotionListener(this);
158 addMouseListener(this);
159 addMouseWheelListener(this);
160 ssm = av.getStructureSelectionManager();
161 ssm.addStructureViewerListener(this);
162 ssm.addSelectionListener(this);
166 int startWrapBlock = -1;
168 int wrappedBlock = -1;
171 * Returns the aligned sequence position (base 0) at the mouse position, or
172 * the closest visible one
177 int findRes(MouseEvent evt)
182 if (av.getWrapAlignment())
185 int hgap = av.getCharHeight();
186 if (av.getScaleAboveWrapped())
188 hgap += av.getCharHeight();
191 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
193 + seqCanvas.getAnnotationHeight();
197 x -= seqCanvas.LABEL_WEST;
199 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
205 wrappedBlock = y / cHeight;
206 wrappedBlock += av.getStartRes() / cwidth;
208 res = wrappedBlock * cwidth + x / av.getCharWidth();
213 if (x > seqCanvas.getX() + seqCanvas.getWidth())
215 // make sure we calculate relative to visible alignment, rather than
217 x = seqCanvas.getX() + seqCanvas.getWidth();
219 res = (x / av.getCharWidth()) + av.getStartRes();
220 if (res > av.getEndRes())
223 res = av.getEndRes();
227 if (av.hasHiddenColumns())
229 res = av.getColumnSelection().adjustForHiddenColumns(res);
236 int findSeq(MouseEvent evt)
241 if (av.getWrapAlignment())
243 int hgap = av.getCharHeight();
244 if (av.getScaleAboveWrapped())
246 hgap += av.getCharHeight();
249 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
251 + seqCanvas.getAnnotationHeight();
255 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
260 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
261 .getAlignment().getHeight() - 1);
268 * When all of a sequence of edits are complete, put the resulting edit list
269 * on the history stack (undo list), and reset flags for editing in progress.
275 if (editCommand != null && editCommand.getSize() > 0)
277 ap.alignFrame.addHistoryItem(editCommand);
278 av.firePropertyChange("alignment", null, av.getAlignment()
284 * Tidy up come what may...
289 groupEditing = false;
298 seqCanvas.cursorY = getKeyboardNo1() - 1;
302 void setCursorColumn()
304 seqCanvas.cursorX = getKeyboardNo1() - 1;
308 void setCursorRowAndColumn()
310 if (keyboardNo2 == null)
312 keyboardNo2 = new StringBuffer();
316 seqCanvas.cursorX = getKeyboardNo1() - 1;
317 seqCanvas.cursorY = getKeyboardNo2() - 1;
322 void setCursorPosition()
324 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
326 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
330 void moveCursor(int dx, int dy)
332 seqCanvas.cursorX += dx;
333 seqCanvas.cursorY += dy;
334 if (av.hasHiddenColumns()
335 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
337 int original = seqCanvas.cursorX - dx;
338 int maxWidth = av.getAlignment().getWidth();
340 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
341 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
343 seqCanvas.cursorX += dx;
346 if (seqCanvas.cursorX >= maxWidth
347 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
349 seqCanvas.cursorX = original;
356 void scrollToVisible()
358 if (seqCanvas.cursorX < 0)
360 seqCanvas.cursorX = 0;
362 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
364 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
367 if (seqCanvas.cursorY < 0)
369 seqCanvas.cursorY = 0;
371 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
373 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
377 if (av.getWrapAlignment())
379 ap.scrollToWrappedVisible(seqCanvas.cursorX);
383 while (seqCanvas.cursorY < av.startSeq)
387 while (seqCanvas.cursorY + 1 > av.endSeq)
391 if (!av.getWrapAlignment())
393 while (seqCanvas.cursorX < av.getColumnSelection()
394 .adjustForHiddenColumns(av.startRes))
396 if (!ap.scrollRight(false))
401 while (seqCanvas.cursorX > av.getColumnSelection()
402 .adjustForHiddenColumns(av.endRes))
404 if (!ap.scrollRight(true))
411 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
412 seqCanvas.cursorX, seqCanvas.cursorY);
417 void setSelectionAreaAtCursor(boolean topLeft)
419 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
421 if (av.getSelectionGroup() != null)
423 SequenceGroup sg = av.getSelectionGroup();
424 // Find the top and bottom of this group
425 int min = av.getAlignment().getHeight(), max = 0;
426 for (int i = 0; i < sg.getSize(); i++)
428 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
443 sg.setStartRes(seqCanvas.cursorX);
444 if (sg.getEndRes() < seqCanvas.cursorX)
446 sg.setEndRes(seqCanvas.cursorX);
449 min = seqCanvas.cursorY;
453 sg.setEndRes(seqCanvas.cursorX);
454 if (sg.getStartRes() > seqCanvas.cursorX)
456 sg.setStartRes(seqCanvas.cursorX);
459 max = seqCanvas.cursorY + 1;
464 // Only the user can do this
465 av.setSelectionGroup(null);
469 // Now add any sequences between min and max
470 sg.getSequences(null).clear();
471 for (int i = min; i < max; i++)
473 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
478 if (av.getSelectionGroup() == null)
480 SequenceGroup sg = new SequenceGroup();
481 sg.setStartRes(seqCanvas.cursorX);
482 sg.setEndRes(seqCanvas.cursorX);
483 sg.addSequence(sequence, false);
484 av.setSelectionGroup(sg);
487 ap.paintAlignment(false);
491 void insertGapAtCursor(boolean group)
493 groupEditing = group;
494 startseq = seqCanvas.cursorY;
495 lastres = seqCanvas.cursorX;
496 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
500 void deleteGapAtCursor(boolean group)
502 groupEditing = group;
503 startseq = seqCanvas.cursorY;
504 lastres = seqCanvas.cursorX + getKeyboardNo1();
505 editSequence(false, false, seqCanvas.cursorX);
509 void insertNucAtCursor(boolean group, String nuc)
511 groupEditing = group;
512 startseq = seqCanvas.cursorY;
513 lastres = seqCanvas.cursorX;
514 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
518 void numberPressed(char value)
520 if (keyboardNo1 == null)
522 keyboardNo1 = new StringBuffer();
525 if (keyboardNo2 != null)
527 keyboardNo2.append(value);
531 keyboardNo1.append(value);
539 if (keyboardNo1 != null)
541 int value = Integer.parseInt(keyboardNo1.toString());
545 } catch (Exception x)
556 if (keyboardNo2 != null)
558 int value = Integer.parseInt(keyboardNo2.toString());
562 } catch (Exception x)
576 public void mouseReleased(MouseEvent evt)
578 mouseDragging = false;
579 mouseWheelPressed = false;
583 doMouseReleasedDefineMode(evt);
597 public void mousePressed(MouseEvent evt)
599 lastMousePress = evt.getPoint();
601 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
603 mouseWheelPressed = true;
607 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
609 if (evt.isAltDown() || evt.isControlDown())
617 doMousePressedDefineMode(evt);
621 int seq = findSeq(evt);
622 int res = findRes(evt);
624 if (seq < 0 || res < 0)
629 if ((seq < av.getAlignment().getHeight())
630 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
647 public void mouseOverSequence(SequenceI sequence, int index, int pos)
649 String tmp = sequence.hashCode() + " " + index + " " + pos;
651 if (lastMessage == null || !lastMessage.equals(tmp))
653 // System.err.println("mouseOver Sequence: "+tmp);
654 ssm.mouseOverSequence(sequence, index, pos, av);
660 public void highlightSequence(SearchResults results)
662 if (av.isFollowHighlight())
665 * if scrollToPosition requires a scroll adjustment, this flag prevents
666 * another scroll event being propagated back to the originator
668 * @see AlignmentPanel#adjustmentValueChanged
670 ap.setFollowingComplementScroll(true);
671 if (ap.scrollToPosition(results, false))
673 seqCanvas.revalidate();
676 setStatusMessage(results);
677 seqCanvas.highlightSearchResults(results);
681 public VamsasSource getVamsasSource()
683 return this.ap == null ? null : this.ap.av;
686 public void updateColours(SequenceI seq, int index)
688 System.out.println("update the seqPanel colours");
699 public void mouseMoved(MouseEvent evt)
703 // This is because MacOSX creates a mouseMoved
704 // If control is down, other platforms will not.
708 int res = findRes(evt);
709 int seq = findSeq(evt);
711 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
716 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
718 if (res >= sequence.getLength())
723 pos = setStatusMessage(sequence, res, seq);
724 if (ssm != null && pos > -1)
726 mouseOverSequence(sequence, res, pos);
729 tooltipText.setLength(6); // Cuts the buffer back to <html>
731 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
734 for (int g = 0; g < groups.length; g++)
736 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
738 if (!groups[g].getName().startsWith("JTreeGroup")
739 && !groups[g].getName().startsWith("JGroup"))
741 tooltipText.append(groups[g].getName());
744 if (groups[g].getDescription() != null)
746 tooltipText.append(": " + groups[g].getDescription());
752 // use aa to see if the mouse pointer is on a
753 if (av.isShowSequenceFeatures())
756 List<SequenceFeature> features = ap.getFeatureRenderer().findFeaturesAtRes(
757 sequence.getDatasetSequence(),
758 rpos = sequence.findPosition(res));
759 seqARep.appendFeatures(tooltipText, rpos, features,
760 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
762 if (tooltipText.length() == 6) // <html></html>
764 setToolTipText(null);
769 if (lastTooltip == null
770 || !lastTooltip.equals(tooltipText.toString()))
772 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
773 tooltipText.toString());
774 // String formatedTooltipText = tooltipText.toString();
775 setToolTipText(formatedTooltipText);
776 lastTooltip = tooltipText.toString();
783 private Point lastp = null;
788 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
790 public Point getToolTipLocation(MouseEvent event)
792 int x = event.getX(), w = getWidth();
793 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
796 if (!event.isShiftDown() || p == null)
798 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
799 event.getX() + wdth, event.getY() - 20) : null;
802 * TODO: try to modify position region is not obcured by tooltip
810 * Set status message in alignment panel
813 * aligned sequence object
817 * index of sequence in alignment
818 * @return position of res in sequence
820 int setStatusMessage(SequenceI sequence, int res, int seq)
822 StringBuilder text = new StringBuilder(32);
825 * Sequence number (if known), and sequence name.
827 String seqno = seq == -1 ? "" : " " + (seq + 1);
828 text.append("Sequence" + seqno + " ID: " + sequence.getName());
830 String residue = null;
832 * Try to translate the display character to residue name (null for gap).
834 final String displayChar = String.valueOf(sequence.getCharAt(res));
835 if (av.getAlignment().isNucleotide())
837 residue = ResidueProperties.nucleotideName.get(displayChar);
840 text.append(" Nucleotide: ").append(residue);
845 residue = "X".equalsIgnoreCase(displayChar) ? "X"
846 : ResidueProperties.aa2Triplet.get(displayChar);
849 text.append(" Residue: ").append(residue);
856 pos = sequence.findPosition(res);
857 text.append(" (").append(Integer.toString(pos)).append(")");
859 ap.alignFrame.statusBar.setText(text.toString());
864 * Set the status bar message to highlight the first matched position in
869 private void setStatusMessage(SearchResults results)
871 List<Match> matches = results.getResults();
872 if (!matches.isEmpty())
874 Match m = matches.get(0);
875 SequenceI seq = m.getSequence();
876 int sequenceIndex = this.av.getAlignment().findIndex(seq);
879 * Convert position in sequence (base 1) to sequence character array index
882 int start = m.getStart() - 1;
883 setStatusMessage(seq, start, sequenceIndex);
894 public void mouseDragged(MouseEvent evt)
896 if (mouseWheelPressed)
898 int oldWidth = av.getCharWidth();
900 // Which is bigger, left-right or up-down?
901 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
902 .getX() - lastMousePress.getX()))
904 int fontSize = av.font.getSize();
906 if (evt.getY() < lastMousePress.getY())
910 else if (evt.getY() > lastMousePress.getY())
921 new Font(av.font.getName(), av.font.getStyle(), fontSize),
923 av.setCharWidth(oldWidth);
928 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
930 av.setCharWidth(av.getCharWidth() - 1);
932 else if (evt.getX() > lastMousePress.getX())
934 av.setCharWidth(av.getCharWidth() + 1);
937 ap.paintAlignment(false);
940 FontMetrics fm = getFontMetrics(av.getFont());
941 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
943 lastMousePress = evt.getPoint();
950 doMouseDraggedDefineMode(evt);
954 int res = findRes(evt);
961 if ((lastres == -1) || (lastres == res))
966 if ((res < av.getAlignment().getWidth()) && (res < lastres))
968 // dragLeft, delete gap
969 editSequence(false, false, res);
973 editSequence(true, false, res);
976 mouseDragging = true;
977 if (scrollThread != null)
979 scrollThread.setEvent(evt);
983 // TODO: Make it more clever than many booleans
984 synchronized void editSequence(boolean insertGap, boolean editSeq,
989 boolean fixedColumns = false;
990 SequenceGroup sg = av.getSelectionGroup();
992 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
994 // No group, but the sequence may represent a group
995 if (!groupEditing && av.hasHiddenRows())
997 if (av.isHiddenRepSequence(seq))
999 sg = av.getRepresentedSequences(seq);
1000 groupEditing = true;
1004 StringBuilder message = new StringBuilder(64);
1007 message.append("Edit group:");
1008 if (editCommand == null)
1010 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
1015 message.append("Edit sequence: " + seq.getName());
1016 String label = seq.getName();
1017 if (label.length() > 10)
1019 label = label.substring(0, 10);
1021 if (editCommand == null)
1023 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
1029 message.append(" insert ");
1033 message.append(" delete ");
1036 message.append(Math.abs(startres - lastres) + " gaps.");
1037 ap.alignFrame.statusBar.setText(message.toString());
1039 // Are we editing within a selection group?
1041 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1044 fixedColumns = true;
1046 // sg might be null as the user may only see 1 sequence,
1047 // but the sequence represents a group
1050 if (!av.isHiddenRepSequence(seq))
1055 sg = av.getRepresentedSequences(seq);
1058 fixedLeft = sg.getStartRes();
1059 fixedRight = sg.getEndRes();
1061 if ((startres < fixedLeft && lastres >= fixedLeft)
1062 || (startres >= fixedLeft && lastres < fixedLeft)
1063 || (startres > fixedRight && lastres <= fixedRight)
1064 || (startres <= fixedRight && lastres > fixedRight))
1070 if (fixedLeft > startres)
1072 fixedRight = fixedLeft - 1;
1075 else if (fixedRight < startres)
1077 fixedLeft = fixedRight;
1082 if (av.hasHiddenColumns())
1084 fixedColumns = true;
1085 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1086 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1088 if ((insertGap && startres > y1 && lastres < y1)
1089 || (!insertGap && startres < y2 && lastres > y2))
1095 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1096 // Selection spans a hidden region
1097 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1105 fixedRight = y2 - 1;
1112 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1113 int g, groupSize = vseqs.size();
1114 SequenceI[] groupSeqs = new SequenceI[groupSize];
1115 for (g = 0; g < groupSeqs.length; g++)
1117 groupSeqs[g] = vseqs.get(g);
1123 // If the user has selected the whole sequence, and is dragging to
1124 // the right, we can still extend the alignment and selectionGroup
1125 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1126 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1128 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1129 fixedRight = sg.getEndRes();
1132 // Is it valid with fixed columns??
1133 // Find the next gap before the end
1134 // of the visible region boundary
1135 boolean blank = false;
1136 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1140 for (g = 0; g < groupSize; g++)
1142 for (int j = 0; j < startres - lastres; j++)
1144 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1145 .getCharAt(fixedRight - j)))
1160 if (sg.getSize() == av.getAlignment().getHeight())
1162 if ((av.hasHiddenColumns() && startres < av
1163 .getColumnSelection().getHiddenBoundaryRight(startres)))
1169 int alWidth = av.getAlignment().getWidth();
1170 if (av.hasHiddenRows())
1172 int hwidth = av.getAlignment().getHiddenSequences()
1174 if (hwidth > alWidth)
1179 // We can still insert gaps if the selectionGroup
1180 // contains all the sequences
1181 sg.setEndRes(sg.getEndRes() + startres - lastres);
1182 fixedRight = alWidth + startres - lastres;
1193 else if (!insertGap)
1195 // / Are we able to delete?
1196 // ie are all columns blank?
1198 for (g = 0; g < groupSize; g++)
1200 for (int j = startres; j < lastres; j++)
1202 if (groupSeqs[g].getLength() <= j)
1207 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1209 // Not a gap, block edit not valid
1219 // dragging to the right
1220 if (fixedColumns && fixedRight != -1)
1222 for (int j = lastres; j < startres; j++)
1224 insertChar(j, groupSeqs, fixedRight);
1229 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1235 // dragging to the left
1236 if (fixedColumns && fixedRight != -1)
1238 for (int j = lastres; j > startres; j--)
1240 deleteChar(startres, groupSeqs, fixedRight);
1245 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1252 // ///Editing a single sequence///////////
1256 // dragging to the right
1257 if (fixedColumns && fixedRight != -1)
1259 for (int j = lastres; j < startres; j++)
1261 insertChar(j, new SequenceI[]
1262 { seq }, fixedRight);
1267 appendEdit(Action.INSERT_GAP, new SequenceI[]
1268 { seq }, lastres, startres - lastres);
1275 // dragging to the left
1276 if (fixedColumns && fixedRight != -1)
1278 for (int j = lastres; j > startres; j--)
1280 if (!Comparison.isGap(seq.getCharAt(startres)))
1285 deleteChar(startres, new SequenceI[]
1286 { seq }, fixedRight);
1291 // could be a keyboard edit trying to delete none gaps
1293 for (int m = startres; m < lastres; m++)
1295 if (!Comparison.isGap(seq.getCharAt(m)))
1304 appendEdit(Action.DELETE_GAP, new SequenceI[]
1305 { seq }, startres, max);
1310 {// insertGap==false AND editSeq==TRUE;
1311 if (fixedColumns && fixedRight != -1)
1313 for (int j = lastres; j < startres; j++)
1315 insertChar(j, new SequenceI[]
1316 { seq }, fixedRight);
1321 appendEdit(Action.INSERT_NUC, new SequenceI[]
1322 { seq }, lastres, startres - lastres);
1329 seqCanvas.repaint();
1332 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1334 int blankColumn = fixedColumn;
1335 for (int s = 0; s < seq.length; s++)
1337 // Find the next gap before the end of the visible region boundary
1338 // If lastCol > j, theres a boundary after the gap insertion
1340 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1342 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1344 // Theres a space, so break and insert the gap
1349 if (blankColumn <= j)
1351 blankColumn = fixedColumn;
1357 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1359 appendEdit(Action.INSERT_GAP, seq, j, 1);
1364 * Helper method to add and perform one edit action.
1371 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1375 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1376 av.getAlignment().getGapCharacter());
1378 editCommand.appendEdit(edit, av.getAlignment(),
1382 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1385 appendEdit(Action.DELETE_GAP, seq, j, 1);
1387 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1397 public void mouseEntered(MouseEvent e)
1404 if (scrollThread != null)
1406 scrollThread.running = false;
1407 scrollThread = null;
1418 public void mouseExited(MouseEvent e)
1420 if (av.getWrapAlignment())
1427 scrollThread = new ScrollThread();
1432 public void mouseClicked(MouseEvent evt)
1434 SequenceGroup sg = null;
1435 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1436 if (evt.getClickCount() > 1)
1438 sg = av.getSelectionGroup();
1439 if (sg != null && sg.getSize() == 1
1440 && sg.getEndRes() - sg.getStartRes() < 2)
1442 av.setSelectionGroup(null);
1445 List<SequenceFeature> features = seqCanvas.getFeatureRenderer().findFeaturesAtRes(
1446 sequence.getDatasetSequence(),
1447 sequence.findPosition(findRes(evt)));
1449 if (features != null && features.size()> 0)
1451 SearchResults highlight = new SearchResults();
1452 highlight.addResult(sequence, features.get(0).getBegin(),
1453 features.get(0).getEnd());
1454 seqCanvas.highlightSearchResults(highlight);
1456 if (features != null && features.size()> 0)
1458 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1459 { sequence }, features.toArray(new SequenceFeature[features.size()]), false, ap);
1461 seqCanvas.highlightSearchResults(null);
1467 public void mouseWheelMoved(MouseWheelEvent e)
1470 if (e.getWheelRotation() > 0)
1472 if (e.isShiftDown())
1474 ap.scrollRight(true);
1484 if (e.isShiftDown())
1486 ap.scrollRight(false);
1493 // TODO Update tooltip for new position.
1502 public void doMousePressedDefineMode(MouseEvent evt)
1504 int res = findRes(evt);
1505 int seq = findSeq(evt);
1508 startWrapBlock = wrappedBlock;
1510 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1512 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1513 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1514 MessageManager.getString("label.wrapped_view_no_edit"),
1515 JOptionPane.WARNING_MESSAGE);
1519 if (seq < 0 || res < 0)
1524 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1526 if ((sequence == null) || (res > sequence.getLength()))
1531 stretchGroup = av.getSelectionGroup();
1533 if (stretchGroup == null)
1535 stretchGroup = av.getAlignment().findGroup(sequence);
1537 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1538 && (res < stretchGroup.getEndRes()))
1540 av.setSelectionGroup(stretchGroup);
1544 stretchGroup = null;
1547 else if (!stretchGroup.getSequences(null).contains(sequence)
1548 || (stretchGroup.getStartRes() > res)
1549 || (stretchGroup.getEndRes() < res))
1551 stretchGroup = null;
1553 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1555 if (allGroups != null)
1557 for (int i = 0; i < allGroups.length; i++)
1559 if ((allGroups[i].getStartRes() <= res)
1560 && (allGroups[i].getEndRes() >= res))
1562 stretchGroup = allGroups[i];
1568 av.setSelectionGroup(stretchGroup);
1572 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1574 List<SequenceFeature> allFeatures = ap.getFeatureRenderer().findFeaturesAtRes(
1575 sequence.getDatasetSequence(), sequence.findPosition(res));
1576 Vector links = new Vector();
1577 for (SequenceFeature sf:allFeatures)
1579 if (sf.links != null)
1581 for (int j = 0; j < sf.links.size(); j++)
1583 links.addElement(sf.links.elementAt(j));
1588 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1589 pop.show(this, evt.getX(), evt.getY());
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);
1614 if (av.getConservationSelected())
1616 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1620 if (av.getAbovePIDThreshold())
1622 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
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();
1648 public void doMouseReleasedDefineMode(MouseEvent evt)
1650 if (stretchGroup == null)
1655 stretchGroup.recalcConservation(); // always do this - annotation has own
1657 if (stretchGroup.cs != null)
1659 stretchGroup.cs.alignmentChanged(stretchGroup,
1660 av.getHiddenRepSequences());
1662 if (stretchGroup.cs.conservationApplied())
1664 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1665 stretchGroup.getName());
1669 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1670 stretchGroup.getName());
1673 PaintRefresher.Refresh(this, av.getSequenceSetId());
1674 ap.paintAlignment(true);
1676 changeEndRes = false;
1677 changeStartRes = false;
1678 stretchGroup = null;
1688 public void doMouseDraggedDefineMode(MouseEvent evt)
1690 int res = findRes(evt);
1691 int y = findSeq(evt);
1693 if (wrappedBlock != startWrapBlock)
1698 if (stretchGroup == null)
1703 if (res >= av.getAlignment().getWidth())
1705 res = av.getAlignment().getWidth() - 1;
1708 if (stretchGroup.getEndRes() == res)
1710 // Edit end res position of selected group
1711 changeEndRes = true;
1713 else if (stretchGroup.getStartRes() == res)
1715 // Edit start res position of selected group
1716 changeStartRes = true;
1719 if (res < av.getStartRes())
1721 res = av.getStartRes();
1726 if (res > (stretchGroup.getStartRes() - 1))
1728 stretchGroup.setEndRes(res);
1731 else if (changeStartRes)
1733 if (res < (stretchGroup.getEndRes() + 1))
1735 stretchGroup.setStartRes(res);
1739 int dragDirection = 0;
1745 else if (y < oldSeq)
1750 while ((y != oldSeq) && (oldSeq > -1)
1751 && (y < av.getAlignment().getHeight()))
1753 // This routine ensures we don't skip any sequences, as the
1754 // selection is quite slow.
1755 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1757 oldSeq += dragDirection;
1764 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1766 if (stretchGroup.getSequences(null).contains(nextSeq))
1768 stretchGroup.deleteSequence(seq, false);
1774 stretchGroup.addSequence(seq, false);
1777 stretchGroup.addSequence(nextSeq, false);
1786 mouseDragging = true;
1788 if (scrollThread != null)
1790 scrollThread.setEvent(evt);
1793 seqCanvas.repaint();
1796 void scrollCanvas(MouseEvent evt)
1800 if (scrollThread != null)
1802 scrollThread.running = false;
1803 scrollThread = null;
1805 mouseDragging = false;
1809 if (scrollThread == null)
1811 scrollThread = new ScrollThread();
1814 mouseDragging = true;
1815 scrollThread.setEvent(evt);
1820 // this class allows scrolling off the bottom of the visible alignment
1821 class ScrollThread extends Thread
1825 boolean running = false;
1827 public ScrollThread()
1832 public void setEvent(MouseEvent e)
1837 public void stopScrolling()
1851 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1853 running = ap.scrollUp(true);
1856 if (mouseDragging && (evt.getY() >= getHeight())
1857 && (av.getAlignment().getHeight() > av.getEndSeq()))
1859 running = ap.scrollUp(false);
1862 if (mouseDragging && (evt.getX() < 0))
1864 running = ap.scrollRight(false);
1866 else if (mouseDragging && (evt.getX() >= getWidth()))
1868 running = ap.scrollRight(true);
1875 } catch (Exception ex)
1883 * modify current selection according to a received message.
1886 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1887 SelectionSource source)
1889 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1890 // handles selection messages...
1891 // TODO: extend config options to allow user to control if selections may be
1892 // shared between viewports.
1893 boolean iSentTheSelection = (av == source
1894 || (source instanceof AlignViewport && ((AlignmentViewport) source)
1895 .getSequenceSetId().equals(av.getSequenceSetId())));
1896 if (iSentTheSelection || !av.followSelection)
1902 * Ignore the selection if there is one of our own pending.
1904 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1910 * Check for selection in a view of which this one is a dna/protein
1913 if (selectionFromTranslation(seqsel, colsel, source))
1918 // do we want to thread this ? (contention with seqsel and colsel locks, I
1920 // rules are: colsel is copied if there is a real intersection between
1921 // sequence selection
1922 boolean repaint = false;
1923 boolean copycolsel = true;
1925 SequenceGroup sgroup = null;
1926 if (seqsel != null && seqsel.getSize() > 0)
1928 if (av.getAlignment() == null)
1930 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1931 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1932 + " 's alignment is NULL! returning immediately.");
1935 sgroup = seqsel.intersect(av.getAlignment(),
1936 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1937 if ((sgroup == null || sgroup.getSize() == 0)
1938 || (colsel == null || colsel.size() == 0))
1940 // don't copy columns if the region didn't intersect.
1944 if (sgroup != null && sgroup.getSize() > 0)
1946 av.setSelectionGroup(sgroup);
1950 av.setSelectionGroup(null);
1952 av.isSelectionGroupChanged(true);
1957 // the current selection is unset or from a previous message
1958 // so import the new colsel.
1959 if (colsel == null || colsel.size() == 0)
1961 if (av.getColumnSelection() != null)
1963 av.getColumnSelection().clear();
1969 // TODO: shift colSel according to the intersecting sequences
1970 if (av.getColumnSelection() == null)
1972 av.setColumnSelection(new ColumnSelection(colsel));
1976 av.getColumnSelection().setElementsFrom(colsel);
1979 av.isColSelChanged(true);
1984 && av.hasHiddenColumns()
1985 && (av.getColumnSelection() == null || av.getColumnSelection()
1986 .getHiddenColumns() == null))
1988 System.err.println("Bad things");
1990 if (repaint) // always true!
1992 // probably finessing with multiple redraws here
1993 PaintRefresher.Refresh(this, av.getSequenceSetId());
1994 // ap.paintAlignment(false);
1999 * If this panel is a cdna/protein translation view of the selection source,
2000 * tries to map the source selection to a local one, and returns true. Else
2007 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2008 ColumnSelection colsel, SelectionSource source)
2010 if (!(source instanceof AlignViewportI)) {
2013 final AlignViewportI sourceAv = (AlignViewportI) source;
2014 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
2020 * Map sequence selection
2022 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2023 av.setSelectionGroup(sg);
2024 av.isSelectionGroupChanged(true);
2027 * Map column selection
2029 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2031 av.setColumnSelection(cs);
2032 av.isColSelChanged(true);
2034 PaintRefresher.Refresh(this, av.getSequenceSetId());