Adjust features when cutting and pasting
[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   int columnsDeleted;\r
42 \r
43   public TrimRegionCommand(String description,\r
44                      String command,\r
45                      SequenceI[] seqs,\r
46                      int column,\r
47                      AlignmentI al,\r
48                      ColumnSelection colSel,\r
49                      SequenceGroup selectedRegion)\r
50   {\r
51     this.description = description;\r
52     this.selectionGroup = selectedRegion;\r
53     this.colSel = colSel;\r
54     if (command.equalsIgnoreCase(TRIM_LEFT))\r
55     {\r
56       if(column==0)\r
57         return;\r
58 \r
59       columnsDeleted = column;\r
60 \r
61       edits = new Edit[] { new Edit(CUT, seqs, 0, column, al)};\r
62     }\r
63     else if (command.equalsIgnoreCase(TRIM_RIGHT))\r
64     {\r
65       int width = al.getWidth()-column;\r
66       if(width<2)\r
67       {\r
68         return;\r
69       }\r
70 \r
71       columnsDeleted = width-1;\r
72 \r
73       edits = new Edit[]\r
74           { new Edit(CUT, seqs, column+1, width, al)};\r
75     }\r
76 \r
77     //We need to keep a record of the sequence start\r
78     //in order to restore the state after a redo\r
79     int i, isize = edits[0].seqs.length;\r
80     start = new int[isize];\r
81     for(i=0; i<isize; i++)\r
82       start[i] = edits[0].seqs[i].getStart();\r
83 \r
84     performEdit(0);\r
85   }\r
86 \r
87   void cut(Edit command)\r
88   {\r
89     int column, j, jSize = command.seqs.length;\r
90     for (j = 0; j < jSize; j++)\r
91     {\r
92       if(command.position==0)\r
93       {\r
94         //This is a TRIM_LEFT command\r
95         column = command.seqs[j].findPosition(command.number);\r
96         command.seqs[j].setStart(column);\r
97       }\r
98       else\r
99       {\r
100         //This is a TRIM_RIGHT command\r
101         column = command.seqs[j].findPosition(command.position)-1;\r
102         command.seqs[j].setEnd(column);\r
103       }\r
104     }\r
105 \r
106     super.cut(command);\r
107 \r
108     if (command.position == 0)\r
109     {\r
110       deletedHiddenColumns = colSel.compensateForEdit(0, command.number);\r
111       if(selectionGroup!=null)\r
112         selectionGroup.adjustForRemoveLeft(command.number);\r
113     }\r
114     else\r
115     {\r
116       deletedHiddenColumns = colSel.compensateForEdit(command.position, command.number);\r
117       if(selectionGroup!=null)\r
118         selectionGroup.adjustForRemoveRight(command.position);\r
119     }\r
120   }\r
121 \r
122   void paste(Edit command)\r
123   {\r
124     super.paste(command);\r
125     int column, j, jSize = command.seqs.length;\r
126     for (j = 0; j < jSize; j++)\r
127     {\r
128       if(command.position==0)\r
129       {\r
130         command.seqs[j].setStart(start[j]);\r
131       }\r
132       else\r
133       {\r
134         column = command.seqs[j]\r
135             .findPosition(command.number+command.position)-1;\r
136         command.seqs[j].setEnd(column);\r
137       }\r
138     }\r
139 \r
140     if (command.position == 0)\r
141     {\r
142       colSel.compensateForEdit(0, -command.number);\r
143       if(selectionGroup!=null)\r
144         selectionGroup.adjustForRemoveLeft(-command.number);\r
145     }\r
146 \r
147     if (deletedHiddenColumns != null)\r
148     {\r
149       int[] region;\r
150       for (int i = 0; i < deletedHiddenColumns.size(); i++)\r
151       {\r
152         region = (int[]) deletedHiddenColumns.elementAt(i);\r
153         colSel.hideColumns(region[0], region[1]);\r
154       }\r
155     }\r
156   }\r
157 \r
158   public int getSize()\r
159   {\r
160     return columnsDeleted;\r
161   }\r
162 \r
163 }\r