7ce08037bf2f923cdcac776cf188d24647284ee5
[jalview.git] / src / jalview / datamodel / HistoryItem.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19 package jalview.datamodel;
20
21 import jalview.util.ShiftList;
22
23 import java.util.*;
24
25
26 /**
27  * DOCUMENT ME!
28  *
29  * @author $author$
30  * @version $Revision$
31  */
32 public class HistoryItem
33 {
34   /** DOCUMENT ME!! */
35   public static final int EDIT = 0;
36
37   /** DOCUMENT ME!! */
38   public static final int SORT = 1;
39
40   /** DOCUMENT ME!! */
41   public static final int HIDE = 2;
42
43   /** DOCUMENT ME!! */
44   public static final int PASTE = 3;
45
46   final int type;
47
48   AlignmentI alignment;
49   String description;
50
51   Vector sequences;
52   Vector seqAsString;
53   Vector alignIndex;
54
55   Vector hiddenSeqs;
56   Vector hiddenSeqsAsString;
57   /**
58    * public field - set directly if history involves a frame shift
59    * should contain the <em>inverse</em> frame shift operations.
60    */
61   public ShiftList alColumnChanges=null;
62
63   /**
64    * Creates a new HistoryItem object.
65    *
66    * @param description DOCUMENT ME!
67    * @param al DOCUMENT ME!
68    * @param type DOCUMENT ME!
69    */
70   public HistoryItem(String description, AlignmentI al, int type)
71   {
72     alignment = al;
73     this.type = type;
74     this.description = description;
75     sequences = new Vector();
76     alignIndex = new Vector();
77     seqAsString = new Vector();
78
79     for (int i = 0; i < al.getHeight(); i++)
80     {
81       SequenceI seq = al.getSequenceAt(i);
82       sequences.addElement(seq);
83       alignIndex.addElement(i + "");
84       seqAsString.addElement(seq.getStart()
85                              +" "+seq.getEnd()
86                              +" "+seq.getSequence().toString());
87     }
88
89     if(alignment.getHiddenSequences()!=null
90        && alignment.getHiddenSequences().getSize()>0)
91     {
92       hiddenSeqs = new Vector();
93       hiddenSeqsAsString = new Vector();
94       Enumeration en = alignment.getHiddenSequences().hiddenSequences.elements();
95       while (en.hasMoreElements())
96       {
97         SequenceI key = (SequenceI) en.nextElement();
98         hiddenSeqs.addElement(key);
99         hiddenSeqsAsString.addElement(key.getSequence().toString());
100       }
101     }
102   }
103   /**
104    * DOCUMENT ME!
105    *
106    * @return DOCUMENT ME!
107    */
108   public String getDescription()
109   {
110     return description;
111   }
112
113   /**
114    * restore state - adjusting gui hiddenColumn view as necessary
115    * @param columnSelection
116    */
117   public void restore(ColumnSelection columnSelection)
118   {
119     if (type == HistoryItem.SORT)
120     {
121       for (int i = 0; i < sequences.size(); i++)
122       {
123         alignment.getSequences().setElementAt(sequences.elementAt(i), i);
124       }
125     }
126     else
127     {
128       StringTokenizer st;
129       for (int i = 0; i < sequences.size(); i++)
130       {
131         SequenceI restore = (SequenceI) sequences.elementAt(i);
132
133
134         if (restore.getLength() == 0)
135         {
136           //This is for edits which remove all residues in a sequence
137           alignment.getSequences().insertElementAt(restore,
138               Integer.parseInt(alignIndex.elementAt(i).toString()));
139         }
140
141         st = new StringTokenizer(seqAsString.elementAt(i).toString());
142         restore.setStart(Integer.parseInt(st.nextToken()));
143         restore.setEnd(Integer.parseInt(st.nextToken()));
144         restore.setSequence(st.nextToken());
145       }
146
147       if(hiddenSeqs!=null)
148       {
149         for(int hs=0; hs<hiddenSeqs.size(); hs++)
150         {
151           SequenceI key = (SequenceI) hiddenSeqs.elementAt(hs);
152           key.setSequence(hiddenSeqsAsString.elementAt(hs).toString());
153         }
154       }
155
156       if (type == HistoryItem.PASTE)
157       {
158         for (int i = alignment.getHeight() - 1;
159              i > (sequences.size() - 1); i--)
160         {
161           alignment.deleteSequence(i);
162         }
163       }
164       if (alColumnChanges!=null) {
165         columnSelection.compensateForEdits(alColumnChanges);
166       }
167     }
168
169   }
170   /**
171    * note a frame shift that must be compensated for
172    * @param pos start position for shift (in original reference frame)
173    * @param shift length of shift
174    */
175   public void addShift(int pos, int shift) {
176     if (alColumnChanges==null)
177       alColumnChanges = new ShiftList();
178     alColumnChanges.addShift(pos, -shift);
179   }
180 }