/* * Jalview - A Sequence Alignment Editor and Viewer * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.commands; import jalview.datamodel.*; import java.util.Hashtable; /** * *

Title: EditCommmand

* *

Description: Essential information for performing * undo and redo for cut/paste insert/delete gap * which can be stored in the HistoryList

* *

Copyright: Copyright (c) 2006

* *

Company: Dundee University

* * @author not attributable * @version 1.0 */ public class EditCommand implements CommandI { public static final int INSERT_GAP = 0; public static final int DELETE_GAP = 1; public static final int CUT = 2; public static final int PASTE = 3; Edit[] edits; String description; public EditCommand() {} public EditCommand(String description) { this.description = description; } public EditCommand( String description, int command, SequenceI[] seqs, int position, int number, AlignmentI al) { this.description = description; if ( command==CUT || command==PASTE) { edits = new Edit[]{new Edit(command, seqs, position, number, al)}; } performEdit(0); } public String getDescription() { return description; } public int getSize() { return edits==null?0:edits.length; } public AlignmentI getAlignment() { return edits[0].al; } public void appendEdit(int command, SequenceI[] seqs, int position, int number, AlignmentI al, boolean performEdit) { Edit edit = new Edit(command, seqs, position, number, al.getGapCharacter()); if(al.getHeight()==seqs.length) { edit.al = al; edit.fullAlignmentHeight = true; } if (edits != null) { Edit[] temp = new Edit[edits.length + 1]; System.arraycopy(edits, 0, temp, 0, edits.length); edits = temp; edits[edits.length - 1] = edit; } else edits = new Edit[] { edit }; if (performEdit) performEdit(edits.length - 1); } void performEdit(int commandIndex) { 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) { paste(edits[e]); } } } public void doCommand() { performEdit(0); } public void undoCommand() { 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) { cut(edits[e]); } } } void insertGap(Edit command) { for(int s=0; scommand.position) { command.string[i] = command.seqs[i].getSequence(command.position, command.position + command.number); command.seqs[i].deleteChars(command.position, command.position + command.number); } if(command.seqs[i].getLength()<1) { command.al.deleteSequence(command.seqs[i]); } } if (command.fullAlignmentHeight) { adjustAnnotations(command, false); } } void paste(Edit command) { StringBuffer tmp; for(int i=0; i=tmp.length()) { //This occurs if padding is on, and residues //are removed from end of alignment int length = command.position-tmp.length(); while (length > 0) { tmp.append(command.gapChar); length--; } } tmp.insert(command.position, command.string[i]); command.string[i] = null; } command.seqs[i].setSequence(tmp.toString()); } if (command.fullAlignmentHeight) { adjustAnnotations(command, true); } command.string = null; } void adjustAnnotations(Edit command, boolean insert) { AlignmentAnnotation [] annotations = command.al.getAlignmentAnnotation(); if(annotations!=null) { if(!insert) command.deletedAnnotations = new Hashtable(); int aSize, tSize; Annotation [] temp; for (int a = 0; a < annotations.length; a++) { if(annotations[a].autoCalculated) { continue; } aSize = annotations[a].annotations.length; if(insert) tSize = aSize + command.number; else tSize = aSize - command.number; temp = new Annotation[tSize]; if(insert) { System.arraycopy(annotations[a].annotations, 0, temp, 0, command.position); if(command.deletedAnnotations!=null && command.deletedAnnotations.containsKey(annotations[a].annotationId)) { Annotation [] restore = (Annotation []) command.deletedAnnotations.get(annotations[a].annotationId); System.arraycopy(restore, 0, temp, command.position, command.number); } System.arraycopy(annotations[a].annotations, command.position, temp, command.position+command.number, aSize - command.position); } else { if(command.position < annotations[a].annotations.length) { System.arraycopy(annotations[a].annotations, 0, temp, 0, command.position); Annotation[] deleted = new Annotation[command.number]; System.arraycopy(annotations[a].annotations, command.position, deleted, 0, command.number); command.deletedAnnotations.put(annotations[a].annotationId, deleted); System.arraycopy(annotations[a].annotations, command.position + command.number, temp, command.position, aSize - command.position - command.number); } else temp = annotations[a].annotations; } annotations[a].annotations = temp; } } } class Edit { boolean fullAlignmentHeight = false; Hashtable deletedAnnotations; AlignmentI al; int command; char [][] string; SequenceI[] seqs; int [] alIndex; int position, number; char gapChar; Edit(int command, SequenceI[] seqs, int position, int number, char gapChar) { this.command = command; this.seqs = seqs; this.position = position; this.number = number; this.gapChar = gapChar; } Edit(int command, SequenceI[] seqs, int position, int number, AlignmentI al) { this.gapChar = al.getGapCharacter(); this.command = command; this.seqs = seqs; this.position = position; this.number = number; this.al = al; alIndex = new int[seqs.length]; for(int i=0; i