2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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(av.applet);
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()
158 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
160 int original = seqCanvas.cursorX - dx;
161 int maxWidth = av.getAlignment().getWidth();
163 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
164 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
166 seqCanvas.cursorX += dx;
169 if (seqCanvas.cursorX >= maxWidth
170 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
172 seqCanvas.cursorX = original;
178 void scrollToVisible()
180 if (seqCanvas.cursorX < 0)
182 seqCanvas.cursorX = 0;
184 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
186 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
189 if (seqCanvas.cursorY < 0)
191 seqCanvas.cursorY = 0;
193 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
195 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
199 if (av.wrapAlignment)
201 ap.scrollToWrappedVisible(seqCanvas.cursorX);
205 while (seqCanvas.cursorY < av.startSeq)
209 while (seqCanvas.cursorY + 1 > av.endSeq)
213 while (seqCanvas.cursorX < av.getColumnSelection()
214 .adjustForHiddenColumns(av.startRes))
217 if (!ap.scrollRight(false))
222 while (seqCanvas.cursorX > av.getColumnSelection()
223 .adjustForHiddenColumns(av.endRes))
225 if (!ap.scrollRight(true))
231 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
232 seqCanvas.cursorX, seqCanvas.cursorY);
237 void setSelectionAreaAtCursor(boolean topLeft)
239 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
242 if (av.getSelectionGroup() != null)
244 SequenceGroup sg = av.getSelectionGroup();
245 // Find the top and bottom of this group
246 int min = av.getAlignment().getHeight(), max = 0;
247 for (int i = 0; i < sg.getSize(); i++)
249 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
264 sg.setStartRes(seqCanvas.cursorX);
265 if (sg.getEndRes() < seqCanvas.cursorX)
267 sg.setEndRes(seqCanvas.cursorX);
270 min = seqCanvas.cursorY;
274 sg.setEndRes(seqCanvas.cursorX);
275 if (sg.getStartRes() > seqCanvas.cursorX)
277 sg.setStartRes(seqCanvas.cursorX);
280 max = seqCanvas.cursorY + 1;
285 // Only the user can do this
286 av.setSelectionGroup(null);
290 // Now add any sequences between min and max
292 for (int i = min; i < max; i++)
294 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
299 if (av.getSelectionGroup() == null)
301 SequenceGroup sg = new SequenceGroup();
302 sg.setStartRes(seqCanvas.cursorX);
303 sg.setEndRes(seqCanvas.cursorX);
304 sg.addSequence(sequence, false);
305 av.setSelectionGroup(sg);
307 ap.paintAlignment(false);
311 void insertGapAtCursor(boolean group)
313 groupEditing = group;
314 startseq = seqCanvas.cursorY;
315 lastres = seqCanvas.cursorX;
316 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
320 void deleteGapAtCursor(boolean group)
322 groupEditing = group;
323 startseq = seqCanvas.cursorY;
324 lastres = seqCanvas.cursorX + getKeyboardNo1();
325 editSequence(false, seqCanvas.cursorX);
329 void numberPressed(char value)
331 if (keyboardNo1 == null)
333 keyboardNo1 = new StringBuffer();
336 if (keyboardNo2 != null)
338 keyboardNo2.append(value);
342 keyboardNo1.append(value);
348 if (keyboardNo1 == null)
352 int value = Integer.parseInt(keyboardNo1.toString());
360 if (keyboardNo2 == null)
364 int value = Integer.parseInt(keyboardNo2.toString());
370 void setStatusMessage(SequenceI sequence, int res, int seq)
372 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
373 + sequence.getName());
376 if (av.getAlignment().isNucleotide())
378 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
382 text.append(" Nucleotide: ");
387 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
390 text.append(" Residue: ");
399 text.append(obj + " (" + sequence.findPosition(res) + ")");
403 ap.alignFrame.statusBar.setText(text.toString());
407 public void mousePressed(MouseEvent evt)
409 lastMousePress = evt.getPoint();
411 // For now, ignore the mouseWheel font resizing on Macs
412 // As the Button2_mask always seems to be true
413 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
416 mouseWheelPressed = true;
420 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
422 if (evt.isControlDown() || evt.isAltDown())
430 doMousePressedDefineMode(evt);
434 int seq = findSeq(evt);
435 int res = findRes(evt);
437 if (seq < 0 || res < 0)
442 if ((seq < av.getAlignment().getHeight())
443 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
457 public void mouseClicked(MouseEvent evt)
459 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
460 if (evt.getClickCount() > 1)
462 if (av.getSelectionGroup() != null
463 && av.getSelectionGroup().getSize() == 1
464 && av.getSelectionGroup().getEndRes()
465 - av.getSelectionGroup().getStartRes() < 2)
467 av.setSelectionGroup(null);
470 SequenceFeature[] features = findFeaturesAtRes(sequence,
471 sequence.findPosition(findRes(evt)));
473 if (features != null && features.length > 0)
475 SearchResults highlight = new SearchResults();
476 highlight.addResult(sequence, features[0].getBegin(),
477 features[0].getEnd());
478 seqCanvas.highlightSearchResults(highlight);
480 if (features != null && features.length > 0)
482 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
483 { sequence }, features, false, ap);
485 seqCanvas.highlightSearchResults(null);
490 public void mouseReleased(MouseEvent evt)
492 mouseDragging = false;
493 mouseWheelPressed = false;
494 ap.paintAlignment(true);
498 doMouseReleasedDefineMode(evt);
506 int startWrapBlock = -1;
508 int wrappedBlock = -1;
510 int findRes(MouseEvent evt)
515 if (av.wrapAlignment)
518 int hgap = av.charHeight;
519 if (av.scaleAboveWrapped)
521 hgap += av.charHeight;
524 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
525 + seqCanvas.getAnnotationHeight();
529 x -= seqCanvas.LABEL_WEST;
531 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
537 wrappedBlock = y / cHeight;
538 wrappedBlock += av.getStartRes() / cwidth;
540 res = wrappedBlock * cwidth + x / av.getCharWidth();
545 res = (x / av.getCharWidth()) + av.getStartRes();
548 if (av.hasHiddenColumns())
550 res = av.getColumnSelection().adjustForHiddenColumns(res);
557 int findSeq(MouseEvent evt)
559 final int sqnum = findAlRow(evt);
560 return (sqnum < 0) ? 0 : sqnum;
566 * @return row in alignment that was selected (or -1 for column selection)
568 private int findAlRow(MouseEvent evt)
573 if (av.wrapAlignment)
575 int hgap = av.charHeight;
576 if (av.scaleAboveWrapped)
578 hgap += av.charHeight;
581 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
582 + seqCanvas.getAnnotationHeight();
586 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
595 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
596 .getAlignment().getHeight() - 1);
606 public void doMousePressed(MouseEvent evt)
609 int seq = findSeq(evt);
610 int res = findRes(evt);
612 if (seq < av.getAlignment().getHeight()
613 && res < av.getAlignment().getSequenceAt(seq).getLength())
615 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
616 // Find the residue's position in the sequence (res is the position
633 public void mouseOverSequence(SequenceI sequence, int index, int pos)
635 String tmp = sequence.hashCode() + index + "";
636 if (lastMessage == null || !lastMessage.equals(tmp))
637 ssm.mouseOverSequence(sequence, index, pos, av);
642 public void highlightSequence(SearchResults results)
644 if (av.followHighlight)
646 if (ap.scrollToPosition(results, true))
648 ap.alignFrame.repaint();
651 seqCanvas.highlightSearchResults(results);
655 public void updateColours(SequenceI seq, int index)
657 System.out.println("update the seqPanel colours");
661 public void mouseMoved(MouseEvent evt)
663 int res = findRes(evt);
664 int seq = findSeq(evt);
666 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
675 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
676 if (res > sequence.getLength())
685 int respos = sequence.findPosition(res);
687 mouseOverSequence(sequence, res, respos);
689 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
690 + sequence.getName());
693 if (av.getAlignment().isNucleotide())
695 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
699 text.append(" Nucleotide: ");
704 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
707 text.append(" Residue: ");
715 text.append(obj + " (" + respos + ")");
719 ap.alignFrame.statusBar.setText(text.toString());
721 StringBuffer tooltipText = new StringBuffer();
722 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
725 for (int g = 0; g < groups.length; g++)
727 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
729 if (!groups[g].getName().startsWith("JTreeGroup")
730 && !groups[g].getName().startsWith("JGroup"))
732 tooltipText.append(groups[g].getName() + " ");
734 if (groups[g].getDescription() != null)
736 tooltipText.append(groups[g].getDescription());
738 tooltipText.append("\n");
743 // use aa to see if the mouse pointer is on a
744 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
745 sequence.findPosition(res));
748 while (index < allFeatures.length)
750 SequenceFeature sf = allFeatures[index];
752 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
754 if (sf.getDescription() != null)
756 tooltipText.append(" " + sf.getDescription());
759 if (sf.getValue("status") != null)
761 String status = sf.getValue("status").toString();
762 if (status.length() > 0)
764 tooltipText.append(" (" + sf.getValue("status") + ")");
767 tooltipText.append("\n");
774 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
778 tooltip.setTip(tooltipText.toString());
782 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
784 Vector tmp = new Vector();
785 SequenceFeature[] features = sequence.getSequenceFeatures();
786 if (features != null)
788 for (int i = 0; i < features.length; i++)
790 if (av.featuresDisplayed == null
791 || !av.featuresDisplayed.containsKey(features[i].getType()))
796 if (features[i].featureGroup != null
797 && seqCanvas.fr.featureGroups != null
798 && seqCanvas.fr.featureGroups
799 .containsKey(features[i].featureGroup)
800 && !((Boolean) seqCanvas.fr.featureGroups
801 .get(features[i].featureGroup)).booleanValue())
804 if ((features[i].getBegin() <= res)
805 && (features[i].getEnd() >= res))
807 tmp.addElement(features[i]);
812 features = new SequenceFeature[tmp.size()];
813 tmp.copyInto(features);
820 public void mouseDragged(MouseEvent evt)
822 if (mouseWheelPressed)
824 int oldWidth = av.charWidth;
826 // Which is bigger, left-right or up-down?
827 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
830 int fontSize = av.font.getSize();
832 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
836 else if (evt.getY() > lastMousePress.y)
846 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
847 av.charWidth = oldWidth;
851 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
855 else if (evt.getX() > lastMousePress.x)
860 if (av.charWidth < 1)
868 FontMetrics fm = getFontMetrics(av.getFont());
869 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
871 lastMousePress = evt.getPoint();
873 ap.paintAlignment(false);
874 ap.annotationPanel.image = null;
880 doMouseDraggedDefineMode(evt);
884 int res = findRes(evt);
891 if ((lastres == -1) || (lastres == res))
896 if ((res < av.getAlignment().getWidth()) && (res < lastres))
898 // dragLeft, delete gap
899 editSequence(false, res);
903 editSequence(true, res);
906 mouseDragging = true;
907 if (scrollThread != null)
909 scrollThread.setEvent(evt);
914 synchronized void editSequence(boolean insertGap, int startres)
918 boolean fixedColumns = false;
919 SequenceGroup sg = av.getSelectionGroup();
921 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
923 if (!groupEditing && av.hasHiddenRows())
925 if (av.isHiddenRepSequence(seq))
927 sg = (SequenceGroup) av.getRepresentedSequences(seq);
932 StringBuffer message = new StringBuffer();
935 message.append("Edit group:");
936 if (editCommand == null)
938 editCommand = new EditCommand("Edit Group");
943 message.append("Edit sequence: " + seq.getName());
944 String label = seq.getName();
945 if (label.length() > 10)
947 label = label.substring(0, 10);
949 if (editCommand == null)
951 editCommand = new EditCommand("Edit " + label);
957 message.append(" insert ");
961 message.append(" delete ");
964 message.append(Math.abs(startres - lastres) + " gaps.");
965 ap.alignFrame.statusBar.setText(message.toString());
967 // Are we editing within a selection group?
969 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
974 // sg might be null as the user may only see 1 sequence,
975 // but the sequence represents a group
978 if (!av.isHiddenRepSequence(seq))
984 sg = av.getRepresentedSequences(seq);
987 fixedLeft = sg.getStartRes();
988 fixedRight = sg.getEndRes();
990 if ((startres < fixedLeft && lastres >= fixedLeft)
991 || (startres >= fixedLeft && lastres < fixedLeft)
992 || (startres > fixedRight && lastres <= fixedRight)
993 || (startres <= fixedRight && lastres > fixedRight))
999 if (fixedLeft > startres)
1001 fixedRight = fixedLeft - 1;
1004 else if (fixedRight < startres)
1006 fixedLeft = fixedRight;
1011 if (av.hasHiddenColumns())
1013 fixedColumns = true;
1014 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1015 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1017 if ((insertGap && startres > y1 && lastres < y1)
1018 || (!insertGap && startres < y2 && lastres > y2))
1024 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1025 // Selection spans a hidden region
1026 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1034 fixedRight = y2 - 1;
1041 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1042 .toArray(new SequenceI[0]);
1047 // If the user has selected the whole sequence, and is dragging to
1048 // the right, we can still extend the alignment and selectionGroup
1049 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1050 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1052 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1053 fixedRight = sg.getEndRes();
1056 // Is it valid with fixed columns??
1057 // Find the next gap before the end
1058 // of the visible region boundary
1059 boolean blank = false;
1060 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1064 for (SequenceI gs : groupSeqs)
1066 for (int j = 0; j < startres - lastres; j++)
1068 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1084 if (sg.getSize() == av.getAlignment().getHeight())
1086 if ((av.hasHiddenColumns() && startres < av
1087 .getColumnSelection().getHiddenBoundaryRight(startres)))
1093 int alWidth = av.getAlignment().getWidth();
1094 if (av.hasHiddenRows())
1096 int hwidth = av.getAlignment().getHiddenSequences()
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 (SequenceI gs : groupSeqs)
1124 for (int j = startres; j < lastres; j++)
1126 if (gs.getLength() <= j)
1131 if (!jalview.util.Comparison.isGap(gs.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.getAlignment(), 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.getAlignment(), 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.getAlignment(), 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.getAlignment(), 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.getAlignment(), true);
1265 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1266 av.getAlignment(), true);
1270 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1273 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1274 av.getAlignment(), true);
1276 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1277 av.getAlignment(), 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.getAlignment().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.getAlignment().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 stretchGroup.recalcConservation(); // always do this - annotation has own
1420 if (stretchGroup.cs != null)
1422 stretchGroup.cs.alignmentChanged(stretchGroup,
1423 av.getHiddenRepSequences());
1425 if (stretchGroup.cs.conservationApplied())
1427 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1428 stretchGroup.getName());
1432 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1433 stretchGroup.getName());
1436 changeEndRes = false;
1437 changeStartRes = false;
1438 stretchGroup = null;
1439 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1440 ap.paintAlignment(true);
1444 public void doMouseDraggedDefineMode(MouseEvent evt)
1446 int res = findRes(evt);
1447 int y = findSeq(evt);
1449 if (wrappedBlock != startWrapBlock)
1454 if (stretchGroup == null)
1459 mouseDragging = true;
1461 if (y > av.getAlignment().getHeight())
1463 y = av.getAlignment().getHeight() - 1;
1466 if (res >= av.getAlignment().getWidth())
1468 res = av.getAlignment().getWidth() - 1;
1471 if (stretchGroup.getEndRes() == res)
1473 // Edit end res position of selected group
1474 changeEndRes = true;
1476 else if (stretchGroup.getStartRes() == res)
1478 // Edit start res position of selected group
1479 changeStartRes = true;
1489 if (res > (stretchGroup.getStartRes() - 1))
1491 stretchGroup.setEndRes(res);
1494 else if (changeStartRes)
1496 if (res < (stretchGroup.getEndRes() + 1))
1498 stretchGroup.setStartRes(res);
1502 int dragDirection = 0;
1508 else if (y < oldSeq)
1513 while ((y != oldSeq) && (oldSeq > -1)
1514 && (y < av.getAlignment().getHeight()))
1516 // This routine ensures we don't skip any sequences, as the
1517 // selection is quite slow.
1518 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1520 oldSeq += dragDirection;
1527 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1529 if (stretchGroup.getSequences(null).contains(nextSeq))
1531 stretchGroup.deleteSequence(seq, false);
1537 stretchGroup.addSequence(seq, false);
1540 stretchGroup.addSequence(nextSeq, false);
1549 if (res > av.endRes || res < av.startRes || y < av.startSeq
1555 if (scrollThread != null)
1557 scrollThread.setEvent(evt);
1560 seqCanvas.repaint();
1563 public void mouseEntered(MouseEvent e)
1570 if (scrollThread != null)
1572 scrollThread.running = false;
1573 scrollThread = null;
1577 public void mouseExited(MouseEvent e)
1579 if (av.getWrapAlignment())
1584 if (mouseDragging && scrollThread == null)
1586 scrollThread = new ScrollThread();
1590 void scrollCanvas(MouseEvent evt)
1594 if (scrollThread != null)
1596 scrollThread.running = false;
1597 scrollThread = null;
1599 mouseDragging = false;
1603 if (scrollThread == null)
1605 scrollThread = new ScrollThread();
1608 mouseDragging = true;
1609 scrollThread.setEvent(evt);
1614 // this class allows scrolling off the bottom of the visible alignment
1615 class ScrollThread extends Thread
1619 boolean running = false;
1621 public ScrollThread()
1626 public void setEvent(MouseEvent e)
1631 public void stopScrolling()
1645 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1647 running = ap.scrollUp(true);
1650 if (mouseDragging && evt.getY() >= getSize().height
1651 && av.getAlignment().getHeight() > av.getEndSeq())
1653 running = ap.scrollUp(false);
1656 if (mouseDragging && evt.getX() < 0)
1658 running = ap.scrollRight(false);
1661 else if (mouseDragging && evt.getX() >= getSize().width)
1663 running = ap.scrollRight(true);
1670 } catch (Exception ex)
1678 * modify current selection according to a received message.
1680 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1681 SelectionSource source)
1683 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1684 // handles selection messages...
1685 // TODO: extend config options to allow user to control if selections may be
1686 // shared between viewports.
1688 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1689 .getSequenceSetId().equals(av.getSequenceSetId()))))
1693 // do we want to thread this ? (contention with seqsel and colsel locks, I
1695 // rules are: colsel is copied if there is a real intersection between
1696 // sequence selection
1697 boolean repaint = false, copycolsel = true;
1698 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1700 SequenceGroup sgroup = null;
1701 if (seqsel != null && seqsel.getSize() > 0)
1703 if (av.getAlignment() == null)
1706 .println("Selection message: alignviewport av SeqSetId="
1707 + av.getSequenceSetId() + " ViewId="
1709 + " 's alignment is NULL! returning immediatly.");
1712 sgroup = seqsel.intersect(av.getAlignment(),
1713 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1714 if ((sgroup == null || sgroup.getSize() == 0)
1715 && (colsel == null || colsel.size() == 0))
1717 // don't copy columns if the region didn't intersect.
1721 if (sgroup != null && sgroup.getSize() > 0)
1723 av.setSelectionGroup(sgroup);
1727 av.setSelectionGroup(null);
1729 repaint = av.isSelectionGroupChanged(true);
1732 && (av.getColumnSelection() == null || !av
1733 .isColSelChanged(true)))
1735 // the current selection is unset or from a previous message
1736 // so import the new colsel.
1737 if (colsel == null || colsel.size() == 0)
1739 if (av.getColumnSelection() != null)
1741 av.getColumnSelection().clear();
1746 // TODO: shift colSel according to the intersecting sequences
1747 if (av.getColumnSelection() == null)
1749 av.setColumnSelection(new ColumnSelection(colsel));
1753 av.getColumnSelection().setElementsFrom(colsel);
1756 repaint |= av.isColSelChanged(true);
1759 && av.hasHiddenColumns()
1760 && (av.getColumnSelection() == null || av.getColumnSelection()
1761 .getHiddenColumns() == null))
1763 System.err.println("Bad things");
1767 ap.scalePanelHolder.repaint();
1773 * scroll to the given row/column - or nearest visible location
1778 public void scrollTo(int row, int column)
1781 row = row < 0 ? ap.av.startSeq : row;
1782 column = column < 0 ? ap.av.startRes : column;
1783 ap.scrollTo(column, column, row, true, true);
1787 * scroll to the given row - or nearest visible location
1791 public void scrollToRow(int row)
1794 row = row < 0 ? ap.av.startSeq : row;
1795 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1799 * scroll to the given column - or nearest visible location
1803 public void scrollToColumn(int column)
1806 column = column < 0 ? ap.av.startRes : column;
1807 ap.scrollTo(column, column, ap.av.startSeq, true, true);