7a74e08e4fcdb6fddb9f75ecf168353e2f588e77
[jalview.git] / src / jalview / commands / ChangeCaseCommand.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
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 }