JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / commands / ChangeCaseCommand.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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 static int TO_LOWER = 0;
33
34   public static int TO_UPPER = 1;
35
36   public static int TOGGLE_CASE = 2;
37
38   int caseChange = -1;
39
40   SequenceI[] seqs;
41
42   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   public String getDescription()
55   {
56     return description;
57   }
58
59   public int getSize()
60   {
61     return 1;
62   }
63
64   public void doCommand(AlignmentI[] views)
65   {
66     changeCase(true);
67   }
68
69   public void undoCommand(AlignmentI[] views)
70   {
71     changeCase(false);
72   }
73
74   void changeCase(boolean doCommand)
75   {
76     String sequence;
77     int start, end;
78     char nextChar;
79     for (int[] r : regions)
80     {
81       start = r[0];
82       for (int s = 0; s < seqs.length; s++)
83       {
84         sequence = seqs[s].getSequenceAsString();
85         StringBuffer newSeq = new StringBuffer();
86
87         if (r[1] > sequence.length())
88         {
89           end = sequence.length();
90         }
91         else
92         {
93           end = r[1];
94         }
95
96         if (start > 0)
97         {
98           newSeq.append(sequence.substring(0, start));
99         }
100
101         if ((caseChange == TO_UPPER && doCommand)
102                 || (caseChange == TO_LOWER && !doCommand))
103         {
104           newSeq.append(sequence.substring(start, end).toUpperCase());
105         }
106
107         else if ((caseChange == TO_LOWER && doCommand)
108                 || (caseChange == TO_UPPER && !doCommand))
109         {
110           newSeq.append(sequence.substring(start, end).toLowerCase());
111         }
112
113         else
114         // TOGGLE CASE
115         {
116           for (int c = start; c < end; c++)
117           {
118             nextChar = sequence.charAt(c);
119             if ('a' <= nextChar && nextChar <= 'z')
120             {
121               // TO UPPERCASE !!!
122               nextChar -= ('a' - 'A');
123             }
124             else if ('A' <= nextChar && nextChar <= 'Z')
125             {
126               // TO LOWERCASE !!!
127               nextChar += ('a' - 'A');
128             }
129             newSeq.append(nextChar);
130           }
131         }
132
133         if (end < sequence.length())
134         {
135           newSeq.append(sequence.substring(end));
136         }
137
138         seqs[s].setSequence(newSeq.toString());
139       }
140     }
141   }
142
143 }