2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
23 import java.awt.event.*;
25 import jalview.commands.*;
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
28 import jalview.structure.SequenceListener;
29 import jalview.structure.StructureSelectionManager;
31 public class SeqPanel extends Panel implements MouseMotionListener,
32 MouseListener, SequenceListener
35 public SeqCanvas seqCanvas;
37 public AlignmentPanel ap;
39 protected int lastres;
41 protected int startseq;
43 protected AlignViewport av;
45 // if character is inserted or deleted, we will need to recalculate the
47 boolean seqEditOccurred = false;
49 ScrollThread scrollThread = null;
51 boolean mouseDragging = false;
53 boolean editingSeqs = false;
55 boolean groupEditing = false;
59 boolean changeEndSeq = false;
61 boolean changeStartSeq = false;
63 boolean changeEndRes = false;
65 boolean changeStartRes = false;
67 SequenceGroup stretchGroup = null;
69 StringBuffer keyboardNo1;
71 StringBuffer keyboardNo2;
73 boolean mouseWheelPressed = false;
77 EditCommand editCommand;
79 StructureSelectionManager ssm;
81 public SeqPanel(AlignViewport avp, AlignmentPanel p)
85 seqCanvas = new SeqCanvas(avp);
86 setLayout(new BorderLayout());
91 seqCanvas.addMouseMotionListener(this);
92 seqCanvas.addMouseListener(this);
93 ssm = StructureSelectionManager.getStructureSelectionManager();
94 ssm.addStructureViewerListener(this);
101 if (editCommand != null && editCommand.getSize() > 0)
103 ap.alignFrame.addHistoryItem(editCommand);
104 av.firePropertyChange("alignment", null, av.getAlignment()
111 groupEditing = false;
119 seqCanvas.cursorY = getKeyboardNo1() - 1;
123 void setCursorColumn()
125 seqCanvas.cursorX = getKeyboardNo1() - 1;
129 void setCursorRowAndColumn()
131 if (keyboardNo2 == null)
133 keyboardNo2 = new StringBuffer();
137 seqCanvas.cursorX = getKeyboardNo1() - 1;
138 seqCanvas.cursorY = getKeyboardNo2() - 1;
143 void setCursorPosition()
145 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
148 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
152 void moveCursor(int dx, int dy)
154 seqCanvas.cursorX += dx;
155 seqCanvas.cursorY += dy;
156 if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))
158 int original = seqCanvas.cursorX - dx;
159 int maxWidth = av.alignment.getWidth();
161 while (!av.colSel.isVisible(seqCanvas.cursorX)
162 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
164 seqCanvas.cursorX += dx;
167 if (seqCanvas.cursorX >= maxWidth
168 || !av.colSel.isVisible(seqCanvas.cursorX))
170 seqCanvas.cursorX = original;
176 void scrollToVisible()
178 if (seqCanvas.cursorX < 0)
180 seqCanvas.cursorX = 0;
182 else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)
184 seqCanvas.cursorX = av.alignment.getWidth() - 1;
187 if (seqCanvas.cursorY < 0)
189 seqCanvas.cursorY = 0;
191 else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)
193 seqCanvas.cursorY = av.alignment.getHeight() - 1;
197 if (av.wrapAlignment)
199 ap.scrollToWrappedVisible(seqCanvas.cursorX);
203 while (seqCanvas.cursorY < av.startSeq)
207 while (seqCanvas.cursorY + 1 > av.endSeq)
211 while (seqCanvas.cursorX < av.colSel
212 .adjustForHiddenColumns(av.startRes))
215 if (!ap.scrollRight(false))
220 while (seqCanvas.cursorX > av.colSel
221 .adjustForHiddenColumns(av.endRes))
223 if (!ap.scrollRight(true))
229 setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),
230 seqCanvas.cursorX, seqCanvas.cursorY);
235 void setSelectionAreaAtCursor(boolean topLeft)
237 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
240 if (av.getSelectionGroup() != null)
242 SequenceGroup sg = av.selectionGroup;
243 // Find the top and bottom of this group
244 int min = av.alignment.getHeight(), max = 0;
245 for (int i = 0; i < sg.getSize(); i++)
247 int index = av.alignment.findIndex(sg.getSequenceAt(i));
262 sg.setStartRes(seqCanvas.cursorX);
263 if (sg.getEndRes() < seqCanvas.cursorX)
265 sg.setEndRes(seqCanvas.cursorX);
268 min = seqCanvas.cursorY;
272 sg.setEndRes(seqCanvas.cursorX);
273 if (sg.getStartRes() > seqCanvas.cursorX)
275 sg.setStartRes(seqCanvas.cursorX);
278 max = seqCanvas.cursorY + 1;
283 // Only the user can do this
284 av.setSelectionGroup(null);
288 // Now add any sequences between min and max
289 sg.getSequences(null).removeAllElements();
290 for (int i = min; i < max; i++)
292 sg.addSequence(av.alignment.getSequenceAt(i), false);
297 if (av.getSelectionGroup() == null)
299 SequenceGroup sg = new SequenceGroup();
300 sg.setStartRes(seqCanvas.cursorX);
301 sg.setEndRes(seqCanvas.cursorX);
302 sg.addSequence(sequence, false);
303 av.setSelectionGroup(sg);
306 ap.paintAlignment(false);
309 void insertGapAtCursor(boolean group)
311 groupEditing = group;
312 startseq = seqCanvas.cursorY;
313 lastres = seqCanvas.cursorX;
314 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
318 void deleteGapAtCursor(boolean group)
320 groupEditing = group;
321 startseq = seqCanvas.cursorY;
322 lastres = seqCanvas.cursorX + getKeyboardNo1();
323 editSequence(false, seqCanvas.cursorX);
327 void numberPressed(char value)
329 if (keyboardNo1 == null)
331 keyboardNo1 = new StringBuffer();
334 if (keyboardNo2 != null)
336 keyboardNo2.append(value);
340 keyboardNo1.append(value);
346 if (keyboardNo1 == null)
350 int value = Integer.parseInt(keyboardNo1.toString());
358 if (keyboardNo2 == null)
362 int value = Integer.parseInt(keyboardNo2.toString());
368 void setStatusMessage(SequenceI sequence, int res, int seq)
370 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
371 + sequence.getName());
374 if (av.alignment.isNucleotide())
376 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
380 text.append(" Nucleotide: ");
385 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
388 text.append(" Residue: ");
397 text.append(obj + " (" + sequence.findPosition(res) + ")");
401 ap.alignFrame.statusBar.setText(text.toString());
405 public void mousePressed(MouseEvent evt)
407 lastMousePress = evt.getPoint();
409 // For now, ignore the mouseWheel font resizing on Macs
410 // As the Button2_mask always seems to be true
411 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
414 mouseWheelPressed = true;
418 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
420 if (evt.isControlDown() || evt.isAltDown())
428 doMousePressedDefineMode(evt);
432 int seq = findSeq(evt);
433 int res = findRes(evt);
435 if (seq < 0 || res < 0)
440 if ((seq < av.getAlignment().getHeight())
441 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
455 public void mouseClicked(MouseEvent evt)
457 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
458 if (evt.getClickCount() > 1)
460 if (av.getSelectionGroup().getSize() == 1
461 && av.getSelectionGroup().getEndRes()
462 - av.getSelectionGroup().getStartRes() < 2)
464 av.setSelectionGroup(null);
467 SequenceFeature[] features = findFeaturesAtRes(sequence, sequence
468 .findPosition(findRes(evt)));
470 if (features != null && features.length > 0)
472 SearchResults highlight = new SearchResults();
473 highlight.addResult(sequence, features[0].getBegin(), features[0]
475 seqCanvas.highlightSearchResults(highlight);
477 if (features != null && features.length > 0)
479 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
480 { sequence }, features, false, ap);
482 seqCanvas.highlightSearchResults(null);
487 public void mouseReleased(MouseEvent evt)
489 mouseDragging = false;
490 mouseWheelPressed = false;
491 ap.paintAlignment(true);
495 doMouseReleasedDefineMode(evt);
503 int startWrapBlock = -1;
505 int wrappedBlock = -1;
507 int findRes(MouseEvent evt)
512 if (av.wrapAlignment)
515 int hgap = av.charHeight;
516 if (av.scaleAboveWrapped)
518 hgap += av.charHeight;
521 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
522 + seqCanvas.getAnnotationHeight();
526 x -= seqCanvas.LABEL_WEST;
528 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
534 wrappedBlock = y / cHeight;
535 wrappedBlock += av.getStartRes() / cwidth;
537 res = wrappedBlock * cwidth + x / av.getCharWidth();
542 res = (x / av.getCharWidth()) + av.getStartRes();
545 if (av.hasHiddenColumns)
547 res = av.getColumnSelection().adjustForHiddenColumns(res);
554 int findSeq(MouseEvent evt)
556 final int sqnum = findAlRow(evt);
557 return (sqnum < 0) ? 0 : sqnum;
563 * @return row in alignment that was selected (or -1 for column selection)
565 private int findAlRow(MouseEvent evt)
570 if (av.wrapAlignment)
572 int hgap = av.charHeight;
573 if (av.scaleAboveWrapped)
575 hgap += av.charHeight;
578 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
579 + seqCanvas.getAnnotationHeight();
583 seq = Math.min((y % cHeight) / av.getCharHeight(), av.alignment
592 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
593 av.alignment.getHeight() - 1);
603 public void doMousePressed(MouseEvent evt)
606 int seq = findSeq(evt);
607 int res = findRes(evt);
609 if (seq < av.getAlignment().getHeight()
610 && res < av.getAlignment().getSequenceAt(seq).getLength())
612 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
613 // Find the residue's position in the sequence (res is the position
630 public void mouseOverSequence(SequenceI sequence, int index, int pos)
632 String tmp = sequence.hashCode() + index + "";
633 if (lastMessage == null || !lastMessage.equals(tmp))
634 ssm.mouseOverSequence(sequence, index, pos);
639 public void highlightSequence(SearchResults results)
641 seqCanvas.highlightSearchResults(results);
642 if (av.followHighlight)
644 ap.scrollToPosition(results);
648 public void updateColours(SequenceI seq, int index)
650 System.out.println("update the seqPanel colours");
654 public void mouseMoved(MouseEvent evt)
656 int res = findRes(evt);
657 int seq = findSeq(evt);
659 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
668 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
669 if (res > sequence.getLength())
678 int respos = sequence.findPosition(res);
680 mouseOverSequence(sequence, res, respos);
682 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
683 + sequence.getName());
686 if (av.alignment.isNucleotide())
688 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
692 text.append(" Nucleotide: ");
697 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
700 text.append(" Residue: ");
708 text.append(obj + " (" + respos + ")");
712 ap.alignFrame.statusBar.setText(text.toString());
714 StringBuffer tooltipText = new StringBuffer();
715 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
718 for (int g = 0; g < groups.length; g++)
720 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
722 if (!groups[g].getName().startsWith("JTreeGroup")
723 && !groups[g].getName().startsWith("JGroup"))
725 tooltipText.append(groups[g].getName() + " ");
727 if (groups[g].getDescription() != null)
729 tooltipText.append(groups[g].getDescription());
731 tooltipText.append("\n");
736 // use aa to see if the mouse pointer is on a
737 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence, sequence
741 while (index < allFeatures.length)
743 SequenceFeature sf = allFeatures[index];
745 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
747 if (sf.getDescription() != null)
749 tooltipText.append(" " + sf.getDescription());
752 if (sf.getValue("status") != null)
754 String status = sf.getValue("status").toString();
755 if (status.length() > 0)
757 tooltipText.append(" (" + sf.getValue("status") + ")");
760 tooltipText.append("\n");
767 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
771 tooltip.setTip(tooltipText.toString());
775 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
777 Vector tmp = new Vector();
778 SequenceFeature[] features = sequence.getSequenceFeatures();
779 if (features != null)
781 for (int i = 0; i < features.length; i++)
783 if (av.featuresDisplayed == null
784 || !av.featuresDisplayed.containsKey(features[i].getType()))
789 if (features[i].featureGroup != null
790 && seqCanvas.fr.featureGroups != null
791 && seqCanvas.fr.featureGroups
792 .containsKey(features[i].featureGroup)
793 && !((Boolean) seqCanvas.fr.featureGroups
794 .get(features[i].featureGroup)).booleanValue())
797 if ((features[i].getBegin() <= res)
798 && (features[i].getEnd() >= res))
800 tmp.addElement(features[i]);
805 features = new SequenceFeature[tmp.size()];
806 tmp.copyInto(features);
813 public void mouseDragged(MouseEvent evt)
815 if (mouseWheelPressed)
817 int oldWidth = av.charWidth;
819 // Which is bigger, left-right or up-down?
820 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
823 int fontSize = av.font.getSize();
825 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
829 else if (evt.getY() > lastMousePress.y)
840 .setFont(new Font(av.font.getName(), av.font.getStyle(),
842 av.charWidth = oldWidth;
846 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
850 else if (evt.getX() > lastMousePress.x)
855 if (av.charWidth < 1)
863 FontMetrics fm = getFontMetrics(av.getFont());
864 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
866 lastMousePress = evt.getPoint();
868 ap.paintAlignment(false);
869 ap.annotationPanel.image = null;
875 doMouseDraggedDefineMode(evt);
879 int res = findRes(evt);
886 if ((lastres == -1) || (lastres == res))
891 if ((res < av.getAlignment().getWidth()) && (res < lastres))
893 // dragLeft, delete gap
894 editSequence(false, res);
898 editSequence(true, res);
901 mouseDragging = true;
902 if (scrollThread != null)
904 scrollThread.setEvent(evt);
909 synchronized void editSequence(boolean insertGap, int startres)
913 boolean fixedColumns = false;
914 SequenceGroup sg = av.getSelectionGroup();
916 SequenceI seq = av.alignment.getSequenceAt(startseq);
918 if (!groupEditing && av.hasHiddenRows)
920 if (av.hiddenRepSequences != null
921 && av.hiddenRepSequences.containsKey(seq))
923 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
928 StringBuffer message = new StringBuffer();
931 message.append("Edit group:");
932 if (editCommand == null)
934 editCommand = new EditCommand("Edit Group");
939 message.append("Edit sequence: " + seq.getName());
940 String label = seq.getName();
941 if (label.length() > 10)
943 label = label.substring(0, 10);
945 if (editCommand == null)
947 editCommand = new EditCommand("Edit " + label);
953 message.append(" insert ");
957 message.append(" delete ");
960 message.append(Math.abs(startres - lastres) + " gaps.");
961 ap.alignFrame.statusBar.setText(message.toString());
963 // Are we editing within a selection group?
965 || (sg != null && sg.getSequences(av.hiddenRepSequences)
970 // sg might be null as the user may only see 1 sequence,
971 // but the sequence represents a group
974 if (av.hiddenRepSequences == null
975 || !av.hiddenRepSequences.containsKey(seq))
981 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
984 fixedLeft = sg.getStartRes();
985 fixedRight = sg.getEndRes();
987 if ((startres < fixedLeft && lastres >= fixedLeft)
988 || (startres >= fixedLeft && lastres < fixedLeft)
989 || (startres > fixedRight && lastres <= fixedRight)
990 || (startres <= fixedRight && lastres > fixedRight))
996 if (fixedLeft > startres)
998 fixedRight = fixedLeft - 1;
1001 else if (fixedRight < startres)
1003 fixedLeft = fixedRight;
1008 if (av.hasHiddenColumns)
1010 fixedColumns = true;
1011 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1012 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1014 if ((insertGap && startres > y1 && lastres < y1)
1015 || (!insertGap && startres < y2 && lastres > y2))
1021 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1022 // Selection spans a hidden region
1023 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1031 fixedRight = y2 - 1;
1038 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1039 int g, groupSize = vseqs.size();
1040 SequenceI[] groupSeqs = new SequenceI[groupSize];
1041 for (g = 0; g < groupSeqs.length; g++)
1043 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1049 // If the user has selected the whole sequence, and is dragging to
1050 // the right, we can still extend the alignment and selectionGroup
1051 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1052 && sg.getEndRes() == av.alignment.getWidth() - 1)
1054 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1055 fixedRight = sg.getEndRes();
1058 // Is it valid with fixed columns??
1059 // Find the next gap before the end
1060 // of the visible region boundary
1061 boolean blank = false;
1062 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1066 for (g = 0; g < groupSize; g++)
1068 for (int j = 0; j < startres - lastres; j++)
1070 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1071 .getCharAt(fixedRight - j)))
1086 if (sg.getSize() == av.alignment.getHeight())
1088 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1089 .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(groupSeqs[g].getCharAt(j)))
1134 // Not a gap, block edit not valid
1144 // dragging to the right
1145 if (fixedColumns && fixedRight != -1)
1147 for (int j = lastres; j < startres; j++)
1149 insertChar(j, groupSeqs, fixedRight);
1154 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1155 startres, startres - lastres, av.alignment, true);
1160 // dragging to the left
1161 if (fixedColumns && fixedRight != -1)
1163 for (int j = lastres; j > startres; j--)
1165 deleteChar(startres, groupSeqs, fixedRight);
1170 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1171 startres, lastres - startres, av.alignment, true);
1177 // ///Editing a single sequence///////////
1181 // dragging to the right
1182 if (fixedColumns && fixedRight != -1)
1184 for (int j = lastres; j < startres; j++)
1186 insertChar(j, new SequenceI[]
1187 { seq }, fixedRight);
1192 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1193 { seq }, lastres, startres - lastres, av.alignment, true);
1198 // dragging to the left
1199 if (fixedColumns && fixedRight != -1)
1201 for (int j = lastres; j > startres; j--)
1203 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1208 deleteChar(startres, new SequenceI[]
1209 { seq }, fixedRight);
1214 // could be a keyboard edit trying to delete none gaps
1216 for (int m = startres; m < lastres; m++)
1218 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1227 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1228 { seq }, startres, max, av.alignment, true);
1235 seqCanvas.repaint();
1238 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1240 int blankColumn = fixedColumn;
1241 for (int s = 0; s < seq.length; s++)
1243 // Find the next gap before the end of the visible region boundary
1244 // If lastCol > j, theres a boundary after the gap insertion
1246 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1248 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1250 // Theres a space, so break and insert the gap
1255 if (blankColumn <= j)
1257 blankColumn = fixedColumn;
1263 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1264 av.alignment, true);
1266 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1271 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1274 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1277 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1278 av.alignment, true);
1281 // ////////////////////////////////////////
1282 // ///Everything below this is for defining the boundary of the rubberband
1283 // ////////////////////////////////////////
1284 public void doMousePressedDefineMode(MouseEvent evt)
1286 if (scrollThread != null)
1288 scrollThread.running = false;
1289 scrollThread = null;
1292 int res = findRes(evt);
1293 int seq = findSeq(evt);
1295 startWrapBlock = wrappedBlock;
1302 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1304 if (sequence == null || res > sequence.getLength())
1309 stretchGroup = av.getSelectionGroup();
1311 if (stretchGroup == null)
1313 stretchGroup = av.alignment.findGroup(sequence);
1314 if (stretchGroup != null && res > stretchGroup.getStartRes()
1315 && res < stretchGroup.getEndRes())
1317 av.setSelectionGroup(stretchGroup);
1321 stretchGroup = null;
1325 else if (!stretchGroup.getSequences(null).contains(sequence)
1326 || stretchGroup.getStartRes() > res
1327 || stretchGroup.getEndRes() < res)
1329 stretchGroup = null;
1331 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1333 if (allGroups != null)
1335 for (int i = 0; i < allGroups.length; i++)
1337 if (allGroups[i].getStartRes() <= res
1338 && allGroups[i].getEndRes() >= res)
1340 stretchGroup = allGroups[i];
1345 av.setSelectionGroup(stretchGroup);
1348 // DETECT RIGHT MOUSE BUTTON IN AWT
1349 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1351 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence, sequence
1352 .findPosition(res));
1354 Vector links = null;
1355 if (allFeatures != null)
1357 for (int i = 0; i < allFeatures.length; i++)
1359 if (allFeatures[i].links != null)
1363 links = new Vector();
1365 for (int j = 0; j < allFeatures[i].links.size(); j++)
1367 links.addElement(allFeatures[i].links.elementAt(j));
1372 APopupMenu popup = new APopupMenu(ap, null, links);
1374 popup.show(this, evt.getX(), evt.getY());
1380 seqCanvas.cursorX = findRes(evt);
1381 seqCanvas.cursorY = findSeq(evt);
1382 seqCanvas.repaint();
1386 // Only if left mouse button do we want to change group sizes
1388 if (stretchGroup == null)
1390 // define a new group here
1391 SequenceGroup sg = new SequenceGroup();
1392 sg.setStartRes(res);
1394 sg.addSequence(sequence, false);
1395 av.setSelectionGroup(sg);
1398 if (av.getConservationSelected())
1400 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1403 if (av.getAbovePIDThreshold())
1405 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1412 public void doMouseReleasedDefineMode(MouseEvent evt)
1414 if (stretchGroup == null)
1419 if (stretchGroup.cs != null)
1421 if (stretchGroup.cs instanceof ClustalxColourScheme)
1423 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup
1424 .getSequences(av.hiddenRepSequences), stretchGroup
1428 if (stretchGroup.cs instanceof Blosum62ColourScheme
1429 || stretchGroup.cs instanceof PIDColourScheme
1430 || stretchGroup.cs.conservationApplied()
1431 || stretchGroup.cs.getThreshold() > 0)
1433 stretchGroup.recalcConservation();
1436 if (stretchGroup.cs.conservationApplied())
1438 SliderPanel.setConservationSlider(ap, stretchGroup.cs, stretchGroup
1440 stretchGroup.recalcConservation();
1444 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs, stretchGroup
1448 changeEndRes = false;
1449 changeStartRes = false;
1450 stretchGroup = null;
1451 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1452 ap.paintAlignment(true);
1455 public void doMouseDraggedDefineMode(MouseEvent evt)
1457 int res = findRes(evt);
1458 int y = findSeq(evt);
1460 if (wrappedBlock != startWrapBlock)
1465 if (stretchGroup == null)
1470 mouseDragging = true;
1472 if (y > av.alignment.getHeight())
1474 y = av.alignment.getHeight() - 1;
1477 if (res >= av.alignment.getWidth())
1479 res = av.alignment.getWidth() - 1;
1482 if (stretchGroup.getEndRes() == res)
1484 // Edit end res position of selected group
1485 changeEndRes = true;
1487 else if (stretchGroup.getStartRes() == res)
1489 // Edit start res position of selected group
1490 changeStartRes = true;
1500 if (res > (stretchGroup.getStartRes() - 1))
1502 stretchGroup.setEndRes(res);
1505 else if (changeStartRes)
1507 if (res < (stretchGroup.getEndRes() + 1))
1509 stretchGroup.setStartRes(res);
1513 int dragDirection = 0;
1519 else if (y < oldSeq)
1524 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1526 // This routine ensures we don't skip any sequences, as the
1527 // selection is quite slow.
1528 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1530 oldSeq += dragDirection;
1537 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1539 if (stretchGroup.getSequences(null).contains(nextSeq))
1541 stretchGroup.deleteSequence(seq, false);
1547 stretchGroup.addSequence(seq, false);
1550 stretchGroup.addSequence(nextSeq, false);
1559 if (res > av.endRes || res < av.startRes || y < av.startSeq
1565 if (scrollThread != null)
1567 scrollThread.setEvent(evt);
1570 seqCanvas.repaint();
1573 public void mouseEntered(MouseEvent e)
1580 if (scrollThread != null)
1582 scrollThread.running = false;
1583 scrollThread = null;
1587 public void mouseExited(MouseEvent e)
1589 if (av.getWrapAlignment())
1594 if (mouseDragging && scrollThread == null)
1596 scrollThread = new ScrollThread();
1600 void scrollCanvas(MouseEvent evt)
1604 if (scrollThread != null)
1606 scrollThread.running = false;
1607 scrollThread = null;
1609 mouseDragging = false;
1613 if (scrollThread == null)
1615 scrollThread = new ScrollThread();
1618 mouseDragging = true;
1619 scrollThread.setEvent(evt);
1624 // this class allows scrolling off the bottom of the visible alignment
1625 class ScrollThread extends Thread
1629 boolean running = false;
1631 public ScrollThread()
1636 public void setEvent(MouseEvent e)
1641 public void stopScrolling()
1655 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1657 running = ap.scrollUp(true);
1660 if (mouseDragging && evt.getY() >= getSize().height
1661 && av.alignment.getHeight() > av.getEndSeq())
1663 running = ap.scrollUp(false);
1666 if (mouseDragging && evt.getX() < 0)
1668 running = ap.scrollRight(false);
1671 else if (mouseDragging && evt.getX() >= getSize().width)
1673 running = ap.scrollRight(true);
1680 } catch (Exception ex)