JAL-3438 spotless for 2.11.2.0
[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 java.util.Locale;
24
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceI;
27
28 import java.util.List;
29
30 public class ChangeCaseCommand implements CommandI
31 {
32   String description;
33
34   public static int TO_LOWER = 0;
35
36   public static int TO_UPPER = 1;
37
38   public static int TOGGLE_CASE = 2;
39
40   int caseChange = -1;
41
42   SequenceI[] seqs;
43
44   List<int[]> regions;
45
46   public ChangeCaseCommand(String description, SequenceI[] seqs,
47           List<int[]> regions, int caseChange)
48   {
49     this.description = description;
50     this.seqs = seqs;
51     this.regions = regions;
52     this.caseChange = caseChange;
53     doCommand(null);
54   }
55
56   public String getDescription()
57   {
58     return description;
59   }
60
61   public int getSize()
62   {
63     return 1;
64   }
65
66   public void doCommand(AlignmentI[] views)
67   {
68     changeCase(true);
69   }
70
71   public void undoCommand(AlignmentI[] views)
72   {
73     changeCase(false);
74   }
75
76   void changeCase(boolean doCommand)
77   {
78     String sequence;
79     int start, end;
80     char nextChar;
81     for (int[] r : regions)
82     {
83       start = r[0];
84       for (int s = 0; s < seqs.length; s++)
85       {
86         sequence = seqs[s].getSequenceAsString();
87         StringBuffer newSeq = new StringBuffer();
88
89         if (r[1] > sequence.length())
90         {
91           end = sequence.length();
92         }
93         else
94         {
95           end = r[1];
96         }
97
98         if (start > 0)
99         {
100           newSeq.append(sequence.substring(0, start));
101         }
102
103         if ((caseChange == TO_UPPER && doCommand)
104                 || (caseChange == TO_LOWER && !doCommand))
105         {
106           newSeq.append(
107                   sequence.substring(start, end).toUpperCase(Locale.ROOT));
108         }
109
110         else if ((caseChange == TO_LOWER && doCommand)
111                 || (caseChange == TO_UPPER && !doCommand))
112         {
113           newSeq.append(
114                   sequence.substring(start, end).toLowerCase(Locale.ROOT));
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 }