2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 package jalview.appletgui;
25 import java.awt.event.*;
27 import jalview.commands.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import jalview.structure.SequenceListener;
31 import jalview.structure.StructureSelectionManager;
34 extends Panel implements MouseMotionListener, MouseListener, SequenceListener
37 public SeqCanvas seqCanvas;
38 public AlignmentPanel ap;
40 protected int lastres;
41 protected int startseq;
43 protected AlignViewport av;
45 // if character is inserted or deleted, we will need to recalculate the conservation
46 boolean seqEditOccurred = false;
48 ScrollThread scrollThread = null;
49 boolean mouseDragging = false;
50 boolean editingSeqs = false;
51 boolean groupEditing = false;
54 boolean changeEndSeq = false;
55 boolean changeStartSeq = false;
56 boolean changeEndRes = false;
57 boolean changeStartRes = false;
58 SequenceGroup stretchGroup = null;
60 StringBuffer keyboardNo1;
61 StringBuffer keyboardNo2;
63 boolean mouseWheelPressed = false;
66 EditCommand editCommand;
68 StructureSelectionManager ssm;
71 public SeqPanel(AlignViewport avp, AlignmentPanel p)
75 seqCanvas = new SeqCanvas(avp);
76 setLayout(new BorderLayout());
81 seqCanvas.addMouseMotionListener(this);
82 seqCanvas.addMouseListener(this);
83 ssm = StructureSelectionManager.getStructureSelectionManager();
84 ssm.addStructureViewerListener(this);
91 if (editCommand != null && editCommand.getSize() > 0)
93 ap.alignFrame.addHistoryItem(editCommand);
94 av.firePropertyChange("alignment", null,
95 av.getAlignment().getSequences());
101 groupEditing = false;
109 seqCanvas.cursorY = getKeyboardNo1() - 1;
113 void setCursorColumn()
115 seqCanvas.cursorX = getKeyboardNo1() - 1;
119 void setCursorRowAndColumn()
121 if (keyboardNo2 == null)
123 keyboardNo2 = new StringBuffer();
127 seqCanvas.cursorX = getKeyboardNo1() - 1;
128 seqCanvas.cursorY = getKeyboardNo2() - 1;
133 void setCursorPosition()
136 (Sequence) av.getAlignment().getSequenceAt(seqCanvas.cursorY);
138 seqCanvas.cursorX = sequence.findIndex(
144 void moveCursor(int dx, int dy)
146 seqCanvas.cursorX += dx;
147 seqCanvas.cursorY += dy;
148 if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))
150 int original = seqCanvas.cursorX - dx;
151 int maxWidth = av.alignment.getWidth();
153 while (!av.colSel.isVisible(seqCanvas.cursorX)
154 && seqCanvas.cursorX < maxWidth
155 && seqCanvas.cursorX > 0)
157 seqCanvas.cursorX += dx;
160 if (seqCanvas.cursorX >= maxWidth
161 || !av.colSel.isVisible(seqCanvas.cursorX))
163 seqCanvas.cursorX = original;
169 void scrollToVisible()
171 if (seqCanvas.cursorX < 0)
173 seqCanvas.cursorX = 0;
175 else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)
177 seqCanvas.cursorX = av.alignment.getWidth() - 1;
180 if (seqCanvas.cursorY < 0)
182 seqCanvas.cursorY = 0;
184 else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)
186 seqCanvas.cursorY = av.alignment.getHeight() - 1;
190 if (av.wrapAlignment)
192 ap.scrollToWrappedVisible(seqCanvas.cursorX);
196 while (seqCanvas.cursorY < av.startSeq)
200 while (seqCanvas.cursorY + 1 > av.endSeq)
204 while (seqCanvas.cursorX < av.colSel.adjustForHiddenColumns(av.startRes))
207 if (!ap.scrollRight(false))
212 while (seqCanvas.cursorX > av.colSel.adjustForHiddenColumns(av.endRes))
214 if (!ap.scrollRight(true))
220 setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),
221 seqCanvas.cursorX, seqCanvas.cursorY);
226 void setSelectionAreaAtCursor(boolean topLeft)
229 (Sequence) av.getAlignment().getSequenceAt(seqCanvas.cursorY);
231 if (av.getSelectionGroup() != null)
233 SequenceGroup sg = av.selectionGroup;
234 //Find the top and bottom of this group
235 int min = av.alignment.getHeight(), max = 0;
236 for (int i = 0; i < sg.getSize(); i++)
238 int index = av.alignment.findIndex(sg.getSequenceAt(i));
253 sg.setStartRes(seqCanvas.cursorX);
254 if (sg.getEndRes() < seqCanvas.cursorX)
256 sg.setEndRes(seqCanvas.cursorX);
259 min = seqCanvas.cursorY;
263 sg.setEndRes(seqCanvas.cursorX);
264 if (sg.getStartRes() > seqCanvas.cursorX)
266 sg.setStartRes(seqCanvas.cursorX);
269 max = seqCanvas.cursorY + 1;
274 // Only the user can do this
275 av.setSelectionGroup(null);
279 // Now add any sequences between min and max
280 sg.getSequences(null).removeAllElements();
281 for (int i = min; i < max; i++)
283 sg.addSequence(av.alignment.getSequenceAt(i), false);
288 if (av.getSelectionGroup() == null)
290 SequenceGroup sg = new SequenceGroup();
291 sg.setStartRes(seqCanvas.cursorX);
292 sg.setEndRes(seqCanvas.cursorX);
293 sg.addSequence(sequence, false);
294 av.setSelectionGroup(sg);
297 ap.paintAlignment(false);
300 void insertGapAtCursor(boolean group)
302 groupEditing = group;
303 startseq = seqCanvas.cursorY;
304 lastres = seqCanvas.cursorX;
305 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
309 void deleteGapAtCursor(boolean group)
311 groupEditing = group;
312 startseq = seqCanvas.cursorY;
313 lastres = seqCanvas.cursorX + getKeyboardNo1();
314 editSequence(false, seqCanvas.cursorX);
318 void numberPressed(char value)
320 if (keyboardNo1 == null)
322 keyboardNo1 = new StringBuffer();
325 if (keyboardNo2 != null)
327 keyboardNo2.append(value);
331 keyboardNo1.append(value);
337 if (keyboardNo1 == null)
341 int value = Integer.parseInt(keyboardNo1.toString());
349 if (keyboardNo2 == null)
353 int value = Integer.parseInt(keyboardNo2.toString());
360 void setStatusMessage(SequenceI sequence, int res, int seq)
362 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: " +
366 if (av.alignment.isNucleotide())
368 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res) +
372 text.append(" Nucleotide: ");
377 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
380 text.append(" Residue: ");
389 text.append(obj + " (" + sequence.findPosition(res) +
394 ap.alignFrame.statusBar.setText(text.toString());
398 public void mousePressed(MouseEvent evt)
400 lastMousePress = evt.getPoint();
402 //For now, ignore the mouseWheel font resizing on Macs
403 //As the Button2_mask always seems to be true
404 if ( (evt.getModifiers() & InputEvent.BUTTON2_MASK) ==
405 InputEvent.BUTTON2_MASK && !av.MAC)
407 mouseWheelPressed = true;
411 if (evt.isShiftDown()
412 || evt.isControlDown()
415 if (evt.isControlDown() || evt.isAltDown())
423 doMousePressedDefineMode(evt);
427 int seq = findSeq(evt);
428 int res = findRes(evt);
430 if (seq < 0 || res < 0)
435 if ( (seq < av.getAlignment().getHeight()) &&
436 (res < av.getAlignment().getSequenceAt(seq).getLength()))
450 public void mouseClicked(MouseEvent evt)
452 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
453 if (evt.getClickCount() > 1)
455 if (av.getSelectionGroup().getSize() == 1
456 && av.getSelectionGroup().getEndRes()
457 - av.getSelectionGroup().getStartRes() < 2)
459 av.setSelectionGroup(null);
462 SequenceFeature[] features = findFeaturesAtRes(
464 sequence.findPosition(findRes(evt))
467 if (features != null && features.length > 0)
469 SearchResults highlight = new SearchResults();
470 highlight.addResult(sequence,
471 features[0].getBegin(),
472 features[0].getEnd());
473 seqCanvas.highlightSearchResults(highlight);
475 if (features != null && features.length > 0)
477 seqCanvas.getFeatureRenderer().amendFeatures(
479 {sequence}, features, false, ap);
481 seqCanvas.highlightSearchResults(null);
486 public void mouseReleased(MouseEvent evt)
488 mouseDragging = false;
489 mouseWheelPressed = false;
490 ap.paintAlignment(true);
494 doMouseReleasedDefineMode(evt);
502 int startWrapBlock = -1;
503 int wrappedBlock = -1;
504 int findRes(MouseEvent evt)
509 if (av.wrapAlignment)
512 int hgap = av.charHeight;
513 if (av.scaleAboveWrapped)
515 hgap += av.charHeight;
518 int cHeight = av.getAlignment().getHeight() * av.charHeight
519 + hgap + seqCanvas.getAnnotationHeight();
523 x -= seqCanvas.LABEL_WEST;
525 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
531 wrappedBlock = y / cHeight;
532 wrappedBlock += av.getStartRes() / cwidth;
534 res = wrappedBlock * cwidth + x / av.getCharWidth();
539 res = (x / av.getCharWidth()) + av.getStartRes();
542 if (av.hasHiddenColumns)
544 res = av.getColumnSelection().adjustForHiddenColumns(res);
551 int findSeq(MouseEvent evt)
553 final int sqnum = findAlRow(evt);
554 return (sqnum<0) ? 0 : sqnum;
559 * @return row in alignment that was selected (or -1 for column selection)
561 private int findAlRow(MouseEvent evt)
566 if (av.wrapAlignment)
568 int hgap = av.charHeight;
569 if (av.scaleAboveWrapped)
571 hgap += av.charHeight;
574 int cHeight = av.getAlignment().getHeight() * av.charHeight
575 + hgap + seqCanvas.getAnnotationHeight();
579 seq = Math.min( (y % cHeight) / av.getCharHeight(),
580 av.alignment.getHeight() - 1);
588 seq = Math.min( (y / av.getCharHeight()) + av.getStartSeq(),
589 av.alignment.getHeight() - 1);
601 public void doMousePressed(MouseEvent evt)
604 int seq = findSeq(evt);
605 int res = findRes(evt);
607 if (seq < av.getAlignment().getHeight() &&
608 res < av.getAlignment().getSequenceAt(seq).getLength())
610 //char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
611 // Find the residue's position in the sequence (res is the position
628 public void mouseOverSequence(SequenceI sequence, int index, int pos)
630 String tmp = sequence.hashCode()+index+"";
631 if (lastMessage == null || !lastMessage.equals(tmp))
632 ssm.mouseOverSequence(sequence, index, pos);
638 public void highlightSequence(SearchResults results)
640 seqCanvas.highlightSearchResults(results);
643 public void updateColours(SequenceI seq, int index)
645 System.out.println("update the seqPanel colours");
649 public void mouseMoved(MouseEvent evt)
651 int res = findRes(evt);
652 int seq = findSeq(evt);
654 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
663 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
664 if (res > sequence.getLength())
673 int respos = sequence.findPosition(res);
675 mouseOverSequence(sequence, res, respos);
678 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: " +
682 if (av.alignment.isNucleotide())
684 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res) +
688 text.append(" Nucleotide: ");
693 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
696 text.append(" Residue: ");
704 text.append(obj + " (" + respos + ")");
708 ap.alignFrame.statusBar.setText(text.toString());
710 StringBuffer tooltipText = new StringBuffer();
711 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
714 for (int g = 0; g < groups.length; g++)
716 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
718 if (!groups[g].getName().startsWith("JTreeGroup") &&
719 !groups[g].getName().startsWith("JGroup"))
721 tooltipText.append(groups[g].getName() + " ");
723 if (groups[g].getDescription() != null)
725 tooltipText.append(groups[g].getDescription());
727 tooltipText.append("\n");
732 // use aa to see if the mouse pointer is on a
733 SequenceFeature [] allFeatures = findFeaturesAtRes(sequence,
734 sequence.findPosition(res));
737 while (index < allFeatures.length)
739 SequenceFeature sf = allFeatures[index];
741 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
743 if (sf.getDescription() != null)
745 tooltipText.append(" " + sf.getDescription());
748 if (sf.getValue("status") != null)
750 String status = sf.getValue("status").toString();
751 if (status.length() > 0)
753 tooltipText.append(" (" + sf.getValue("status") + ")");
756 tooltipText.append("\n");
763 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
767 tooltip.setTip(tooltipText.toString());
771 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
773 Vector tmp = new Vector();
774 SequenceFeature[] features = sequence.getSequenceFeatures();
775 if (features != null)
777 for (int i = 0; i < features.length; i++)
779 if (av.featuresDisplayed == null
780 || !av.featuresDisplayed.containsKey(features[i].getType()))
787 if (features[i].featureGroup != null
788 && seqCanvas.fr.featureGroups!=null
789 && seqCanvas.fr.featureGroups.containsKey(features[i].featureGroup)
790 && !((Boolean)seqCanvas.fr.featureGroups.get(features[i].featureGroup)).booleanValue())
794 if ( (features[i].getBegin() <= res) &&
795 (features[i].getEnd() >= res))
797 tmp.addElement(features[i]);
802 features = new SequenceFeature[tmp.size()];
803 tmp.copyInto(features);
811 public void mouseDragged(MouseEvent evt)
813 if (mouseWheelPressed)
815 int oldWidth = av.charWidth;
817 //Which is bigger, left-right or up-down?
818 if (Math.abs(evt.getY() - lastMousePress.y)
819 > Math.abs(evt.getX() - lastMousePress.x))
821 int fontSize = av.font.getSize();
823 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
827 else if (evt.getY() > lastMousePress.y)
837 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
838 av.charWidth = oldWidth;
842 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
846 else if (evt.getX() > lastMousePress.x)
851 if (av.charWidth < 1)
859 FontMetrics fm = getFontMetrics(av.getFont());
860 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
862 lastMousePress = evt.getPoint();
864 ap.paintAlignment(false);
865 ap.annotationPanel.image = null;
871 doMouseDraggedDefineMode(evt);
875 int res = findRes(evt);
882 if ( (lastres == -1) || (lastres == res))
887 if ( (res < av.getAlignment().getWidth()) && (res < lastres))
889 // dragLeft, delete gap
890 editSequence(false, res);
894 editSequence(true, res);
897 mouseDragging = true;
898 if (scrollThread != null)
900 scrollThread.setEvent(evt);
905 synchronized void editSequence(boolean insertGap, int startres)
909 boolean fixedColumns = false;
910 SequenceGroup sg = av.getSelectionGroup();
912 SequenceI seq = av.alignment.getSequenceAt(startseq);
914 if (!groupEditing && av.hasHiddenRows)
916 if (av.hiddenRepSequences != null
917 && av.hiddenRepSequences.containsKey(seq))
919 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
924 StringBuffer message = new StringBuffer();
927 message.append("Edit group:");
928 if (editCommand == null)
930 editCommand = new EditCommand("Edit Group");
935 message.append("Edit sequence: " + seq.getName());
936 String label = seq.getName();
937 if (label.length() > 10)
939 label = label.substring(0, 10);
941 if (editCommand == null)
943 editCommand = new EditCommand("Edit " + label);
949 message.append(" insert ");
953 message.append(" delete ");
956 message.append(Math.abs(startres - lastres) + " gaps.");
957 ap.alignFrame.statusBar.setText(message.toString());
959 //Are we editing within a selection group?
961 || (sg != null && sg.getSequences(av.hiddenRepSequences).contains(seq)))
965 //sg might be null as the user may only see 1 sequence,
966 //but the sequence represents a group
969 if (av.hiddenRepSequences == null
970 || !av.hiddenRepSequences.containsKey(seq))
976 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
979 fixedLeft = sg.getStartRes();
980 fixedRight = sg.getEndRes();
982 if ( (startres < fixedLeft && lastres >= fixedLeft)
983 || (startres >= fixedLeft && lastres < fixedLeft)
984 || (startres > fixedRight && lastres <= fixedRight)
985 || (startres <= fixedRight && lastres > fixedRight))
991 if (fixedLeft > startres)
993 fixedRight = fixedLeft - 1;
996 else if (fixedRight < startres)
998 fixedLeft = fixedRight;
1003 if (av.hasHiddenColumns)
1005 fixedColumns = true;
1006 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1007 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1009 if ( (insertGap && startres > y1 && lastres < y1)
1010 || (!insertGap && startres < y2 && lastres > y2))
1016 //System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1017 //Selection spans a hidden region
1018 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1026 fixedRight = y2 - 1;
1033 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1034 int g, groupSize = vseqs.size();
1035 SequenceI[] groupSeqs = new SequenceI[groupSize];
1036 for (g = 0; g < groupSeqs.length; g++)
1038 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1044 //If the user has selected the whole sequence, and is dragging to
1045 // the right, we can still extend the alignment and selectionGroup
1046 if (sg.getStartRes() == 0
1047 && sg.getEndRes() == fixedRight
1048 && sg.getEndRes() == av.alignment.getWidth() - 1
1051 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1052 fixedRight = sg.getEndRes();
1055 // Is it valid with fixed columns??
1056 // Find the next gap before the end
1057 // of the visible region boundary
1058 boolean blank = false;
1059 for (fixedRight = fixedRight;
1060 fixedRight > lastres;
1065 for (g = 0; g < groupSize; g++)
1067 for (int j = 0; j < startres - lastres; j++)
1069 if (!jalview.util.Comparison.isGap(
1070 groupSeqs[g].getCharAt(fixedRight - j)))
1085 if (sg.getSize() == av.alignment.getHeight())
1087 if ( (av.hasHiddenColumns
1089 startres < av.getColumnSelection().getHiddenBoundaryRight(startres)))
1095 int alWidth = av.alignment.getWidth();
1096 if (av.hasHiddenRows)
1098 int hwidth = av.alignment.getHiddenSequences().getWidth();
1099 if (hwidth > alWidth)
1104 //We can still insert gaps if the selectionGroup
1105 //contains all the sequences
1106 sg.setEndRes(sg.getEndRes() + startres - lastres);
1107 fixedRight = alWidth + startres - lastres;
1118 else if (!insertGap)
1120 /// Are we able to delete?
1121 // ie are all columns blank?
1123 for (g = 0; g < groupSize; g++)
1125 for (int j = startres; j < lastres; j++)
1127 if (groupSeqs[g].getLength() <= j)
1132 if (!jalview.util.Comparison.isGap(
1133 groupSeqs[g].getCharAt(j)))
1135 // Not a gap, block edit not valid
1145 // dragging to the right
1146 if (fixedColumns && fixedRight != -1)
1148 for (int j = lastres; j < startres; j++)
1150 insertChar(j, groupSeqs, fixedRight);
1155 editCommand.appendEdit(EditCommand.INSERT_GAP,
1157 startres, startres - lastres,
1164 // dragging to the left
1165 if (fixedColumns && fixedRight != -1)
1167 for (int j = lastres; j > startres; j--)
1169 deleteChar(startres, groupSeqs, fixedRight);
1174 editCommand.appendEdit(EditCommand.DELETE_GAP,
1176 startres, lastres - startres,
1183 else /////Editing a single sequence///////////
1187 // dragging to the right
1188 if (fixedColumns && fixedRight != -1)
1190 for (int j = lastres; j < startres; j++)
1192 insertChar(j, new SequenceI[]
1198 editCommand.appendEdit(EditCommand.INSERT_GAP,
1201 lastres, startres - lastres,
1208 // dragging to the left
1209 if (fixedColumns && fixedRight != -1)
1211 for (int j = lastres; j > startres; j--)
1213 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1218 deleteChar(startres, new SequenceI[]
1224 //could be a keyboard edit trying to delete none gaps
1226 for (int m = startres; m < lastres; m++)
1228 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1237 editCommand.appendEdit(EditCommand.DELETE_GAP,
1249 seqCanvas.repaint();
1252 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1254 int blankColumn = fixedColumn;
1255 for (int s = 0; s < seq.length; s++)
1257 //Find the next gap before the end of the visible region boundary
1258 //If lastCol > j, theres a boundary after the gap insertion
1260 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1262 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1264 //Theres a space, so break and insert the gap
1269 if (blankColumn <= j)
1271 blankColumn = fixedColumn;
1277 editCommand.appendEdit(EditCommand.DELETE_GAP,
1279 blankColumn, 1, av.alignment, true);
1281 editCommand.appendEdit(EditCommand.INSERT_GAP,
1288 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1291 editCommand.appendEdit(EditCommand.DELETE_GAP,
1293 j, 1, av.alignment, true);
1295 editCommand.appendEdit(EditCommand.INSERT_GAP,
1297 fixedColumn, 1, av.alignment, true);
1300 //////////////////////////////////////////
1301 /////Everything below this is for defining the boundary of the rubberband
1302 //////////////////////////////////////////
1303 public void doMousePressedDefineMode(MouseEvent evt)
1305 if (scrollThread != null)
1307 scrollThread.running = false;
1308 scrollThread = null;
1311 int res = findRes(evt);
1312 int seq = findSeq(evt);
1314 startWrapBlock = wrappedBlock;
1321 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1323 if (sequence == null || res > sequence.getLength())
1328 stretchGroup = av.getSelectionGroup();
1330 if (stretchGroup == null)
1332 stretchGroup = av.alignment.findGroup(sequence);
1333 if (stretchGroup != null && res > stretchGroup.getStartRes() &&
1334 res < stretchGroup.getEndRes())
1336 av.setSelectionGroup(stretchGroup);
1340 stretchGroup = null;
1344 else if (!stretchGroup.getSequences(null).contains(sequence)
1345 || stretchGroup.getStartRes() > res
1346 || stretchGroup.getEndRes() < res)
1348 stretchGroup = null;
1350 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1352 if (allGroups != null)
1354 for (int i = 0; i < allGroups.length; i++)
1356 if (allGroups[i].getStartRes() <= res &&
1357 allGroups[i].getEndRes() >= res)
1359 stretchGroup = allGroups[i];
1364 av.setSelectionGroup(stretchGroup);
1367 // DETECT RIGHT MOUSE BUTTON IN AWT
1368 if ( (evt.getModifiers() & InputEvent.BUTTON3_MASK) ==
1369 InputEvent.BUTTON3_MASK)
1371 SequenceFeature [] allFeatures = findFeaturesAtRes(sequence,
1372 sequence.findPosition(res));
1374 Vector links = null;
1375 if (allFeatures != null)
1377 for (int i = 0; i < allFeatures.length; i++)
1379 if (allFeatures[i].links != null)
1381 links = new Vector();
1382 for (int j = 0; j < allFeatures[i].links.size(); j++)
1384 links.addElement(allFeatures[i].links.elementAt(j));
1389 APopupMenu popup = new APopupMenu(ap, null, links);
1391 popup.show(this, evt.getX(), evt.getY());
1397 seqCanvas.cursorX = findRes(evt);
1398 seqCanvas.cursorY = findSeq(evt);
1399 seqCanvas.repaint();
1403 //Only if left mouse button do we want to change group sizes
1405 if (stretchGroup == null)
1407 // define a new group here
1408 SequenceGroup sg = new SequenceGroup();
1409 sg.setStartRes(res);
1411 sg.addSequence(sequence, false);
1412 av.setSelectionGroup(sg);
1415 if (av.getConservationSelected())
1417 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1420 if (av.getAbovePIDThreshold())
1422 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1429 public void doMouseReleasedDefineMode(MouseEvent evt)
1431 if (stretchGroup == null)
1436 if (stretchGroup.cs != null)
1438 if (stretchGroup.cs instanceof ClustalxColourScheme)
1440 ( (ClustalxColourScheme) stretchGroup.cs).resetClustalX(
1441 stretchGroup.getSequences(av.hiddenRepSequences),
1442 stretchGroup.getWidth());
1445 if (stretchGroup.cs instanceof Blosum62ColourScheme
1446 || stretchGroup.cs instanceof PIDColourScheme
1447 || stretchGroup.cs.conservationApplied()
1448 || stretchGroup.cs.getThreshold() > 0)
1450 stretchGroup.recalcConservation();
1453 if (stretchGroup.cs.conservationApplied())
1455 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1456 stretchGroup.getName());
1457 stretchGroup.recalcConservation();
1461 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1462 stretchGroup.getName());
1465 changeEndRes = false;
1466 changeStartRes = false;
1467 stretchGroup = null;
1468 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1469 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.alignment.getHeight())
1491 y = av.alignment.getHeight() - 1;
1494 if (res >= av.alignment.getWidth())
1496 res = av.alignment.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) && (y < av.alignment.getHeight()))
1543 // This routine ensures we don't skip any sequences, as the
1544 // selection is quite slow.
1545 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1547 oldSeq += dragDirection;
1554 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1556 if (stretchGroup.getSequences(null).contains(nextSeq))
1558 stretchGroup.deleteSequence(seq, false);
1564 stretchGroup.addSequence(seq, false);
1567 stretchGroup.addSequence(nextSeq, false);
1576 if (res > av.endRes || res < av.startRes
1577 || y < av.startSeq || y > av.endSeq)
1582 if (scrollThread != null)
1584 scrollThread.setEvent(evt);
1587 seqCanvas.repaint();
1590 public void mouseEntered(MouseEvent e)
1597 if (scrollThread != null)
1599 scrollThread.running = false;
1600 scrollThread = null;
1604 public void mouseExited(MouseEvent e)
1606 if (av.getWrapAlignment())
1611 if (mouseDragging && scrollThread == null)
1613 scrollThread = new ScrollThread();
1617 void scrollCanvas(MouseEvent evt)
1621 if (scrollThread != null)
1623 scrollThread.running = false;
1624 scrollThread = null;
1626 mouseDragging = false;
1630 if (scrollThread == null)
1632 scrollThread = new ScrollThread();
1635 mouseDragging = true;
1636 scrollThread.setEvent(evt);
1641 // this class allows scrolling off the bottom of the visible alignment
1646 boolean running = false;
1647 public ScrollThread()
1652 public void setEvent(MouseEvent e)
1657 public void stopScrolling()
1671 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1673 running = ap.scrollUp(true);
1676 if (mouseDragging && evt.getY() >= getSize().height &&
1677 av.alignment.getHeight() > av.getEndSeq())
1679 running = ap.scrollUp(false);
1682 if (mouseDragging && evt.getX() < 0)
1684 running = ap.scrollRight(false);
1687 else if (mouseDragging && evt.getX() >= getSize().width)
1689 running = ap.scrollRight(true);
1697 catch (Exception ex)