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