Formatting
[jalview.git] / src / jalview / commands / EditCommand.java
index d9f51e0..d0a2e03 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
  * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
  *\r
  * This program is free software; you can redistribute it and/or\r
  * modify it under the terms of the GNU General Public License\r
@@ -18,9 +18,9 @@
  */\r
 package jalview.commands;\r
 \r
-import jalview.datamodel.*;\r
+import java.util.*;\r
 \r
-import java.util.Hashtable;\r
+import jalview.datamodel.*;\r
 \r
 /**\r
  *\r
@@ -37,7 +37,8 @@ import java.util.Hashtable;
  * @author not attributable\r
  * @version 1.0\r
  */\r
-public class EditCommand implements CommandI\r
+public class EditCommand\r
+    implements CommandI\r
 {\r
   public static final int INSERT_GAP = 0;\r
   public static final int DELETE_GAP = 1;\r
@@ -56,22 +57,23 @@ public class EditCommand implements CommandI
     this.description = description;\r
   }\r
 \r
-  public EditCommand( String description,\r
-                      int command,\r
-                      SequenceI[] seqs,\r
-                      int position,\r
-                      int number,\r
-                      AlignmentI al)\r
-   {\r
-     this.description = description;\r
-     if ( command==CUT || command==PASTE)\r
-     {\r
-       edits = new Edit[]{new Edit(command, seqs, position, number, al)};\r
-     }\r
-\r
-     performEdit(0);\r
-  }\r
+  public EditCommand(String description,\r
+                     int command,\r
+                     SequenceI[] seqs,\r
+                     int position,\r
+                     int number,\r
+                     AlignmentI al)\r
+  {\r
+    this.description = description;\r
+    if (command == CUT || command == PASTE)\r
+    {\r
+      edits = new Edit[]\r
+          {\r
+          new Edit(command, seqs, position, number, al)};\r
+    }\r
 \r
+    performEdit(0);\r
+  }\r
 \r
   final public String getDescription()\r
   {\r
@@ -80,7 +82,7 @@ public class EditCommand implements CommandI
 \r
   public int getSize()\r
   {\r
-    return edits==null?0:edits.length;\r
+    return edits == null ? 0 : edits.length;\r
   }\r
 \r
   final public AlignmentI getAlignment()\r
@@ -88,16 +90,15 @@ public class EditCommand implements CommandI
     return edits[0].al;\r
   }\r
 \r
-\r
   final public void appendEdit(int command,\r
-                         SequenceI[] seqs,\r
-                         int position,\r
-                         int number,\r
-                         AlignmentI al,\r
-                         boolean performEdit)\r
+                               SequenceI[] seqs,\r
+                               int position,\r
+                               int number,\r
+                               AlignmentI al,\r
+                               boolean performEdit)\r
   {\r
     Edit edit = new Edit(command, seqs, position, number, al.getGapCharacter());\r
-    if(al.getHeight()==seqs.length)\r
+    if (al.getHeight() == seqs.length)\r
     {\r
       edit.al = al;\r
       edit.fullAlignmentHeight = true;\r
@@ -111,10 +112,16 @@ public class EditCommand implements CommandI
       edits[edits.length - 1] = edit;\r
     }\r
     else\r
-      edits = new Edit[] {  edit  };\r
+    {\r
+      edits = new Edit[]\r
+          {\r
+          edit};\r
+    }\r
 \r
     if (performEdit)\r
+    {\r
       performEdit(edits.length - 1);\r
+    }\r
   }\r
 \r
   final void performEdit(int commandIndex)\r
