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
66 if (command==INSERT_GAP || command==DELETE_GAP)
\r
68 edits = new Edit[] { new Edit(command, seqs, position, number, gapChar)};
\r
74 public EditCommand( String description,
\r
81 this.description = description;
\r
82 if ( command==CUT || command==PASTE)
\r
84 edits = new Edit[]{new Edit(command, seqs, position, number, al)};
\r
91 public String getDescription()
\r
96 public int getSize()
\r
98 return edits==null?0:edits.length;
\r
101 public AlignmentI getAlignment()
\r
103 return edits[0].al;
\r
107 public void appendEdit(int command,
\r
112 boolean performEdit)
\r
114 Edit edit = new Edit(command, seqs, position, number, gapChar);
\r
118 Edit[] temp = new Edit[edits.length + 1];
\r
119 System.arraycopy(edits, 0, temp, 0, edits.length);
\r
121 edits[edits.length - 1] = edit;
\r
124 edits = new Edit[] { edit };
\r
127 performEdit(edits.length - 1);
\r
130 void performEdit(int commandIndex)
\r
132 int eSize = edits.length;
\r
133 for (int e = commandIndex; e < eSize; e++)
\r
135 if (edits[e].command==INSERT_GAP)
\r
137 insertGap(edits[e]);
\r
139 else if (edits[e].command==DELETE_GAP)
\r
141 deleteGap(edits[e]);
\r
143 else if(edits[e].command==CUT)
\r
147 else if(edits[e].command==PASTE)
\r
154 public void doCommand()
\r
159 public void undoCommand()
\r
161 int e = 0, eSize = edits.length;
\r
162 for (e = eSize-1; e > -1; e--)
\r
164 if (edits[e].command==INSERT_GAP)
\r
166 deleteGap(edits[e]);
\r
168 else if (edits[e].command==DELETE_GAP)
\r
170 insertGap(edits[e]);
\r
172 else if (edits[e].command==CUT)
\r
176 else if (edits[e].command==PASTE)
\r
183 void insertGap(Edit command)
\r
185 for(int s=0; s<command.seqs.length; s++)
\r
187 command.seqs[s].insertCharAt(command.position,
\r
193 void deleteGap(Edit command)
\r
195 for (int s = 0; s < command.seqs.length; s++)
\r
197 command.seqs[s].deleteChars(command.position, command.position+command.number);
\r
201 void cut(Edit command)
\r
203 command.string = new char [command.seqs.length][];
\r
205 for(int i=0; i<command.seqs.length; i++)
\r
207 if(command.seqs[i].getLength()>command.position)
\r
209 command.string[i] = command.seqs[i].getSequence(command.position,
\r
210 command.position + command.number);
\r
212 command.seqs[i].deleteChars(command.position,
\r
213 command.position + command.number);
\r
216 if(command.seqs[i].getLength()<1)
\r
218 command.al.deleteSequence(command.seqs[i]);
\r
223 void paste(Edit command)
\r
226 for(int i=0; i<command.seqs.length; i++)
\r
228 if(command.seqs[i].getLength()<1)
\r
230 // ie this sequence was deleted, we need to
\r
231 // read it to the alignment
\r
232 if (command.alIndex[i] < command.al.getHeight())
\r
233 command.al.getSequences().insertElementAt(command.seqs[i],
\r
234 command.alIndex[i]);
\r
236 command.al.addSequence(command.seqs[i]);
\r
238 tmp = new StringBuffer();
\r
239 tmp.append(command.seqs[i].getSequence());
\r
241 if(command.string!=null && command.string[i]!=null)
\r
243 if(command.position>=tmp.length())
\r
245 //This occurs if padding is on, and residues
\r
246 //are removed from end of alignment
\r
247 int length = command.position-tmp.length();
\r
250 tmp.append(command.gapChar);
\r
254 tmp.insert(command.position, command.string[i]);
\r
255 command.string[i] = null;
\r
257 command.seqs[i].setSequence(tmp.toString());
\r
260 command.string = null;
\r
270 int position, number;
\r
279 this.command = command;
\r
281 this.position = position;
\r
282 this.number = number;
\r
283 this.gapChar = gapChar;
\r
293 this.gapChar = al.getGapCharacter();
\r
294 this.command = command;
\r
296 this.position = position;
\r
297 this.number = number;
\r
299 alIndex = new int[seqs.length];
\r
300 for(int i=0; i<seqs.length; i++)
\r
301 alIndex[i] = al.findIndex(seqs[i]);
\r