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