2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.commands;
\r
21 import jalview.datamodel.*;
\r
25 * <p>Title: EditCommmand</p>
\r
27 * <p>Description: Essential information for performing
\r
28 * undo and redo for cut/paste insert/delete gap
\r
29 * which can be stored in the HistoryList </p>
\r
31 * <p>Copyright: Copyright (c) 2006</p>
\r
33 * <p>Company: Dundee University</p>
\r
35 * @author not attributable
\r
38 public class EditCommand implements CommandI
\r
40 public static final int INSERT_GAP = 0;
\r
41 public static final int DELETE_GAP = 1;
\r
42 public static final int CUT = 2;
\r
43 public static final int PASTE = 3;
\r
49 public EditCommand()
\r
52 public EditCommand(String description)
\r
54 this.description = description;
\r
57 public EditCommand( String description,
\r
64 this.description = description;
\r
65 if ( command==CUT || command==PASTE)
\r
67 edits = new Edit[]{new Edit(command, seqs, position, number, al)};
\r
74 public String getDescription()
\r
79 public int getSize()
\r
81 return edits==null?0:edits.length;
\r
84 public AlignmentI getAlignment()
\r
90 public void appendEdit(int command,
\r
95 boolean performEdit)
\r
97 Edit edit = new Edit(command, seqs, position, number, gapChar);
\r
101 Edit[] temp = new Edit[edits.length + 1];
\r
102 System.arraycopy(edits, 0, temp, 0, edits.length);
\r
104 edits[edits.length - 1] = edit;
\r
107 edits = new Edit[] { edit };
\r
110 performEdit(edits.length - 1);
\r
113 void performEdit(int commandIndex)
\r
115 int eSize = edits.length;
\r
116 for (int e = commandIndex; e < eSize; e++)
\r
118 if (edits[e].command==INSERT_GAP)
\r
120 insertGap(edits[e]);
\r
122 else if (edits[e].command==DELETE_GAP)
\r
124 deleteGap(edits[e]);
\r
126 else if(edits[e].command==CUT)
\r
130 else if(edits[e].command==PASTE)
\r
137 public void doCommand()
\r
142 public void undoCommand()
\r
144 int e = 0, eSize = edits.length;
\r
145 for (e = eSize-1; e > -1; e--)
\r
147 if (edits[e].command==INSERT_GAP)
\r
149 deleteGap(edits[e]);
\r
151 else if (edits[e].command==DELETE_GAP)
\r
153 insertGap(edits[e]);
\r
155 else if (edits[e].command==CUT)
\r
159 else if (edits[e].command==PASTE)
\r
166 void insertGap(Edit command)
\r
168 for(int s=0; s<command.seqs.length; s++)
\r
170 command.seqs[s].insertCharAt(command.position,
\r
176 void deleteGap(Edit command)
\r
178 for (int s = 0; s < command.seqs.length; s++)
\r
180 command.seqs[s].deleteChars(command.position, command.position+command.number);
\r
184 void cut(Edit command)
\r
186 command.string = new char [command.seqs.length][];
\r
188 for(int i=0; i<command.seqs.length; i++)
\r
190 if(command.seqs[i].getLength()>command.position)
\r
192 command.string[i] = command.seqs[i].getSequence(command.position,
\r
193 command.position + command.number);
\r
195 command.seqs[i].deleteChars(command.position,
\r
196 command.position + command.number);
\r
199 if(command.seqs[i].getLength()<1)
\r
201 command.al.deleteSequence(command.seqs[i]);
\r
208 void paste(Edit command)
\r
211 for(int i=0; i<command.seqs.length; i++)
\r
213 if(command.seqs[i].getLength()<1)
\r
215 // ie this sequence was deleted, we need to
\r
216 // read it to the alignment
\r
217 if (command.alIndex[i] < command.al.getHeight())
\r
218 command.al.getSequences().insertElementAt(command.seqs[i],
\r
219 command.alIndex[i]);
\r
221 command.al.addSequence(command.seqs[i]);
\r
223 tmp = new StringBuffer();
\r
224 tmp.append(command.seqs[i].getSequence());
\r
226 if(command.string!=null && command.string[i]!=null)
\r
228 if(command.position>=tmp.length())
\r
230 //This occurs if padding is on, and residues
\r
231 //are removed from end of alignment
\r
232 int length = command.position-tmp.length();
\r
235 tmp.append(command.gapChar);
\r
239 tmp.insert(command.position, command.string[i]);
\r
240 command.string[i] = null;
\r
242 command.seqs[i].setSequence(tmp.toString());
\r
248 command.string = null;
\r
258 int position, number;
\r
267 this.command = command;
\r
269 this.position = position;
\r
270 this.number = number;
\r
271 this.gapChar = gapChar;
\r
281 this.gapChar = al.getGapCharacter();
\r
282 this.command = command;
\r
284 this.position = position;
\r
285 this.number = number;
\r
288 alIndex = new int[seqs.length];
\r
289 for(int i=0; i<seqs.length; i++)
\r
290 alIndex[i] = al.findIndex(seqs[i]);
\r