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