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.featuresDisplayed == null
821 || !av.featuresDisplayed.containsKey(features[i].getType()))
826 if (features[i].featureGroup != null
827 && seqCanvas.fr.featureGroups != null
828 && seqCanvas.fr.featureGroups
829 .containsKey(features[i].featureGroup)
830 && !((Boolean) seqCanvas.fr.featureGroups
831 .get(features[i].featureGroup)).booleanValue())
836 if ((features[i].getBegin() <= res)
837 && (features[i].getEnd() >= res))
839 tmp.addElement(features[i]);
844 features = new SequenceFeature[tmp.size()];
845 tmp.copyInto(features);
852 public void mouseDragged(MouseEvent evt)
854 if (mouseWheelPressed)
856 int oldWidth = av.charWidth;
858 // Which is bigger, left-right or up-down?
859 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
862 int fontSize = av.font.getSize();
864 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
868 else if (evt.getY() > lastMousePress.y)
878 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
879 av.charWidth = oldWidth;
883 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
887 else if (evt.getX() > lastMousePress.x)
892 if (av.charWidth < 1)
900 FontMetrics fm = getFontMetrics(av.getFont());
901 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
903 lastMousePress = evt.getPoint();
905 ap.paintAlignment(false);
906 ap.annotationPanel.image = null;
912 doMouseDraggedDefineMode(evt);
916 int res = findRes(evt);
923 if ((lastres == -1) || (lastres == res))
928 if ((res < av.getAlignment().getWidth()) && (res < lastres))
930 // dragLeft, delete gap
931 editSequence(false, res);
935 editSequence(true, res);
938 mouseDragging = true;
939 if (scrollThread != null)
941 scrollThread.setEvent(evt);
946 synchronized void editSequence(boolean insertGap, int startres)
950 boolean fixedColumns = false;
951 SequenceGroup sg = av.getSelectionGroup();
953 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
955 if (!groupEditing && av.hasHiddenRows())
957 if (av.isHiddenRepSequence(seq))
959 sg = av.getRepresentedSequences(seq);
964 StringBuffer message = new StringBuffer();
967 message.append(MessageManager.getString("action.edit_group")).append(":");
968 if (editCommand == null)
970 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
975 message.append(MessageManager.getString("label.edit_sequence")).append(" " + seq.getName());
976 String label = seq.getName();
977 if (label.length() > 10)
979 label = label.substring(0, 10);
981 if (editCommand == null)
983 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
989 message.append(" insert ");
993 message.append(" delete ");
996 message.append(Math.abs(startres - lastres) + " gaps.");
997 ap.alignFrame.statusBar.setText(message.toString());
999 // Are we editing within a selection group?
1001 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1004 fixedColumns = true;
1006 // sg might be null as the user may only see 1 sequence,
1007 // but the sequence represents a group
1010 if (!av.isHiddenRepSequence(seq))
1016 sg = av.getRepresentedSequences(seq);
1019 fixedLeft = sg.getStartRes();
1020 fixedRight = sg.getEndRes();
1022 if ((startres < fixedLeft && lastres >= fixedLeft)
1023 || (startres >= fixedLeft && lastres < fixedLeft)
1024 || (startres > fixedRight && lastres <= fixedRight)
1025 || (startres <= fixedRight && lastres > fixedRight))
1031 if (fixedLeft > startres)
1033 fixedRight = fixedLeft - 1;
1036 else if (fixedRight < startres)
1038 fixedLeft = fixedRight;
1043 if (av.hasHiddenColumns())
1045 fixedColumns = true;
1046 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1047 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1049 if ((insertGap && startres > y1 && lastres < y1)
1050 || (!insertGap && startres < y2 && lastres > y2))
1056 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1057 // Selection spans a hidden region
1058 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1066 fixedRight = y2 - 1;
1073 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1074 .toArray(new SequenceI[0]);
1079 // If the user has selected the whole sequence, and is dragging to
1080 // the right, we can still extend the alignment and selectionGroup
1081 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1082 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1084 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1085 fixedRight = sg.getEndRes();
1088 // Is it valid with fixed columns??
1089 // Find the next gap before the end
1090 // of the visible region boundary
1091 boolean blank = false;
1092 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1096 for (SequenceI gs : groupSeqs)
1098 for (int j = 0; j < startres - lastres; j++)
1100 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1116 if (sg.getSize() == av.getAlignment().getHeight())
1118 if ((av.hasHiddenColumns() && startres < av
1119 .getColumnSelection().getHiddenBoundaryRight(startres)))
1125 int alWidth = av.getAlignment().getWidth();
1126 if (av.hasHiddenRows())
1128 int hwidth = av.getAlignment().getHiddenSequences()
1130 if (hwidth > alWidth)
1135 // We can still insert gaps if the selectionGroup
1136 // contains all the sequences
1137 sg.setEndRes(sg.getEndRes() + startres - lastres);
1138 fixedRight = alWidth + startres - lastres;
1149 else if (!insertGap)
1151 // / Are we able to delete?
1152 // ie are all columns blank?
1154 for (SequenceI gs : groupSeqs)
1156 for (int j = startres; j < lastres; j++)
1158 if (gs.getLength() <= j)
1163 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1165 // Not a gap, block edit not valid
1175 // dragging to the right
1176 if (fixedColumns && fixedRight != -1)
1178 for (int j = lastres; j < startres; j++)
1180 insertChar(j, groupSeqs, fixedRight);
1185 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs,
1186 startres, startres - lastres, av.getAlignment(), true);
1191 // dragging to the left
1192 if (fixedColumns && fixedRight != -1)
1194 for (int j = lastres; j > startres; j--)
1196 deleteChar(startres, groupSeqs, fixedRight);
1201 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs,
1202 startres, lastres - startres, av.getAlignment(), true);
1208 // ///Editing a single sequence///////////
1212 // dragging to the right
1213 if (fixedColumns && fixedRight != -1)
1215 for (int j = lastres; j < startres; j++)
1217 insertChar(j, new SequenceI[]
1218 { seq }, fixedRight);
1223 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1224 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1229 // dragging to the left
1230 if (fixedColumns && fixedRight != -1)
1232 for (int j = lastres; j > startres; j--)
1234 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1239 deleteChar(startres, new SequenceI[]
1240 { seq }, fixedRight);
1245 // could be a keyboard edit trying to delete none gaps
1247 for (int m = startres; m < lastres; m++)
1249 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1258 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1259 { seq }, startres, max, av.getAlignment(), true);
1266 seqCanvas.repaint();
1269 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1271 int blankColumn = fixedColumn;
1272 for (int s = 0; s < seq.length; s++)
1274 // Find the next gap before the end of the visible region boundary
1275 // If lastCol > j, theres a boundary after the gap insertion
1277 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1279 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1281 // Theres a space, so break and insert the gap
1286 if (blankColumn <= j)
1288 blankColumn = fixedColumn;
1294 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1295 av.getAlignment(), true);
1297 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1,
1298 av.getAlignment(), true);
1302 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1305 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1,
1306 av.getAlignment(), true);
1308 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1309 av.getAlignment(), true);
1312 // ////////////////////////////////////////
1313 // ///Everything below this is for defining the boundary of the rubberband
1314 // ////////////////////////////////////////
1315 public void doMousePressedDefineMode(MouseEvent evt)
1317 if (scrollThread != null)
1319 scrollThread.running = false;
1320 scrollThread = null;
1323 int res = findRes(evt);
1324 int seq = findSeq(evt);
1326 startWrapBlock = wrappedBlock;
1333 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1335 if (sequence == null || res > sequence.getLength())
1340 stretchGroup = av.getSelectionGroup();
1342 if (stretchGroup == null)
1344 stretchGroup = av.getAlignment().findGroup(sequence);
1345 if (stretchGroup != null && res > stretchGroup.getStartRes()
1346 && res < stretchGroup.getEndRes())
1348 av.setSelectionGroup(stretchGroup);
1352 stretchGroup = null;
1356 else if (!stretchGroup.getSequences(null).contains(sequence)
1357 || stretchGroup.getStartRes() > res
1358 || stretchGroup.getEndRes() < res)
1360 stretchGroup = null;
1362 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1364 if (allGroups != null)
1366 for (int i = 0; i < allGroups.length; i++)
1368 if (allGroups[i].getStartRes() <= res
1369 && allGroups[i].getEndRes() >= res)
1371 stretchGroup = allGroups[i];
1376 av.setSelectionGroup(stretchGroup);
1379 // DETECT RIGHT MOUSE BUTTON IN AWT
1380 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1382 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1383 sequence.findPosition(res));
1385 Vector links = null;
1386 if (allFeatures != null)
1388 for (int i = 0; i < allFeatures.length; i++)
1390 if (allFeatures[i].links != null)
1394 links = new Vector();
1396 for (int j = 0; j < allFeatures[i].links.size(); j++)
1398 links.addElement(allFeatures[i].links.elementAt(j));
1403 APopupMenu popup = new APopupMenu(ap, null, links);
1405 popup.show(this, evt.getX(), evt.getY());
1411 seqCanvas.cursorX = findRes(evt);
1412 seqCanvas.cursorY = findSeq(evt);
1413 seqCanvas.repaint();
1417 // Only if left mouse button do we want to change group sizes
1419 if (stretchGroup == null)
1421 // define a new group here
1422 SequenceGroup sg = new SequenceGroup();
1423 sg.setStartRes(res);
1425 sg.addSequence(sequence, false);
1426 av.setSelectionGroup(sg);
1429 if (av.getConservationSelected())
1431 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1434 if (av.getAbovePIDThreshold())
1436 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1443 public void doMouseReleasedDefineMode(MouseEvent evt)
1445 if (stretchGroup == null)
1450 stretchGroup.recalcConservation(); // always do this - annotation has own
1452 if (stretchGroup.cs != null)
1454 stretchGroup.cs.alignmentChanged(stretchGroup,
1455 av.getHiddenRepSequences());
1457 if (stretchGroup.cs.conservationApplied())
1459 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1460 stretchGroup.getName());
1464 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1465 stretchGroup.getName());
1468 changeEndRes = false;
1469 changeStartRes = false;
1470 stretchGroup = null;
1471 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1472 ap.paintAlignment(true);
1476 public void doMouseDraggedDefineMode(MouseEvent evt)
1478 int res = findRes(evt);
1479 int y = findSeq(evt);
1481 if (wrappedBlock != startWrapBlock)
1486 if (stretchGroup == null)
1491 mouseDragging = true;
1493 if (y > av.getAlignment().getHeight())
1495 y = av.getAlignment().getHeight() - 1;
1498 if (res >= av.getAlignment().getWidth())
1500 res = av.getAlignment().getWidth() - 1;
1503 if (stretchGroup.getEndRes() == res)
1505 // Edit end res position of selected group
1506 changeEndRes = true;
1508 else if (stretchGroup.getStartRes() == res)
1510 // Edit start res position of selected group
1511 changeStartRes = true;
1521 if (res > (stretchGroup.getStartRes() - 1))
1523 stretchGroup.setEndRes(res);
1526 else if (changeStartRes)
1528 if (res < (stretchGroup.getEndRes() + 1))
1530 stretchGroup.setStartRes(res);
1534 int dragDirection = 0;
1540 else if (y < oldSeq)
1545 while ((y != oldSeq) && (oldSeq > -1)
1546 && (y < av.getAlignment().getHeight()))
1548 // This routine ensures we don't skip any sequences, as the
1549 // selection is quite slow.
1550 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1552 oldSeq += dragDirection;
1559 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1561 if (stretchGroup.getSequences(null).contains(nextSeq))
1563 stretchGroup.deleteSequence(seq, false);
1569 stretchGroup.addSequence(seq, false);
1572 stretchGroup.addSequence(nextSeq, false);
1581 if (res > av.endRes || res < av.startRes || y < av.startSeq
1587 if (scrollThread != null)
1589 scrollThread.setEvent(evt);
1592 seqCanvas.repaint();
1595 public void mouseEntered(MouseEvent e)
1602 if (scrollThread != null)
1604 scrollThread.running = false;
1605 scrollThread = null;
1609 public void mouseExited(MouseEvent e)
1611 if (av.getWrapAlignment())
1616 if (mouseDragging && scrollThread == null)
1618 scrollThread = new ScrollThread();
1622 void scrollCanvas(MouseEvent evt)
1626 if (scrollThread != null)
1628 scrollThread.running = false;
1629 scrollThread = null;
1631 mouseDragging = false;
1635 if (scrollThread == null)
1637 scrollThread = new ScrollThread();
1640 mouseDragging = true;
1641 scrollThread.setEvent(evt);
1646 // this class allows scrolling off the bottom of the visible alignment
1647 class ScrollThread extends Thread
1651 boolean running = false;
1653 public ScrollThread()
1658 public void setEvent(MouseEvent e)
1663 public void stopScrolling()
1677 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1679 running = ap.scrollUp(true);
1682 if (mouseDragging && evt.getY() >= getSize().height
1683 && av.getAlignment().getHeight() > av.getEndSeq())
1685 running = ap.scrollUp(false);
1688 if (mouseDragging && evt.getX() < 0)
1690 running = ap.scrollRight(false);
1693 else if (mouseDragging && evt.getX() >= getSize().width)
1695 running = ap.scrollRight(true);
1702 } catch (Exception ex)
1710 * modify current selection according to a received message.
1712 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1713 SelectionSource source)
1715 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1716 // handles selection messages...
1717 // TODO: extend config options to allow user to control if selections may be
1718 // shared between viewports.
1720 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1721 .getSequenceSetId().equals(av.getSequenceSetId()))))
1725 // do we want to thread this ? (contention with seqsel and colsel locks, I
1727 // rules are: colsel is copied if there is a real intersection between
1728 // sequence selection
1729 boolean repaint = false, copycolsel = true;
1730 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1732 SequenceGroup sgroup = null;
1733 if (seqsel != null && seqsel.getSize() > 0)
1735 if (av.getAlignment() == null)
1738 .println("Selection message: alignviewport av SeqSetId="
1739 + av.getSequenceSetId() + " ViewId="
1741 + " 's alignment is NULL! returning immediatly.");
1744 sgroup = seqsel.intersect(av.getAlignment(),
1745 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1746 if ((sgroup == null || sgroup.getSize() == 0)
1747 && (colsel == null || colsel.size() == 0))
1749 // don't copy columns if the region didn't intersect.
1753 if (sgroup != null && sgroup.getSize() > 0)
1755 av.setSelectionGroup(sgroup);
1759 av.setSelectionGroup(null);
1761 repaint = av.isSelectionGroupChanged(true);
1764 && (av.getColumnSelection() == null || !av
1765 .isColSelChanged(true)))
1767 // the current selection is unset or from a previous message
1768 // so import the new colsel.
1769 if (colsel == null || colsel.size() == 0)
1771 if (av.getColumnSelection() != null)
1773 av.getColumnSelection().clear();
1778 // TODO: shift colSel according to the intersecting sequences
1779 if (av.getColumnSelection() == null)
1781 av.setColumnSelection(new ColumnSelection(colsel));
1785 av.getColumnSelection().setElementsFrom(colsel);
1788 repaint |= av.isColSelChanged(true);
1791 && av.hasHiddenColumns()
1792 && (av.getColumnSelection() == null || av.getColumnSelection()
1793 .getHiddenColumns() == null))
1795 System.err.println("Bad things");
1799 ap.scalePanelHolder.repaint();
1805 * scroll to the given row/column - or nearest visible location
1810 public void scrollTo(int row, int column)
1813 row = row < 0 ? ap.av.startSeq : row;
1814 column = column < 0 ? ap.av.startRes : column;
1815 ap.scrollTo(column, column, row, true, true);
1819 * scroll to the given row - or nearest visible location
1823 public void scrollToRow(int row)
1826 row = row < 0 ? ap.av.startSeq : row;
1827 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1831 * scroll to the given column - or nearest visible location
1835 public void scrollToColumn(int column)
1838 column = column < 0 ? ap.av.startRes : column;
1839 ap.scrollTo(column, column, ap.av.startSeq, true, true);