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