2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
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.SelectionSource;
29 import jalview.structure.SequenceListener;
30 import jalview.structure.StructureSelectionManager;
32 public class SeqPanel extends Panel implements MouseMotionListener,
33 MouseListener, SequenceListener
36 public SeqCanvas seqCanvas;
38 public AlignmentPanel ap;
40 protected int lastres;
42 protected int startseq;
44 protected AlignViewport av;
46 // if character is inserted or deleted, we will need to recalculate the
48 boolean seqEditOccurred = false;
50 ScrollThread scrollThread = null;
52 boolean mouseDragging = false;
54 boolean editingSeqs = false;
56 boolean groupEditing = false;
60 boolean changeEndSeq = false;
62 boolean changeStartSeq = false;
64 boolean changeEndRes = false;
66 boolean changeStartRes = false;
68 SequenceGroup stretchGroup = null;
70 StringBuffer keyboardNo1;
72 StringBuffer keyboardNo2;
74 boolean mouseWheelPressed = false;
78 EditCommand editCommand;
80 StructureSelectionManager ssm;
82 public SeqPanel(AlignViewport avp, AlignmentPanel p)
86 seqCanvas = new SeqCanvas(avp);
87 setLayout(new BorderLayout());
92 seqCanvas.addMouseMotionListener(this);
93 seqCanvas.addMouseListener(this);
94 ssm = StructureSelectionManager.getStructureSelectionManager();
95 ssm.addStructureViewerListener(this);
102 if (editCommand != null && editCommand.getSize() > 0)
104 ap.alignFrame.addHistoryItem(editCommand);
105 av.firePropertyChange("alignment", null, av.getAlignment()
112 groupEditing = false;
120 seqCanvas.cursorY = getKeyboardNo1() - 1;
124 void setCursorColumn()
126 seqCanvas.cursorX = getKeyboardNo1() - 1;
130 void setCursorRowAndColumn()
132 if (keyboardNo2 == null)
134 keyboardNo2 = new StringBuffer();
138 seqCanvas.cursorX = getKeyboardNo1() - 1;
139 seqCanvas.cursorY = getKeyboardNo2() - 1;
144 void setCursorPosition()
146 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
149 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
153 void moveCursor(int dx, int dy)
155 seqCanvas.cursorX += dx;
156 seqCanvas.cursorY += dy;
157 if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))
159 int original = seqCanvas.cursorX - dx;
160 int maxWidth = av.alignment.getWidth();
162 while (!av.colSel.isVisible(seqCanvas.cursorX)
163 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
165 seqCanvas.cursorX += dx;
168 if (seqCanvas.cursorX >= maxWidth
169 || !av.colSel.isVisible(seqCanvas.cursorX))
171 seqCanvas.cursorX = original;
177 void scrollToVisible()
179 if (seqCanvas.cursorX < 0)
181 seqCanvas.cursorX = 0;
183 else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)
185 seqCanvas.cursorX = av.alignment.getWidth() - 1;
188 if (seqCanvas.cursorY < 0)
190 seqCanvas.cursorY = 0;
192 else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)
194 seqCanvas.cursorY = av.alignment.getHeight() - 1;
198 if (av.wrapAlignment)
200 ap.scrollToWrappedVisible(seqCanvas.cursorX);
204 while (seqCanvas.cursorY < av.startSeq)
208 while (seqCanvas.cursorY + 1 > av.endSeq)
212 while (seqCanvas.cursorX < av.colSel
213 .adjustForHiddenColumns(av.startRes))
216 if (!ap.scrollRight(false))
221 while (seqCanvas.cursorX > av.colSel
222 .adjustForHiddenColumns(av.endRes))
224 if (!ap.scrollRight(true))
230 setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),
231 seqCanvas.cursorX, seqCanvas.cursorY);
236 void setSelectionAreaAtCursor(boolean topLeft)
238 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
241 if (av.getSelectionGroup() != null)
243 SequenceGroup sg = av.selectionGroup;
244 // Find the top and bottom of this group
245 int min = av.alignment.getHeight(), max = 0;
246 for (int i = 0; i < sg.getSize(); i++)
248 int index = av.alignment.findIndex(sg.getSequenceAt(i));
263 sg.setStartRes(seqCanvas.cursorX);
264 if (sg.getEndRes() < seqCanvas.cursorX)
266 sg.setEndRes(seqCanvas.cursorX);
269 min = seqCanvas.cursorY;
273 sg.setEndRes(seqCanvas.cursorX);
274 if (sg.getStartRes() > seqCanvas.cursorX)
276 sg.setStartRes(seqCanvas.cursorX);
279 max = seqCanvas.cursorY + 1;
284 // Only the user can do this
285 av.setSelectionGroup(null);
289 // Now add any sequences between min and max
290 sg.getSequences(null).removeAllElements();
291 for (int i = min; i < max; i++)
293 sg.addSequence(av.alignment.getSequenceAt(i), false);
298 if (av.getSelectionGroup() == null)
300 SequenceGroup sg = new SequenceGroup();
301 sg.setStartRes(seqCanvas.cursorX);
302 sg.setEndRes(seqCanvas.cursorX);
303 sg.addSequence(sequence, false);
304 av.setSelectionGroup(sg);
306 ap.paintAlignment(false);
310 void insertGapAtCursor(boolean group)
312 groupEditing = group;
313 startseq = seqCanvas.cursorY;
314 lastres = seqCanvas.cursorX;
315 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
319 void deleteGapAtCursor(boolean group)
321 groupEditing = group;
322 startseq = seqCanvas.cursorY;
323 lastres = seqCanvas.cursorX + getKeyboardNo1();
324 editSequence(false, seqCanvas.cursorX);
328 void numberPressed(char value)
330 if (keyboardNo1 == null)
332 keyboardNo1 = new StringBuffer();
335 if (keyboardNo2 != null)
337 keyboardNo2.append(value);
341 keyboardNo1.append(value);
347 if (keyboardNo1 == null)
351 int value = Integer.parseInt(keyboardNo1.toString());
359 if (keyboardNo2 == null)
363 int value = Integer.parseInt(keyboardNo2.toString());
369 void setStatusMessage(SequenceI sequence, int res, int seq)
371 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
372 + sequence.getName());
375 if (av.alignment.isNucleotide())
377 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
381 text.append(" Nucleotide: ");
386 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
389 text.append(" Residue: ");
398 text.append(obj + " (" + sequence.findPosition(res) + ")");
402 ap.alignFrame.statusBar.setText(text.toString());
406 public void mousePressed(MouseEvent evt)
408 lastMousePress = evt.getPoint();
410 // For now, ignore the mouseWheel font resizing on Macs
411 // As the Button2_mask always seems to be true
412 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
415 mouseWheelPressed = true;
419 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
421 if (evt.isControlDown() || evt.isAltDown())
429 doMousePressedDefineMode(evt);
433 int seq = findSeq(evt);
434 int res = findRes(evt);
436 if (seq < 0 || res < 0)
441 if ((seq < av.getAlignment().getHeight())
442 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
456 public void mouseClicked(MouseEvent evt)
458 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
459 if (evt.getClickCount() > 1)
461 if (av.getSelectionGroup().getSize() == 1
462 && av.getSelectionGroup().getEndRes()
463 - av.getSelectionGroup().getStartRes() < 2)
465 av.setSelectionGroup(null);
468 SequenceFeature[] features = findFeaturesAtRes(sequence,
469 sequence.findPosition(findRes(evt)));
471 if (features != null && features.length > 0)
473 SearchResults highlight = new SearchResults();
474 highlight.addResult(sequence, features[0].getBegin(),
475 features[0].getEnd());
476 seqCanvas.highlightSearchResults(highlight);
478 if (features != null && features.length > 0)
480 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
481 { sequence }, features, false, ap);
483 seqCanvas.highlightSearchResults(null);
488 public void mouseReleased(MouseEvent evt)
490 mouseDragging = false;
491 mouseWheelPressed = false;
492 ap.paintAlignment(true);
496 doMouseReleasedDefineMode(evt);
504 int startWrapBlock = -1;
506 int wrappedBlock = -1;
508 int findRes(MouseEvent evt)
513 if (av.wrapAlignment)
516 int hgap = av.charHeight;
517 if (av.scaleAboveWrapped)
519 hgap += av.charHeight;
522 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
523 + seqCanvas.getAnnotationHeight();
527 x -= seqCanvas.LABEL_WEST;
529 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
535 wrappedBlock = y / cHeight;
536 wrappedBlock += av.getStartRes() / cwidth;
538 res = wrappedBlock * cwidth + x / av.getCharWidth();
543 res = (x / av.getCharWidth()) + av.getStartRes();
546 if (av.hasHiddenColumns)
548 res = av.getColumnSelection().adjustForHiddenColumns(res);
555 int findSeq(MouseEvent evt)
557 final int sqnum = findAlRow(evt);
558 return (sqnum < 0) ? 0 : sqnum;
564 * @return row in alignment that was selected (or -1 for column selection)
566 private int findAlRow(MouseEvent evt)
571 if (av.wrapAlignment)
573 int hgap = av.charHeight;
574 if (av.scaleAboveWrapped)
576 hgap += av.charHeight;
579 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
580 + seqCanvas.getAnnotationHeight();
584 seq = Math.min((y % cHeight) / av.getCharHeight(),
585 av.alignment.getHeight() - 1);
593 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
594 av.alignment.getHeight() - 1);
604 public void doMousePressed(MouseEvent evt)
607 int seq = findSeq(evt);
608 int res = findRes(evt);
610 if (seq < av.getAlignment().getHeight()
611 && res < av.getAlignment().getSequenceAt(seq).getLength())
613 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
614 // Find the residue's position in the sequence (res is the position
631 public void mouseOverSequence(SequenceI sequence, int index, int pos)
633 String tmp = sequence.hashCode() + index + "";
634 if (lastMessage == null || !lastMessage.equals(tmp))
635 ssm.mouseOverSequence(sequence, index, pos);
640 public void highlightSequence(SearchResults results)
642 seqCanvas.highlightSearchResults(results);
643 if (av.followHighlight)
645 ap.scrollToPosition(results);
649 public void updateColours(SequenceI seq, int index)
651 System.out.println("update the seqPanel colours");
655 public void mouseMoved(MouseEvent evt)
657 int res = findRes(evt);
658 int seq = findSeq(evt);
660 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
669 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
670 if (res > sequence.getLength())
679 int respos = sequence.findPosition(res);
681 mouseOverSequence(sequence, res, respos);
683 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
684 + sequence.getName());
687 if (av.alignment.isNucleotide())
689 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
693 text.append(" Nucleotide: ");
698 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
701 text.append(" Residue: ");
709 text.append(obj + " (" + respos + ")");
713 ap.alignFrame.statusBar.setText(text.toString());
715 StringBuffer tooltipText = new StringBuffer();
716 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
719 for (int g = 0; g < groups.length; g++)
721 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
723 if (!groups[g].getName().startsWith("JTreeGroup")
724 && !groups[g].getName().startsWith("JGroup"))
726 tooltipText.append(groups[g].getName() + " ");
728 if (groups[g].getDescription() != null)
730 tooltipText.append(groups[g].getDescription());
732 tooltipText.append("\n");
737 // use aa to see if the mouse pointer is on a
738 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
739 sequence.findPosition(res));
742 while (index < allFeatures.length)
744 SequenceFeature sf = allFeatures[index];
746 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
748 if (sf.getDescription() != null)
750 tooltipText.append(" " + sf.getDescription());
753 if (sf.getValue("status") != null)
755 String status = sf.getValue("status").toString();
756 if (status.length() > 0)
758 tooltipText.append(" (" + sf.getValue("status") + ")");
761 tooltipText.append("\n");
768 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
772 tooltip.setTip(tooltipText.toString());
776 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
778 Vector tmp = new Vector();
779 SequenceFeature[] features = sequence.getSequenceFeatures();
780 if (features != null)
782 for (int i = 0; i < features.length; i++)
784 if (av.featuresDisplayed == null
785 || !av.featuresDisplayed.containsKey(features[i].getType()))
790 if (features[i].featureGroup != null
791 && seqCanvas.fr.featureGroups != null
792 && seqCanvas.fr.featureGroups
793 .containsKey(features[i].featureGroup)
794 && !((Boolean) seqCanvas.fr.featureGroups
795 .get(features[i].featureGroup)).booleanValue())
798 if ((features[i].getBegin() <= res)
799 && (features[i].getEnd() >= res))
801 tmp.addElement(features[i]);
806 features = new SequenceFeature[tmp.size()];
807 tmp.copyInto(features);
814 public void mouseDragged(MouseEvent evt)
816 if (mouseWheelPressed)
818 int oldWidth = av.charWidth;
820 // Which is bigger, left-right or up-down?
821 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
824 int fontSize = av.font.getSize();
826 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
830 else if (evt.getY() > lastMousePress.y)
840 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
841 av.charWidth = oldWidth;
845 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
849 else if (evt.getX() > lastMousePress.x)
854 if (av.charWidth < 1)
862 FontMetrics fm = getFontMetrics(av.getFont());
863 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
865 lastMousePress = evt.getPoint();
867 ap.paintAlignment(false);
868 ap.annotationPanel.image = null;
874 doMouseDraggedDefineMode(evt);
878 int res = findRes(evt);
885 if ((lastres == -1) || (lastres == res))
890 if ((res < av.getAlignment().getWidth()) && (res < lastres))
892 // dragLeft, delete gap
893 editSequence(false, res);
897 editSequence(true, res);
900 mouseDragging = true;
901 if (scrollThread != null)
903 scrollThread.setEvent(evt);
908 synchronized void editSequence(boolean insertGap, int startres)
912 boolean fixedColumns = false;
913 SequenceGroup sg = av.getSelectionGroup();
915 SequenceI seq = av.alignment.getSequenceAt(startseq);
917 if (!groupEditing && av.hasHiddenRows)
919 if (av.hiddenRepSequences != null
920 && av.hiddenRepSequences.containsKey(seq))
922 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
927 StringBuffer message = new StringBuffer();
930 message.append("Edit group:");
931 if (editCommand == null)
933 editCommand = new EditCommand("Edit Group");
938 message.append("Edit sequence: " + seq.getName());
939 String label = seq.getName();
940 if (label.length() > 10)
942 label = label.substring(0, 10);
944 if (editCommand == null)
946 editCommand = new EditCommand("Edit " + label);
952 message.append(" insert ");
956 message.append(" delete ");
959 message.append(Math.abs(startres - lastres) + " gaps.");
960 ap.alignFrame.statusBar.setText(message.toString());
962 // Are we editing within a selection group?
964 || (sg != null && sg.getSequences(av.hiddenRepSequences)
969 // sg might be null as the user may only see 1 sequence,
970 // but the sequence represents a group
973 if (av.hiddenRepSequences == null
974 || !av.hiddenRepSequences.containsKey(seq))
980 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
983 fixedLeft = sg.getStartRes();
984 fixedRight = sg.getEndRes();
986 if ((startres < fixedLeft && lastres >= fixedLeft)
987 || (startres >= fixedLeft && lastres < fixedLeft)
988 || (startres > fixedRight && lastres <= fixedRight)
989 || (startres <= fixedRight && lastres > fixedRight))
995 if (fixedLeft > startres)
997 fixedRight = fixedLeft - 1;
1000 else if (fixedRight < startres)
1002 fixedLeft = fixedRight;
1007 if (av.hasHiddenColumns)
1009 fixedColumns = true;
1010 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1011 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1013 if ((insertGap && startres > y1 && lastres < y1)
1014 || (!insertGap && startres < y2 && lastres > y2))
1020 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1021 // Selection spans a hidden region
1022 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1030 fixedRight = y2 - 1;
1037 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1038 int g, groupSize = vseqs.size();
1039 SequenceI[] groupSeqs = new SequenceI[groupSize];
1040 for (g = 0; g < groupSeqs.length; g++)
1042 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1048 // If the user has selected the whole sequence, and is dragging to
1049 // the right, we can still extend the alignment and selectionGroup
1050 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1051 && sg.getEndRes() == av.alignment.getWidth() - 1)
1053 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1054 fixedRight = sg.getEndRes();
1057 // Is it valid with fixed columns??
1058 // Find the next gap before the end
1059 // of the visible region boundary
1060 boolean blank = false;
1061 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1065 for (g = 0; g < groupSize; g++)
1067 for (int j = 0; j < startres - lastres; j++)
1069 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1070 .getCharAt(fixedRight - j)))
1085 if (sg.getSize() == av.alignment.getHeight())
1087 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1088 .getHiddenBoundaryRight(startres)))
1094 int alWidth = av.alignment.getWidth();
1095 if (av.hasHiddenRows)
1097 int hwidth = av.alignment.getHiddenSequences().getWidth();
1098 if (hwidth > alWidth)
1103 // We can still insert gaps if the selectionGroup
1104 // contains all the sequences
1105 sg.setEndRes(sg.getEndRes() + startres - lastres);
1106 fixedRight = alWidth + startres - lastres;
1117 else if (!insertGap)
1119 // / Are we able to delete?
1120 // ie are all columns blank?
1122 for (g = 0; g < groupSize; g++)
1124 for (int j = startres; j < lastres; j++)
1126 if (groupSeqs[g].getLength() <= j)
1131 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1133 // Not a gap, block edit not valid
1143 // dragging to the right
1144 if (fixedColumns && fixedRight != -1)
1146 for (int j = lastres; j < startres; j++)
1148 insertChar(j, groupSeqs, fixedRight);
1153 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1154 startres, startres - lastres, av.alignment, true);
1159 // dragging to the left
1160 if (fixedColumns && fixedRight != -1)
1162 for (int j = lastres; j > startres; j--)
1164 deleteChar(startres, groupSeqs, fixedRight);
1169 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1170 startres, lastres - startres, av.alignment, true);
1176 // ///Editing a single sequence///////////
1180 // dragging to the right
1181 if (fixedColumns && fixedRight != -1)
1183 for (int j = lastres; j < startres; j++)
1185 insertChar(j, new SequenceI[]
1186 { seq }, fixedRight);
1191 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1192 { seq }, lastres, startres - lastres, av.alignment, true);
1197 // dragging to the left
1198 if (fixedColumns && fixedRight != -1)
1200 for (int j = lastres; j > startres; j--)
1202 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1207 deleteChar(startres, new SequenceI[]
1208 { seq }, fixedRight);
1213 // could be a keyboard edit trying to delete none gaps
1215 for (int m = startres; m < lastres; m++)
1217 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1226 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1227 { seq }, startres, max, av.alignment, true);
1234 seqCanvas.repaint();
1237 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1239 int blankColumn = fixedColumn;
1240 for (int s = 0; s < seq.length; s++)
1242 // Find the next gap before the end of the visible region boundary
1243 // If lastCol > j, theres a boundary after the gap insertion
1245 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1247 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1249 // Theres a space, so break and insert the gap
1254 if (blankColumn <= j)
1256 blankColumn = fixedColumn;
1262 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1263 av.alignment, true);
1265 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1270 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1273 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1276 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1277 av.alignment, true);
1280 // ////////////////////////////////////////
1281 // ///Everything below this is for defining the boundary of the rubberband
1282 // ////////////////////////////////////////
1283 public void doMousePressedDefineMode(MouseEvent evt)
1285 if (scrollThread != null)
1287 scrollThread.running = false;
1288 scrollThread = null;
1291 int res = findRes(evt);
1292 int seq = findSeq(evt);
1294 startWrapBlock = wrappedBlock;
1301 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1303 if (sequence == null || res > sequence.getLength())
1308 stretchGroup = av.getSelectionGroup();
1310 if (stretchGroup == null)
1312 stretchGroup = av.alignment.findGroup(sequence);
1313 if (stretchGroup != null && res > stretchGroup.getStartRes()
1314 && res < stretchGroup.getEndRes())
1316 av.setSelectionGroup(stretchGroup);
1320 stretchGroup = null;
1324 else if (!stretchGroup.getSequences(null).contains(sequence)
1325 || stretchGroup.getStartRes() > res
1326 || stretchGroup.getEndRes() < res)
1328 stretchGroup = null;
1330 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1332 if (allGroups != null)
1334 for (int i = 0; i < allGroups.length; i++)
1336 if (allGroups[i].getStartRes() <= res
1337 && allGroups[i].getEndRes() >= res)
1339 stretchGroup = allGroups[i];
1344 av.setSelectionGroup(stretchGroup);
1347 // DETECT RIGHT MOUSE BUTTON IN AWT
1348 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1350 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1351 sequence.findPosition(res));
1353 Vector links = null;
1354 if (allFeatures != null)
1356 for (int i = 0; i < allFeatures.length; i++)
1358 if (allFeatures[i].links != null)
1362 links = new Vector();
1364 for (int j = 0; j < allFeatures[i].links.size(); j++)
1366 links.addElement(allFeatures[i].links.elementAt(j));
1371 APopupMenu popup = new APopupMenu(ap, null, links);
1373 popup.show(this, evt.getX(), evt.getY());
1379 seqCanvas.cursorX = findRes(evt);
1380 seqCanvas.cursorY = findSeq(evt);
1381 seqCanvas.repaint();
1385 // Only if left mouse button do we want to change group sizes
1387 if (stretchGroup == null)
1389 // define a new group here
1390 SequenceGroup sg = new SequenceGroup();
1391 sg.setStartRes(res);
1393 sg.addSequence(sequence, false);
1394 av.setSelectionGroup(sg);
1397 if (av.getConservationSelected())
1399 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1402 if (av.getAbovePIDThreshold())
1404 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1411 public void doMouseReleasedDefineMode(MouseEvent evt)
1413 if (stretchGroup == null)
1418 if (stretchGroup.cs != null)
1420 if (stretchGroup.cs instanceof ClustalxColourScheme)
1422 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(
1423 stretchGroup.getSequences(av.hiddenRepSequences),
1424 stretchGroup.getWidth());
1427 if (stretchGroup.cs instanceof Blosum62ColourScheme
1428 || stretchGroup.cs instanceof PIDColourScheme
1429 || stretchGroup.cs.conservationApplied()
1430 || stretchGroup.cs.getThreshold() > 0)
1432 stretchGroup.recalcConservation();
1435 if (stretchGroup.cs.conservationApplied())
1437 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1438 stretchGroup.getName());
1439 stretchGroup.recalcConservation();
1443 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1444 stretchGroup.getName());
1447 changeEndRes = false;
1448 changeStartRes = false;
1449 stretchGroup = null;
1450 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1451 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)
1687 * modify current selection according to a received message.
1689 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1690 SelectionSource source)
1692 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1693 // handles selection messages...
1694 // TODO: extend config options to allow user to control if selections may be
1695 // shared between viewports.
1696 if (av!=null && (av == source
1697 || !av.followSelection
1698 || (source instanceof AlignViewport && ((AlignViewport) source)
1699 .getSequenceSetId().equals(av.getSequenceSetId()))))
1703 // do we want to thread this ? (contention with seqsel and colsel locks, I
1705 // rules are: colsel is copied if there is a real intersection between
1706 // sequence selection
1707 boolean repaint = false, copycolsel = true;
1708 if (av.selectionGroup == null || !av.isSelectionGroupChanged())
1710 SequenceGroup sgroup = null;
1713 if (av.alignment == null)
1715 System.out.println("Selection message: alignviewport av SeqSetId="
1716 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1717 + " 's alignment is NULL! returning immediatly.");
1720 sgroup = seqsel.intersect(av.alignment,
1721 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1722 if ((sgroup == null || sgroup.getSize() == 0)
1723 && (colsel == null || colsel.size() == 0))
1725 // don't copy columns if the region didn't intersect.
1729 if (sgroup != null && sgroup.getSize() > 0)
1731 av.setSelectionGroup(sgroup);
1735 av.setSelectionGroup(null);
1737 repaint = av.isSelectionGroupChanged();
1739 if (copycolsel && (av.colSel == null || !av.isColSelChanged()))
1741 // the current selection is unset or from a previous message
1742 // so import the new colsel.
1743 if (colsel == null || colsel.size() == 0)
1745 if (av.colSel != null)
1752 // TODO: shift colSel according to the intersecting sequences
1753 if (av.colSel == null)
1755 av.colSel = new ColumnSelection(colsel);
1759 av.colSel.setElementsFrom(colsel);
1762 repaint |= av.isColSelChanged();
1764 if (copycolsel && av.hasHiddenColumns
1765 && (av.colSel == null || av.colSel.getHiddenColumns() == null))
1767 System.err.println("Bad things");
1771 // probably finessing with multiple redraws here
1772 PaintRefresher.Refresh(this, av.getSequenceSetId());
1773 // ap.paintAlignment(false);