b4d92a03880ee6c5c028755cd9c57bb1a3dfa560
[jalview.git] / src / jalview / commands / TrimRegionCommand.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.commands;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.ColumnSelection;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.ShiftList;
28
29 import java.util.List;
30
31 public class TrimRegionCommand extends EditCommand
32 {
33   public static String TRIM_LEFT = "TrimLeft";
34
35   public static String TRIM_RIGHT = "TrimRight";
36
37   public ColumnSelection colSel = null;
38
39   int[] start;
40
41   ShiftList shiftList;
42
43   SequenceGroup selectionGroup;
44
45   List<int[]> deletedHiddenColumns;
46
47   int columnsDeleted;
48
49   public TrimRegionCommand(String description, String command,
50           SequenceI[] seqs, int column, AlignmentI al,
51           ColumnSelection colSel, SequenceGroup selectedRegion)
52   {
53     this.description = description;
54     this.selectionGroup = selectedRegion;
55     this.colSel = colSel;
56     if (command.equalsIgnoreCase(TRIM_LEFT))
57     {
58       if (column == 0)
59       {
60         return;
61       }
62
63       columnsDeleted = column;
64
65       setEdit(new Edit(Action.CUT, seqs, 0, column, al));
66     }
67     else if (command.equalsIgnoreCase(TRIM_RIGHT))
68     {
69       int width = al.getWidth() - column - 1;
70       if (width < 2)
71       {
72         return;
73       }
74
75       columnsDeleted = width - 1;
76
77       setEdit(new Edit(Action.CUT, seqs, column + 1, width, al));
78     }
79
80     // We need to keep a record of the sequence start
81     // in order to restore the state after a redo
82     int i, isize = getEdit(0).seqs.length;
83     start = new int[isize];
84     for (i = 0; i < isize; i++)
85     {
86       start[i] = getEdit(0).seqs[i].getStart();
87     }
88
89     performEdit(0, null);
90   }
91
92   void cut(Edit command)
93   {
94     int column, j, jSize = command.seqs.length;
95     for (j = 0; j < jSize; j++)
96     {
97       if (command.position == 0)
98       {
99         // This is a TRIM_LEFT command
100         column = command.seqs[j].findPosition(command.number);
101         command.seqs[j].setStart(column);
102       }
103       else
104       {
105         // This is a TRIM_RIGHT command
106         column = command.seqs[j].findPosition(command.position) - 1;
107         command.seqs[j].setEnd(column);
108       }
109     }
110
111     super.cut(command, null);
112
113     if (command.position == 0)
114     {
115       deletedHiddenColumns = colSel.compensateForEdit(0, command.number);
116       if (selectionGroup != null)
117       {
118         selectionGroup.adjustForRemoveLeft(command.number);
119       }
120     }
121     else
122     {
123       deletedHiddenColumns = colSel.compensateForEdit(command.position,
124               command.number);
125       if (selectionGroup != null)
126       {
127         selectionGroup.adjustForRemoveRight(command.position);
128       }
129     }
130   }
131
132   void paste(Edit command)
133   {
134     super.paste(command, null);
135     int column, j, jSize = command.seqs.length;
136     for (j = 0; j < jSize; j++)
137     {
138       if (command.position == 0)
139       {
140         command.seqs[j].setStart(start[j]);
141       }
142       else
143       {
144         column = command.seqs[j].findPosition(command.number
145                 + command.position) - 1;
146         command.seqs[j].setEnd(column);
147       }
148     }
149
150     if (command.position == 0)
151     {
152       colSel.compensateForEdit(0, -command.number);
153       if (selectionGroup != null)
154       {
155         selectionGroup.adjustForRemoveLeft(-command.number);
156       }
157     }
158
159     if (deletedHiddenColumns != null)
160     {
161       int[] region;
162       for (int i = 0; i < deletedHiddenColumns.size(); i++)
163       {
164         region = deletedHiddenColumns.get(i);
165         colSel.hideColumns(region[0], region[1]);
166       }
167     }
168   }
169
170   public int getSize()
171   {
172     return columnsDeleted;
173   }
174
175 }