Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
ChangeCaseCommand | 30 | 36 | 23 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | */ | |
21 | package jalview.commands; | |
22 | ||
23 | import java.util.Locale; | |
24 | ||
25 | import jalview.datamodel.AlignmentI; | |
26 | import jalview.datamodel.SequenceI; | |
27 | ||
28 | import java.util.List; | |
29 | ||
30 | public class ChangeCaseCommand implements CommandI | |
31 | { | |
32 | String description; | |
33 | ||
34 | public static int TO_LOWER = 0; | |
35 | ||
36 | public static int TO_UPPER = 1; | |
37 | ||
38 | public static int TOGGLE_CASE = 2; | |
39 | ||
40 | int caseChange = -1; | |
41 | ||
42 | SequenceI[] seqs; | |
43 | ||
44 | List<int[]> regions; | |
45 | ||
46 | 0 | public ChangeCaseCommand(String description, SequenceI[] seqs, |
47 | List<int[]> regions, int caseChange) | |
48 | { | |
49 | 0 | this.description = description; |
50 | 0 | this.seqs = seqs; |
51 | 0 | this.regions = regions; |
52 | 0 | this.caseChange = caseChange; |
53 | 0 | doCommand(null); |
54 | } | |
55 | ||
56 | 0 | public String getDescription() |
57 | { | |
58 | 0 | return description; |
59 | } | |
60 | ||
61 | 0 | public int getSize() |
62 | { | |
63 | 0 | return 1; |
64 | } | |
65 | ||
66 | 0 | public void doCommand(AlignmentI[] views) |
67 | { | |
68 | 0 | changeCase(true); |
69 | } | |
70 | ||
71 | 0 | public void undoCommand(AlignmentI[] views) |
72 | { | |
73 | 0 | changeCase(false); |
74 | } | |
75 | ||
76 | 0 | void changeCase(boolean doCommand) |
77 | { | |
78 | 0 | String sequence; |
79 | 0 | int start, end; |
80 | 0 | char nextChar; |
81 | 0 | for (int[] r : regions) |
82 | { | |
83 | 0 | start = r[0]; |
84 | 0 | for (int s = 0; s < seqs.length; s++) |
85 | { | |
86 | 0 | sequence = seqs[s].getSequenceAsString(); |
87 | 0 | StringBuffer newSeq = new StringBuffer(); |
88 | ||
89 | 0 | if (r[1] > sequence.length()) |
90 | { | |
91 | 0 | end = sequence.length(); |
92 | } | |
93 | else | |
94 | { | |
95 | 0 | end = r[1]; |
96 | } | |
97 | ||
98 | 0 | if (start > 0) |
99 | { | |
100 | 0 | newSeq.append(sequence.substring(0, start)); |
101 | } | |
102 | ||
103 | 0 | if ((caseChange == TO_UPPER && doCommand) |
104 | || (caseChange == TO_LOWER && !doCommand)) | |
105 | { | |
106 | 0 | newSeq.append( |
107 | sequence.substring(start, end).toUpperCase(Locale.ROOT)); | |
108 | } | |
109 | ||
110 | 0 | else if ((caseChange == TO_LOWER && doCommand) |
111 | || (caseChange == TO_UPPER && !doCommand)) | |
112 | { | |
113 | 0 | newSeq.append( |
114 | sequence.substring(start, end).toLowerCase(Locale.ROOT)); | |
115 | } | |
116 | ||
117 | else | |
118 | // TOGGLE CASE | |
119 | { | |
120 | 0 | for (int c = start; c < end; c++) |
121 | { | |
122 | 0 | nextChar = sequence.charAt(c); |
123 | 0 | if ('a' <= nextChar && nextChar <= 'z') |
124 | { | |
125 | // TO UPPERCASE !!! | |
126 | 0 | nextChar -= ('a' - 'A'); |
127 | } | |
128 | 0 | else if ('A' <= nextChar && nextChar <= 'Z') |
129 | { | |
130 | // TO LOWERCASE !!! | |
131 | 0 | nextChar += ('a' - 'A'); |
132 | } | |
133 | 0 | newSeq.append(nextChar); |
134 | } | |
135 | } | |
136 | ||
137 | 0 | if (end < sequence.length()) |
138 | { | |
139 | 0 | newSeq.append(sequence.substring(end)); |
140 | } | |
141 | ||
142 | 0 | seqs[s].setSequence(newSeq.toString()); |
143 | } | |
144 | } | |
145 | } | |
146 | ||
147 | } |