2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.commands;
20 import jalview.datamodel.*;
22 public class ChangeCaseCommand implements CommandI
26 public static int TO_LOWER = 0;
28 public static int TO_UPPER = 1;
30 public static int TOGGLE_CASE = 2;
38 public ChangeCaseCommand(String description, SequenceI[] seqs,
39 int[][] regions, int caseChange)
41 this.description = description;
43 this.regions = regions;
44 this.caseChange = caseChange;
48 public String getDescription()
58 public void doCommand(AlignmentI[] views)
63 public void undoCommand(AlignmentI[] views)
68 void changeCase(boolean doCommand)
73 for (int r = 0; r < regions.length; r++)
75 start = regions[r][0];
76 for (int s = 0; s < seqs.length; s++)
78 sequence = seqs[s].getSequenceAsString();
79 StringBuffer newSeq = new StringBuffer();
81 if (regions[r][1] > sequence.length())
83 end = sequence.length();
92 newSeq.append(sequence.substring(0, start));
95 if ((caseChange == TO_UPPER && doCommand)
96 || (caseChange == TO_LOWER && !doCommand))
98 newSeq.append(sequence.substring(start, end).toUpperCase());
101 else if ((caseChange == TO_LOWER && doCommand)
102 || (caseChange == TO_UPPER && !doCommand))
104 newSeq.append(sequence.substring(start, end).toLowerCase());
110 for (int c = start; c < end; c++)
112 nextChar = sequence.charAt(c);
113 if ('a' <= nextChar && nextChar <= 'z')
116 nextChar -= ('a' - 'A');
118 else if ('A' <= nextChar && nextChar <= 'Z')
121 nextChar += ('a' - 'A');
123 newSeq.append(nextChar);
127 if (end < sequence.length())
129 newSeq.append(sequence.substring(end));
132 seqs[s].setSequence(newSeq.toString());