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