Commands which can be done, or undone
[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       end = regions[r][1];\r
73       for (int s = 0; s < seqs.length; s++)\r
74       {\r
75         sequence = seqs[s].getSequence();\r
76         StringBuffer newSeq = new StringBuffer();\r
77 \r
78         if (end > sequence.length())\r
79           end = sequence.length();\r
80 \r
81         if (start > 0)\r
82         {\r
83           newSeq.append(sequence.substring(0, start));\r
84         }\r
85 \r
86         if ( (caseChange == TO_UPPER && doCommand)\r
87             || (caseChange == TO_LOWER && !doCommand))\r
88           newSeq.append(sequence.substring(start, end).toUpperCase());\r
89 \r
90         else if ( (caseChange == TO_LOWER && doCommand)\r
91                  || (caseChange == TO_UPPER && !doCommand))\r
92           newSeq.append(sequence.substring(start, end).toLowerCase());\r
93 \r
94         else //TOGGLE CASE\r
95         {\r
96           for (int c = start; c < end; c++)\r
97           {\r
98             nextChar = sequence.charAt(c);\r
99             if ('a' <= nextChar && nextChar <= 'z')\r
100             {\r
101               // TO UPPERCASE !!!\r
102               nextChar -= ('a' - 'A');\r
103             }\r
104             else if ('A' <= nextChar && nextChar <= 'Z')\r
105             {\r
106               // TO LOWERCASE !!!\r
107               nextChar += ('a' - 'A');\r
108             }\r
109             newSeq.append(nextChar);\r
110           }\r
111         }\r
112 \r
113         if (end < sequence.length())\r
114           newSeq.append(sequence.substring(end));\r
115 \r
116         seqs[s].setSequence(newSeq.toString());\r
117       }\r
118     }\r
119   }\r
120 \r
121 }\r