00d90003c3c992a90ec966b0b406a9c8e9315bd0
[jalview.git] / src / jalview / commands / ChangeCaseCommand.java
1   /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.commands;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 public class ChangeCaseCommand implements CommandI\r
24 {\r
25   String description;\r
26   public static int TO_LOWER = 0;\r
27   public static int TO_UPPER = 1;\r
28   public static int TOGGLE_CASE = 2;\r
29   int caseChange = -1;\r
30   SequenceI [] seqs;\r
31   int [][] regions;\r
32   public ChangeCaseCommand(String description,\r
33                            SequenceI[] seqs,\r
34                            int [][] regions,\r
35                            int caseChange)\r
36   {\r
37     this.description = description;\r
38     this.seqs = seqs;\r
39     this.regions = regions;\r
40     this.caseChange = caseChange;\r
41     doCommand();\r
42   }\r
43 \r
44   public String getDescription()\r
45   {\r
46     return description;\r
47   }\r
48 \r
49   public int getSize()\r
50   {\r
51     return 1;\r
52   }\r
53 \r
54   public void doCommand()\r
55   {\r
56     changeCase(true);\r
57   }\r
58 \r
59   public void undoCommand()\r
60   {\r
61     changeCase(false);\r
62   }\r
63 \r
64   void changeCase(boolean doCommand)\r
65   {\r
66     String sequence;\r
67     int start, end;\r
68     char nextChar;\r
69     for (int r = 0; r < regions.length; r++)\r
70     {\r
71       start = regions[r][0];\r
72       for (int s = 0; s < seqs.length; s++)\r
73       {\r
74         sequence = seqs[s].getSequenceAsString();\r
75         StringBuffer newSeq = new StringBuffer();\r
76 \r
77         if (regions[r][1] > sequence.length())\r
78           end = sequence.length();\r
79         else\r
80           end = regions[r][1];\r
81 \r
82         if (start > 0)\r
83         {\r
84           newSeq.append(sequence.substring(0, start));\r
85         }\r
86 \r
87         if ( (caseChange == TO_UPPER && doCommand)\r
88             || (caseChange == TO_LOWER && !doCommand))\r
89           newSeq.append(sequence.substring(start, end).toUpperCase());\r
90 \r
91         else if ( (caseChange == TO_LOWER && doCommand)\r
92                  || (caseChange == TO_UPPER && !doCommand))\r
93           newSeq.append(sequence.substring(start, end).toLowerCase());\r
94 \r
95         else //TOGGLE CASE\r
96         {\r
97           for (int c = start; c < end; c++)\r
98           {\r
99             nextChar = sequence.charAt(c);\r
100             if ('a' <= nextChar && nextChar <= 'z')\r
101             {\r
102               // TO UPPERCASE !!!\r
103               nextChar -= ('a' - 'A');\r
104             }\r
105             else if ('A' <= nextChar && nextChar <= 'Z')\r
106             {\r
107               // TO LOWERCASE !!!\r
108               nextChar += ('a' - 'A');\r
109             }\r
110             newSeq.append(nextChar);\r
111           }\r
112         }\r
113 \r
114         if (end < sequence.length())\r
115           newSeq.append(sequence.substring(end));\r
116 \r
117         seqs[s].setSequence(newSeq.toString());\r
118       }\r
119     }\r
120   }\r
121 \r
122 }\r