JAL-1932 TrimRegionCommand bug fix, tests and purge of unused code
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 22 Oct 2015 10:10:49 +0000 (11:10 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 22 Oct 2015 10:10:49 +0000 (11:10 +0100)
help/html/menus/alignmentMenu.html
help/html/menus/alwedit.html
src/jalview/appletgui/AlignFrame.java
src/jalview/commands/TrimRegionCommand.java
src/jalview/gui/AlignFrame.java
test/jalview/commands/TrimRegionCommandTest.java [new file with mode: 0644]

index b006c89..ce339cc 100755 (executable)
             &quot;Deselect All&quot; to deselect all columns.</em></li>
         <li><strong>Remove Right (Control R)<br>
         </strong><em>If the alignment has marked columns, the alignment will
-            be trimmed to the left of the leftmost marked column. To
+            be trimmed to the right of the rightmost marked column. To
             mark a column, mouse click the scale bar above the
             alignment. Click again to unmark a column, or select
             &quot;Deselect All&quot; to deselect all columns.</em></li>
index a8390c2..846a1bd 100755 (executable)
@@ -75,7 +75,7 @@
         deselect all columns.</em></li>
     <li><strong>Remove Right (Control R)<br>
     </strong><em>If the alignment has marked columns, the alignment will be
-        trimmed to the left of the leftmost marked column. To mark a
+        trimmed to the right of the rightmost marked column. To mark a
         column, mouse click the scale bar above the alignment. Click
         again to unmark a column, or select &quot;Deselect All&quot; to
         deselect all columns.</em></li>
index 80ab5d5..50ec7e0 100644 (file)
@@ -2294,18 +2294,14 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
       TrimRegionCommand trimRegion;
       if (trimLeft)
       {
-        trimRegion = new TrimRegionCommand("Remove Left",
-                TrimRegionCommand.TRIM_LEFT, seqs, column,
-                viewport.getAlignment(), viewport.getColumnSelection(),
-                viewport.getSelectionGroup());
+        trimRegion = new TrimRegionCommand("Remove Left", true, seqs,
+                column, viewport.getAlignment());
         viewport.setStartRes(0);
       }
       else
       {
-        trimRegion = new TrimRegionCommand("Remove Right",
-                TrimRegionCommand.TRIM_RIGHT, seqs, column,
-                viewport.getAlignment(), viewport.getColumnSelection(),
-                viewport.getSelectionGroup());
+        trimRegion = new TrimRegionCommand("Remove Right", false, seqs,
+                column, viewport.getAlignment());
       }
 
       statusBar.setText(MessageManager.formatMessage(
@@ -2609,6 +2605,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
 
   }
 
+  @Override
   public void changeColour(ColourSchemeI cs)
   {
 
@@ -3759,6 +3756,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     this.add(statusBar, BorderLayout.SOUTH);
   }
 
+  @Override
   public void setStatus(String string)
   {
     statusBar.setText(string);
index b4d92a0..dc6c424 100644 (file)
 package jalview.commands;
 
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.ColumnSelection;
-import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
-import jalview.util.ShiftList;
-
-import java.util.List;
 
 public class TrimRegionCommand extends EditCommand
 {
-  public static String TRIM_LEFT = "TrimLeft";
-
-  public static String TRIM_RIGHT = "TrimRight";
-
-  public ColumnSelection colSel = null;
-
-  int[] start;
-
-  ShiftList shiftList;
-
-  SequenceGroup selectionGroup;
-
-  List<int[]> deletedHiddenColumns;
-
   int columnsDeleted;
 
-  public TrimRegionCommand(String description, String command,
-          SequenceI[] seqs, int column, AlignmentI al,
-          ColumnSelection colSel, SequenceGroup selectedRegion)
+  /**
+   * Constructs and performs a trim alignment command
+   * 
+   * @param description
+   *          (to show in Undo/Redo menu)
+   * @param trimLeft
+   *          if true trim to left of column, else to right
+   * @param seqs
+   *          the sequences to trim
+   * @param column
+   *          the alignment column (base 0) from which to trim
+   * @param al
+   */
+  public TrimRegionCommand(String description, boolean trimLeft,
+          SequenceI[] seqs, int column, AlignmentI al)
   {
     this.description = description;
-    this.selectionGroup = selectedRegion;
-    this.colSel = colSel;
-    if (command.equalsIgnoreCase(TRIM_LEFT))
+    if (trimLeft)
     {
       if (column == 0)
       {
@@ -64,109 +55,24 @@ public class TrimRegionCommand extends EditCommand
 
       setEdit(new Edit(Action.CUT, seqs, 0, column, al));
     }
-    else if (command.equalsIgnoreCase(TRIM_RIGHT))
+    else
     {
       int width = al.getWidth() - column - 1;
-      if (width < 2)
+      if (width < 1)
       {
         return;
       }
 
-      columnsDeleted = width - 1;
+      columnsDeleted = width;
 
       setEdit(new Edit(Action.CUT, seqs, column + 1, width, al));
     }
 
-    // We need to keep a record of the sequence start
-    // in order to restore the state after a redo
-    int i, isize = getEdit(0).seqs.length;
-    start = new int[isize];
-    for (i = 0; i < isize; i++)
-    {
-      start[i] = getEdit(0).seqs[i].getStart();
-    }
 
     performEdit(0, null);
   }
 
-  void cut(Edit command)
-  {
-    int column, j, jSize = command.seqs.length;
-    for (j = 0; j < jSize; j++)
-    {
-      if (command.position == 0)
-      {
-        // This is a TRIM_LEFT command
-        column = command.seqs[j].findPosition(command.number);
-        command.seqs[j].setStart(column);
-      }
-      else
-      {
-        // This is a TRIM_RIGHT command
-        column = command.seqs[j].findPosition(command.position) - 1;
-        command.seqs[j].setEnd(column);
-      }
-    }
-
-    super.cut(command, null);
-
-    if (command.position == 0)
-    {
-      deletedHiddenColumns = colSel.compensateForEdit(0, command.number);
-      if (selectionGroup != null)
-      {
-        selectionGroup.adjustForRemoveLeft(command.number);
-      }
-    }
-    else
-    {
-      deletedHiddenColumns = colSel.compensateForEdit(command.position,
-              command.number);
-      if (selectionGroup != null)
-      {
-        selectionGroup.adjustForRemoveRight(command.position);
-      }
-    }
-  }
-
-  void paste(Edit command)
-  {
-    super.paste(command, null);
-    int column, j, jSize = command.seqs.length;
-    for (j = 0; j < jSize; j++)
-    {
-      if (command.position == 0)
-      {
-        command.seqs[j].setStart(start[j]);
-      }
-      else
-      {
-        column = command.seqs[j].findPosition(command.number
-                + command.position) - 1;
-        command.seqs[j].setEnd(column);
-      }
-    }
-
-    if (command.position == 0)
-    {
-      colSel.compensateForEdit(0, -command.number);
-      if (selectionGroup != null)
-      {
-        selectionGroup.adjustForRemoveLeft(-command.number);
-      }
-    }
-
-    if (deletedHiddenColumns != null)
-    {
-      int[] region;
-      for (int i = 0; i < deletedHiddenColumns.size(); i++)
-      {
-        region = deletedHiddenColumns.get(i);
-        colSel.hideColumns(region[0], region[1]);
-      }
-    }
-  }
-
+  @Override
   public int getSize()
   {
     return columnsDeleted;
index 5c29b9b..d20d62e 100644 (file)
@@ -845,6 +845,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * operation that affects the data in the current view (selection changed,
    * etc) to update the menus to reflect the new state.
    */
+  @Override
   public void setMenusForViewport()
   {
     setMenusFromViewport(viewport);
@@ -1401,6 +1402,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     alignPanel.makeEPS(f);
   }
 
+  @Override
   public void createSVG(File f)
   {
     alignPanel.makeSVG(f);
@@ -1574,6 +1576,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
   }
 
+  @Override
   public void addHistoryItem(CommandI command)
   {
     if (command.getSize() > 0)
@@ -2569,18 +2572,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       TrimRegionCommand trimRegion;
       if (trimLeft)
       {
-        trimRegion = new TrimRegionCommand("Remove Left",
-                TrimRegionCommand.TRIM_LEFT, seqs, column,
-                viewport.getAlignment(), viewport.getColumnSelection(),
-                viewport.getSelectionGroup());
+        trimRegion = new TrimRegionCommand("Remove Left", true, seqs,
+                column, viewport.getAlignment());
         viewport.setStartRes(0);
       }
       else
       {
-        trimRegion = new TrimRegionCommand("Remove Right",
-                TrimRegionCommand.TRIM_RIGHT, seqs, column,
-                viewport.getAlignment(), viewport.getColumnSelection(),
-                viewport.getSelectionGroup());
+        trimRegion = new TrimRegionCommand("Remove Right", false, seqs,
+                column, viewport.getAlignment());
       }
 
       statusBar.setText(MessageManager.formatMessage(
@@ -3497,6 +3496,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * @param cs
    *          DOCUMENT ME!
    */
+  @Override
   public void changeColour(ColourSchemeI cs)
   {
     // TODO: pull up to controller method
@@ -5755,6 +5755,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     viewport.firePropertyChange("alignment", null, al);
   }
 
+  @Override
   public void setShowSeqFeatures(boolean b)
   {
     showSeqFeatures.setSelected(b);
diff --git a/test/jalview/commands/TrimRegionCommandTest.java b/test/jalview/commands/TrimRegionCommandTest.java
new file mode 100644 (file)
index 0000000..d593d41
--- /dev/null
@@ -0,0 +1,160 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ 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 static org.testng.AssertJUnit.assertEquals;
+
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Unit tests for TrimRegionCommand
+ * 
+ * @author gmcarstairs
+ *
+ */
+public class TrimRegionCommandTest
+{
+  private AlignmentI al;
+
+  @BeforeMethod(alwaysRun = true)
+  public void setUp()
+  {
+    SequenceI[] seqs = new SequenceI[2];
+    seqs[0] = new Sequence("seq0", "abcde-");
+    seqs[1] = new Sequence("seq1", "-ghjkl");
+    al = new Alignment(seqs);
+    al.setDataset(null);
+  }
+
+  /**
+   * Test performing, undoing and redoing a 'trim left'
+   */
+  @Test(groups = { "Functional" })
+  public void testTrimLeft_withUndoAndRedo()
+  {
+    TrimRegionCommand cmd = new TrimRegionCommand("Remove Left", true,
+            al.getSequencesArray(), 2, al);
+    assertEquals(2, cmd.getSize());
+    assertEquals("cde-", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("hjkl", al.getSequenceAt(1).getSequenceAsString());
+
+    /*
+     * undo and verify
+     */
+    cmd.undoCommand(new AlignmentI[] { al });
+    assertEquals("abcde-", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("-ghjkl", al.getSequenceAt(1).getSequenceAsString());
+
+    /*
+     * redo and verify
+     */
+    cmd.doCommand(new AlignmentI[] { al });
+    assertEquals("cde-", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("hjkl", al.getSequenceAt(1).getSequenceAsString());
+  }
+
+  /**
+   * Trim left of no columns - should do nothing. This is the case where the
+   * first column is selected and 'Remove Left' is selected.
+   */
+  @Test(groups = { "Functional" })
+  public void testTrimLeft_noColumns()
+  {
+    TrimRegionCommand cmd = new TrimRegionCommand("Remove Left", true,
+            al.getSequencesArray(), 0, al);
+    assertEquals(0, cmd.getSize());
+    assertEquals("abcde-", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("-ghjkl", al.getSequenceAt(1).getSequenceAsString());
+  }
+
+  /**
+   * Trim left of a single column
+   */
+  @Test(groups = { "Functional" })
+  public void testTrimLeft_oneColumn()
+  {
+    TrimRegionCommand cmd = new TrimRegionCommand("Remove Left", true,
+            al.getSequencesArray(), 1, al);
+    assertEquals(1, cmd.getSize());
+    assertEquals("bcde-", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("ghjkl", al.getSequenceAt(1).getSequenceAsString());
+  }
+
+  /**
+   * Trim right of no columns - should do nothing. This is the case where the
+   * last column is selected and 'Remove Right' is selected.
+   */
+  @Test(groups = { "Functional" })
+  public void testTrimRight_noColumns()
+  {
+    TrimRegionCommand cmd = new TrimRegionCommand("Remove Right", false,
+            al.getSequencesArray(), 5, al);
+    assertEquals(0, cmd.getSize());
+    assertEquals("abcde-", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("-ghjkl", al.getSequenceAt(1).getSequenceAsString());
+  }
+
+  /**
+   * Trim right of a single column
+   */
+  @Test(groups = { "Functional" })
+  public void testTrimRight_oneColumn()
+  {
+    TrimRegionCommand cmd = new TrimRegionCommand("Remove Right", false,
+            al.getSequencesArray(), 4, al);
+    assertEquals(1, cmd.getSize());
+    assertEquals("abcde", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("-ghjk", al.getSequenceAt(1).getSequenceAsString());
+  }
+
+  /**
+   * Test performing, undoing and redoing a 'trim right'
+   */
+  @Test(groups = { "Functional" })
+  public void testTrimRight_withUndoAndRedo()
+  {
+    TrimRegionCommand cmd = new TrimRegionCommand("Remove Right", false,
+            al.getSequencesArray(), 2, al);
+    assertEquals(3, cmd.getSize());
+    assertEquals("abc", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("-gh", al.getSequenceAt(1).getSequenceAsString());
+
+    /*
+     * undo and verify
+     */
+    cmd.undoCommand(new AlignmentI[] { al });
+    assertEquals("abcde-", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("-ghjkl", al.getSequenceAt(1).getSequenceAsString());
+
+    /*
+     * redo and verify
+     */
+    cmd.doCommand(new AlignmentI[] { al });
+    assertEquals("abc", al.getSequenceAt(0).getSequenceAsString());
+    assertEquals("-gh", al.getSequenceAt(1).getSequenceAsString());
+  }
+}