2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.*;
25 public class ChangeCaseCommand implements CommandI
29 public static int TO_LOWER = 0;
31 public static int TO_UPPER = 1;
33 public static int TOGGLE_CASE = 2;
41 public ChangeCaseCommand(String description, SequenceI[] seqs,
42 int[][] regions, int caseChange)
44 this.description = description;
46 this.regions = regions;
47 this.caseChange = caseChange;
51 public String getDescription()
61 public void doCommand(AlignmentI[] views)
66 public void undoCommand(AlignmentI[] views)
71 void changeCase(boolean doCommand)
76 for (int r = 0; r < regions.length; r++)
78 start = regions[r][0];
79 for (int s = 0; s < seqs.length; s++)
81 sequence = seqs[s].getSequenceAsString();
82 StringBuffer newSeq = new StringBuffer();
84 if (regions[r][1] > sequence.length())
86 end = sequence.length();
95 newSeq.append(sequence.substring(0, start));
98 if ((caseChange == TO_UPPER && doCommand)
99 || (caseChange == TO_LOWER && !doCommand))
101 newSeq.append(sequence.substring(start, end).toUpperCase());
104 else if ((caseChange == TO_LOWER && doCommand)
105 || (caseChange == TO_UPPER && !doCommand))
107 newSeq.append(sequence.substring(start, end).toLowerCase());
113 for (int c = start; c < end; c++)
115 nextChar = sequence.charAt(c);
116 if ('a' <= nextChar && nextChar <= 'z')
119 nextChar -= ('a' - 'A');
121 else if ('A' <= nextChar && nextChar <= 'Z')
124 nextChar += ('a' - 'A');
126 newSeq.append(nextChar);
130 if (end < sequence.length())
132 newSeq.append(sequence.substring(end));
135 seqs[s].setSequence(newSeq.toString());