@@ -122,19 +129,19 @@ public class EditCommand implements CommandI
     int eSize = edits.length;\r
     for (int e = commandIndex; e < eSize; e++)\r
     {\r
-      if (edits[e].command==INSERT_GAP)\r
+      if (edits[e].command == INSERT_GAP)\r
       {\r
         insertGap(edits[e]);\r
       }\r
-      else if (edits[e].command==DELETE_GAP)\r
+      else if (edits[e].command == DELETE_GAP)\r
       {\r
         deleteGap(edits[e]);\r
       }\r
-      else if(edits[e].command==CUT)\r
+      else if (edits[e].command == CUT)\r
       {\r
         cut(edits[e]);\r
       }\r
-      else if(edits[e].command==PASTE)\r
+      else if (edits[e].command == PASTE)\r
       {\r
         paste(edits[e]);\r
       }\r
@@ -149,21 +156,21 @@ public class EditCommand implements CommandI
   final public void undoCommand()\r
   {\r
     int e = 0, eSize = edits.length;\r
-    for (e = eSize-1; e > -1; e--)\r
+    for (e = eSize - 1; e > -1; e--)\r
     {\r
-      if (edits[e].command==INSERT_GAP)\r
+      if (edits[e].command == INSERT_GAP)\r
       {\r
         deleteGap(edits[e]);\r
       }\r
-      else if (edits[e].command==DELETE_GAP)\r
+      else if (edits[e].command == DELETE_GAP)\r
       {\r
         insertGap(edits[e]);\r
       }\r
-      else if (edits[e].command==CUT)\r
+      else if (edits[e].command == CUT)\r
       {\r
         paste(edits[e]);\r
       }\r
-      else if (edits[e].command==PASTE)\r
+      else if (edits[e].command == PASTE)\r
       {\r
         cut(edits[e]);\r
       }\r
@@ -172,11 +179,11 @@ public class EditCommand implements CommandI
 \r
   final void insertGap(Edit command)\r
   {\r
-    for(int s=0; s<command.seqs.length; s++)\r
+    for (int s = 0; s < command.seqs.length; s++)\r
     {\r
-        command.seqs[s].insertCharAt(command.position,\r
-                                     command.number,\r
-                                     command.gapChar);\r
+      command.seqs[s].insertCharAt(command.position,\r
+                                   command.number,\r
+                                   command.gapChar);\r
     }\r
 \r
     adjustAnnotations(command, true);\r
@@ -186,7 +193,8 @@ public class EditCommand implements CommandI
   {\r
     for (int s = 0; s < command.seqs.length; s++)\r
     {\r
-      command.seqs[s].deleteChars(command.position, command.position+command.number);\r
+      command.seqs[s].deleteChars(command.position,\r
+                                  command.position + command.number);\r
     }\r
 \r
     adjustAnnotations(command, false);\r
@@ -194,19 +202,20 @@ public class EditCommand implements CommandI
 \r
   void cut(Edit command)\r
   {\r
-    command.string = new char [command.seqs.length][];\r
+    command.string = new char[command.seqs.length][];\r
 \r
-    for(int i=0; i<command.seqs.length; i++)\r
+    for (int i = 0; i < command.seqs.length; i++)\r
     {\r
-      if(command.seqs[i].getLength()>command.position)\r
+      if (command.seqs[i].getLength() > command.position)\r
       {\r
         command.string[i] = command.seqs[i].getSequence(command.position,\r
             command.position + command.number);\r
 \r
-        if(command.seqs[i].getDatasetSequence()!=null\r
-        || command.seqs[i].getSequenceFeatures()!=null)\r
+        if (command.seqs[i].getDatasetSequence() != null\r
+            || command.seqs[i].getSequenceFeatures() != null)\r
         {\r
-          for (int s = command.position; s < command.position + command.number; s++)\r
+          for (int s = command.position; s < command.position + command.number;\r
+               s++)\r
           {\r
             if (jalview.schemes.ResidueProperties\r
                 .aaIndex[command.seqs[i].getCharAt(s)] != 23)\r
@@ -214,7 +223,7 @@ public class EditCommand implements CommandI
               adjustFeatures(command, i,\r
                              command.seqs[i].findPosition(command.position),\r
                              command.seqs[i].findPosition(command.position +\r
-                                                          command.number),\r
+                  command.number),\r
                              false);\r
               break;\r
             }\r
@@ -224,7 +233,7 @@ public class EditCommand implements CommandI
                                     command.position + command.number);\r
       }\r
 \r
-      if(command.seqs[i].getLength()<1)\r
+      if (command.seqs[i].getLength() < 1)\r
       {\r
         command.al.deleteSequence(command.seqs[i]);\r
       }\r
@@ -237,31 +246,35 @@ public class EditCommand implements CommandI
   {\r
     StringBuffer tmp;\r
     boolean newDSNeeded;\r
-    int start=0, end=0;\r
+    int start = 0, end = 0;\r
 \r
-    for(int i=0; i<command.seqs.length; i++)\r
+    for (int i = 0; i < command.seqs.length; i++)\r
     {\r
       newDSNeeded = false;\r
-      if(command.seqs[i].getLength()<1)\r
+      if (command.seqs[i].getLength() < 1)\r
       {\r
         // ie this sequence was deleted, we need to\r
         // read it to the alignment\r
         if (command.alIndex[i] < command.al.getHeight())\r
+        {\r
           command.al.getSequences().insertElementAt(command.seqs[i],\r
               command.alIndex[i]);\r
+        }\r
         else\r
+        {\r
           command.al.addSequence(command.seqs[i]);\r
+        }\r
       }\r
       tmp = new StringBuffer();\r
       tmp.append(command.seqs[i].getSequence());\r
 \r
-      if(command.string!=null && command.string[i]!=null)\r
+      if (command.string != null && command.string[i] != null)\r
       {\r
-        if(command.position>=tmp.length())\r
+        if (command.position >= tmp.length())\r
         {\r
           //This occurs if padding is on, and residues\r
           //are removed from end of alignment\r
-          int length = command.position-tmp.length();\r
+          int length = command.position - tmp.length();\r
           while (length > 0)\r
           {\r
             tmp.append(command.gapChar);\r
@@ -272,21 +285,22 @@ public class EditCommand implements CommandI
 \r
         for (int s = 0; s < command.string[i].length; s++)\r
         {\r
-          if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)\r
+          if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=\r
+              23)\r
           {\r
             newDSNeeded = true;\r
             start = command.seqs[i].findPosition(command.position);\r
-            end = command.seqs[i].findPosition(command.position+command.number);\r
+            end = command.seqs[i].findPosition(command.position +\r
+                                               command.number);\r
             break;\r
           }\r
         }\r
         command.string[i] = null;\r
       }\r
 \r
-\r
       command.seqs[i].setSequence(tmp.toString());\r
 \r
-      if(newDSNeeded)\r
+      if (newDSNeeded)\r
       {\r
         if (command.seqs[i].getDatasetSequence() != null)\r
         {\r
@@ -305,17 +319,15 @@ public class EditCommand implements CommandI
       }\r
     }\r
 \r
-\r
     adjustAnnotations(command, true);\r
 \r
     command.string = null;\r
   }\r
 \r
-\r
   final void adjustAnnotations(Edit command, boolean insert)\r
   {\r
 \r
-    AlignmentAnnotation [] annotations = null;\r
+    AlignmentAnnotation[] annotations = null;\r
 \r
     if (command.fullAlignmentHeight)\r
     {\r
@@ -324,196 +336,220 @@ public class EditCommand implements CommandI
     else\r
     {\r
       int aSize = 0;\r
-      AlignmentAnnotation [] tmp;\r
-      for(int s=0; s<command.seqs.length; s++)\r
+      AlignmentAnnotation[] tmp;\r
+      for (int s = 0; s < command.seqs.length; s++)\r
       {\r
-        if(command.seqs[s].getAnnotation()==null)\r
+        if (command.seqs[s].getAnnotation() == null)\r
+        {\r
           continue;\r
+        }\r
 \r
         if (aSize == 0)\r
-            annotations = command.seqs[s].getAnnotation();\r
+        {\r
+          annotations = command.seqs[s].getAnnotation();\r
+        }\r
         else\r
         {\r
           tmp = new AlignmentAnnotation\r
               [aSize + command.seqs[s].getAnnotation().length];\r
 \r
-          System.arraycopy(annotations,0,tmp,0,aSize);\r
+          System.arraycopy(annotations, 0, tmp, 0, aSize);\r
 \r
           System.arraycopy(command.seqs[s].getAnnotation(),\r
-              0,tmp,aSize,command.seqs[s].getAnnotation().length);\r
+                           0, tmp, aSize,\r
+                           command.seqs[s].getAnnotation().length);\r
 \r
           annotations = tmp;\r
         }\r
 \r
-\r
         aSize = annotations.length;\r
       }\r
     }\r
 \r
-    if(annotations==null)\r
+    if (annotations == null)\r
+    {\r
       return;\r
+    }\r
 \r
+    if (!insert)\r
+    {\r
+      command.deletedAnnotations = new Hashtable();\r
+    }\r
 \r
-      if(!insert)\r
-        command.deletedAnnotations = new Hashtable();\r
-\r
-      int aSize;\r
-      Annotation [] temp;\r
-      for (int a = 0; a < annotations.length; a++)\r
+    int aSize;\r
+    Annotation[] temp;\r
+    for (int a = 0; a < annotations.length; a++)\r
+    {\r
+      if (annotations[a].autoCalculated)\r
       {\r
-        if(annotations[a].autoCalculated)\r
-        {\r
-          continue;\r
-        }\r
+        continue;\r
+      }\r
 \r
-        int tSize = 0;\r
+      int tSize = 0;\r
 \r
-        aSize = annotations[a].annotations.length;\r
-        if(insert)\r
-          temp = new Annotation[aSize + command.number];\r
-        else\r
+      aSize = annotations[a].annotations.length;\r
+      if (insert)\r
+      {\r
+        temp = new Annotation[aSize + command.number];\r
+      }\r
+      else\r
+      {\r
+        if (command.position < aSize)\r
         {\r
-          if(command.position<aSize)\r
+          if (command.position + command.number > aSize)\r
           {\r
-            if(command.position+command.number>aSize)\r
-              tSize = aSize;\r
-            else\r
-             {\r
-                tSize = aSize - command.number + command.position;\r
-             }\r
+            tSize = aSize;\r
           }\r
           else\r
-            tSize = aSize;\r
-\r
-          if(tSize<0)\r
           {\r
-            tSize = aSize;\r
+            tSize = aSize - command.number + command.position;\r
           }\r
-          temp = new Annotation[tSize];\r
+        }\r
+        else\r
+        {\r
+          tSize = aSize;\r
+        }\r
 \r
+        if (tSize < 0)\r
+        {\r
+          tSize = aSize;\r
         }\r
+        temp = new Annotation[tSize];\r
+\r
+      }\r
 \r
-        if(insert)\r
+      if (insert)\r
+      {\r
+        if (command.position < annotations[a].annotations.length)\r
         {\r
-          if(command.position < annotations[a].annotations.length)\r
+          System.arraycopy(annotations[a].annotations,\r
+                           0, temp, 0, command.position);\r
+\r
+          if (command.deletedAnnotations != null\r
+              &&\r
+              command.deletedAnnotations.containsKey(annotations[a].\r
+              annotationId))\r
           {\r
-            System.arraycopy(annotations[a].annotations,\r
-                             0, temp, 0, command.position);\r
+            Annotation[] restore = (Annotation[])\r
+                command.deletedAnnotations.get(annotations[a].annotationId);\r
 \r
-            if (command.deletedAnnotations != null\r
-                &&\r
-                command.deletedAnnotations.containsKey(annotations[a].annotationId))\r
-            {\r
-              Annotation[] restore = (Annotation[])\r
-                  command.deletedAnnotations.get(annotations[a].annotationId);\r
+            System.arraycopy(restore,\r
+                             0,\r
+                             temp,\r
+                             command.position,\r
+                             command.number);\r
 \r
-              System.arraycopy(restore,\r
-                               0,\r
-                               temp,\r
-                               command.position,\r
-                               command.number);\r
+          }\r
 \r
-            }\r
+          System.arraycopy(annotations[a].annotations,\r
+                           command.position, temp,\r
+                           command.position + command.number,\r
+                           aSize - command.position);\r
+        }\r
+        else\r
+        {\r
+          if (command.deletedAnnotations != null\r
+              &&\r
+              command.deletedAnnotations.containsKey(annotations[a].\r
+              annotationId))\r
+          {\r
+            Annotation[] restore = (Annotation[])\r
+                command.deletedAnnotations.get(annotations[a].annotationId);\r
 \r
+            temp = new Annotation[annotations[a].annotations.length +\r
+                restore.length];\r
             System.arraycopy(annotations[a].annotations,\r
-                             command.position, temp,\r
-                             command.position + command.number,\r
-                             aSize - command.position);\r
+                             0, temp, 0,\r
+                             annotations[a].annotations.length);\r
+            System.arraycopy(restore, 0, temp,\r
+                             annotations[a].annotations.length, restore.length);\r
           }\r
           else\r
           {\r
-            if (command.deletedAnnotations != null\r
-                           &&\r
-                           command.deletedAnnotations.containsKey(annotations[a].annotationId))\r
-            {\r
-              Annotation[] restore = (Annotation[])\r
-                  command.deletedAnnotations.get(annotations[a].annotationId);\r
-\r
-              temp = new Annotation[annotations[a].annotations.length+\r
-                  restore.length];\r
-              System.arraycopy(annotations[a].annotations,\r
-                               0,temp,0,\r
-                               annotations[a].annotations.length);\r
-              System.arraycopy(restore,0,temp,annotations[a].annotations.length,restore.length);\r
-            }\r
-            else\r
-              temp = annotations[a].annotations;\r
+            temp = annotations[a].annotations;\r
           }\r
         }\r
+      }\r
+      else\r
+      {\r
+        if (tSize != aSize || command.position < 2)\r
+        {\r
+          System.arraycopy(annotations[a].annotations,\r
+                           0, temp, 0, command.position);\r
+\r
+          Annotation[] deleted = new Annotation[command.number];\r
+          System.arraycopy(annotations[a].annotations,\r
+                           command.position, deleted, 0, command.number);\r
+\r
+          command.deletedAnnotations.put(annotations[a].annotationId,\r
+                                         deleted);\r
+\r
+          System.arraycopy(annotations[a].annotations,\r
+                           command.position + command.number,\r
+                           temp, command.position,\r
+                           aSize - command.position - command.number);\r
+        }\r
         else\r
         {\r
-          if(tSize!=aSize || command.position<2)\r
-          {\r
-            System.arraycopy(annotations[a].annotations,\r
-                             0, temp, 0, command.position);\r
+          int dSize = aSize - command.position;\r
 \r
+          if (dSize > 0)\r
+          {\r
             Annotation[] deleted = new Annotation[command.number];\r
             System.arraycopy(annotations[a].annotations,\r
-                             command.position, deleted, 0, command.number);\r
+                             command.position, deleted, 0, dSize);\r
 \r
             command.deletedAnnotations.put(annotations[a].annotationId,\r
                                            deleted);\r
 \r
+            tSize = Math.min(annotations[a].annotations.length,\r
+                             command.position);\r
+            temp = new Annotation[tSize];\r
             System.arraycopy(annotations[a].annotations,\r
-                             command.position + command.number,\r
-                             temp, command.position,\r
-                             aSize - command.position - command.number);\r
+                             0, temp, 0, tSize);\r
           }\r
           else\r
           {\r
-            int dSize = aSize - command.position;\r
-\r
-            if(dSize>0)\r
-            {\r
-              Annotation[] deleted = new Annotation[command.number];\r
-              System.arraycopy(annotations[a].annotations,\r
-                               command.position, deleted, 0, dSize);\r
-\r
-              command.deletedAnnotations.put(annotations[a].annotationId,\r
-                                             deleted);\r
-\r
-              tSize = Math.min(annotations[a].annotations.length, command.position);\r
-              temp = new Annotation[tSize];\r
-              System.arraycopy(annotations[a].annotations,\r
-                  0, temp, 0, tSize);\r
-            }\r
-            else\r
-              temp = annotations[a].annotations;\r
+            temp = annotations[a].annotations;\r
           }\r
         }\r
+      }\r
 \r
-        annotations[a].annotations = temp;\r
-     }\r
+      annotations[a].annotations = temp;\r
+    }\r
   }\r
 \r
-  final void adjustFeatures(Edit command, int index, int i, int j, boolean insert)\r
+  final void adjustFeatures(Edit command, int index, int i, int j,\r
+                            boolean insert)\r
   {\r
     SequenceI seq = command.seqs[index];\r
     SequenceI sequence = seq.getDatasetSequence();\r
-    if(sequence==null)\r
+    if (sequence == null)\r
+    {\r
       sequence = seq;\r
+    }\r
 \r
-    if(insert)\r
+    if (insert)\r
     {\r
       if (command.editedFeatures != null\r
           && command.editedFeatures.containsKey(seq))\r
+      {\r
         sequence.setSequenceFeatures(\r
             (SequenceFeature[]) command.editedFeatures.get(seq)\r
             );\r
+      }\r
 \r
       return;\r
     }\r
 \r
+    SequenceFeature[] sf = sequence.getSequenceFeatures();\r
 \r
-    SequenceFeature [] sf = sequence.getSequenceFeatures();\r
-\r
-\r
-    if(sf==null)\r
+    if (sf == null)\r
     {\r
       return;\r
     }\r
 \r
-    SequenceFeature [] oldsf = new SequenceFeature[sf.length];\r
+    SequenceFeature[] oldsf = new SequenceFeature[sf.length];\r
 \r
     int cSize = j - i;\r
 \r
@@ -524,7 +560,9 @@ public class EditCommand implements CommandI
       oldsf[s] = copy;\r
 \r
       if (sf[s].getEnd() < i)\r
+      {\r
         continue;\r
+      }\r
 \r
       if (sf[s].getBegin() > j)\r
       {\r
@@ -534,25 +572,32 @@ public class EditCommand implements CommandI
       }\r
 \r
       if (sf[s].getBegin() >= i)\r
+      {\r
         sf[s].setBegin(i);\r
+      }\r
 \r
       if (sf[s].getEnd() < j)\r
+      {\r
         sf[s].setEnd(j - 1);\r
+      }\r
 \r
       sf[s].setEnd(sf[s].getEnd() - (cSize));\r
 \r
       if (sf[s].getBegin() > sf[s].getEnd())\r
+      {\r
         sequence.deleteFeature(sf[s]);\r
+      }\r
     }\r
 \r
     if (command.editedFeatures == null)\r
+    {\r
       command.editedFeatures = new Hashtable();\r
+    }\r
 \r
     command.editedFeatures.put(seq, oldsf);\r
 \r
   }\r
 \r
-\r
   class Edit\r
   {\r
     boolean fullAlignmentHeight = false;\r
@@ -560,9 +605,9 @@ public class EditCommand implements CommandI
     Hashtable editedFeatures;\r
     AlignmentI al;\r
     int command;\r
-    char [][] string;\r
+    char[][] string;\r
     SequenceI[] seqs;\r
-    int [] alIndex;\r
+    int[] alIndex;\r
     int position, number;\r
     char gapChar;\r
 \r
@@ -579,7 +624,6 @@ public class EditCommand implements CommandI
       this.gapChar = gapChar;\r
     }\r
 \r
-\r
     Edit(int command,\r
          SequenceI[] seqs,\r
          int position,\r
@@ -594,12 +638,12 @@ public class EditCommand implements CommandI
       this.al = al;\r
 \r
       alIndex = new int[seqs.length];\r
-      for(int i=0; i<seqs.length; i++)\r
+      for (int i = 0; i < seqs.length; i++)\r
       {\r
         alIndex[i] = al.findIndex(seqs[i]);\r
       }\r
 \r
-      fullAlignmentHeight = (al.getHeight()==seqs.length);\r
+      fullAlignmentHeight = (al.getHeight() == seqs.length);\r
     }\r
   }\r
 \r