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;
26 import java.awt.event.*;
28 import jalview.commands.*;
29 import jalview.datamodel.*;
30 import jalview.schemes.*;
31 import jalview.structure.SelectionSource;
32 import jalview.structure.SequenceListener;
33 import jalview.structure.StructureSelectionManager;
34 import jalview.util.MessageManager;
36 public class SeqPanel extends Panel implements MouseMotionListener,
37 MouseListener, SequenceListener
40 public SeqCanvas seqCanvas;
42 public AlignmentPanel ap;
44 protected int lastres;
46 protected int startseq;
48 protected AlignViewport av;
50 // if character is inserted or deleted, we will need to recalculate the
52 boolean seqEditOccurred = false;
54 ScrollThread scrollThread = null;
56 boolean mouseDragging = false;
58 boolean editingSeqs = false;
60 boolean groupEditing = false;
64 boolean changeEndSeq = false;
66 boolean changeStartSeq = false;
68 boolean changeEndRes = false;
70 boolean changeStartRes = false;
72 SequenceGroup stretchGroup = null;
74 StringBuffer keyboardNo1;
76 StringBuffer keyboardNo2;
78 boolean mouseWheelPressed = false;
82 EditCommand editCommand;
84 StructureSelectionManager ssm;
86 public SeqPanel(AlignViewport avp, AlignmentPanel p)
90 seqCanvas = new SeqCanvas(avp);
91 setLayout(new BorderLayout());
96 seqCanvas.addMouseMotionListener(this);
97 seqCanvas.addMouseListener(this);
98 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
99 ssm.addStructureViewerListener(this);
106 if (editCommand != null && editCommand.getSize() > 0)
108 ap.alignFrame.addHistoryItem(editCommand);
109 av.firePropertyChange("alignment", null, av.getAlignment()
116 groupEditing = false;
124 seqCanvas.cursorY = getKeyboardNo1() - 1;
128 void setCursorColumn()
130 seqCanvas.cursorX = getKeyboardNo1() - 1;
134 void setCursorRowAndColumn()
136 if (keyboardNo2 == null)
138 keyboardNo2 = new StringBuffer();
142 seqCanvas.cursorX = getKeyboardNo1() - 1;
143 seqCanvas.cursorY = getKeyboardNo2() - 1;
148 void setCursorPosition()
150 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
153 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
157 void moveCursor(int dx, int dy)
159 seqCanvas.cursorX += dx;
160 seqCanvas.cursorY += dy;
161 if (av.hasHiddenColumns()
162 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
164 int original = seqCanvas.cursorX - dx;
165 int maxWidth = av.getAlignment().getWidth();
167 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
168 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
170 seqCanvas.cursorX += dx;
173 if (seqCanvas.cursorX >= maxWidth
174 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
176 seqCanvas.cursorX = original;
182 void scrollToVisible()
184 if (seqCanvas.cursorX < 0)
186 seqCanvas.cursorX = 0;
188 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
190 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
193 if (seqCanvas.cursorY < 0)
195 seqCanvas.cursorY = 0;
197 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
199 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
203 if (av.wrapAlignment)
205 ap.scrollToWrappedVisible(seqCanvas.cursorX);
209 while (seqCanvas.cursorY < av.startSeq)
213 while (seqCanvas.cursorY + 1 > av.endSeq)
217 while (seqCanvas.cursorX < av.getColumnSelection()
218 .adjustForHiddenColumns(av.startRes))
221 if (!ap.scrollRight(false))
226 while (seqCanvas.cursorX > av.getColumnSelection()
227 .adjustForHiddenColumns(av.endRes))
229 if (!ap.scrollRight(true))
235 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
236 seqCanvas.cursorX, seqCanvas.cursorY);
241 void setSelectionAreaAtCursor(boolean topLeft)
243 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
246 if (av.getSelectionGroup() != null)
248 SequenceGroup sg = av.getSelectionGroup();
249 // Find the top and bottom of this group
250 int min = av.getAlignment().getHeight(), max = 0;
251 for (int i = 0; i < sg.getSize(); i++)
253 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
268 sg.setStartRes(seqCanvas.cursorX);
269 if (sg.getEndRes() < seqCanvas.cursorX)
271 sg.setEndRes(seqCanvas.cursorX);
274 min = seqCanvas.cursorY;
278 sg.setEndRes(seqCanvas.cursorX);
279 if (sg.getStartRes() > seqCanvas.cursorX)
281 sg.setStartRes(seqCanvas.cursorX);
284 max = seqCanvas.cursorY + 1;
289 // Only the user can do this
290 av.setSelectionGroup(null);
294 // Now add any sequences between min and max
296 for (int i = min; i < max; i++)
298 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
303 if (av.getSelectionGroup() == null)
305 SequenceGroup sg = new SequenceGroup();
306 sg.setStartRes(seqCanvas.cursorX);
307 sg.setEndRes(seqCanvas.cursorX);
308 sg.addSequence(sequence, false);
309 av.setSelectionGroup(sg);
311 ap.paintAlignment(false);
315 void insertGapAtCursor(boolean group)
317 groupEditing = group;
318 startseq = seqCanvas.cursorY;
319 lastres = seqCanvas.cursorX;
320 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
324 void deleteGapAtCursor(boolean group)
326 groupEditing = group;
327 startseq = seqCanvas.cursorY;
328 lastres = seqCanvas.cursorX + getKeyboardNo1();
329 editSequence(false, seqCanvas.cursorX);
333 void numberPressed(char value)
335 if (keyboardNo1 == null)
337 keyboardNo1 = new StringBuffer();
340 if (keyboardNo2 != null)
342 keyboardNo2.append(value);
346 keyboardNo1.append(value);
354 if (keyboardNo1 != null)
356 int value = Integer.parseInt(keyboardNo1.toString());
360 } catch (Exception x)
371 if (keyboardNo2 != null)
373 int value = Integer.parseInt(keyboardNo2.toString());
377 } catch (Exception x)
384 void setStatusMessage(SequenceI sequence, int res, int seq)
386 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
387 + sequence.getName());
390 if (av.getAlignment().isNucleotide())
392 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
396 text.append(" Nucleotide: ");
401 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
404 text.append(" Residue: ");
413 text.append(obj + " (" + sequence.findPosition(res) + ")");
417 ap.alignFrame.statusBar.setText(text.toString());
421 public void mousePressed(MouseEvent evt)
423 lastMousePress = evt.getPoint();
425 // For now, ignore the mouseWheel font resizing on Macs
426 // As the Button2_mask always seems to be true
427 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
430 mouseWheelPressed = true;
434 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
436 if (evt.isControlDown() || evt.isAltDown())
444 doMousePressedDefineMode(evt);
448 int seq = findSeq(evt);
449 int res = findRes(evt);
451 if (seq < 0 || res < 0)
456 if ((seq < av.getAlignment().getHeight())
457 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
471 public void mouseClicked(MouseEvent evt)
473 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
474 if (evt.getClickCount() > 1)
476 if (av.getSelectionGroup() != null
477 && av.getSelectionGroup().getSize() == 1
478 && av.getSelectionGroup().getEndRes()
479 - av.getSelectionGroup().getStartRes() < 2)
481 av.setSelectionGroup(null);
484 SequenceFeature[] features = findFeaturesAtRes(sequence,
485 sequence.findPosition(findRes(evt)));
487 if (features != null && features.length > 0)
489 SearchResults highlight = new SearchResults();
490 highlight.addResult(sequence, features[0].getBegin(),
491 features[0].getEnd());
492 seqCanvas.highlightSearchResults(highlight);
494 if (features != null && features.length > 0)
496 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
497 { sequence }, features, false, ap);
499 seqCanvas.highlightSearchResults(null);
504 public void mouseReleased(MouseEvent evt)
506 mouseDragging = false;
507 mouseWheelPressed = false;
508 ap.paintAlignment(true);
512 doMouseReleasedDefineMode(evt);
520 int startWrapBlock = -1;
522 int wrappedBlock = -1;
524 int findRes(MouseEvent evt)
529 if (av.wrapAlignment)
532 int hgap = av.charHeight;
533 if (av.scaleAboveWrapped)
535 hgap += av.charHeight;
538 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
539 + seqCanvas.getAnnotationHeight();
543 x -= seqCanvas.LABEL_WEST;
545 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
551 wrappedBlock = y / cHeight;
552 wrappedBlock += av.getStartRes() / cwidth;
554 res = wrappedBlock * cwidth + x / av.getCharWidth();
559 res = (x / av.getCharWidth()) + av.getStartRes();
562 if (av.hasHiddenColumns())
564 res = av.getColumnSelection().adjustForHiddenColumns(res);
571 int findSeq(MouseEvent evt)
573 final int sqnum = findAlRow(evt);
574 return (sqnum < 0) ? 0 : sqnum;
580 * @return row in alignment that was selected (or -1 for column selection)
582 private int findAlRow(MouseEvent evt)
587 if (av.wrapAlignment)
589 int hgap = av.charHeight;
590 if (av.scaleAboveWrapped)
592 hgap += av.charHeight;
595 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
596 + seqCanvas.getAnnotationHeight();
600 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
609 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
610 .getAlignment().getHeight() - 1);
620 public void doMousePressed(MouseEvent evt)
623 int seq = findSeq(evt);
624 int res = findRes(evt);
626 if (seq < av.getAlignment().getHeight()
627 && res < av.getAlignment().getSequenceAt(seq).getLength())
629 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
630 // Find the residue's position in the sequence (res is the position
647 public void mouseOverSequence(SequenceI sequence, int index, int pos)
649 String tmp = sequence.hashCode() + index + "";
650 if (lastMessage == null || !lastMessage.equals(tmp))
651 ssm.mouseOverSequence(sequence, index, pos, av);
656 public void highlightSequence(SearchResults results)
658 if (av.followHighlight)
660 if (ap.scrollToPosition(results, true))
662 ap.alignFrame.repaint();
665 seqCanvas.highlightSearchResults(results);
669 public void updateColours(SequenceI seq, int index)
671 System.out.println("update the seqPanel colours");
675 public void mouseMoved(MouseEvent evt)
677 int res = findRes(evt);
678 int seq = findSeq(evt);
680 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
689 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
690 if (res > sequence.getLength())
699 int respos = sequence.findPosition(res);
701 mouseOverSequence(sequence, res, respos);
703 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
704 + sequence.getName());
707 if (av.getAlignment().isNucleotide())
709 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
713 text.append(" Nucleotide: ");
718 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
721 text.append(" Residue: ");
729 text.append(obj + " (" + respos + ")");
733 ap.alignFrame.statusBar.setText(text.toString());
735 StringBuffer tooltipText = new StringBuffer();
736 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
739 for (int g = 0; g < groups.length; g++)
741 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
743 if (!groups[g].getName().startsWith("JTreeGroup")
744 && !groups[g].getName().startsWith("JGroup"))
746 tooltipText.append(groups[g].getName() + " ");
748 if (groups[g].getDescription() != null)
750 tooltipText.append(groups[g].getDescription());
752 tooltipText.append("\n");
757 // use aa to see if the mouse pointer is on a
758 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
759 sequence.findPosition(res));
762 while (index < allFeatures.length)
764 SequenceFeature sf = allFeatures[index];
766 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
768 if (sf.getDescription() != null)
770 tooltipText.append(" " + sf.getDescription());
773 if (sf.getValue("status") != null)
775 String status = sf.getValue("status").toString();
776 if (status.length() > 0)
778 tooltipText.append(" (" + sf.getValue("status") + ")");
781 tooltipText.append("\n");
788 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
792 tooltip.setTip(tooltipText.toString());
796 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
798 Vector tmp = new Vector();
799 SequenceFeature[] features = sequence.getSequenceFeatures();
800 if (features != null)
802 for (int i = 0; i < features.length; i++)
804 if (av.featuresDisplayed == null
805 || !av.featuresDisplayed.containsKey(features[i].getType()))
810 if (features[i].featureGroup != null
811 && seqCanvas.fr.featureGroups != null
812 && seqCanvas.fr.featureGroups
813 .containsKey(features[i].featureGroup)
814 && !((Boolean) seqCanvas.fr.featureGroups
815 .get(features[i].featureGroup)).booleanValue())
818 if ((features[i].getBegin() <= res)
819 && (features[i].getEnd() >= res))
821 tmp.addElement(features[i]);
826 features = new SequenceFeature[tmp.size()];
827 tmp.copyInto(features);
834 public void mouseDragged(MouseEvent evt)
836 if (mouseWheelPressed)
838 int oldWidth = av.charWidth;
840 // Which is bigger, left-right or up-down?
841 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
844 int fontSize = av.font.getSize();
846 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
850 else if (evt.getY() > lastMousePress.y)
860 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
861 av.charWidth = oldWidth;
865 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
869 else if (evt.getX() > lastMousePress.x)
874 if (av.charWidth < 1)
882 FontMetrics fm = getFontMetrics(av.getFont());
883 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
885 lastMousePress = evt.getPoint();
887 ap.paintAlignment(false);
888 ap.annotationPanel.image = null;
894 doMouseDraggedDefineMode(evt);
898 int res = findRes(evt);
905 if ((lastres == -1) || (lastres == res))
910 if ((res < av.getAlignment().getWidth()) && (res < lastres))
912 // dragLeft, delete gap
913 editSequence(false, res);
917 editSequence(true, res);
920 mouseDragging = true;
921 if (scrollThread != null)
923 scrollThread.setEvent(evt);
928 synchronized void editSequence(boolean insertGap, int startres)
932 boolean fixedColumns = false;
933 SequenceGroup sg = av.getSelectionGroup();
935 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
937 if (!groupEditing && av.hasHiddenRows())
939 if (av.isHiddenRepSequence(seq))
941 sg = (SequenceGroup) av.getRepresentedSequences(seq);
946 StringBuffer message = new StringBuffer();
949 message.append(MessageManager.getString("action.edit_group")).append(":");
950 if (editCommand == null)
952 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
957 message.append(MessageManager.getString("label.edit_sequence")).append(" " + seq.getName());
958 String label = seq.getName();
959 if (label.length() > 10)
961 label = label.substring(0, 10);
963 if (editCommand == null)
965 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
971 message.append(" insert ");
975 message.append(" delete ");
978 message.append(Math.abs(startres - lastres) + " gaps.");
979 ap.alignFrame.statusBar.setText(message.toString());
981 // Are we editing within a selection group?
983 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
988 // sg might be null as the user may only see 1 sequence,
989 // but the sequence represents a group
992 if (!av.isHiddenRepSequence(seq))
998 sg = av.getRepresentedSequences(seq);
1001 fixedLeft = sg.getStartRes();
1002 fixedRight = sg.getEndRes();
1004 if ((startres < fixedLeft && lastres >= fixedLeft)
1005 || (startres >= fixedLeft && lastres < fixedLeft)
1006 || (startres > fixedRight && lastres <= fixedRight)
1007 || (startres <= fixedRight && lastres > fixedRight))
1013 if (fixedLeft > startres)
1015 fixedRight = fixedLeft - 1;
1018 else if (fixedRight < startres)
1020 fixedLeft = fixedRight;
1025 if (av.hasHiddenColumns())
1027 fixedColumns = true;
1028 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1029 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1031 if ((insertGap && startres > y1 && lastres < y1)
1032 || (!insertGap && startres < y2 && lastres > y2))
1038 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1039 // Selection spans a hidden region
1040 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1048 fixedRight = y2 - 1;
1055 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1056 .toArray(new SequenceI[0]);
1061 // If the user has selected the whole sequence, and is dragging to
1062 // the right, we can still extend the alignment and selectionGroup
1063 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1064 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1066 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1067 fixedRight = sg.getEndRes();
1070 // Is it valid with fixed columns??
1071 // Find the next gap before the end
1072 // of the visible region boundary
1073 boolean blank = false;
1074 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1078 for (SequenceI gs : groupSeqs)
1080 for (int j = 0; j < startres - lastres; j++)
1082 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1098 if (sg.getSize() == av.getAlignment().getHeight())
1100 if ((av.hasHiddenColumns() && startres < av
1101 .getColumnSelection().getHiddenBoundaryRight(startres)))
1107 int alWidth = av.getAlignment().getWidth();
1108 if (av.hasHiddenRows())
1110 int hwidth = av.getAlignment().getHiddenSequences()
1112 if (hwidth > alWidth)
1117 // We can still insert gaps if the selectionGroup
1118 // contains all the sequences
1119 sg.setEndRes(sg.getEndRes() + startres - lastres);
1120 fixedRight = alWidth + startres - lastres;
1131 else if (!insertGap)
1133 // / Are we able to delete?
1134 // ie are all columns blank?
1136 for (SequenceI gs : groupSeqs)
1138 for (int j = startres; j < lastres; j++)
1140 if (gs.getLength() <= j)
1145 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1147 // Not a gap, block edit not valid
1157 // dragging to the right
1158 if (fixedColumns && fixedRight != -1)
1160 for (int j = lastres; j < startres; j++)
1162 insertChar(j, groupSeqs, fixedRight);
1167 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1168 startres, startres - lastres, av.getAlignment(), true);
1173 // dragging to the left
1174 if (fixedColumns && fixedRight != -1)
1176 for (int j = lastres; j > startres; j--)
1178 deleteChar(startres, groupSeqs, fixedRight);
1183 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1184 startres, lastres - startres, av.getAlignment(), true);
1190 // ///Editing a single sequence///////////
1194 // dragging to the right
1195 if (fixedColumns && fixedRight != -1)
1197 for (int j = lastres; j < startres; j++)
1199 insertChar(j, new SequenceI[]
1200 { seq }, fixedRight);
1205 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1206 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1211 // dragging to the left
1212 if (fixedColumns && fixedRight != -1)
1214 for (int j = lastres; j > startres; j--)
1216 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1221 deleteChar(startres, new SequenceI[]
1222 { seq }, fixedRight);
1227 // could be a keyboard edit trying to delete none gaps
1229 for (int m = startres; m < lastres; m++)
1231 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1240 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1241 { seq }, startres, max, av.getAlignment(), true);
1248 seqCanvas.repaint();
1251 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1253 int blankColumn = fixedColumn;
1254 for (int s = 0; s < seq.length; s++)
1256 // Find the next gap before the end of the visible region boundary
1257 // If lastCol > j, theres a boundary after the gap insertion
1259 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1261 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1263 // Theres a space, so break and insert the gap
1268 if (blankColumn <= j)
1270 blankColumn = fixedColumn;
1276 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1277 av.getAlignment(), true);
1279 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1280 av.getAlignment(), true);
1284 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1287 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1288 av.getAlignment(), true);
1290 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1291 av.getAlignment(), true);
1294 // ////////////////////////////////////////
1295 // ///Everything below this is for defining the boundary of the rubberband
1296 // ////////////////////////////////////////
1297 public void doMousePressedDefineMode(MouseEvent evt)
1299 if (scrollThread != null)
1301 scrollThread.running = false;
1302 scrollThread = null;
1305 int res = findRes(evt);
1306 int seq = findSeq(evt);
1308 startWrapBlock = wrappedBlock;
1315 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1317 if (sequence == null || res > sequence.getLength())
1322 stretchGroup = av.getSelectionGroup();
1324 if (stretchGroup == null)
1326 stretchGroup = av.getAlignment().findGroup(sequence);
1327 if (stretchGroup != null && res > stretchGroup.getStartRes()
1328 && res < stretchGroup.getEndRes())
1330 av.setSelectionGroup(stretchGroup);
1334 stretchGroup = null;
1338 else if (!stretchGroup.getSequences(null).contains(sequence)
1339 || stretchGroup.getStartRes() > res
1340 || stretchGroup.getEndRes() < res)
1342 stretchGroup = null;
1344 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1346 if (allGroups != null)
1348 for (int i = 0; i < allGroups.length; i++)
1350 if (allGroups[i].getStartRes() <= res
1351 && allGroups[i].getEndRes() >= res)
1353 stretchGroup = allGroups[i];
1358 av.setSelectionGroup(stretchGroup);
1361 // DETECT RIGHT MOUSE BUTTON IN AWT
1362 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1364 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1365 sequence.findPosition(res));
1367 Vector links = null;
1368 if (allFeatures != null)
1370 for (int i = 0; i < allFeatures.length; i++)
1372 if (allFeatures[i].links != null)
1376 links = new Vector();
1378 for (int j = 0; j < allFeatures[i].links.size(); j++)
1380 links.addElement(allFeatures[i].links.elementAt(j));
1385 APopupMenu popup = new APopupMenu(ap, null, links);
1387 popup.show(this, evt.getX(), evt.getY());
1393 seqCanvas.cursorX = findRes(evt);
1394 seqCanvas.cursorY = findSeq(evt);
1395 seqCanvas.repaint();
1399 // Only if left mouse button do we want to change group sizes
1401 if (stretchGroup == null)
1403 // define a new group here
1404 SequenceGroup sg = new SequenceGroup();
1405 sg.setStartRes(res);
1407 sg.addSequence(sequence, false);
1408 av.setSelectionGroup(sg);
1411 if (av.getConservationSelected())
1413 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1416 if (av.getAbovePIDThreshold())
1418 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1425 public void doMouseReleasedDefineMode(MouseEvent evt)
1427 if (stretchGroup == null)
1432 stretchGroup.recalcConservation(); // always do this - annotation has own
1434 if (stretchGroup.cs != null)
1436 stretchGroup.cs.alignmentChanged(stretchGroup,
1437 av.getHiddenRepSequences());
1439 if (stretchGroup.cs.conservationApplied())
1441 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1442 stretchGroup.getName());
1446 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1447 stretchGroup.getName());
1450 changeEndRes = false;
1451 changeStartRes = false;
1452 stretchGroup = null;
1453 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1454 ap.paintAlignment(true);
1458 public void doMouseDraggedDefineMode(MouseEvent evt)
1460 int res = findRes(evt);
1461 int y = findSeq(evt);
1463 if (wrappedBlock != startWrapBlock)
1468 if (stretchGroup == null)
1473 mouseDragging = true;
1475 if (y > av.getAlignment().getHeight())
1477 y = av.getAlignment().getHeight() - 1;
1480 if (res >= av.getAlignment().getWidth())
1482 res = av.getAlignment().getWidth() - 1;
1485 if (stretchGroup.getEndRes() == res)
1487 // Edit end res position of selected group
1488 changeEndRes = true;
1490 else if (stretchGroup.getStartRes() == res)
1492 // Edit start res position of selected group
1493 changeStartRes = true;
1503 if (res > (stretchGroup.getStartRes() - 1))
1505 stretchGroup.setEndRes(res);
1508 else if (changeStartRes)
1510 if (res < (stretchGroup.getEndRes() + 1))
1512 stretchGroup.setStartRes(res);
1516 int dragDirection = 0;
1522 else if (y < oldSeq)
1527 while ((y != oldSeq) && (oldSeq > -1)
1528 && (y < av.getAlignment().getHeight()))
1530 // This routine ensures we don't skip any sequences, as the
1531 // selection is quite slow.
1532 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1534 oldSeq += dragDirection;
1541 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1543 if (stretchGroup.getSequences(null).contains(nextSeq))
1545 stretchGroup.deleteSequence(seq, false);
1551 stretchGroup.addSequence(seq, false);
1554 stretchGroup.addSequence(nextSeq, false);
1563 if (res > av.endRes || res < av.startRes || y < av.startSeq
1569 if (scrollThread != null)
1571 scrollThread.setEvent(evt);
1574 seqCanvas.repaint();
1577 public void mouseEntered(MouseEvent e)
1584 if (scrollThread != null)
1586 scrollThread.running = false;
1587 scrollThread = null;
1591 public void mouseExited(MouseEvent e)
1593 if (av.getWrapAlignment())
1598 if (mouseDragging && scrollThread == null)
1600 scrollThread = new ScrollThread();
1604 void scrollCanvas(MouseEvent evt)
1608 if (scrollThread != null)
1610 scrollThread.running = false;
1611 scrollThread = null;
1613 mouseDragging = false;
1617 if (scrollThread == null)
1619 scrollThread = new ScrollThread();
1622 mouseDragging = true;
1623 scrollThread.setEvent(evt);
1628 // this class allows scrolling off the bottom of the visible alignment
1629 class ScrollThread extends Thread
1633 boolean running = false;
1635 public ScrollThread()
1640 public void setEvent(MouseEvent e)
1645 public void stopScrolling()
1659 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1661 running = ap.scrollUp(true);
1664 if (mouseDragging && evt.getY() >= getSize().height
1665 && av.getAlignment().getHeight() > av.getEndSeq())
1667 running = ap.scrollUp(false);
1670 if (mouseDragging && evt.getX() < 0)
1672 running = ap.scrollRight(false);
1675 else if (mouseDragging && evt.getX() >= getSize().width)
1677 running = ap.scrollRight(true);
1684 } catch (Exception ex)
1692 * modify current selection according to a received message.
1694 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1695 SelectionSource source)
1697 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1698 // handles selection messages...
1699 // TODO: extend config options to allow user to control if selections may be
1700 // shared between viewports.
1702 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1703 .getSequenceSetId().equals(av.getSequenceSetId()))))
1707 // do we want to thread this ? (contention with seqsel and colsel locks, I
1709 // rules are: colsel is copied if there is a real intersection between
1710 // sequence selection
1711 boolean repaint = false, copycolsel = true;
1712 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1714 SequenceGroup sgroup = null;
1715 if (seqsel != null && seqsel.getSize() > 0)
1717 if (av.getAlignment() == null)
1720 .println("Selection message: alignviewport av SeqSetId="
1721 + av.getSequenceSetId() + " ViewId="
1723 + " 's alignment is NULL! returning immediatly.");
1726 sgroup = seqsel.intersect(av.getAlignment(),
1727 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1728 if ((sgroup == null || sgroup.getSize() == 0)
1729 && (colsel == null || colsel.size() == 0))
1731 // don't copy columns if the region didn't intersect.
1735 if (sgroup != null && sgroup.getSize() > 0)
1737 av.setSelectionGroup(sgroup);
1741 av.setSelectionGroup(null);
1743 repaint = av.isSelectionGroupChanged(true);
1746 && (av.getColumnSelection() == null || !av
1747 .isColSelChanged(true)))
1749 // the current selection is unset or from a previous message
1750 // so import the new colsel.
1751 if (colsel == null || colsel.size() == 0)
1753 if (av.getColumnSelection() != null)
1755 av.getColumnSelection().clear();
1760 // TODO: shift colSel according to the intersecting sequences
1761 if (av.getColumnSelection() == null)
1763 av.setColumnSelection(new ColumnSelection(colsel));
1767 av.getColumnSelection().setElementsFrom(colsel);
1770 repaint |= av.isColSelChanged(true);
1773 && av.hasHiddenColumns()
1774 && (av.getColumnSelection() == null || av.getColumnSelection()
1775 .getHiddenColumns() == null))
1777 System.err.println("Bad things");
1781 ap.scalePanelHolder.repaint();
1787 * scroll to the given row/column - or nearest visible location
1792 public void scrollTo(int row, int column)
1795 row = row < 0 ? ap.av.startSeq : row;
1796 column = column < 0 ? ap.av.startRes : column;
1797 ap.scrollTo(column, column, row, true, true);
1801 * scroll to the given row - or nearest visible location
1805 public void scrollToRow(int row)
1808 row = row < 0 ? ap.av.startSeq : row;
1809 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1813 * scroll to the given column - or nearest visible location
1817 public void scrollToColumn(int column)
1820 column = column < 0 ? ap.av.startRes : column;
1821 ap.scrollTo(column, column, ap.av.startSeq, true, true);