+ StringBuilder tmp;
+ String oldstring;
+ int start = command.position;
+ int end = command.number;
+ // TODO TUTORIAL - Fix for replacement with different length of sequence (or
+ // whole sequence)
+ // TODO Jalview 2.4 bugfix change to an aggregate command - original
+ // sequence string is cut, new string is pasted in.
+ command.number = start + command.string[0].length;
+ for (int i = 0; i < command.seqs.length; i++)
+ {
+ boolean newDSWasNeeded = command.oldds != null
+ && command.oldds[i] != null;
+ boolean newStartEndWasNeeded = command.oldStartEnd != null
+ && command.oldStartEnd[i] != null;
+
+ /**
+ * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
+ * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
+ * viewport.alignment));
+ *
+ */
+ /**
+ * then addHistoryItem(new EditCommand( "Add sequences",
+ * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
+ *
+ */
+ ContiguousI beforeEditedPositions = command.seqs[i].findPositions(1,
+ start);
+ ContiguousI afterEditedPositions = command.seqs[i]
+ .findPositions(end + 1, command.seqs[i].getLength());
+
+ oldstring = command.seqs[i].getSequenceAsString();
+ tmp = new StringBuilder(oldstring.substring(0, start));
+ tmp.append(command.string[i]);
+ String nogaprep = AlignSeq.extractGaps(Comparison.GapChars,
+ new String(command.string[i]));
+ if (end < oldstring.length())
+ {
+ tmp.append(oldstring.substring(end));
+ }
+ // stash end prior to updating the sequence object so we can save it if
+ // need be.
+ Range oldstartend = new Range(command.seqs[i].getStart(),
+ command.seqs[i].getEnd());
+ command.seqs[i].setSequence(tmp.toString());
+ command.string[i] = oldstring
+ .substring(start, Math.min(end, oldstring.length()))
+ .toCharArray();
+ String nogapold = AlignSeq.extractGaps(Comparison.GapChars,
+ new String(command.string[i]));
+
+ if (!nogaprep.toLowerCase(Locale.ROOT)
+ .equals(nogapold.toLowerCase(Locale.ROOT)))
+ {
+ // we may already have dataset and limits stashed...
+ if (newDSWasNeeded || newStartEndWasNeeded)
+ {
+ if (newDSWasNeeded)
+ {
+ // then just switch the dataset sequence
+ SequenceI oldds = command.seqs[i].getDatasetSequence();
+ command.seqs[i].setDatasetSequence(command.oldds[i]);
+ command.oldds[i] = oldds;
+ }
+ if (newStartEndWasNeeded)
+ {
+ Range newStart = command.oldStartEnd[i];
+ command.oldStartEnd[i] = oldstartend;
+ command.seqs[i].setStart(newStart.getBegin());
+ command.seqs[i].setEnd(newStart.getEnd());
+ }
+ }
+ else
+ {
+ // decide if we need a new dataset sequence or modify start/end
+ // first edit the original dataset sequence string
+ SequenceI oldds = command.seqs[i].getDatasetSequence();
+ String osp = oldds.getSequenceAsString();
+ int beforeStartOfEdit = -oldds.getStart() + 1
+ + (beforeEditedPositions == null
+ ? ((afterEditedPositions != null)
+ ? afterEditedPositions.getBegin() - 1
+ : oldstartend.getBegin()
+ + nogapold.length())
+ : beforeEditedPositions.getEnd());
+ int afterEndOfEdit = -oldds.getStart() + 1
+ + ((afterEditedPositions == null) ? oldstartend.getEnd()
+ : afterEditedPositions.getBegin() - 1);
+ String fullseq = osp.substring(0, beforeStartOfEdit) + nogaprep
+ + osp.substring(afterEndOfEdit);
+
+ // and check if new sequence data is different..
+ if (!fullseq.equalsIgnoreCase(osp))
+ {
+ // old ds and edited ds are different, so
+ // create the new dataset sequence
+ SequenceI newds = new Sequence(oldds);
+ newds.setSequence(fullseq.toUpperCase(Locale.ROOT));
+
+ if (command.oldds == null)
+ {
+ command.oldds = new SequenceI[command.seqs.length];
+ }
+ command.oldds[i] = command.seqs[i].getDatasetSequence();