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