REPLACE added to edit command
authoramwaterhouse <Andrew Waterhouse>
Thu, 19 Apr 2007 14:42:24 +0000 (14:42 +0000)
committeramwaterhouse <Andrew Waterhouse>
Thu, 19 Apr 2007 14:42:24 +0000 (14:42 +0000)
src/jalview/appletgui/APopupMenu.java
src/jalview/commands/EditCommand.java

index b281fcd..8af2759 100755 (executable)
@@ -70,6 +70,7 @@ public class APopupMenu
   MenuItem repGroup = new MenuItem();\r
   MenuItem sequenceName = new MenuItem("Edit Name/Description");\r
   MenuItem sequenceFeature = new MenuItem("Create Sequence Feature");\r
+  MenuItem editSequence = new MenuItem("Edit Sequence");\r
 \r
   Sequence seq;\r
   MenuItem revealAll = new MenuItem();\r
@@ -310,7 +311,6 @@ public class APopupMenu
 \r
       if (dialog.accept)\r
       {\r
-\r
         getGroup().setName(dialog.getName().replace(' ', '_'));\r
         getGroup().setDescription(dialog.getDescription());\r
       }\r
@@ -324,6 +324,42 @@ public class APopupMenu
     {\r
       ap.alignFrame.cut_actionPerformed();\r
     }\r
+    else if(source == editSequence)\r
+    {\r
+      SequenceGroup sg = ap.av.getSelectionGroup();\r
+\r
+      if(sg!=null)\r
+      {\r
+        if (seq == null)\r
+          seq = (Sequence) sg.getSequenceAt(0);\r
+\r
+        EditNameDialog dialog = new EditNameDialog(seq.getSequenceAsString(\r
+            sg.getStartRes(),\r
+            sg.getEndRes() + 1),\r
+                                                   null,\r
+                                                   "Edit Sequence ",\r
+                                                   null,\r
+\r
+                                                   ap.alignFrame,\r
+                                                   "Edit Sequence",\r
+                                                   500, 100);\r
+\r
+        if (dialog.accept)\r
+        {\r
+          EditCommand editCommand = new EditCommand(\r
+              "Edit Sequences", EditCommand.REPLACE,\r
+              dialog.getName(),\r
+              sg.getSequencesAsArray(ap.av.hiddenRepSequences),\r
+              sg.getStartRes(), sg.getEndRes()+1, ap.av.alignment\r
+              );\r
+\r
+          ap.alignFrame.addHistoryItem(editCommand);\r
+\r
+          ap.av.firePropertyChange("alignment", null,\r
+                                 ap.av.getAlignment().getSequences());\r
+        }\r
+      }\r
+    }\r
     else if (source == toUpper || source == toLower || source == toggleCase)\r
     {\r
       SequenceGroup sg = ap.av.getSelectionGroup();\r
@@ -597,6 +633,10 @@ public class APopupMenu
     copy.addActionListener(this);\r
     editMenu.add(cut);\r
     cut.addActionListener(this);\r
+\r
+    editMenu.add(editSequence);\r
+    editSequence.addActionListener(this);\r
+\r
     editMenu.add(toUpper);\r
     toUpper.addActionListener(this);\r
     editMenu.add(toLower);\r
index 45b19af..ccc6a81 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)
-      {
-        insertGap(edits[e]);
-      }
-      else if (edits[e].command == DELETE_GAP)
-      {
-        deleteGap(edits[e]);
-      }
-      else if (edits[e].command == CUT)
-      {
-        cut(edits[e]);
-      }
-      else if (edits[e].command == PASTE)
+      switch(edits[e].command)
       {
-        paste(edits[e]);
+        case INSERT_GAP:
+          insertGap(edits[e]);
+          break;
+        case DELETE_GAP:
+          deleteGap(edits[e]);
+          break;
+        case CUT:
+          cut(edits[e]);
+          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)
-      {
-        deleteGap(edits[e]);
-      }
-      else if (edits[e].command == DELETE_GAP)
-      {
-        insertGap(edits[e]);
-      }
-      else if (edits[e].command == CUT)
-      {
-        paste(edits[e]);
-      }
-      else if (edits[e].command == PASTE)
+      switch (edits[e].command)
       {
-        cut(edits[e]);
+        case INSERT_GAP:
+          deleteGap(edits[e]);
+          break;
+        case DELETE_GAP:
+          insertGap(edits[e]);
+          break;
+        case CUT:
+          paste(edits[e]);
+          break;
+        case PASTE:
+          cut(edits[e]);
+          break;
+        case REPLACE:
+          replace(edits[e]);
+          break;
       }
     }
   }
@@ -327,9 +350,29 @@ public class EditCommand
     command.string = null;
   }
 
-  final void adjustAnnotations(Edit command, boolean insert, boolean modifyVisibility)
+  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)
+  {
     AlignmentAnnotation[] annotations = null;
 
     if (modifyVisibility && !insert)
@@ -704,6 +747,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;
+      string = new char[seqs.length][];
+      for (int i = 0; i < seqs.length; i++)
+      {
+        string[i] = replace.toCharArray();
+      }
+
+      fullAlignmentHeight = (al.getHeight() == seqs.length);
+    }
   }
 
 }