JAL-1807 includes ?j2sdebug flag and DebugJS._(msg)
[jalviewjs.git] / bin / jalview / commands / ChangeCaseCommand.js
1 Clazz.declarePackage ("jalview.commands");
2 Clazz.load (["jalview.commands.CommandI"], "jalview.commands.ChangeCaseCommand", ["java.lang.StringBuffer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.description = null;
5 this.caseChange = -1;
6 this.seqs = null;
7 this.regions = null;
8 Clazz.instantialize (this, arguments);
9 }, jalview.commands, "ChangeCaseCommand", null, jalview.commands.CommandI);
10 Clazz.makeConstructor (c$, 
11 function (description, seqs, regions, caseChange) {
12 this.description = description;
13 this.seqs = seqs;
14 this.regions = regions;
15 this.caseChange = caseChange;
16 this.doCommand (null);
17 }, "~S,~A,java.util.List,~N");
18 Clazz.overrideMethod (c$, "getDescription", 
19 function () {
20 return this.description;
21 });
22 Clazz.overrideMethod (c$, "getSize", 
23 function () {
24 return 1;
25 });
26 Clazz.overrideMethod (c$, "doCommand", 
27 function (views) {
28 this.changeCase (true);
29 }, "~A");
30 Clazz.overrideMethod (c$, "undoCommand", 
31 function (views) {
32 this.changeCase (false);
33 }, "~A");
34 Clazz.defineMethod (c$, "changeCase", 
35 function (doCommand) {
36 var sequence;
37 var start;
38 var end;
39 var nextChar;
40 for (var r, $r = this.regions.iterator (); $r.hasNext () && ((r = $r.next ()) || true);) {
41 start = r[0];
42 for (var s = 0; s < this.seqs.length; s++) {
43 sequence = this.seqs[s].getSequenceAsString ();
44 var newSeq =  new StringBuffer ();
45 if (r[1] > sequence.length) {
46 end = sequence.length;
47 } else {
48 end = r[1];
49 }if (start > 0) {
50 newSeq.append (sequence.substring (0, start));
51 }if ((this.caseChange == jalview.commands.ChangeCaseCommand.TO_UPPER && doCommand) || (this.caseChange == jalview.commands.ChangeCaseCommand.TO_LOWER && !doCommand)) {
52 newSeq.append (sequence.substring (start, end).toUpperCase ());
53 } else if ((this.caseChange == jalview.commands.ChangeCaseCommand.TO_LOWER && doCommand) || (this.caseChange == jalview.commands.ChangeCaseCommand.TO_UPPER && !doCommand)) {
54 newSeq.append (sequence.substring (start, end).toLowerCase ());
55 } else {
56 for (var c = start; c < end; c++) {
57 nextChar = sequence.charAt (c);
58 if ('a' <= nextChar && nextChar <= 'z') {
59 nextChar = String.fromCharCode (nextChar.charCodeAt (0) - (32));
60 } else if ('A' <= nextChar && nextChar <= 'Z') {
61 nextChar = String.fromCharCode (nextChar.charCodeAt (0) + (32));
62 }newSeq.append (nextChar);
63 }
64 }if (end < sequence.length) {
65 newSeq.append (sequence.substring (end));
66 }this.seqs[s].setSequence (newSeq.toString ());
67 }
68 }
69 }, "~B");
70 Clazz.defineStatics (c$,
71 "TO_LOWER", 0,
72 "TO_UPPER", 1,
73 "TOGGLE_CASE", 2);
74 });