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 String INSERT_GAP = "InsertGap";
\r
41 public static String DELETE_GAP = "DeleteGap";
\r
42 public static String CUT = "Cut";
\r
43 public static String PASTE = "Paste";
\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.equalsIgnoreCase(INSERT_GAP)
\r
67 || command.equalsIgnoreCase(DELETE_GAP))
\r
69 edits = new Edit[] { new Edit(command, seqs, position, number, gapChar)};
\r
75 public EditCommand( String description,
\r
82 this.description = description;
\r
83 if ( command.equalsIgnoreCase(CUT) || command.equalsIgnoreCase(PASTE))
\r
85 edits = new Edit[]{new Edit(command, seqs, position, number, al)};
\r
92 public String getDescription()
\r
97 public int getSize()
\r
99 return edits==null?0:edits.length;
\r
102 public AlignmentI getAlignment()
\r
104 return edits[0].al;
\r
108 public void appendEdit(String command,
\r
113 boolean performEdit)
\r
115 Edit edit = new Edit(command, seqs, position, number, gapChar);
\r
119 Edit[] temp = new Edit[edits.length + 1];
\r
120 System.arraycopy(edits, 0, temp, 0, edits.length);
\r
122 edits[edits.length - 1] = edit;
\r
125 edits = new Edit[] { edit };
\r
128 performEdit(edits.length - 1);
\r
131 void performEdit(int commandIndex)
\r
133 int eSize = edits.length;
\r
134 for (int e = commandIndex; e < eSize; e++)
\r
136 if (edits[e].command.equals(INSERT_GAP))
\r
138 insertGap(edits[e]);
\r
140 else if (edits[e].command.equals(DELETE_GAP))
\r
142 deleteGap(edits[e]);
\r
144 else if(edits[e].command.equals(CUT))
\r
148 else if(edits[e].command.equals(PASTE))
\r
155 public void doCommand()
\r
160 public void undoCommand()
\r
162 int e = 0, eSize = edits.length;
\r
163 for (e = eSize-1; e > -1; e--)
\r
165 if (edits[e].command.equals(INSERT_GAP))
\r
167 deleteGap(edits[e]);
\r
169 else if (edits[e].command.equals(DELETE_GAP))
\r
171 insertGap(edits[e]);
\r
173 else if (edits[e].command.equals(CUT))
\r
177 else if (edits[e].command.equals(PASTE))
\r
184 void insertGap(Edit command)
\r
186 for(int s=0; s<command.seqs.length; s++)
\r
188 command.seqs[s].insertCharAt(command.position,
\r
194 void deleteGap(Edit command)
\r
196 for (int s = 0; s < command.seqs.length; s++)
\r
198 command.seqs[s].deleteChars(command.position, command.position+command.number);
\r
202 void cut(Edit command)
\r
204 command.string = new String [command.seqs.length];
\r
206 for(int i=0; i<command.seqs.length; i++)
\r
208 if(command.seqs[i].getLength()>command.position)
\r
210 command.string[i] = command.seqs[i].getSequence(command.position,
\r
211 command.position + command.number);
\r
213 command.seqs[i].deleteChars(command.position,
\r
214 command.position + command.number);
\r
217 if(command.seqs[i].getLength()<1)
\r
219 command.al.deleteSequence(command.seqs[i]);
\r
224 void paste(Edit command)
\r
227 for(int i=0; i<command.seqs.length; i++)
\r
229 if(command.seqs[i].getLength()<1)
\r
231 // ie this sequence was deleted, we need to
\r
232 // read it to the alignment
\r
233 if (command.alIndex[i] < command.al.getHeight())
\r
234 command.al.getSequences().insertElementAt(command.seqs[i],
\r
235 command.alIndex[i]);
\r
237 command.al.addSequence(command.seqs[i]);
\r
239 tmp = new StringBuffer(command.seqs[i].getSequence());
\r
241 if(command.string!=null && command.string[i]!=null)
\r
243 tmp.insert(command.position, command.string[i]);
\r
244 command.string[i] = null;
\r
246 command.seqs[i].setSequence(tmp.toString());
\r
249 command.string = null;
\r
259 int position, number;
\r
262 Edit(String command,
\r
268 this.command = command;
\r
270 this.position = position;
\r
271 this.number = number;
\r
272 this.gapChar = gapChar;
\r
276 Edit(String command,
\r
282 this.command = command;
\r
284 this.position = position;
\r
285 this.number = number;
\r
287 alIndex = new int[seqs.length];
\r
288 for(int i=0; i<seqs.length; i++)
\r
289 alIndex[i] = al.findIndex(seqs[i]);
\r