2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
21 package jalview.appletgui;
23 import jalview.commands.EditCommand;
24 import jalview.commands.EditCommand.Action;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.SearchResults;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.schemes.ResidueProperties;
32 import jalview.structure.SelectionSource;
33 import jalview.structure.SequenceListener;
34 import jalview.structure.StructureSelectionManager;
35 import jalview.util.MessageManager;
37 import java.awt.BorderLayout;
39 import java.awt.FontMetrics;
40 import java.awt.Panel;
41 import java.awt.Point;
42 import java.awt.event.InputEvent;
43 import java.awt.event.MouseEvent;
44 import java.awt.event.MouseListener;
45 import java.awt.event.MouseMotionListener;
46 import java.util.Vector;
48 public class SeqPanel extends Panel implements MouseMotionListener,
49 MouseListener, SequenceListener
52 public SeqCanvas seqCanvas;
54 public AlignmentPanel ap;
56 protected int lastres;
58 protected int startseq;
60 protected AlignViewport av;
62 // if character is inserted or deleted, we will need to recalculate the
64 boolean seqEditOccurred = false;
66 ScrollThread scrollThread = null;
68 boolean mouseDragging = false;
70 boolean editingSeqs = false;
72 boolean groupEditing = false;
76 boolean changeEndSeq = false;
78 boolean changeStartSeq = false;
80 boolean changeEndRes = false;
82 boolean changeStartRes = false;
84 SequenceGroup stretchGroup = null;
86 StringBuffer keyboardNo1;
88 StringBuffer keyboardNo2;
90 boolean mouseWheelPressed = false;
94 EditCommand editCommand;
96 StructureSelectionManager ssm;
98 public SeqPanel(AlignViewport avp, AlignmentPanel p)
102 seqCanvas = new SeqCanvas(avp);
103 setLayout(new BorderLayout());
108 seqCanvas.addMouseMotionListener(this);
109 seqCanvas.addMouseListener(this);
110 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
111 ssm.addStructureViewerListener(this);
118 if (editCommand != null && editCommand.getSize() > 0)
120 ap.alignFrame.addHistoryItem(editCommand);
121 av.firePropertyChange("alignment", null, av.getAlignment()
128 groupEditing = false;
136 seqCanvas.cursorY = getKeyboardNo1() - 1;
140 void setCursorColumn()
142 seqCanvas.cursorX = getKeyboardNo1() - 1;
146 void setCursorRowAndColumn()
148 if (keyboardNo2 == null)
150 keyboardNo2 = new StringBuffer();
154 seqCanvas.cursorX = getKeyboardNo1() - 1;
155 seqCanvas.cursorY = getKeyboardNo2() - 1;
160 void setCursorPosition()
162 SequenceI sequence = av.getAlignment().getSequenceAt(
165 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
169 void moveCursor(int dx, int dy)
171 seqCanvas.cursorX += dx;
172 seqCanvas.cursorY += dy;
173 if (av.hasHiddenColumns()
174 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
176 int original = seqCanvas.cursorX - dx;
177 int maxWidth = av.getAlignment().getWidth();
179 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
180 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
182 seqCanvas.cursorX += dx;
185 if (seqCanvas.cursorX >= maxWidth
186 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
188 seqCanvas.cursorX = original;
194 void scrollToVisible()
196 if (seqCanvas.cursorX < 0)
198 seqCanvas.cursorX = 0;
200 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
202 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
205 if (seqCanvas.cursorY < 0)
207 seqCanvas.cursorY = 0;
209 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
211 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
215 if (av.wrapAlignment)
217 ap.scrollToWrappedVisible(seqCanvas.cursorX);
221 while (seqCanvas.cursorY < av.startSeq)
225 while (seqCanvas.cursorY + 1 > av.endSeq)
229 while (seqCanvas.cursorX < av.getColumnSelection()
230 .adjustForHiddenColumns(av.startRes))
233 if (!ap.scrollRight(false))
238 while (seqCanvas.cursorX > av.getColumnSelection()
239 .adjustForHiddenColumns(av.endRes))
241 if (!ap.scrollRight(true))
247 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
248 seqCanvas.cursorX, seqCanvas.cursorY);
253 void setSelectionAreaAtCursor(boolean topLeft)
255 SequenceI sequence = av.getAlignment().getSequenceAt(
258 if (av.getSelectionGroup() != null)
260 SequenceGroup sg = av.getSelectionGroup();
261 // Find the top and bottom of this group
262 int min = av.getAlignment().getHeight(), max = 0;
263 for (int i = 0; i < sg.getSize(); i++)
265 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
280 sg.setStartRes(seqCanvas.cursorX);
281 if (sg.getEndRes() < seqCanvas.cursorX)
283 sg.setEndRes(seqCanvas.cursorX);
286 min = seqCanvas.cursorY;
290 sg.setEndRes(seqCanvas.cursorX);
291 if (sg.getStartRes() > seqCanvas.cursorX)
293 sg.setStartRes(seqCanvas.cursorX);
296 max = seqCanvas.cursorY + 1;
301 // Only the user can do this
302 av.setSelectionGroup(null);
306 // Now add any sequences between min and max
308 for (int i = min; i < max; i++)
310 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
315 if (av.getSelectionGroup() == null)
317 SequenceGroup sg = new SequenceGroup();
318 sg.setStartRes(seqCanvas.cursorX);
319 sg.setEndRes(seqCanvas.cursorX);
320 sg.addSequence(sequence, false);
321 av.setSelectionGroup(sg);
323 ap.paintAlignment(false);
327 void insertGapAtCursor(boolean group)
329 groupEditing = group;
330 startseq = seqCanvas.cursorY;
331 lastres = seqCanvas.cursorX;
332 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
336 void deleteGapAtCursor(boolean group)
338 groupEditing = group;
339 startseq = seqCanvas.cursorY;
340 lastres = seqCanvas.cursorX + getKeyboardNo1();
341 editSequence(false, seqCanvas.cursorX);
345 void numberPressed(char value)
347 if (keyboardNo1 == null)
349 keyboardNo1 = new StringBuffer();
352 if (keyboardNo2 != null)
354 keyboardNo2.append(value);
358 keyboardNo1.append(value);
366 if (keyboardNo1 != null)
368 int value = Integer.parseInt(keyboardNo1.toString());
372 } catch (Exception x)
383 if (keyboardNo2 != null)
385 int value = Integer.parseInt(keyboardNo2.toString());
389 } catch (Exception x)
396 void setStatusMessage(SequenceI sequence, int res, int seq)
398 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
399 + sequence.getName());
402 if (av.getAlignment().isNucleotide())
404 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
408 text.append(" Nucleotide: ");
413 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
416 text.append(" Residue: ");
425 text.append(obj + " (" + sequence.findPosition(res) + ")");
429 ap.alignFrame.statusBar.setText(text.toString());
433 public void mousePressed(MouseEvent evt)
435 lastMousePress = evt.getPoint();
437 // For now, ignore the mouseWheel font resizing on Macs
438 // As the Button2_mask always seems to be true
439 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
442 mouseWheelPressed = true;
446 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
448 if (evt.isControlDown() || evt.isAltDown())
456 doMousePressedDefineMode(evt);
460 int seq = findSeq(evt);
461 int res = findRes(evt);
463 if (seq < 0 || res < 0)
468 if ((seq < av.getAlignment().getHeight())
469 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
483 public void mouseClicked(MouseEvent evt)
485 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
486 if (evt.getClickCount() > 1)
488 if (av.getSelectionGroup() != null
489 && av.getSelectionGroup().getSize() == 1
490 && av.getSelectionGroup().getEndRes()
491 - av.getSelectionGroup().getStartRes() < 2)
493 av.setSelectionGroup(null);
496 SequenceFeature[] features = findFeaturesAtRes(sequence,
497 sequence.findPosition(findRes(evt)));
499 if (features != null && features.length > 0)
501 SearchResults highlight = new SearchResults();
502 highlight.addResult(sequence, features[0].getBegin(),
503 features[0].getEnd());
504 seqCanvas.highlightSearchResults(highlight);
506 if (features != null && features.length > 0)
508 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
509 { sequence }, features, false, ap);
511 seqCanvas.highlightSearchResults(null);
516 public void mouseReleased(MouseEvent evt)
518 mouseDragging = false;
519 mouseWheelPressed = false;
520 ap.paintAlignment(true);
524 doMouseReleasedDefineMode(evt);
532 int startWrapBlock = -1;
534 int wrappedBlock = -1;
536 int findRes(MouseEvent evt)
541 if (av.wrapAlignment)
544 int hgap = av.charHeight;
545 if (av.scaleAboveWrapped)
547 hgap += av.charHeight;
550 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
551 + seqCanvas.getAnnotationHeight();
555 x -= seqCanvas.LABEL_WEST;
557 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
563 wrappedBlock = y / cHeight;
564 wrappedBlock += av.getStartRes() / cwidth;
566 res = wrappedBlock * cwidth + x / av.getCharWidth();
571 res = (x / av.getCharWidth()) + av.getStartRes();
574 if (av.hasHiddenColumns())
576 res = av.getColumnSelection().adjustForHiddenColumns(res);
583 int findSeq(MouseEvent evt)
585 final int sqnum = findAlRow(evt);
586 return (sqnum < 0) ? 0 : sqnum;
592 * @return row in alignment that was selected (or -1 for column selection)
594 private int findAlRow(MouseEvent evt)
599 if (av.wrapAlignment)
601 int hgap = av.charHeight;
602 if (av.scaleAboveWrapped)
604 hgap += av.charHeight;
607 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
608 + seqCanvas.getAnnotationHeight();
612 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
621 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
622 .getAlignment().getHeight() - 1);
632 public void doMousePressed(MouseEvent evt)
635 int seq = findSeq(evt);
636 int res = findRes(evt);
638 if (seq < av.getAlignment().getHeight()
639 && res < av.getAlignment().getSequenceAt(seq).getLength())
641 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
642 // Find the residue's position in the sequence (res is the position
659 public void mouseOverSequence(SequenceI sequence, int index, int pos)
661 String tmp = sequence.hashCode() + index + "";
662 if (lastMessage == null || !lastMessage.equals(tmp))
664 ssm.mouseOverSequence(sequence, index, pos, av);
670 public void highlightSequence(SearchResults results)
672 if (av.followHighlight)
674 if (ap.scrollToPosition(results, true))
676 ap.alignFrame.repaint();
679 seqCanvas.highlightSearchResults(results);
683 public void updateColours(SequenceI seq, int index)
685 System.out.println("update the seqPanel colours");
689 public void mouseMoved(MouseEvent evt)
691 int res = findRes(evt);
692 int seq = findSeq(evt);
694 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
703 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
704 if (res > sequence.getLength())
713 int respos = sequence.findPosition(res);
716 mouseOverSequence(sequence, res, respos);
719 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
720 + sequence.getName());
723 if (av.getAlignment().isNucleotide())
725 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
729 text.append(" Nucleotide: ");
734 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
737 text.append(" Residue: ");
745 text.append(obj + " (" + respos + ")");
749 ap.alignFrame.statusBar.setText(text.toString());
751 StringBuffer tooltipText = new StringBuffer();
752 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
755 for (int g = 0; g < groups.length; g++)
757 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
759 if (!groups[g].getName().startsWith("JTreeGroup")
760 && !groups[g].getName().startsWith("JGroup"))
762 tooltipText.append(groups[g].getName() + " ");
764 if (groups[g].getDescription() != null)
766 tooltipText.append(groups[g].getDescription());
768 tooltipText.append("\n");
773 // use aa to see if the mouse pointer is on a
774 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
775 sequence.findPosition(res));
778 while (index < allFeatures.length)
780 SequenceFeature sf = allFeatures[index];
782 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
784 if (sf.getDescription() != null)
786 tooltipText.append(" " + sf.getDescription());
789 if (sf.getValue("status") != null)
791 String status = sf.getValue("status").toString();
792 if (status.length() > 0)
794 tooltipText.append(" (" + sf.getValue("status") + ")");
797 tooltipText.append("\n");
804 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
808 tooltip.setTip(tooltipText.toString());
812 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
814 Vector tmp = new Vector();
815 SequenceFeature[] features = sequence.getSequenceFeatures();
816 if (features != null)
818 for (int i = 0; i < features.length; i++)
820 if (av.getFeaturesDisplayed() == null
821 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
826 if (features[i].featureGroup != null
827 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
832 if ((features[i].getBegin() <= res)
833 && (features[i].getEnd() >= res))
835 tmp.addElement(features[i]);
840 features = new SequenceFeature[tmp.size()];
841 tmp.copyInto(features);
848 public void mouseDragged(MouseEvent evt)
850 if (mouseWheelPressed)
852 int oldWidth = av.charWidth;
854 // Which is bigger, left-right or up-down?
855 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
858 int fontSize = av.font.getSize();
860 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
864 else if (evt.getY() > lastMousePress.y)
874 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
875 av.charWidth = oldWidth;
879 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
883 else if (evt.getX() > lastMousePress.x)
888 if (av.charWidth < 1)
896 FontMetrics fm = getFontMetrics(av.getFont());
897 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
899 lastMousePress = evt.getPoint();
901 ap.paintAlignment(false);
902 ap.annotationPanel.image = null;
908 doMouseDraggedDefineMode(evt);
912 int res = findRes(evt);
919 if ((lastres == -1) || (lastres == res))
924 if ((res < av.getAlignment().getWidth()) && (res < lastres))
926 // dragLeft, delete gap
927 editSequence(false, res);
931 editSequence(true, res);
934 mouseDragging = true;
935 if (scrollThread != null)
937 scrollThread.setEvent(evt);
942 synchronized void editSequence(boolean insertGap, int startres)
946 boolean fixedColumns = false;
947 SequenceGroup sg = av.getSelectionGroup();
949 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
951 if (!groupEditing && av.hasHiddenRows())
953 if (av.isHiddenRepSequence(seq))
955 sg = av.getRepresentedSequences(seq);
960 StringBuffer message = new StringBuffer();
963 message.append(MessageManager.getString("action.edit_group")).append(":");
964 if (editCommand == null)
966 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
971 message.append(MessageManager.getString("label.edit_sequence")).append(" " + seq.getName());
972 String label = seq.getName();
973 if (label.length() > 10)
975 label = label.substring(0, 10);
977 if (editCommand == null)
979 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
985 message.append(" insert ");
989 message.append(" delete ");
992 message.append(Math.abs(startres - lastres) + " gaps.");
993 ap.alignFrame.statusBar.setText(message.toString());
995 // Are we editing within a selection group?
997 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1000 fixedColumns = true;
1002 // sg might be null as the user may only see 1 sequence,
1003 // but the sequence represents a group
1006 if (!av.isHiddenRepSequence(seq))
1012 sg = av.getRepresentedSequences(seq);
1015 fixedLeft = sg.getStartRes();
1016 fixedRight = sg.getEndRes();
1018 if ((startres < fixedLeft && lastres >= fixedLeft)
1019 || (startres >= fixedLeft && lastres < fixedLeft)
1020 || (startres > fixedRight && lastres <= fixedRight)
1021 || (startres <= fixedRight && lastres > fixedRight))
1027 if (fixedLeft > startres)
1029 fixedRight = fixedLeft - 1;
1032 else if (fixedRight < startres)
1034 fixedLeft = fixedRight;
1039 if (av.hasHiddenColumns())
1041 fixedColumns = true;
1042 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1043 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1045 if ((insertGap && startres > y1 && lastres < y1)
1046 || (!insertGap && startres < y2 && lastres > y2))
1052 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1053 // Selection spans a hidden region
1054 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1062 fixedRight = y2 - 1;
1069 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1070 .toArray(new SequenceI[0]);
1075 // If the user has selected the whole sequence, and is dragging to
1076 // the right, we can still extend the alignment and selectionGroup
1077 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1078 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1080 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1081 fixedRight = sg.getEndRes();
1084 // Is it valid with fixed columns??
1085 // Find the next gap before the end
1086 // of the visible region boundary
1087 boolean blank = false;
1088 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1092 for (SequenceI gs : groupSeqs)
1094 for (int j = 0; j < startres - lastres; j++)
1096 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1112 if (sg.getSize() == av.getAlignment().getHeight())
1114 if ((av.hasHiddenColumns() && startres < av
1115 .getColumnSelection().getHiddenBoundaryRight(startres)))
1121 int alWidth = av.getAlignment().getWidth();
1122 if (av.hasHiddenRows())
1124 int hwidth = av.getAlignment().getHiddenSequences()
1126 if (hwidth > alWidth)
1131 // We can still insert gaps if the selectionGroup
1132 // contains all the sequences
1133 sg.setEndRes(sg.getEndRes() + startres - lastres);
1134 fixedRight = alWidth + startres - lastres;
1145 else if (!insertGap)
1147 // / Are we able to delete?
1148 // ie are all columns blank?
1150 for (SequenceI gs : groupSeqs)
1152 for (int j = startres; j < lastres; j++)
1154 if (gs.getLength() <= j)
1159 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1161 // Not a gap, block edit not valid
1171 // dragging to the right
1172 if (fixedColumns && fixedRight != -1)
1174 for (int j = lastres; j < startres; j++)
1176 insertChar(j, groupSeqs, fixedRight);
1181 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs,
1182 startres, startres - lastres, av.getAlignment(), true);
1187 // dragging to the left
1188 if (fixedColumns && fixedRight != -1)
1190 for (int j = lastres; j > startres; j--)
1192 deleteChar(startres, groupSeqs, fixedRight);
1197 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs,
1198 startres, lastres - startres, av.getAlignment(), true);
1204 // ///Editing a single sequence///////////
1208 // dragging to the right
1209 if (fixedColumns && fixedRight != -1)
1211 for (int j = lastres; j < startres; j++)
1213 insertChar(j, new SequenceI[]
1214 { seq }, fixedRight);
1219 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1220 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1225 // dragging to the left
1226 if (fixedColumns && fixedRight != -1)
1228 for (int j = lastres; j > startres; j--)
1230 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1235 deleteChar(startres, new SequenceI[]
1236 { seq }, fixedRight);
1241 // could be a keyboard edit trying to delete none gaps
1243 for (int m = startres; m < lastres; m++)
1245 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1254 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1255 { seq }, startres, max, av.getAlignment(), true);
1262 seqCanvas.repaint();
1265 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1267 int blankColumn = fixedColumn;
1268 for (int s = 0; s < seq.length; s++)
1270 // Find the next gap before the end of the visible region boundary
1271 // If lastCol > j, theres a boundary after the gap insertion
1273 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1275 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1277 // Theres a space, so break and insert the gap
1282 if (blankColumn <= j)
1284 blankColumn = fixedColumn;
1290 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1291 av.getAlignment(), true);
1293 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1,
1294 av.getAlignment(), true);
1298 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1301 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1,
1302 av.getAlignment(), true);
1304 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1305 av.getAlignment(), true);
1308 // ////////////////////////////////////////
1309 // ///Everything below this is for defining the boundary of the rubberband
1310 // ////////////////////////////////////////
1311 public void doMousePressedDefineMode(MouseEvent evt)
1313 if (scrollThread != null)
1315 scrollThread.running = false;
1316 scrollThread = null;
1319 int res = findRes(evt);
1320 int seq = findSeq(evt);
1322 startWrapBlock = wrappedBlock;
1329 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1331 if (sequence == null || res > sequence.getLength())
1336 stretchGroup = av.getSelectionGroup();
1338 if (stretchGroup == null)
1340 stretchGroup = av.getAlignment().findGroup(sequence);
1341 if (stretchGroup != null && res > stretchGroup.getStartRes()
1342 && res < stretchGroup.getEndRes())
1344 av.setSelectionGroup(stretchGroup);
1348 stretchGroup = null;
1352 else if (!stretchGroup.getSequences(null).contains(sequence)
1353 || stretchGroup.getStartRes() > res
1354 || stretchGroup.getEndRes() < res)
1356 stretchGroup = null;
1358 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1360 if (allGroups != null)
1362 for (int i = 0; i < allGroups.length; i++)
1364 if (allGroups[i].getStartRes() <= res
1365 && allGroups[i].getEndRes() >= res)
1367 stretchGroup = allGroups[i];
1372 av.setSelectionGroup(stretchGroup);
1375 // DETECT RIGHT MOUSE BUTTON IN AWT
1376 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1378 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1379 sequence.findPosition(res));
1381 Vector links = null;
1382 if (allFeatures != null)
1384 for (int i = 0; i < allFeatures.length; i++)
1386 if (allFeatures[i].links != null)
1390 links = new Vector();
1392 for (int j = 0; j < allFeatures[i].links.size(); j++)
1394 links.addElement(allFeatures[i].links.elementAt(j));
1399 APopupMenu popup = new APopupMenu(ap, null, links);
1401 popup.show(this, evt.getX(), evt.getY());
1407 seqCanvas.cursorX = findRes(evt);
1408 seqCanvas.cursorY = findSeq(evt);
1409 seqCanvas.repaint();
1413 // Only if left mouse button do we want to change group sizes
1415 if (stretchGroup == null)
1417 // define a new group here
1418 SequenceGroup sg = new SequenceGroup();
1419 sg.setStartRes(res);
1421 sg.addSequence(sequence, false);
1422 av.setSelectionGroup(sg);
1425 if (av.getConservationSelected())
1427 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1430 if (av.getAbovePIDThreshold())
1432 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1439 public void doMouseReleasedDefineMode(MouseEvent evt)
1441 if (stretchGroup == null)
1446 stretchGroup.recalcConservation(); // always do this - annotation has own
1448 if (stretchGroup.cs != null)
1450 stretchGroup.cs.alignmentChanged(stretchGroup,
1451 av.getHiddenRepSequences());
1453 if (stretchGroup.cs.conservationApplied())
1455 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1456 stretchGroup.getName());
1460 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1461 stretchGroup.getName());
1464 changeEndRes = false;
1465 changeStartRes = false;
1466 stretchGroup = null;
1467 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1468 ap.paintAlignment(true);
1472 public void doMouseDraggedDefineMode(MouseEvent evt)
1474 int res = findRes(evt);
1475 int y = findSeq(evt);
1477 if (wrappedBlock != startWrapBlock)
1482 if (stretchGroup == null)
1487 mouseDragging = true;
1489 if (y > av.getAlignment().getHeight())
1491 y = av.getAlignment().getHeight() - 1;
1494 if (res >= av.getAlignment().getWidth())
1496 res = av.getAlignment().getWidth() - 1;
1499 if (stretchGroup.getEndRes() == res)
1501 // Edit end res position of selected group
1502 changeEndRes = true;
1504 else if (stretchGroup.getStartRes() == res)
1506 // Edit start res position of selected group
1507 changeStartRes = true;
1517 if (res > (stretchGroup.getStartRes() - 1))
1519 stretchGroup.setEndRes(res);
1522 else if (changeStartRes)
1524 if (res < (stretchGroup.getEndRes() + 1))
1526 stretchGroup.setStartRes(res);
1530 int dragDirection = 0;
1536 else if (y < oldSeq)
1541 while ((y != oldSeq) && (oldSeq > -1)
1542 && (y < av.getAlignment().getHeight()))
1544 // This routine ensures we don't skip any sequences, as the
1545 // selection is quite slow.
1546 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1548 oldSeq += dragDirection;
1555 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1557 if (stretchGroup.getSequences(null).contains(nextSeq))
1559 stretchGroup.deleteSequence(seq, false);
1565 stretchGroup.addSequence(seq, false);
1568 stretchGroup.addSequence(nextSeq, false);
1577 if (res > av.endRes || res < av.startRes || y < av.startSeq
1583 if (scrollThread != null)
1585 scrollThread.setEvent(evt);
1588 seqCanvas.repaint();
1591 public void mouseEntered(MouseEvent e)
1598 if (scrollThread != null)
1600 scrollThread.running = false;
1601 scrollThread = null;
1605 public void mouseExited(MouseEvent e)
1607 if (av.getWrapAlignment())
1612 if (mouseDragging && scrollThread == null)
1614 scrollThread = new ScrollThread();
1618 void scrollCanvas(MouseEvent evt)
1622 if (scrollThread != null)
1624 scrollThread.running = false;
1625 scrollThread = null;
1627 mouseDragging = false;
1631 if (scrollThread == null)
1633 scrollThread = new ScrollThread();
1636 mouseDragging = true;
1637 scrollThread.setEvent(evt);
1642 // this class allows scrolling off the bottom of the visible alignment
1643 class ScrollThread extends Thread
1647 boolean running = false;
1649 public ScrollThread()
1654 public void setEvent(MouseEvent e)
1659 public void stopScrolling()
1673 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1675 running = ap.scrollUp(true);
1678 if (mouseDragging && evt.getY() >= getSize().height
1679 && av.getAlignment().getHeight() > av.getEndSeq())
1681 running = ap.scrollUp(false);
1684 if (mouseDragging && evt.getX() < 0)
1686 running = ap.scrollRight(false);
1689 else if (mouseDragging && evt.getX() >= getSize().width)
1691 running = ap.scrollRight(true);
1698 } catch (Exception ex)
1706 * modify current selection according to a received message.
1708 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1709 SelectionSource source)
1711 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1712 // handles selection messages...
1713 // TODO: extend config options to allow user to control if selections may be
1714 // shared between viewports.
1716 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1717 .getSequenceSetId().equals(av.getSequenceSetId()))))
1721 // do we want to thread this ? (contention with seqsel and colsel locks, I
1723 // rules are: colsel is copied if there is a real intersection between
1724 // sequence selection
1725 boolean repaint = false, copycolsel = true;
1726 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1728 SequenceGroup sgroup = null;
1729 if (seqsel != null && seqsel.getSize() > 0)
1731 if (av.getAlignment() == null)
1734 .println("Selection message: alignviewport av SeqSetId="
1735 + av.getSequenceSetId() + " ViewId="
1737 + " 's alignment is NULL! returning immediatly.");
1740 sgroup = seqsel.intersect(av.getAlignment(),
1741 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1742 if ((sgroup == null || sgroup.getSize() == 0)
1743 && (colsel == null || colsel.size() == 0))
1745 // don't copy columns if the region didn't intersect.
1749 if (sgroup != null && sgroup.getSize() > 0)
1751 av.setSelectionGroup(sgroup);
1755 av.setSelectionGroup(null);
1757 repaint = av.isSelectionGroupChanged(true);
1760 && (av.getColumnSelection() == null || !av
1761 .isColSelChanged(true)))
1763 // the current selection is unset or from a previous message
1764 // so import the new colsel.
1765 if (colsel == null || colsel.size() == 0)
1767 if (av.getColumnSelection() != null)
1769 av.getColumnSelection().clear();
1774 // TODO: shift colSel according to the intersecting sequences
1775 if (av.getColumnSelection() == null)
1777 av.setColumnSelection(new ColumnSelection(colsel));
1781 av.getColumnSelection().setElementsFrom(colsel);
1784 repaint |= av.isColSelChanged(true);
1787 && av.hasHiddenColumns()
1788 && (av.getColumnSelection() == null || av.getColumnSelection()
1789 .getHiddenColumns() == null))
1791 System.err.println("Bad things");
1795 ap.scalePanelHolder.repaint();
1801 * scroll to the given row/column - or nearest visible location
1806 public void scrollTo(int row, int column)
1809 row = row < 0 ? ap.av.startSeq : row;
1810 column = column < 0 ? ap.av.startRes : column;
1811 ap.scrollTo(column, column, row, true, true);
1815 * scroll to the given row - or nearest visible location
1819 public void scrollToRow(int row)
1822 row = row < 0 ? ap.av.startSeq : row;
1823 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1827 * scroll to the given column - or nearest visible location
1831 public void scrollToColumn(int column)
1834 column = column < 0 ? ap.av.startRes : column;
1835 ap.scrollTo(column, column, ap.av.startSeq, true, true);