f0ea2f69b205805e090fe8059ece4673ddc36425
[jalview.git] / src / jalview / commands / ChangeCaseCommand.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.SequenceI;
25
26 import java.util.List;
27
28 public class ChangeCaseCommand implements CommandI
29 {
30   String description;
31
32   public final static int TO_LOWER = 0;
33
34   public final static int TO_UPPER = 1;
35
36   public final static int TOGGLE_CASE = 2;
37
38   private int caseChange;
39
40   private SequenceI[] seqs;
41
42   private List<int[]> regions;
43
44   public ChangeCaseCommand(String description, SequenceI[] seqs,
45           List<int[]> regions, int caseChange)
46   {
47     this.description = description;
48     this.seqs = seqs;
49     this.regions = regions;
50     this.caseChange = caseChange;
51     doCommand(null);
52   }
53
54   @Override
55   public String getDescription()
56   {
57     return description;
58   }
59
60   @Override
61   public int getSize()
62   {
63     return 1;
64   }
65
66   @Override
67   public void doCommand(AlignmentI[] views)
68   {
69     changeCase(true);
70   }
71
72   @Override
73   public void undoCommand(AlignmentI[] views)
74   {
75     changeCase(false);
76   }
77
78   void changeCase(boolean doCommand)
79   {
80     String sequence;
81     int start, end;
82     char nextChar;
83     for (int[] r : regions)
84     {
85       start = r[0];
86       for (int s = 0; s < seqs.length; s++)
87       {
88         sequence = seqs[s].getSequenceAsString();
89         StringBuffer newSeq = new StringBuffer();
90
91         if (r[1] > sequence.length())
92         {
93           end = sequence.length();
94         }
95         else
96         {
97           end = r[1];
98         }
99
100         if (start > 0)
101         {
102           newSeq.append(sequence.substring(0, start));
103         }
104
105         if ((caseChange == TO_UPPER && doCommand)
106                 || (caseChange == TO_LOWER && !doCommand))
107         {
108           newSeq.append(sequence.substring(start, end).toUpperCase());
109         }
110
111         else if ((caseChange == TO_LOWER && doCommand)
112                 || (caseChange == TO_UPPER && !doCommand))
113         {
114           newSeq.append(sequence.substring(start, end).toLowerCase());
115         }
116
117         else
118         // TOGGLE CASE
119         {
120           for (int c = start; c < end; c++)
121           {
122             nextChar = sequence.charAt(c);
123             if ('a' <= nextChar && nextChar <= 'z')
124             {
125               // TO UPPERCASE !!!
126               nextChar -= ('a' - 'A');
127             }
128             else if ('A' <= nextChar && nextChar <= 'Z')
129             {
130               // TO LOWERCASE !!!
131               nextChar += ('a' - 'A');
132             }
133             newSeq.append(nextChar);
134           }
135         }
136
137         if (end < sequence.length())
138         {
139           newSeq.append(sequence.substring(end));
140         }
141
142         seqs[s].setSequence(newSeq.toString());
143       }
144     }
145   }
146
147 }