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)
350 int value = Integer.parseInt(keyboardNo1.toString());
354 } catch (Exception x)
363 if (keyboardNo2!=null){
364 int value = Integer.parseInt(keyboardNo2.toString());
368 } catch (Exception x)
374 void setStatusMessage(SequenceI sequence, int res, int seq)
376 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
377 + sequence.getName());
380 if (av.getAlignment().isNucleotide())
382 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
386 text.append(" Nucleotide: ");
391 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
394 text.append(" Residue: ");
403 text.append(obj + " (" + sequence.findPosition(res) + ")");
407 ap.alignFrame.statusBar.setText(text.toString());
411 public void mousePressed(MouseEvent evt)
413 lastMousePress = evt.getPoint();
415 // For now, ignore the mouseWheel font resizing on Macs
416 // As the Button2_mask always seems to be true
417 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
420 mouseWheelPressed = true;
424 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
426 if (evt.isControlDown() || evt.isAltDown())
434 doMousePressedDefineMode(evt);
438 int seq = findSeq(evt);
439 int res = findRes(evt);
441 if (seq < 0 || res < 0)
446 if ((seq < av.getAlignment().getHeight())
447 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
461 public void mouseClicked(MouseEvent evt)
463 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
464 if (evt.getClickCount() > 1)
466 if (av.getSelectionGroup() != null
467 && av.getSelectionGroup().getSize() == 1
468 && av.getSelectionGroup().getEndRes()
469 - av.getSelectionGroup().getStartRes() < 2)
471 av.setSelectionGroup(null);
474 SequenceFeature[] features = findFeaturesAtRes(sequence,
475 sequence.findPosition(findRes(evt)));
477 if (features != null && features.length > 0)
479 SearchResults highlight = new SearchResults();
480 highlight.addResult(sequence, features[0].getBegin(),
481 features[0].getEnd());
482 seqCanvas.highlightSearchResults(highlight);
484 if (features != null && features.length > 0)
486 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
487 { sequence }, features, false, ap);
489 seqCanvas.highlightSearchResults(null);
494 public void mouseReleased(MouseEvent evt)
496 mouseDragging = false;
497 mouseWheelPressed = false;
498 ap.paintAlignment(true);
502 doMouseReleasedDefineMode(evt);
510 int startWrapBlock = -1;
512 int wrappedBlock = -1;
514 int findRes(MouseEvent evt)
519 if (av.wrapAlignment)
522 int hgap = av.charHeight;
523 if (av.scaleAboveWrapped)
525 hgap += av.charHeight;
528 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
529 + seqCanvas.getAnnotationHeight();
533 x -= seqCanvas.LABEL_WEST;
535 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
541 wrappedBlock = y / cHeight;
542 wrappedBlock += av.getStartRes() / cwidth;
544 res = wrappedBlock * cwidth + x / av.getCharWidth();
549 res = (x / av.getCharWidth()) + av.getStartRes();
552 if (av.hasHiddenColumns())
554 res = av.getColumnSelection().adjustForHiddenColumns(res);
561 int findSeq(MouseEvent evt)
563 final int sqnum = findAlRow(evt);
564 return (sqnum < 0) ? 0 : sqnum;
570 * @return row in alignment that was selected (or -1 for column selection)
572 private int findAlRow(MouseEvent evt)
577 if (av.wrapAlignment)
579 int hgap = av.charHeight;
580 if (av.scaleAboveWrapped)
582 hgap += av.charHeight;
585 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
586 + seqCanvas.getAnnotationHeight();
590 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
599 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
600 .getAlignment().getHeight() - 1);
610 public void doMousePressed(MouseEvent evt)
613 int seq = findSeq(evt);
614 int res = findRes(evt);
616 if (seq < av.getAlignment().getHeight()
617 && res < av.getAlignment().getSequenceAt(seq).getLength())
619 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
620 // Find the residue's position in the sequence (res is the position
637 public void mouseOverSequence(SequenceI sequence, int index, int pos)
639 String tmp = sequence.hashCode() + index + "";
640 if (lastMessage == null || !lastMessage.equals(tmp))
641 ssm.mouseOverSequence(sequence, index, pos, av);
646 public void highlightSequence(SearchResults results)
648 if (av.followHighlight)
650 if (ap.scrollToPosition(results, true))
652 ap.alignFrame.repaint();
655 seqCanvas.highlightSearchResults(results);
659 public void updateColours(SequenceI seq, int index)
661 System.out.println("update the seqPanel colours");
665 public void mouseMoved(MouseEvent evt)
667 int res = findRes(evt);
668 int seq = findSeq(evt);
670 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
679 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
680 if (res > sequence.getLength())
689 int respos = sequence.findPosition(res);
691 mouseOverSequence(sequence, res, respos);
693 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
694 + sequence.getName());
697 if (av.getAlignment().isNucleotide())
699 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
703 text.append(" Nucleotide: ");
708 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
711 text.append(" Residue: ");
719 text.append(obj + " (" + respos + ")");
723 ap.alignFrame.statusBar.setText(text.toString());
725 StringBuffer tooltipText = new StringBuffer();
726 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
729 for (int g = 0; g < groups.length; g++)
731 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
733 if (!groups[g].getName().startsWith("JTreeGroup")
734 && !groups[g].getName().startsWith("JGroup"))
736 tooltipText.append(groups[g].getName() + " ");
738 if (groups[g].getDescription() != null)
740 tooltipText.append(groups[g].getDescription());
742 tooltipText.append("\n");
747 // use aa to see if the mouse pointer is on a
748 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
749 sequence.findPosition(res));
752 while (index < allFeatures.length)
754 SequenceFeature sf = allFeatures[index];
756 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
758 if (sf.getDescription() != null)
760 tooltipText.append(" " + sf.getDescription());
763 if (sf.getValue("status") != null)
765 String status = sf.getValue("status").toString();
766 if (status.length() > 0)
768 tooltipText.append(" (" + sf.getValue("status") + ")");
771 tooltipText.append("\n");
778 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
782 tooltip.setTip(tooltipText.toString());
786 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
788 Vector tmp = new Vector();
789 SequenceFeature[] features = sequence.getSequenceFeatures();
790 if (features != null)
792 for (int i = 0; i < features.length; i++)
794 if (av.featuresDisplayed == null
795 || !av.featuresDisplayed.containsKey(features[i].getType()))
800 if (features[i].featureGroup != null
801 && seqCanvas.fr.featureGroups != null
802 && seqCanvas.fr.featureGroups
803 .containsKey(features[i].featureGroup)
804 && !((Boolean) seqCanvas.fr.featureGroups
805 .get(features[i].featureGroup)).booleanValue())
808 if ((features[i].getBegin() <= res)
809 && (features[i].getEnd() >= res))
811 tmp.addElement(features[i]);
816 features = new SequenceFeature[tmp.size()];
817 tmp.copyInto(features);
824 public void mouseDragged(MouseEvent evt)
826 if (mouseWheelPressed)
828 int oldWidth = av.charWidth;
830 // Which is bigger, left-right or up-down?
831 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
834 int fontSize = av.font.getSize();
836 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
840 else if (evt.getY() > lastMousePress.y)
850 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
851 av.charWidth = oldWidth;
855 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
859 else if (evt.getX() > lastMousePress.x)
864 if (av.charWidth < 1)
872 FontMetrics fm = getFontMetrics(av.getFont());
873 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
875 lastMousePress = evt.getPoint();
877 ap.paintAlignment(false);
878 ap.annotationPanel.image = null;
884 doMouseDraggedDefineMode(evt);
888 int res = findRes(evt);
895 if ((lastres == -1) || (lastres == res))
900 if ((res < av.getAlignment().getWidth()) && (res < lastres))
902 // dragLeft, delete gap
903 editSequence(false, res);
907 editSequence(true, res);
910 mouseDragging = true;
911 if (scrollThread != null)
913 scrollThread.setEvent(evt);
918 synchronized void editSequence(boolean insertGap, int startres)
922 boolean fixedColumns = false;
923 SequenceGroup sg = av.getSelectionGroup();
925 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
927 if (!groupEditing && av.hasHiddenRows())
929 if (av.isHiddenRepSequence(seq))
931 sg = (SequenceGroup) av.getRepresentedSequences(seq);
936 StringBuffer message = new StringBuffer();
939 message.append("Edit group:");
940 if (editCommand == null)
942 editCommand = new EditCommand("Edit Group");
947 message.append("Edit sequence: " + seq.getName());
948 String label = seq.getName();
949 if (label.length() > 10)
951 label = label.substring(0, 10);
953 if (editCommand == null)
955 editCommand = new EditCommand("Edit " + label);
961 message.append(" insert ");
965 message.append(" delete ");
968 message.append(Math.abs(startres - lastres) + " gaps.");
969 ap.alignFrame.statusBar.setText(message.toString());
971 // Are we editing within a selection group?
973 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
978 // sg might be null as the user may only see 1 sequence,
979 // but the sequence represents a group
982 if (!av.isHiddenRepSequence(seq))
988 sg = av.getRepresentedSequences(seq);
991 fixedLeft = sg.getStartRes();
992 fixedRight = sg.getEndRes();
994 if ((startres < fixedLeft && lastres >= fixedLeft)
995 || (startres >= fixedLeft && lastres < fixedLeft)
996 || (startres > fixedRight && lastres <= fixedRight)
997 || (startres <= fixedRight && lastres > fixedRight))
1003 if (fixedLeft > startres)
1005 fixedRight = fixedLeft - 1;
1008 else if (fixedRight < startres)
1010 fixedLeft = fixedRight;
1015 if (av.hasHiddenColumns())
1017 fixedColumns = true;
1018 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1019 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1021 if ((insertGap && startres > y1 && lastres < y1)
1022 || (!insertGap && startres < y2 && lastres > y2))
1028 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1029 // Selection spans a hidden region
1030 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1038 fixedRight = y2 - 1;
1045 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1046 .toArray(new SequenceI[0]);
1051 // If the user has selected the whole sequence, and is dragging to
1052 // the right, we can still extend the alignment and selectionGroup
1053 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1054 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1056 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1057 fixedRight = sg.getEndRes();
1060 // Is it valid with fixed columns??
1061 // Find the next gap before the end
1062 // of the visible region boundary
1063 boolean blank = false;
1064 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1068 for (SequenceI gs : groupSeqs)
1070 for (int j = 0; j < startres - lastres; j++)
1072 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1088 if (sg.getSize() == av.getAlignment().getHeight())
1090 if ((av.hasHiddenColumns() && startres < av
1091 .getColumnSelection().getHiddenBoundaryRight(startres)))
1097 int alWidth = av.getAlignment().getWidth();
1098 if (av.hasHiddenRows())
1100 int hwidth = av.getAlignment().getHiddenSequences()
1102 if (hwidth > alWidth)
1107 // We can still insert gaps if the selectionGroup
1108 // contains all the sequences
1109 sg.setEndRes(sg.getEndRes() + startres - lastres);
1110 fixedRight = alWidth + startres - lastres;
1121 else if (!insertGap)
1123 // / Are we able to delete?
1124 // ie are all columns blank?
1126 for (SequenceI gs : groupSeqs)
1128 for (int j = startres; j < lastres; j++)
1130 if (gs.getLength() <= j)
1135 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1137 // Not a gap, block edit not valid
1147 // dragging to the right
1148 if (fixedColumns && fixedRight != -1)
1150 for (int j = lastres; j < startres; j++)
1152 insertChar(j, groupSeqs, fixedRight);
1157 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1158 startres, startres - lastres, av.getAlignment(), true);
1163 // dragging to the left
1164 if (fixedColumns && fixedRight != -1)
1166 for (int j = lastres; j > startres; j--)
1168 deleteChar(startres, groupSeqs, fixedRight);
1173 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1174 startres, lastres - startres, av.getAlignment(), true);
1180 // ///Editing a single sequence///////////
1184 // dragging to the right
1185 if (fixedColumns && fixedRight != -1)
1187 for (int j = lastres; j < startres; j++)
1189 insertChar(j, new SequenceI[]
1190 { seq }, fixedRight);
1195 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1196 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1201 // dragging to the left
1202 if (fixedColumns && fixedRight != -1)
1204 for (int j = lastres; j > startres; j--)
1206 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1211 deleteChar(startres, new SequenceI[]
1212 { seq }, fixedRight);
1217 // could be a keyboard edit trying to delete none gaps
1219 for (int m = startres; m < lastres; m++)
1221 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1230 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1231 { seq }, startres, max, av.getAlignment(), true);
1238 seqCanvas.repaint();
1241 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1243 int blankColumn = fixedColumn;
1244 for (int s = 0; s < seq.length; s++)
1246 // Find the next gap before the end of the visible region boundary
1247 // If lastCol > j, theres a boundary after the gap insertion
1249 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1251 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1253 // Theres a space, so break and insert the gap
1258 if (blankColumn <= j)
1260 blankColumn = fixedColumn;
1266 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1267 av.getAlignment(), true);
1269 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1270 av.getAlignment(), true);
1274 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1277 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1278 av.getAlignment(), true);
1280 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1281 av.getAlignment(), true);
1284 // ////////////////////////////////////////
1285 // ///Everything below this is for defining the boundary of the rubberband
1286 // ////////////////////////////////////////
1287 public void doMousePressedDefineMode(MouseEvent evt)
1289 if (scrollThread != null)
1291 scrollThread.running = false;
1292 scrollThread = null;
1295 int res = findRes(evt);
1296 int seq = findSeq(evt);
1298 startWrapBlock = wrappedBlock;
1305 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1307 if (sequence == null || res > sequence.getLength())
1312 stretchGroup = av.getSelectionGroup();
1314 if (stretchGroup == null)
1316 stretchGroup = av.getAlignment().findGroup(sequence);
1317 if (stretchGroup != null && res > stretchGroup.getStartRes()
1318 && res < stretchGroup.getEndRes())
1320 av.setSelectionGroup(stretchGroup);
1324 stretchGroup = null;
1328 else if (!stretchGroup.getSequences(null).contains(sequence)
1329 || stretchGroup.getStartRes() > res
1330 || stretchGroup.getEndRes() < res)
1332 stretchGroup = null;
1334 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1336 if (allGroups != null)
1338 for (int i = 0; i < allGroups.length; i++)
1340 if (allGroups[i].getStartRes() <= res
1341 && allGroups[i].getEndRes() >= res)
1343 stretchGroup = allGroups[i];
1348 av.setSelectionGroup(stretchGroup);
1351 // DETECT RIGHT MOUSE BUTTON IN AWT
1352 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1354 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1355 sequence.findPosition(res));
1357 Vector links = null;
1358 if (allFeatures != null)
1360 for (int i = 0; i < allFeatures.length; i++)
1362 if (allFeatures[i].links != null)
1366 links = new Vector();
1368 for (int j = 0; j < allFeatures[i].links.size(); j++)
1370 links.addElement(allFeatures[i].links.elementAt(j));
1375 APopupMenu popup = new APopupMenu(ap, null, links);
1377 popup.show(this, evt.getX(), evt.getY());
1383 seqCanvas.cursorX = findRes(evt);
1384 seqCanvas.cursorY = findSeq(evt);
1385 seqCanvas.repaint();
1389 // Only if left mouse button do we want to change group sizes
1391 if (stretchGroup == null)
1393 // define a new group here
1394 SequenceGroup sg = new SequenceGroup();
1395 sg.setStartRes(res);
1397 sg.addSequence(sequence, false);
1398 av.setSelectionGroup(sg);
1401 if (av.getConservationSelected())
1403 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1406 if (av.getAbovePIDThreshold())
1408 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1415 public void doMouseReleasedDefineMode(MouseEvent evt)
1417 if (stretchGroup == null)
1422 stretchGroup.recalcConservation(); // always do this - annotation has own
1424 if (stretchGroup.cs != null)
1426 stretchGroup.cs.alignmentChanged(stretchGroup,
1427 av.getHiddenRepSequences());
1429 if (stretchGroup.cs.conservationApplied())
1431 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1432 stretchGroup.getName());
1436 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1437 stretchGroup.getName());
1440 changeEndRes = false;
1441 changeStartRes = false;
1442 stretchGroup = null;
1443 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1444 ap.paintAlignment(true);
1448 public void doMouseDraggedDefineMode(MouseEvent evt)
1450 int res = findRes(evt);
1451 int y = findSeq(evt);
1453 if (wrappedBlock != startWrapBlock)
1458 if (stretchGroup == null)
1463 mouseDragging = true;
1465 if (y > av.getAlignment().getHeight())
1467 y = av.getAlignment().getHeight() - 1;
1470 if (res >= av.getAlignment().getWidth())
1472 res = av.getAlignment().getWidth() - 1;
1475 if (stretchGroup.getEndRes() == res)
1477 // Edit end res position of selected group
1478 changeEndRes = true;
1480 else if (stretchGroup.getStartRes() == res)
1482 // Edit start res position of selected group
1483 changeStartRes = true;
1493 if (res > (stretchGroup.getStartRes() - 1))
1495 stretchGroup.setEndRes(res);
1498 else if (changeStartRes)
1500 if (res < (stretchGroup.getEndRes() + 1))
1502 stretchGroup.setStartRes(res);
1506 int dragDirection = 0;
1512 else if (y < oldSeq)
1517 while ((y != oldSeq) && (oldSeq > -1)
1518 && (y < av.getAlignment().getHeight()))
1520 // This routine ensures we don't skip any sequences, as the
1521 // selection is quite slow.
1522 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1524 oldSeq += dragDirection;
1531 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1533 if (stretchGroup.getSequences(null).contains(nextSeq))
1535 stretchGroup.deleteSequence(seq, false);
1541 stretchGroup.addSequence(seq, false);
1544 stretchGroup.addSequence(nextSeq, false);
1553 if (res > av.endRes || res < av.startRes || y < av.startSeq
1559 if (scrollThread != null)
1561 scrollThread.setEvent(evt);
1564 seqCanvas.repaint();
1567 public void mouseEntered(MouseEvent e)
1574 if (scrollThread != null)
1576 scrollThread.running = false;
1577 scrollThread = null;
1581 public void mouseExited(MouseEvent e)
1583 if (av.getWrapAlignment())
1588 if (mouseDragging && scrollThread == null)
1590 scrollThread = new ScrollThread();
1594 void scrollCanvas(MouseEvent evt)
1598 if (scrollThread != null)
1600 scrollThread.running = false;
1601 scrollThread = null;
1603 mouseDragging = false;
1607 if (scrollThread == null)
1609 scrollThread = new ScrollThread();
1612 mouseDragging = true;
1613 scrollThread.setEvent(evt);
1618 // this class allows scrolling off the bottom of the visible alignment
1619 class ScrollThread extends Thread
1623 boolean running = false;
1625 public ScrollThread()
1630 public void setEvent(MouseEvent e)
1635 public void stopScrolling()
1649 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1651 running = ap.scrollUp(true);
1654 if (mouseDragging && evt.getY() >= getSize().height
1655 && av.getAlignment().getHeight() > av.getEndSeq())
1657 running = ap.scrollUp(false);
1660 if (mouseDragging && evt.getX() < 0)
1662 running = ap.scrollRight(false);
1665 else if (mouseDragging && evt.getX() >= getSize().width)
1667 running = ap.scrollRight(true);
1674 } catch (Exception ex)
1682 * modify current selection according to a received message.
1684 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1685 SelectionSource source)
1687 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1688 // handles selection messages...
1689 // TODO: extend config options to allow user to control if selections may be
1690 // shared between viewports.
1692 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1693 .getSequenceSetId().equals(av.getSequenceSetId()))))
1697 // do we want to thread this ? (contention with seqsel and colsel locks, I
1699 // rules are: colsel is copied if there is a real intersection between
1700 // sequence selection
1701 boolean repaint = false, copycolsel = true;
1702 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1704 SequenceGroup sgroup = null;
1705 if (seqsel != null && seqsel.getSize() > 0)
1707 if (av.getAlignment() == null)
1710 .println("Selection message: alignviewport av SeqSetId="
1711 + av.getSequenceSetId() + " ViewId="
1713 + " 's alignment is NULL! returning immediatly.");
1716 sgroup = seqsel.intersect(av.getAlignment(),
1717 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1718 if ((sgroup == null || sgroup.getSize() == 0)
1719 && (colsel == null || colsel.size() == 0))
1721 // don't copy columns if the region didn't intersect.
1725 if (sgroup != null && sgroup.getSize() > 0)
1727 av.setSelectionGroup(sgroup);
1731 av.setSelectionGroup(null);
1733 repaint = av.isSelectionGroupChanged(true);
1736 && (av.getColumnSelection() == null || !av
1737 .isColSelChanged(true)))
1739 // the current selection is unset or from a previous message
1740 // so import the new colsel.
1741 if (colsel == null || colsel.size() == 0)
1743 if (av.getColumnSelection() != null)
1745 av.getColumnSelection().clear();
1750 // TODO: shift colSel according to the intersecting sequences
1751 if (av.getColumnSelection() == null)
1753 av.setColumnSelection(new ColumnSelection(colsel));
1757 av.getColumnSelection().setElementsFrom(colsel);
1760 repaint |= av.isColSelChanged(true);
1763 && av.hasHiddenColumns()
1764 && (av.getColumnSelection() == null || av.getColumnSelection()
1765 .getHiddenColumns() == null))
1767 System.err.println("Bad things");
1771 ap.scalePanelHolder.repaint();
1777 * scroll to the given row/column - or nearest visible location
1782 public void scrollTo(int row, int column)
1785 row = row < 0 ? ap.av.startSeq : row;
1786 column = column < 0 ? ap.av.startRes : column;
1787 ap.scrollTo(column, column, row, true, true);
1791 * scroll to the given row - or nearest visible location
1795 public void scrollToRow(int row)
1798 row = row < 0 ? ap.av.startSeq : row;
1799 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1803 * scroll to the given column - or nearest visible location
1807 public void scrollToColumn(int column)
1810 column = column < 0 ? ap.av.startRes : column;
1811 ap.scrollTo(column, column, ap.av.startSeq, true, true);