recovery of identical dataset sequence object on undo (rather than creation of a...
authorjprocter <Jim Procter>
Sun, 17 Jun 2007 13:01:14 +0000 (13:01 +0000)
committerjprocter <Jim Procter>
Sun, 17 Jun 2007 13:01:14 +0000 (13:01 +0000)
src/jalview/commands/EditCommand.java

index 61e2595..6740b7f 100644 (file)
@@ -265,27 +265,36 @@ public class EditCommand
       {
         command.string[i] = command.seqs[i].getSequence(command.position,
             command.position + command.number);
-
-        if (command.seqs[i].getDatasetSequence() != null
-            || command.seqs[i].getSequenceFeatures() != null)
+        SequenceI oldds = command.seqs[i].getDatasetSequence();
+        if (command.oldds!=null && command.oldds[i]!=null)
+        {
+          // we are redoing an undone cut.
+          command.seqs[i].setDatasetSequence(null);
+        }
+        command.seqs[i].deleteChars(command.position,
+                command.position + command.number);
+        if (command.oldds!=null && command.oldds[i]!=null)
         {
-          for (int s = command.position; s < command.position + command.number;
-               s++)
+          // oldds entry contains the cut dataset sequence.
+          command.seqs[i].setDatasetSequence(command.oldds[i]);
+          command.oldds[i] = oldds;
+        } else {
+          // modify the oldds if necessary
+          if (oldds!=command.seqs[i].getDatasetSequence()
+            || command.seqs[i].getSequenceFeatures() != null)
           {
-            if (jalview.schemes.ResidueProperties
-                .aaIndex[command.seqs[i].getCharAt(s)] != 23)
+            if (command.oldds==null)
             {
-              adjustFeatures(command, i,
-                             command.seqs[i].findPosition(command.position),
-                             command.seqs[i].findPosition(command.position +
-                  command.number),
-                             false);
-              break;
+              command.oldds = new SequenceI[command.seqs.length];
             }
+            command.oldds[i] = oldds;
+            adjustFeatures(command, i,
+                    command.seqs[i].findPosition(command.position),
+                    command.seqs[i].findPosition(command.position +
+                            command.number),
+                            false);
           }
         }
-        command.seqs[i].deleteChars(command.position,
-                                    command.position + command.number);
       }
 
       if (command.seqs[i].getLength() < 1)
@@ -302,12 +311,15 @@ public class EditCommand
   {
     StringBuffer tmp;
     boolean newDSNeeded;
+    boolean newDSWasNeeded;
+    int newstart,newend;
     boolean seqWasDeleted=false;
     int start = 0, end = 0;
 
     for (int i = 0; i < command.seqs.length; i++)
     {
       newDSNeeded = false;
+      newDSWasNeeded = command.oldds!=null && command.oldds[i]!=null;
       if (command.seqs[i].getLength() < 1)
       {
         // ie this sequence was deleted, we need to
@@ -323,9 +335,13 @@ public class EditCommand
         }
         seqWasDeleted=true;
       }
+      newstart = command.seqs[i].getStart();
+      newend = command.seqs[i].getEnd();
+
       tmp = new StringBuffer();
       tmp.append(command.seqs[i].getSequence());
-
+      //Undo of a delete does not replace original dataset sequence on to alignment sequence.
+      
       if (command.string != null && command.string[i] != null)
       {
         if (command.position >= tmp.length())
@@ -340,39 +356,57 @@ public class EditCommand
           }
         }
         tmp.insert(command.position, command.string[i]);
-
         for (int s = 0; s < command.string[i].length; s++)
         {
           if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=
               23)
           {
-            newDSNeeded = true;
-            start = command.seqs[i].findPosition(command.position);
-            end = command.seqs[i].findPosition(command.position +
+            if (!newDSNeeded)
+            {
+              newDSNeeded = true;
+              start = command.seqs[i].findPosition(command.position);
+              end = command.seqs[i].findPosition(command.position +
                                                command.number);
-            break;
+            }
+            if (command.seqs[i].getStart()==start)
+              newstart--;
+            else
+              newend++;
           }
         }
         command.string[i] = null;
       }
 
       command.seqs[i].setSequence(tmp.toString());
-
+      command.seqs[i].setStart(newstart);
+      command.seqs[i].setEnd(newend);
       if (newDSNeeded)
       {
         if (command.seqs[i].getDatasetSequence() != null)
-        { // use new ds mechanism here
-          Sequence ds = new Sequence(command.seqs[i].getName(),
+        { 
+          SequenceI ds;
+          if (newDSWasNeeded)
+          {
+            ds = command.oldds[i];
+          } else {
+            // make a new DS sequence
+            // use new ds mechanism here
+            ds= new Sequence(command.seqs[i].getName(),
                                      jalview.analysis.AlignSeq.extractGaps(
                                          jalview.util.Comparison.GapChars,
                                          command.seqs[i].getSequenceAsString()
                                      ),
                                      command.seqs[i].getStart(),
                                      command.seqs[i].getEnd());
-          ds.setDescription(command.seqs[i].getDescription());
+            ds.setDescription(command.seqs[i].getDescription());
+          }
+          if (command.oldds==null)
+          {
+            command.oldds = new SequenceI[command.seqs.length];
+          }
+          command.oldds[i]=command.seqs[i].getDatasetSequence();
           command.seqs[i].setDatasetSequence(ds);
         }
-
         adjustFeatures(command, i, start, end, true);
       }
     }
@@ -793,6 +827,7 @@ public class EditCommand
 
   class Edit
   {
+    public SequenceI[] oldds;
     boolean fullAlignmentHeight = false;
     Hashtable deletedAnnotationRows;
     Hashtable deletedAnnotations;