2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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() && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
159 int original = seqCanvas.cursorX - dx;
160 int maxWidth = av.getAlignment().getWidth();
162 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
163 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
165 seqCanvas.cursorX += dx;
168 if (seqCanvas.cursorX >= maxWidth
169 || !av.getColumnSelection().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.getAlignment().getWidth() - 1)
185 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
188 if (seqCanvas.cursorY < 0)
190 seqCanvas.cursorY = 0;
192 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
194 seqCanvas.cursorY = av.getAlignment().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.getColumnSelection()
213 .adjustForHiddenColumns(av.startRes))
216 if (!ap.scrollRight(false))
221 while (seqCanvas.cursorX > av.getColumnSelection()
222 .adjustForHiddenColumns(av.endRes))
224 if (!ap.scrollRight(true))
230 setStatusMessage(av.getAlignment().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.getSelectionGroup();
244 // Find the top and bottom of this group
245 int min = av.getAlignment().getHeight(), max = 0;
246 for (int i = 0; i < sg.getSize(); i++)
248 int index = av.getAlignment().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
291 for (int i = min; i < max; i++)
293 sg.addSequence(av.getAlignment().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.getAlignment().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.getAlignment().getSequenceAt(findSeq(evt));
459 if (evt.getClickCount() > 1)
461 if (av.getSelectionGroup()!=null && 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.getAlignment().getHeight() - 1);
593 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
594 av.getAlignment().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, av);
640 public void highlightSequence(SearchResults results)
642 if (av.followHighlight)
644 if (ap.scrollToPosition(results, true))
646 ap.alignFrame.repaint();
649 seqCanvas.highlightSearchResults(results);
653 public void updateColours(SequenceI seq, int index)
655 System.out.println("update the seqPanel colours");
659 public void mouseMoved(MouseEvent evt)
661 int res = findRes(evt);
662 int seq = findSeq(evt);
664 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
673 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
674 if (res > sequence.getLength())
683 int respos = sequence.findPosition(res);
685 mouseOverSequence(sequence, res, respos);
687 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
688 + sequence.getName());
691 if (av.getAlignment().isNucleotide())
693 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
697 text.append(" Nucleotide: ");
702 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
705 text.append(" Residue: ");
713 text.append(obj + " (" + respos + ")");
717 ap.alignFrame.statusBar.setText(text.toString());
719 StringBuffer tooltipText = new StringBuffer();
720 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
723 for (int g = 0; g < groups.length; g++)
725 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
727 if (!groups[g].getName().startsWith("JTreeGroup")
728 && !groups[g].getName().startsWith("JGroup"))
730 tooltipText.append(groups[g].getName() + " ");
732 if (groups[g].getDescription() != null)
734 tooltipText.append(groups[g].getDescription());
736 tooltipText.append("\n");
741 // use aa to see if the mouse pointer is on a
742 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
743 sequence.findPosition(res));
746 while (index < allFeatures.length)
748 SequenceFeature sf = allFeatures[index];
750 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
752 if (sf.getDescription() != null)
754 tooltipText.append(" " + sf.getDescription());
757 if (sf.getValue("status") != null)
759 String status = sf.getValue("status").toString();
760 if (status.length() > 0)
762 tooltipText.append(" (" + sf.getValue("status") + ")");
765 tooltipText.append("\n");
772 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
776 tooltip.setTip(tooltipText.toString());
780 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
782 Vector tmp = new Vector();
783 SequenceFeature[] features = sequence.getSequenceFeatures();
784 if (features != null)
786 for (int i = 0; i < features.length; i++)
788 if (av.featuresDisplayed == null
789 || !av.featuresDisplayed.containsKey(features[i].getType()))
794 if (features[i].featureGroup != null
795 && seqCanvas.fr.featureGroups != null
796 && seqCanvas.fr.featureGroups
797 .containsKey(features[i].featureGroup)
798 && !((Boolean) seqCanvas.fr.featureGroups
799 .get(features[i].featureGroup)).booleanValue())
802 if ((features[i].getBegin() <= res)
803 && (features[i].getEnd() >= res))
805 tmp.addElement(features[i]);
810 features = new SequenceFeature[tmp.size()];
811 tmp.copyInto(features);
818 public void mouseDragged(MouseEvent evt)
820 if (mouseWheelPressed)
822 int oldWidth = av.charWidth;
824 // Which is bigger, left-right or up-down?
825 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
828 int fontSize = av.font.getSize();
830 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
834 else if (evt.getY() > lastMousePress.y)
844 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
845 av.charWidth = oldWidth;
849 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
853 else if (evt.getX() > lastMousePress.x)
858 if (av.charWidth < 1)
866 FontMetrics fm = getFontMetrics(av.getFont());
867 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
869 lastMousePress = evt.getPoint();
871 ap.paintAlignment(false);
872 ap.annotationPanel.image = null;
878 doMouseDraggedDefineMode(evt);
882 int res = findRes(evt);
889 if ((lastres == -1) || (lastres == res))
894 if ((res < av.getAlignment().getWidth()) && (res < lastres))
896 // dragLeft, delete gap
897 editSequence(false, res);
901 editSequence(true, res);
904 mouseDragging = true;
905 if (scrollThread != null)
907 scrollThread.setEvent(evt);
912 synchronized void editSequence(boolean insertGap, int startres)
916 boolean fixedColumns = false;
917 SequenceGroup sg = av.getSelectionGroup();
919 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
921 if (!groupEditing && av.hasHiddenRows())
923 if (av.isHiddenRepSequence(seq))
925 sg = (SequenceGroup) av.getRepresentedSequences(seq);
930 StringBuffer message = new StringBuffer();
933 message.append("Edit group:");
934 if (editCommand == null)
936 editCommand = new EditCommand("Edit Group");
941 message.append("Edit sequence: " + seq.getName());
942 String label = seq.getName();
943 if (label.length() > 10)
945 label = label.substring(0, 10);
947 if (editCommand == null)
949 editCommand = new EditCommand("Edit " + label);
955 message.append(" insert ");
959 message.append(" delete ");
962 message.append(Math.abs(startres - lastres) + " gaps.");
963 ap.alignFrame.statusBar.setText(message.toString());
965 // Are we editing within a selection group?
967 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
972 // sg might be null as the user may only see 1 sequence,
973 // but the sequence represents a group
976 if (!av.isHiddenRepSequence(seq))
982 sg = av.getRepresentedSequences(seq);
985 fixedLeft = sg.getStartRes();
986 fixedRight = sg.getEndRes();
988 if ((startres < fixedLeft && lastres >= fixedLeft)
989 || (startres >= fixedLeft && lastres < fixedLeft)
990 || (startres > fixedRight && lastres <= fixedRight)
991 || (startres <= fixedRight && lastres > fixedRight))
997 if (fixedLeft > startres)
999 fixedRight = fixedLeft - 1;
1002 else if (fixedRight < startres)
1004 fixedLeft = fixedRight;
1009 if (av.hasHiddenColumns())
1011 fixedColumns = true;
1012 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1013 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1015 if ((insertGap && startres > y1 && lastres < y1)
1016 || (!insertGap && startres < y2 && lastres > y2))
1022 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1023 // Selection spans a hidden region
1024 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1032 fixedRight = y2 - 1;
1039 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences()).toArray(new SequenceI[0]);
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 && sg.getEndRes() == fixedRight
1047 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1049 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1050 fixedRight = sg.getEndRes();
1053 // Is it valid with fixed columns??
1054 // Find the next gap before the end
1055 // of the visible region boundary
1056 boolean blank = false;
1057 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1061 for (SequenceI gs:groupSeqs)
1063 for (int j = 0; j < startres - lastres; j++)
1065 if (!jalview.util.Comparison.isGap(gs
1066 .getCharAt(fixedRight - j)))
1081 if (sg.getSize() == av.getAlignment().getHeight())
1083 if ((av.hasHiddenColumns() && startres < av.getColumnSelection()
1084 .getHiddenBoundaryRight(startres)))
1090 int alWidth = av.getAlignment().getWidth();
1091 if (av.hasHiddenRows())
1093 int hwidth = av.getAlignment().getHiddenSequences().getWidth();
1094 if (hwidth > alWidth)
1099 // We can still insert gaps if the selectionGroup
1100 // contains all the sequences
1101 sg.setEndRes(sg.getEndRes() + startres - lastres);
1102 fixedRight = alWidth + startres - lastres;
1113 else if (!insertGap)
1115 // / Are we able to delete?
1116 // ie are all columns blank?
1118 for (SequenceI gs:groupSeqs)
1120 for (int j = startres; j < lastres; j++)
1122 if (gs.getLength() <= j)
1127 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1129 // Not a gap, block edit not valid
1139 // dragging to the right
1140 if (fixedColumns && fixedRight != -1)
1142 for (int j = lastres; j < startres; j++)
1144 insertChar(j, groupSeqs, fixedRight);
1149 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1150 startres, startres - lastres, av.getAlignment(), true);
1155 // dragging to the left
1156 if (fixedColumns && fixedRight != -1)
1158 for (int j = lastres; j > startres; j--)
1160 deleteChar(startres, groupSeqs, fixedRight);
1165 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1166 startres, lastres - startres, av.getAlignment(), true);
1172 // ///Editing a single sequence///////////
1176 // dragging to the right
1177 if (fixedColumns && fixedRight != -1)
1179 for (int j = lastres; j < startres; j++)
1181 insertChar(j, new SequenceI[]
1182 { seq }, fixedRight);
1187 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1188 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1193 // dragging to the left
1194 if (fixedColumns && fixedRight != -1)
1196 for (int j = lastres; j > startres; j--)
1198 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1203 deleteChar(startres, new SequenceI[]
1204 { seq }, fixedRight);
1209 // could be a keyboard edit trying to delete none gaps
1211 for (int m = startres; m < lastres; m++)
1213 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1222 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1223 { seq }, startres, max, av.getAlignment(), true);
1230 seqCanvas.repaint();
1233 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1235 int blankColumn = fixedColumn;
1236 for (int s = 0; s < seq.length; s++)
1238 // Find the next gap before the end of the visible region boundary
1239 // If lastCol > j, theres a boundary after the gap insertion
1241 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1243 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1245 // Theres a space, so break and insert the gap
1250 if (blankColumn <= j)
1252 blankColumn = fixedColumn;
1258 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1259 av.getAlignment(), true);
1261 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.getAlignment(),
1266 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1269 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.getAlignment(),
1272 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1273 av.getAlignment(), true);
1276 // ////////////////////////////////////////
1277 // ///Everything below this is for defining the boundary of the rubberband
1278 // ////////////////////////////////////////
1279 public void doMousePressedDefineMode(MouseEvent evt)
1281 if (scrollThread != null)
1283 scrollThread.running = false;
1284 scrollThread = null;
1287 int res = findRes(evt);
1288 int seq = findSeq(evt);
1290 startWrapBlock = wrappedBlock;
1297 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1299 if (sequence == null || res > sequence.getLength())
1304 stretchGroup = av.getSelectionGroup();
1306 if (stretchGroup == null)
1308 stretchGroup = av.getAlignment().findGroup(sequence);
1309 if (stretchGroup != null && res > stretchGroup.getStartRes()
1310 && res < stretchGroup.getEndRes())
1312 av.setSelectionGroup(stretchGroup);
1316 stretchGroup = null;
1320 else if (!stretchGroup.getSequences(null).contains(sequence)
1321 || stretchGroup.getStartRes() > res
1322 || stretchGroup.getEndRes() < res)
1324 stretchGroup = null;
1326 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1328 if (allGroups != null)
1330 for (int i = 0; i < allGroups.length; i++)
1332 if (allGroups[i].getStartRes() <= res
1333 && allGroups[i].getEndRes() >= res)
1335 stretchGroup = allGroups[i];
1340 av.setSelectionGroup(stretchGroup);
1343 // DETECT RIGHT MOUSE BUTTON IN AWT
1344 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1346 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1347 sequence.findPosition(res));
1349 Vector links = null;
1350 if (allFeatures != null)
1352 for (int i = 0; i < allFeatures.length; i++)
1354 if (allFeatures[i].links != null)
1358 links = new Vector();
1360 for (int j = 0; j < allFeatures[i].links.size(); j++)
1362 links.addElement(allFeatures[i].links.elementAt(j));
1367 APopupMenu popup = new APopupMenu(ap, null, links);
1369 popup.show(this, evt.getX(), evt.getY());
1375 seqCanvas.cursorX = findRes(evt);
1376 seqCanvas.cursorY = findSeq(evt);
1377 seqCanvas.repaint();
1381 // Only if left mouse button do we want to change group sizes
1383 if (stretchGroup == null)
1385 // define a new group here
1386 SequenceGroup sg = new SequenceGroup();
1387 sg.setStartRes(res);
1389 sg.addSequence(sequence, false);
1390 av.setSelectionGroup(sg);
1393 if (av.getConservationSelected())
1395 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1398 if (av.getAbovePIDThreshold())
1400 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1407 public void doMouseReleasedDefineMode(MouseEvent evt)
1409 if (stretchGroup == null)
1414 if (stretchGroup.cs != null)
1416 if (stretchGroup.cs instanceof ClustalxColourScheme)
1418 ((ClustalxColourScheme) stretchGroup.cs).alignmentChanged(
1419 stretchGroup,av.getHiddenRepSequences());
1422 if (stretchGroup.cs instanceof Blosum62ColourScheme
1423 || stretchGroup.cs instanceof PIDColourScheme
1424 || stretchGroup.cs.conservationApplied()
1425 || stretchGroup.cs.getThreshold() > 0)
1427 stretchGroup.recalcConservation();
1430 if (stretchGroup.cs.conservationApplied())
1432 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1433 stretchGroup.getName());
1434 stretchGroup.recalcConservation();
1438 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1439 stretchGroup.getName());
1442 changeEndRes = false;
1443 changeStartRes = false;
1444 stretchGroup = null;
1445 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1446 ap.paintAlignment(true);
1450 public void doMouseDraggedDefineMode(MouseEvent evt)
1452 int res = findRes(evt);
1453 int y = findSeq(evt);
1455 if (wrappedBlock != startWrapBlock)
1460 if (stretchGroup == null)
1465 mouseDragging = true;
1467 if (y > av.getAlignment().getHeight())
1469 y = av.getAlignment().getHeight() - 1;
1472 if (res >= av.getAlignment().getWidth())
1474 res = av.getAlignment().getWidth() - 1;
1477 if (stretchGroup.getEndRes() == res)
1479 // Edit end res position of selected group
1480 changeEndRes = true;
1482 else if (stretchGroup.getStartRes() == res)
1484 // Edit start res position of selected group
1485 changeStartRes = true;
1495 if (res > (stretchGroup.getStartRes() - 1))
1497 stretchGroup.setEndRes(res);
1500 else if (changeStartRes)
1502 if (res < (stretchGroup.getEndRes() + 1))
1504 stretchGroup.setStartRes(res);
1508 int dragDirection = 0;
1514 else if (y < oldSeq)
1519 while ((y != oldSeq) && (oldSeq > -1) && (y < av.getAlignment().getHeight()))
1521 // This routine ensures we don't skip any sequences, as the
1522 // selection is quite slow.
1523 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1525 oldSeq += dragDirection;
1532 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1534 if (stretchGroup.getSequences(null).contains(nextSeq))
1536 stretchGroup.deleteSequence(seq, false);
1542 stretchGroup.addSequence(seq, false);
1545 stretchGroup.addSequence(nextSeq, false);
1554 if (res > av.endRes || res < av.startRes || y < av.startSeq
1560 if (scrollThread != null)
1562 scrollThread.setEvent(evt);
1565 seqCanvas.repaint();
1568 public void mouseEntered(MouseEvent e)
1575 if (scrollThread != null)
1577 scrollThread.running = false;
1578 scrollThread = null;
1582 public void mouseExited(MouseEvent e)
1584 if (av.getWrapAlignment())
1589 if (mouseDragging && scrollThread == null)
1591 scrollThread = new ScrollThread();
1595 void scrollCanvas(MouseEvent evt)
1599 if (scrollThread != null)
1601 scrollThread.running = false;
1602 scrollThread = null;
1604 mouseDragging = false;
1608 if (scrollThread == null)
1610 scrollThread = new ScrollThread();
1613 mouseDragging = true;
1614 scrollThread.setEvent(evt);
1619 // this class allows scrolling off the bottom of the visible alignment
1620 class ScrollThread extends Thread
1624 boolean running = false;
1626 public ScrollThread()
1631 public void setEvent(MouseEvent e)
1636 public void stopScrolling()
1650 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1652 running = ap.scrollUp(true);
1655 if (mouseDragging && evt.getY() >= getSize().height
1656 && av.getAlignment().getHeight() > av.getEndSeq())
1658 running = ap.scrollUp(false);
1661 if (mouseDragging && evt.getX() < 0)
1663 running = ap.scrollRight(false);
1666 else if (mouseDragging && evt.getX() >= getSize().width)
1668 running = ap.scrollRight(true);
1675 } catch (Exception ex)
1683 * modify current selection according to a received message.
1685 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1686 SelectionSource source)
1688 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1689 // handles selection messages...
1690 // TODO: extend config options to allow user to control if selections may be
1691 // shared between viewports.
1693 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1694 .getSequenceSetId().equals(av.getSequenceSetId()))))
1698 // do we want to thread this ? (contention with seqsel and colsel locks, I
1700 // rules are: colsel is copied if there is a real intersection between
1701 // sequence selection
1702 boolean repaint = false, copycolsel = true;
1703 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1705 SequenceGroup sgroup = null;
1706 if (seqsel != null && seqsel.getSize()>0)
1708 if (av.getAlignment() == null)
1711 .println("Selection message: alignviewport av SeqSetId="
1712 + av.getSequenceSetId() + " ViewId="
1714 + " 's alignment is NULL! returning immediatly.");
1717 sgroup = seqsel.intersect(av.getAlignment(),
1718 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1719 if ((sgroup == null || sgroup.getSize() == 0)
1720 && (colsel == null || colsel.size() == 0))
1722 // don't copy columns if the region didn't intersect.
1726 if (sgroup != null && sgroup.getSize() > 0)
1728 av.setSelectionGroup(sgroup);
1732 av.setSelectionGroup(null);
1734 repaint = av.isSelectionGroupChanged(true);
1736 if (copycolsel && (av.getColumnSelection() == null || !av.isColSelChanged(true)))
1738 // the current selection is unset or from a previous message
1739 // so import the new colsel.
1740 if (colsel == null || colsel.size() == 0)
1742 if (av.getColumnSelection() != null)
1744 av.getColumnSelection().clear();
1749 // TODO: shift colSel according to the intersecting sequences
1750 if (av.getColumnSelection() == null)
1752 av.setColumnSelection(new ColumnSelection(colsel));
1756 av.getColumnSelection().setElementsFrom(colsel);
1759 repaint |= av.isColSelChanged(true);
1761 if (copycolsel && av.hasHiddenColumns()
1762 && (av.getColumnSelection() == null || av.getColumnSelection().getHiddenColumns() == null))
1764 System.err.println("Bad things");
1768 ap.scalePanelHolder.repaint();
1774 * 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);
1786 * scroll to the given row - or nearest visible location
1789 public void scrollToRow(int row)
1792 row = row<0 ? ap.av.startSeq : row;
1793 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1796 * scroll to the given column - or nearest visible location
1799 public void scrollToColumn(int column)
1802 column = column<0 ? ap.av.startRes : column;
1803 ap.scrollTo( column, column, ap.av.startSeq, true, true);