bugfix for deleted annotation undo
[jalview.git] / src / jalview / commands / EditCommand.java
index 052b212..cbcc700 100644 (file)
@@ -44,6 +44,7 @@ public class EditCommand
   public static final int DELETE_GAP = 1;
   public static final int CUT = 2;
   public static final int PASTE = 3;
+  public static final int REPLACE = 4;
 
   Edit[] edits;
 
@@ -75,6 +76,24 @@ public class EditCommand
     performEdit(0);
   }
 
+  public EditCommand(String description,
+                     int command,
+                     String replace,
+                     SequenceI[] seqs,
+                     int position,
+                     int number,
+                     AlignmentI al)
+  {
+    this.description = description;
+    if (command == REPLACE)
+    {
+      edits = new Edit[]
+          { new Edit(command, seqs, position, number, al, replace)};
+    }
+
+    performEdit(0);
+  }
+
   final public String getDescription()
   {
     return description;
@@ -129,21 +148,23 @@ public class EditCommand
     int eSize = edits.length;
     for (int e = commandIndex; e < eSize; e++)
     {
-      if (edits[e].command == INSERT_GAP)
+      switch(edits[e].command)
       {
+        case INSERT_GAP:
         insertGap(edits[e]);
-      }
-      else if (edits[e].command == DELETE_GAP)
-      {
+          break;
+        case DELETE_GAP:
         deleteGap(edits[e]);
-      }
-      else if (edits[e].command == CUT)
-      {
+          break;
+        case CUT:
         cut(edits[e]);
-      }
-      else if (edits[e].command == PASTE)
-      {
+          break;
+        case PASTE:
         paste(edits[e]);
+          break;
+        case REPLACE:
+          replace(edits[e]);
+          break;
       }
     }
   }
@@ -158,21 +179,23 @@ public class EditCommand
     int e = 0, eSize = edits.length;
     for (e = eSize - 1; e > -1; e--)
     {
-      if (edits[e].command == INSERT_GAP)
+      switch (edits[e].command)
       {
+        case INSERT_GAP:
         deleteGap(edits[e]);
-      }
-      else if (edits[e].command == DELETE_GAP)
-      {
+          break;
+        case DELETE_GAP:
         insertGap(edits[e]);
-      }
-      else if (edits[e].command == CUT)
-      {
+          break;
+        case CUT:
         paste(edits[e]);
-      }
-      else if (edits[e].command == PASTE)
-      {
+          break;
+        case PASTE:
         cut(edits[e]);
+          break;
+        case REPLACE:
+          replace(edits[e]);
+          break;
       }
     }
   }
@@ -327,6 +350,27 @@ public class EditCommand
     command.string = null;
   }
 
+  void replace(Edit command)
+  {
+    StringBuffer tmp;
+    String oldstring;
+    int start = command.position;
+    int end = command.number;
+
+    command.number = start + command.string[0].length;
+    for (int i = 0; i < command.seqs.length; i++)
+    {
+      oldstring = command.seqs[i].getSequenceAsString();
+      tmp = new StringBuffer(oldstring.substring(0, start));
+      tmp.append(command.string[i]);
+      tmp.append(oldstring.substring(end));
+      command.seqs[i].setSequence(tmp.toString());
+      command.string[i] = oldstring.substring(start, end).toCharArray();
+      tmp = null;
+      oldstring = null;
+    }
+  }
+
   final void adjustAnnotations(Edit command, boolean insert, boolean modifyVisibility)
   {
 
@@ -347,7 +391,7 @@ public class EditCommand
       AlignmentAnnotation[] tmp;
       for (int s = 0; s < command.seqs.length; s++)
       {
-        if (modifyVisibility) 
+        if (modifyVisibility)
         {
           // Rows are only removed or added to sequence object.
           if (!insert) {
@@ -428,11 +472,22 @@ public class EditCommand
       }
 
       int tSize = 0;
-
+      if (annotations[a].annotations == null)
+      {
+        // nothing to edit here ?
+        continue;
+      }
       aSize = annotations[a].annotations.length;
       if (insert)
       {
         temp = new Annotation[aSize + command.number];
+        if(annotations[a].padGaps)
+          for (int aa = 0; aa < temp.length; aa++)
+          {
+            temp[aa] = new Annotation(
+                command.gapChar+"",
+                null, ' ', 0);
+          }
       }
       else
       {
@@ -522,7 +577,7 @@ public class EditCommand
                            0, temp, 0, copylen); //command.position);
 
           Annotation[] deleted = new Annotation[command.number];
-          if (copylen>command.position) {
+          if (copylen>=command.position) {
             copylen = Math.min(command.number, annotations[a].annotations.length-command.position);
             if (copylen>0)
             {
@@ -697,6 +752,27 @@ public class EditCommand
 
       fullAlignmentHeight = (al.getHeight() == seqs.length);
     }
-  }
 
+    Edit(int command,
+         SequenceI[] seqs,
+         int position,
+         int number,
+         AlignmentI al,
+         String replace)
+    {
+      this.command = command;
+      this.seqs = seqs;
+      this.position = position;
+      this.number = number;
+      this.al = al;
+      this.gapChar = al.getGapCharacter();
+      string = new char[seqs.length][];
+      for (int i = 0; i < seqs.length; i++)
+      {
+        string[i] = replace.toCharArray();
+      }
+
+      fullAlignmentHeight = (al.getHeight() == seqs.length);
+    }
+  }
 }