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