2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.commands;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceI;
26 import java.util.List;
28 public class ChangeCaseCommand implements CommandI
32 public final static int TO_LOWER = 0;
34 public final static int TO_UPPER = 1;
36 public final static int TOGGLE_CASE = 2;
38 private int caseChange;
40 private SequenceI[] seqs;
42 private List<int[]> regions;
44 public ChangeCaseCommand(String description, SequenceI[] seqs,
45 List<int[]> regions, int caseChange)
47 this.description = description;
49 this.regions = regions;
50 this.caseChange = caseChange;
55 public String getDescription()
67 public void doCommand(AlignmentI[] views)
73 public void undoCommand(AlignmentI[] views)
78 void changeCase(boolean doCommand)
83 for (int[] r : regions)
86 for (int s = 0; s < seqs.length; s++)
88 sequence = seqs[s].getSequenceAsString();
89 StringBuffer newSeq = new StringBuffer();
91 if (r[1] > sequence.length())
93 end = sequence.length();
102 newSeq.append(sequence.substring(0, start));
105 if ((caseChange == TO_UPPER && doCommand)
106 || (caseChange == TO_LOWER && !doCommand))
108 newSeq.append(sequence.substring(start, end).toUpperCase());
111 else if ((caseChange == TO_LOWER && doCommand)
112 || (caseChange == TO_UPPER && !doCommand))
114 newSeq.append(sequence.substring(start, end).toLowerCase());
120 for (int c = start; c < end; c++)
122 nextChar = sequence.charAt(c);
123 if ('a' <= nextChar && nextChar <= 'z')
126 nextChar -= ('a' - 'A');
128 else if ('A' <= nextChar && nextChar <= 'Z')
131 nextChar += ('a' - 'A');
133 newSeq.append(nextChar);
137 if (end < sequence.length())
139 newSeq.append(sequence.substring(end));
142 seqs[s].setSequence(newSeq.toString());