JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / commands / ChangeCaseCommand.java
index 49faa50..b68b47c 100644 (file)
-  /*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
- */\r
-package jalview.commands;\r
-\r
-import jalview.datamodel.*;\r
-\r
-public class ChangeCaseCommand implements CommandI\r
-{\r
-  String description;\r
-  public static int TO_LOWER = 0;\r
-  public static int TO_UPPER = 1;\r
-  public static int TOGGLE_CASE = 2;\r
-  int caseChange = -1;\r
-  SequenceI [] seqs;\r
-  int [][] regions;\r
-  public ChangeCaseCommand(String description,\r
-                           SequenceI[] seqs,\r
-                           int [][] regions,\r
-                           int caseChange)\r
-  {\r
-    this.description = description;\r
-    this.seqs = seqs;\r
-    this.regions = regions;\r
-    this.caseChange = caseChange;\r
-    doCommand();\r
-  }\r
-\r
-  public String getDescription()\r
-  {\r
-    return description;\r
-  }\r
-\r
-  public int getSize()\r
-  {\r
-    return 1;\r
-  }\r
-\r
-  public void doCommand()\r
-  {\r
-    changeCase(true);\r
-  }\r
-\r
-  public void undoCommand()\r
-  {\r
-    changeCase(false);\r
-  }\r
-\r
-  void changeCase(boolean doCommand)\r
-  {\r
-    String sequence;\r
-    int start, end;\r
-    char nextChar;\r
-    for (int r = 0; r < regions.length; r++)\r
-    {\r
-      start = regions[r][0];\r
-      for (int s = 0; s < seqs.length; s++)\r
-      {\r
-        sequence = seqs[s].getSequence();\r
-        StringBuffer newSeq = new StringBuffer();\r
-\r
-        if (regions[r][1] > sequence.length())\r
-          end = sequence.length();\r
-        else\r
-          end = regions[r][1];\r
-\r
-        if (start > 0)\r
-        {\r
-          newSeq.append(sequence.substring(0, start));\r
-        }\r
-\r
-        if ( (caseChange == TO_UPPER && doCommand)\r
-            || (caseChange == TO_LOWER && !doCommand))\r
-          newSeq.append(sequence.substring(start, end).toUpperCase());\r
-\r
-        else if ( (caseChange == TO_LOWER && doCommand)\r
-                 || (caseChange == TO_UPPER && !doCommand))\r
-          newSeq.append(sequence.substring(start, end).toLowerCase());\r
-\r
-        else //TOGGLE CASE\r
-        {\r
-          for (int c = start; c < end; c++)\r
-          {\r
-            nextChar = sequence.charAt(c);\r
-            if ('a' <= nextChar && nextChar <= 'z')\r
-            {\r
-              // TO UPPERCASE !!!\r
-              nextChar -= ('a' - 'A');\r
-            }\r
-            else if ('A' <= nextChar && nextChar <= 'Z')\r
-            {\r
-              // TO LOWERCASE !!!\r
-              nextChar += ('a' - 'A');\r
-            }\r
-            newSeq.append(nextChar);\r
-          }\r
-        }\r
-\r
-        if (end < sequence.length())\r
-          newSeq.append(sequence.substring(end));\r
-\r
-        seqs[s].setSequence(newSeq.toString());\r
-      }\r
-    }\r
-  }\r
-\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.commands;
+
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceI;
+
+import java.util.List;
+
+public class ChangeCaseCommand implements CommandI
+{
+  String description;
+
+  public static int TO_LOWER = 0;
+
+  public static int TO_UPPER = 1;
+
+  public static int TOGGLE_CASE = 2;
+
+  int caseChange = -1;
+
+  SequenceI[] seqs;
+
+  List<int[]> regions;
+
+  public ChangeCaseCommand(String description, SequenceI[] seqs,
+          List<int[]> regions, int caseChange)
+  {
+    this.description = description;
+    this.seqs = seqs;
+    this.regions = regions;
+    this.caseChange = caseChange;
+    doCommand(null);
+  }
+
+  public String getDescription()
+  {
+    return description;
+  }
+
+  public int getSize()
+  {
+    return 1;
+  }
+
+  public void doCommand(AlignmentI[] views)
+  {
+    changeCase(true);
+  }
+
+  public void undoCommand(AlignmentI[] views)
+  {
+    changeCase(false);
+  }
+
+  void changeCase(boolean doCommand)
+  {
+    String sequence;
+    int start, end;
+    char nextChar;
+    for (int[] r : regions)
+    {
+      start = r[0];
+      for (int s = 0; s < seqs.length; s++)
+      {
+        sequence = seqs[s].getSequenceAsString();
+        StringBuffer newSeq = new StringBuffer();
+
+        if (r[1] > sequence.length())
+        {
+          end = sequence.length();
+        }
+        else
+        {
+          end = r[1];
+        }
+
+        if (start > 0)
+        {
+          newSeq.append(sequence.substring(0, start));
+        }
+
+        if ((caseChange == TO_UPPER && doCommand)
+                || (caseChange == TO_LOWER && !doCommand))
+        {
+          newSeq.append(sequence.substring(start, end).toUpperCase());
+        }
+
+        else if ((caseChange == TO_LOWER && doCommand)
+                || (caseChange == TO_UPPER && !doCommand))
+        {
+          newSeq.append(sequence.substring(start, end).toLowerCase());
+        }
+
+        else
+        // TOGGLE CASE
+        {
+          for (int c = start; c < end; c++)
+          {
+            nextChar = sequence.charAt(c);
+            if ('a' <= nextChar && nextChar <= 'z')
+            {
+              // TO UPPERCASE !!!
+              nextChar -= ('a' - 'A');
+            }
+            else if ('A' <= nextChar && nextChar <= 'Z')
+            {
+              // TO LOWERCASE !!!
+              nextChar += ('a' - 'A');
+            }
+            newSeq.append(nextChar);
+          }
+        }
+
+        if (end < sequence.length())
+        {
+          newSeq.append(sequence.substring(end));
+        }
+
+        seqs[s].setSequence(newSeq.toString());
+      }
+    }
+  }
+
+}