/*
* 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.datamodel;
import jalview.util.ShiftList;
import java.util.*;
/**
* DOCUMENT ME!
*
* @author $author$
* @version $Revision$
*/
public class HistoryItem
{
/** DOCUMENT ME!! */
public static final int EDIT = 0;
/** DOCUMENT ME!! */
public static final int SORT = 1;
/** DOCUMENT ME!! */
public static final int HIDE = 2;
/** DOCUMENT ME!! */
public static final int PASTE = 3;
final int type;
AlignmentI alignment;
String description;
Vector sequences;
Vector seqAsString;
Vector alignIndex;
Vector hiddenSeqs;
Vector hiddenSeqsAsString;
/**
* public field - set directly if history involves a frame shift
* should contain the inverse frame shift operations.
*/
public ShiftList alColumnChanges=null;
/**
* Creates a new HistoryItem object.
*
* @param description DOCUMENT ME!
* @param al DOCUMENT ME!
* @param type DOCUMENT ME!
*/
public HistoryItem(String description, AlignmentI al, int type)
{
alignment = al;
this.type = type;
this.description = description;
sequences = new Vector();
alignIndex = new Vector();
seqAsString = new Vector();
for (int i = 0; i < al.getHeight(); i++)
{
SequenceI seq = al.getSequenceAt(i);
sequences.addElement(seq);
alignIndex.addElement(i + "");
seqAsString.addElement(seq.getStart()
+" "+seq.getEnd()
+" "+seq.getSequence().toString());
}
if(alignment.getHiddenSequences()!=null
&& alignment.getHiddenSequences().getSize()>0)
{
hiddenSeqs = new Vector();
hiddenSeqsAsString = new Vector();
Enumeration en = alignment.getHiddenSequences().hiddenSequences.elements();
while (en.hasMoreElements())
{
SequenceI key = (SequenceI) en.nextElement();
hiddenSeqs.addElement(key);
hiddenSeqsAsString.addElement(key.getSequence().toString());
}
}
}
/**
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public String getDescription()
{
return description;
}
/**
* restore state - adjusting gui hiddenColumn view as necessary
* @param columnSelection
*/
public void restore(ColumnSelection columnSelection)
{
if (type == HistoryItem.SORT)
{
for (int i = 0; i < sequences.size(); i++)
{
alignment.getSequences().setElementAt(sequences.elementAt(i), i);
}
}
else
{
StringTokenizer st;
for (int i = 0; i < sequences.size(); i++)
{
SequenceI restore = (SequenceI) sequences.elementAt(i);
if (restore.getLength() == 0)
{
//This is for edits which remove all residues in a sequence
alignment.getSequences().insertElementAt(restore,
Integer.parseInt(alignIndex.elementAt(i).toString()));
}
st = new StringTokenizer(seqAsString.elementAt(i).toString());
restore.setStart(Integer.parseInt(st.nextToken()));
restore.setEnd(Integer.parseInt(st.nextToken()));
restore.setSequence(st.nextToken());
}
if(hiddenSeqs!=null)
{
for(int hs=0; hs (sequences.size() - 1); i--)
{
alignment.deleteSequence(i);
}
}
if (alColumnChanges!=null) {
columnSelection.compensateForEdits(alColumnChanges);
}
}
}
/**
* note a frame shift that must be compensated for
* @param pos start position for shift (in original reference frame)
* @param shift length of shift
*/
public void addShift(int pos, int shift) {
if (alColumnChanges==null)
alColumnChanges = new ShiftList();
alColumnChanges.addShift(pos, -shift);
}
}