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