4110bd1fcd5ecb1c1c5fe82ceb52cdad531679ec
[jalview.git] / src / jalview / commands / TrimRegionCommand.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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.commands;
20
21 import java.util.*;
22
23 import jalview.datamodel.*;
24 import jalview.util.*;
25
26 public class TrimRegionCommand
27     extends EditCommand
28 {
29   public static String TRIM_LEFT = "TrimLeft";
30   public static String TRIM_RIGHT = "TrimRight";
31
32   public ColumnSelection colSel = null;
33
34   int[] start;
35
36   ShiftList shiftList;
37
38   SequenceGroup selectionGroup;
39
40   Vector deletedHiddenColumns;
41
42   int columnsDeleted;
43
44   public TrimRegionCommand(String description,
45                            String command,
46                            SequenceI[] seqs,
47                            int column,
48                            AlignmentI al,
49                            ColumnSelection colSel,
50                            SequenceGroup selectedRegion)
51   {
52     this.description = description;
53     this.selectionGroup = selectedRegion;
54     this.colSel = colSel;
55     if (command.equalsIgnoreCase(TRIM_LEFT))
56     {
57       if (column == 0)
58       {
59         return;
60       }
61
62       columnsDeleted = column;
63
64       edits = new Edit[]
65           {
66           new Edit(CUT, seqs, 0, column, al)};
67     }
68     else if (command.equalsIgnoreCase(TRIM_RIGHT))
69     {
70       int width = al.getWidth() - column - 1;
71       if (width < 2)
72       {
73         return;
74       }
75
76       columnsDeleted = width - 1;
77
78       edits = new Edit[]
79           {
80           new Edit(CUT, seqs, column + 1, width, al)};
81     }
82
83     //We need to keep a record of the sequence start
84     //in order to restore the state after a redo
85     int i, isize = edits[0].seqs.length;
86     start = new int[isize];
87     for (i = 0; i < isize; i++)
88     {
89       start[i] = edits[0].seqs[i].getStart();
90     }
91
92     performEdit(0,null);
93   }
94
95   void cut(Edit command)
96   {
97     int column, j, jSize = command.seqs.length;
98     for (j = 0; j < jSize; j++)
99     {
100       if (command.position == 0)
101       {
102         //This is a TRIM_LEFT command
103         column = command.seqs[j].findPosition(command.number);
104         command.seqs[j].setStart(column);
105       }
106       else
107       {
108         //This is a TRIM_RIGHT command
109         column = command.seqs[j].findPosition(command.position) - 1;
110         command.seqs[j].setEnd(column);
111       }
112     }
113
114     super.cut(command, null);
115
116     if (command.position == 0)
117     {
118       deletedHiddenColumns = colSel.compensateForEdit(0, command.number);
119       if (selectionGroup != null)
120       {
121         selectionGroup.adjustForRemoveLeft(command.number);
122       }
123     }
124     else
125     {
126       deletedHiddenColumns = colSel.compensateForEdit(command.position,
127           command.number);
128       if (selectionGroup != null)
129       {
130         selectionGroup.adjustForRemoveRight(command.position);
131       }
132     }
133   }
134
135   void paste(Edit command)
136   {
137     super.paste(command, null);
138     int column, j, jSize = command.seqs.length;
139     for (j = 0; j < jSize; j++)
140     {
141       if (command.position == 0)
142       {
143         command.seqs[j].setStart(start[j]);
144       }
145       else
146       {
147         column = command.seqs[j]
148             .findPosition(command.number + command.position) - 1;
149         command.seqs[j].setEnd(column);
150       }
151     }
152
153     if (command.position == 0)
154     {
155       colSel.compensateForEdit(0, -command.number);
156       if (selectionGroup != null)
157       {
158         selectionGroup.adjustForRemoveLeft( -command.number);
159       }
160     }
161
162     if (deletedHiddenColumns != null)
163     {
164       int[] region;
165       for (int i = 0; i < deletedHiddenColumns.size(); i++)
166       {
167         region = (int[]) deletedHiddenColumns.elementAt(i);
168         colSel.hideColumns(region[0], region[1]);
169       }
170     }
171   }
172
173   public int getSize()
174   {
175     return columnsDeleted;
176   }
177
178 }