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