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