Scrolls when editing
authoramwaterhouse <Andrew Waterhouse>
Thu, 16 Mar 2006 15:27:23 +0000 (15:27 +0000)
committeramwaterhouse <Andrew Waterhouse>
Thu, 16 Mar 2006 15:27:23 +0000 (15:27 +0000)
src/jalview/gui/SeqPanel.java

index b9ba9f3..2fe27b3 100755 (executable)
@@ -25,8 +25,6 @@ import jalview.schemes.*;
 import java.awt.*;\r
 import java.awt.event.*;\r
 \r
-import java.util.*;\r
-\r
 import javax.swing.*;\r
 \r
 \r
@@ -36,7 +34,9 @@ import javax.swing.*;
  * @author $author$\r
  * @version $Revision$\r
  */\r
-public class SeqPanel extends JPanel\r
+public class SeqPanel extends JPanel implements MouseListener,\r
+    MouseMotionListener, MouseWheelListener\r
+\r
 {\r
     /** DOCUMENT ME!! */\r
     public SeqCanvas seqCanvas;\r
@@ -50,7 +50,7 @@ public class SeqPanel extends JPanel
     protected AlignViewport av;\r
 \r
     // if character is inserted or deleted, we will need to recalculate the conservation\r
-    int seqEditOccurred = -1;\r
+    boolean seqEditOccurred = false;\r
     ScrollThread scrollThread = null;\r
     boolean mouseDragging = false;\r
     boolean editingSeqs = false;\r
@@ -89,130 +89,12 @@ public class SeqPanel extends JPanel
 \r
         ap = p;\r
 \r
-\r
-        addMouseMotionListener(new MouseMotionAdapter()\r
-            {\r
-                public void mouseMoved(MouseEvent evt)\r
-                {\r
-                    doMouseMoved(evt);\r
-                    if (editingSeqs)\r
-                    {\r
-                      // This is because MacOSX creates a mouseMoved\r
-                      // If control is down\r
-                      if(!av.isDataset())\r
-                        doMouseDragged(evt);\r
-                    }\r
-                }\r
-\r
-                public void mouseDragged(MouseEvent evt)\r
-                {\r
-                    if (editingSeqs)\r
-                    {\r
-                      if(!av.isDataset())\r
-                        doMouseDragged(evt);\r
-                    }\r
-                    else\r
-                    {\r
-                      if(!av.isDataset())\r
-                        doMouseDraggedDefineMode(evt);\r
-                    }\r
-                }\r
-            });\r
-\r
-       addMouseWheelListener(new MouseWheelListener()\r
-       {\r
-         public void mouseWheelMoved(MouseWheelEvent e)\r
-         {\r
-\r
-           if (mouseWheelPressed)\r
-           {\r
-             Font font = av.getFont();\r
-             int fontSize = font.getSize();\r
-             if (e.getWheelRotation() > 0 && fontSize < 51)\r
-               fontSize++;\r
-             else if (fontSize > 1)\r
-               fontSize--;\r
-\r
-             av.setFont(new Font(font.getName(), font.getStyle(), fontSize));\r
-             ap.fontChanged();\r
-           }\r
-           else\r
-           {\r
-             if (e.getWheelRotation() > 0)\r
-               ap.scrollUp(false);\r
-             else\r
-               ap.scrollUp(true);\r
-           }\r
-\r
-         }\r
-       });\r
-\r
-\r
-       if(!av.isDataset())\r
-       {\r
-         addMouseListener(new MouseAdapter()\r
-         {\r
-           public void mouseReleased(MouseEvent evt)\r
-           {\r
-             mouseWheelPressed = false;\r
-\r
-             if (editingSeqs)\r
-             {\r
-               doMouseReleased(evt);\r
-             }\r
-             else\r
-             {\r
-               doMouseReleasedDefineMode(evt);\r
-             }\r
-           }\r
-\r
-           public void mousePressed(MouseEvent evt)\r
-           {\r
-             if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))\r
-             {\r
-               mouseWheelPressed = true;\r
-               return;\r
-             }\r
-\r
-             if (evt.isShiftDown() || evt.isAltDown() ||\r
-                 evt.isControlDown())\r
-             {\r
-               if (evt.isAltDown() || evt.isControlDown())\r
-               {\r
-                 groupEditing = true;\r
-\r
-               }\r
-\r
-               editingSeqs = true;\r
-               doMousePressed(evt);\r
-             }\r
-             else\r
-             {\r
-               doMousePressedDefineMode(evt);\r
-             }\r
-           }\r
-\r
-           public void mouseExited(MouseEvent evt)\r
-           {\r
-             if (editingSeqs)\r
-             {\r
-               return;\r
-             }\r
-\r
-             doMouseExitedDefineMode(evt);\r
-           }\r
-\r
-           public void mouseEntered(MouseEvent evt)\r
-           {\r
-             if (editingSeqs)\r
-             {\r
-               return;\r
-             }\r
-\r
-             doMouseEnteredDefineMode(evt);\r
-           }\r
-         });\r
-       }\r
+        if(!av.isDataset())\r
+        {\r
+          addMouseMotionListener(this);\r
+          addMouseListener(this);\r
+          addMouseWheelListener(this);\r
+        }\r
     }\r
 \r
     int startWrapBlock=-1;\r
@@ -284,35 +166,70 @@ public class SeqPanel extends JPanel
      return seq;\r
    }\r
 \r
+   void endEditing()\r
+   {\r
+     startseq = -1;\r
+     lastres = -1;\r
+     seqEditOccurred = false;\r
+     editingSeqs = false;\r
+     groupEditing = false;\r
+    }\r
 \r
     /**\r
      * DOCUMENT ME!\r
      *\r
      * @param evt DOCUMENT ME!\r
      */\r
-    public void doMouseReleased(MouseEvent evt)\r
+    public void mouseReleased(MouseEvent evt)\r
     {\r
-        if (seqEditOccurred > -1)\r
+      mouseDragging = false;\r
+\r
+      if (!editingSeqs)\r
+      {\r
+         doMouseReleasedDefineMode(evt);\r
+         return;\r
+      }\r
+\r
+        if (seqEditOccurred)\r
         {\r
-            editOccurred(seqEditOccurred);\r
+            editOccurred();\r
         }\r
 \r
-        startseq = -1;\r
-        lastres = -1;\r
-        seqEditOccurred = -1;\r
-        editingSeqs = false;\r
-        groupEditing = false;\r
-\r
+        endEditing();\r
         ap.repaint();\r
     }\r
 \r
+\r
+\r
     /**\r
      * DOCUMENT ME!\r
      *\r
      * @param evt DOCUMENT ME!\r
      */\r
-    public void doMousePressed(MouseEvent evt)\r
+    public void mousePressed(MouseEvent evt)\r
     {\r
+      if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))\r
+      {\r
+        mouseWheelPressed = true;\r
+        return;\r
+      }\r
+\r
+      if (evt.isShiftDown() || evt.isAltDown() ||\r
+          evt.isControlDown())\r
+      {\r
+        if (evt.isAltDown() || evt.isControlDown())\r
+        {\r
+          groupEditing = true;\r
+        }\r
+        editingSeqs = true;\r
+      }\r
+      else\r
+      {\r
+        doMousePressedDefineMode(evt);\r
+        return;\r
+      }\r
+\r
+\r
       ap.alignFrame.addHistoryItem(new HistoryItem("Edit Sequence",\r
                                                    av.alignment, HistoryItem.EDIT));\r
 \r
@@ -345,8 +262,15 @@ public class SeqPanel extends JPanel
      *\r
      * @param evt DOCUMENT ME!\r
      */\r
-    public void doMouseMoved(MouseEvent evt)\r
+    public void mouseMoved(MouseEvent evt)\r
     {\r
+      if (editingSeqs)\r
+      {\r
+       // This is because MacOSX creates a mouseMoved\r
+       // If control is down, other platforms will not.\r
+       mouseDragged(evt);\r
+      }\r
+\r
       int res = findRes(evt);\r
       int seq = findSeq(evt);\r
 \r
@@ -463,9 +387,14 @@ public class SeqPanel extends JPanel
      *\r
      * @param evt DOCUMENT ME!\r
      */\r
-    public void doMouseDragged(MouseEvent evt)\r
+    public void mouseDragged(MouseEvent evt)\r
     {\r
-        // If we're dragging we're editing\r
+      if (!editingSeqs)\r
+      {\r
+        doMouseDraggedDefineMode(evt);\r
+        return;\r
+      }\r
+\r
         int res = findRes(evt);\r
 \r
         if (res < 0)\r
@@ -484,25 +413,131 @@ public class SeqPanel extends JPanel
         {\r
             dragRight = false;\r
         }\r
+        else if(av.hasHiddenColumns)\r
+        {\r
+          //Stop editing if the user has dragged beyond hiddenBoundary\r
+          int lastCol = av.getColumnSelection().getHiddenRegionBoundary(lastres);\r
+          if( lastCol < res)\r
+          {\r
+            if(lastres!=lastCol)\r
+            {\r
+              endEditing();\r
+              return;\r
+            }\r
+          }\r
+        }\r
+\r
+        if(!groupEditing && av.hasHiddenRows)\r
+        {\r
+          if(av.alignment.getSequenceAt(startseq).getHiddenSequences()!=null)\r
+          {\r
+            groupEditing = true;\r
+          }\r
+        }\r
+\r
 \r
         if (res != lastres)\r
         {\r
+            SequenceI seq;\r
             // Group editing\r
             if (groupEditing)\r
             {\r
                 SequenceGroup sg = av.getSelectionGroup();\r
 \r
-                if (sg == null)\r
+                if (av.hasHiddenRows)\r
                 {\r
-                    lastres = -1;\r
+                  //sg might be null as the user may only see 1 sequence\r
+                  if(sg==null)\r
+                  {\r
+                    sg = new SequenceGroup();\r
+                    sg.addSequence(av.alignment.getSequenceAt(startseq), false);\r
+                  }\r
+\r
+                  SequenceGroup tmp = new SequenceGroup();\r
 \r
+                  //Do any of the sequences have hidden associates?\r
+                  for (int s = 0; s < sg.getSize(); s++)\r
+                  {\r
+                    seq = sg.getSequenceAt(s);\r
+                    tmp.addSequence(seq, false);\r
+                    if (seq.getHiddenSequences()!=null)\r
+                    {\r
+                      for(int h=0; h<seq.getHiddenSequences().getSize(); h++)\r
+                        tmp.addSequence(seq.getHiddenSequences().getSequenceAt(h),\r
+                                        false);\r
+                    }\r
+                  }\r
+\r
+                  sg = tmp;\r
+                }\r
+\r
+\r
+                if (sg == null)\r
+                {\r
+                    endEditing();\r
                     return;\r
                 }\r
 \r
+                int blankColumn = -1;\r
+\r
                 // drag to right\r
                 if (dragRight)\r
                 {\r
-                    sg.setEndRes(sg.getEndRes() + (res - lastres));\r
+\r
+                  // Is it valid??\r
+                  // Find the next gap before the end\r
+                  // of the visible region boundary\r
+                  if (av.hasHiddenColumns)\r
+                  {\r
+                    if(res-lastres > 1)\r
+                    {\r
+                      res = lastres+1;\r
+                    }\r
+\r
+\r
+                    int lastCol = av.getColumnSelection().\r
+                          getHiddenRegionBoundary(res);\r
+\r
+                    if(lastCol!=res)\r
+                    {\r
+                      for (blankColumn = lastCol;\r
+                           blankColumn > lastres;\r
+                           blankColumn--)\r
+                      {\r
+                        boolean blank = true;\r
+                        for (int s = 0; s < sg.getSize(); s++)\r
+                        {\r
+                          seq = sg.getSequenceAt(s);\r
+\r
+                          if (seq.getSequence().length() <= blankColumn)\r
+                          {\r
+                            continue;\r
+                          }\r
+\r
+                          if (!jalview.util.Comparison.isGap(\r
+                              seq.getSequence().charAt(blankColumn)))\r
+                          {\r
+                            blank = false;\r
+                            continue;\r
+                          }\r
+                        }\r
+                        if (blank)\r
+                          break;\r
+                      }\r
+\r
+\r
+                      if (blankColumn <= lastres)\r
+                      {\r
+                        endEditing();\r
+                        return;\r
+                      }\r
+                    }\r
+                    else\r
+                      blankColumn = -1;\r
+\r
+                  }\r
+\r
+                  sg.setEndRes(sg.getEndRes() + (res - lastres));\r
                 }\r
 \r
                 // drag to left\r
@@ -510,11 +545,10 @@ public class SeqPanel extends JPanel
                 {\r
                     /// Are we able to delete?\r
                     // ie are all columns blank?\r
-                    boolean deleteAllowed = false;\r
 \r
                     for (int s = 0; s < sg.getSize(); s++)\r
                     {\r
-                        SequenceI seq = sg.getSequenceAt(s);\r
+                        seq = sg.getSequenceAt(s);\r
 \r
                         for (int j = res; j < lastres; j++)\r
                         {\r
@@ -527,37 +561,29 @@ public class SeqPanel extends JPanel
                                         seq.getSequence().charAt(j)))\r
                             {\r
                                 // Not a gap, block edit not valid\r
-                                res = j + 1;\r
-                                deleteAllowed = false;\r
-\r
-                                continue;\r
+                                endEditing();\r
+                                return;\r
                             }\r
-\r
-                            deleteAllowed = true;\r
                         }\r
                     }\r
 \r
-                    if (!deleteAllowed)\r
+                    if(res<sg.getStartRes())\r
                     {\r
-                        lastres = -1;\r
-\r
-                        return;\r
+                      sg.setStartRes(sg.getStartRes() - 1);\r
                     }\r
-\r
                     sg.setEndRes(sg.getEndRes() - (lastres - res));\r
                 }\r
 \r
                 for (int i = 0; i < sg.getSize(); i++)\r
                 {\r
-                    SequenceI s = sg.getSequenceAt(i);\r
-                    int k = av.alignment.findIndex(s);\r
+                    seq = sg.getSequenceAt(i);\r
 \r
                     // drag to right\r
                     if (dragRight)\r
                     {\r
                         for (int j = lastres; j < res; j++)\r
                         {\r
-                            insertChar(j, k);\r
+                            insertChar(j, seq, blankColumn);\r
                         }\r
                     }\r
 \r
@@ -566,9 +592,9 @@ public class SeqPanel extends JPanel
                     {\r
                         for (int j = res; j < lastres; j++)\r
                         {\r
-                            if (s.getLength() > j)\r
+                            if (seq.getLength()-1 > res)\r
                             {\r
-                                deleteChar(res, k);\r
+                                deleteChar(res, seq);\r
                             }\r
                         }\r
                     }\r
@@ -576,12 +602,13 @@ public class SeqPanel extends JPanel
             }\r
             else /////Editing a single sequence///////////\r
             {\r
+                seq = av.alignment.getSequenceAt(startseq);\r
                 if ((res < av.getAlignment().getWidth()) && (res > lastres))\r
                 {\r
                     // dragging to the right\r
                     for (int j = lastres; j < res; j++)\r
                     {\r
-                        insertChar(j, startseq);\r
+                        insertChar(j, seq, j);\r
                     }\r
                 }\r
                 else if ((res < av.getAlignment().getWidth()) &&\r
@@ -594,7 +621,7 @@ public class SeqPanel extends JPanel
                                     av.alignment.getSequenceAt(startseq)\r
                                                     .getSequence().charAt(res)))\r
                         {\r
-                            deleteChar(res, startseq);\r
+                            deleteChar(res, seq);\r
                         }\r
                         else\r
                         {\r
@@ -605,11 +632,77 @@ public class SeqPanel extends JPanel
             }\r
         }\r
 \r
+        mouseDragging = true;\r
+        if(scrollThread!=null)\r
+          scrollThread.setEvent(evt);\r
+\r
         endEdit = res;\r
         lastres = res;\r
         seqCanvas.repaint();\r
     }\r
 \r
+    /**\r
+     * DOCUMENT ME!\r
+     *\r
+     * @param e DOCUMENT ME!\r
+     */\r
+    public void mouseEntered(MouseEvent e)\r
+    {\r
+        if(oldSeq < 0)\r
+          oldSeq = 0;\r
+\r
+        if (scrollThread != null)\r
+        {\r
+            scrollThread.running = false;\r
+            scrollThread = null;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * DOCUMENT ME!\r
+     *\r
+     * @param e DOCUMENT ME!\r
+     */\r
+    public void mouseExited(MouseEvent e)\r
+    {\r
+        if (av.getWrapAlignment())\r
+        {\r
+            return;\r
+        }\r
+\r
+        if (mouseDragging)\r
+        {\r
+            scrollThread = new ScrollThread();\r
+        }\r
+    }\r
+\r
+    public void mouseClicked(MouseEvent evt)\r
+    {}\r
+\r
+    public void mouseWheelMoved(MouseWheelEvent e)\r
+    {\r
+      if (mouseWheelPressed)\r
+      {\r
+        Font font = av.getFont();\r
+        int fontSize = font.getSize();\r
+        if (e.getWheelRotation() > 0 && fontSize < 51)\r
+          fontSize++;\r
+        else if (fontSize > 1)\r
+          fontSize--;\r
+\r
+        av.setFont(new Font(font.getName(), font.getStyle(), fontSize));\r
+        ap.fontChanged();\r
+      }\r
+      else\r
+      {\r
+        if (e.getWheelRotation() > 0)\r
+          ap.scrollUp(false);\r
+        else\r
+          ap.scrollUp(true);\r
+      }\r
+\r
+    }\r
+\r
 \r
     /**\r
      * DOCUMENT ME!\r
@@ -617,10 +710,45 @@ public class SeqPanel extends JPanel
      * @param j DOCUMENT ME!\r
      * @param seq DOCUMENT ME!\r
      */\r
-    public void insertChar(int j, int seq)\r
+    void insertChar(int j, SequenceI seq, int blankColumn)\r
     {\r
-        av.alignment.getSequenceAt(seq).insertCharAt(j, av.getGapCharacter());\r
-        seqEditOccurred = seq;\r
+        if(av.hasHiddenColumns)\r
+        {\r
+          //Find the next gap before the end of the visible region boundary\r
+          int lastCol = av.getColumnSelection().getHiddenRegionBoundary(j);\r
+          if(lastCol != j)\r
+          {\r
+            if (!groupEditing || lastCol <= j)\r
+            {\r
+              blankColumn = lastCol;\r
+              //If lastCol > j, theres a boundary after the gap insertion\r
+              if (lastCol > j)\r
+              {\r
+                for (blankColumn = lastCol; blankColumn > j; blankColumn--)\r
+                {\r
+                  if (jalview.util.Comparison.isGap(seq.getCharAt(blankColumn)))\r
+                  {\r
+                    //Theres a space, so break and insert the gap\r
+                    break;\r
+                  }\r
+                }\r
+\r
+                if (blankColumn <= j)\r
+                {\r
+                  endEditing();\r
+                  return;\r
+                }\r
+              }\r
+\r
+            }\r
+\r
+            // Editing with hidden regions only!!\r
+            seq.deleteCharAt(blankColumn);\r
+          }\r
+      }\r
+\r
+        seq.insertCharAt(j, av.getGapCharacter());\r
+        seqEditOccurred = true;\r
     }\r
 \r
     /**\r
@@ -629,12 +757,23 @@ public class SeqPanel extends JPanel
      * @param j DOCUMENT ME!\r
      * @param seq DOCUMENT ME!\r
      */\r
-    public void deleteChar(int j, int seq)\r
+    public void deleteChar(int j, SequenceI seq)\r
     {\r
-        av.alignment.getSequenceAt(seq).deleteCharAt(j);\r
-        seqEditOccurred = seq;\r
+      if (av.hasHiddenColumns)\r
+      {\r
+        //Find the next gap before the end of the visible region boundary\r
+        int lastCol = av.getColumnSelection().getHiddenRegionBoundary(j);\r
+\r
+        //If lastCol > j, theres a boundary after the gap insertion\r
+        if (lastCol > j)\r
+        {\r
+          seq.insertCharAt(lastCol, av.getGapCharacter());\r
+        }\r
+      }\r
+\r
+        seq.deleteCharAt(j);\r
+        seqEditOccurred = true;\r
 \r
-        av.alignment.getWidth();\r
         seqCanvas.repaint();\r
     }\r
 \r
@@ -643,7 +782,7 @@ public class SeqPanel extends JPanel
      *\r
      * @param i DOCUMENT ME!\r
      */\r
-    void editOccurred(int i)\r
+    void editOccurred()\r
     {\r
       if (endEdit == startEdit)\r
       {\r
@@ -781,17 +920,12 @@ public class SeqPanel extends JPanel
      */\r
     public void doMouseReleasedDefineMode(MouseEvent evt)\r
     {\r
-        if (mouseDragging)\r
-        {\r
-            stretchGroup.recalcConservation();\r
-            mouseDragging = false;\r
-        }\r
-\r
         if (stretchGroup == null)\r
         {\r
             return;\r
         }\r
 \r
+\r
         if(stretchGroup.cs!=null)\r
         {\r
           if (stretchGroup.cs instanceof ClustalxColourScheme)\r
@@ -805,6 +939,7 @@ public class SeqPanel extends JPanel
           {\r
             SliderPanel.setConservationSlider(ap, stretchGroup.cs,\r
                                               stretchGroup.getName());\r
+            stretchGroup.recalcConservation();\r
           }\r
           else\r
           {\r
@@ -831,14 +966,15 @@ public class SeqPanel extends JPanel
       if(wrappedBlock!=startWrapBlock)\r
         return;\r
 \r
-      if (stretchGroup == null)\r
-      {\r
+       if (stretchGroup == null)\r
+       {\r
             return;\r
-        }\r
+       }\r
+\r
 \r
-        if (res > av.alignment.getWidth())\r
+        if(y > av.alignment.getHeight())\r
         {\r
-            res = av.alignment.getWidth() - 1;\r
+          y = av.alignment.getHeight() -1;\r
         }\r
 \r
         if (stretchGroup.getEndRes() == res)\r
@@ -856,10 +992,6 @@ public class SeqPanel extends JPanel
         {\r
             res = av.getStartRes();\r
         }\r
-        else if (res > av.getEndRes() && !av.getWrapAlignment())\r
-        {\r
-            res = av.getEndRes();\r
-        }\r
 \r
         if (changeEndRes)\r
         {\r
@@ -887,7 +1019,8 @@ public class SeqPanel extends JPanel
             dragDirection = -1;\r
         }\r
 \r
-        while ((y != oldSeq) && (oldSeq > 0) && (y < av.alignment.getHeight()))\r
+\r
+        while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))\r
         {\r
             // This routine ensures we don't skip any sequences, as the\r
             // selection is quite slow.\r
@@ -895,6 +1028,9 @@ public class SeqPanel extends JPanel
 \r
             oldSeq += dragDirection;\r
 \r
+            if(oldSeq<0)\r
+              break;\r
+\r
             Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);\r
 \r
             if (stretchGroup.sequences.contains(nextSeq))\r
@@ -912,7 +1048,9 @@ public class SeqPanel extends JPanel
             }\r
         }\r
 \r
-        oldSeq = y;\r
+        if(oldSeq < 0)\r
+          oldSeq = -1;\r
+\r
         mouseDragging = true;\r
 \r
         if (scrollThread != null)\r
@@ -923,36 +1061,7 @@ public class SeqPanel extends JPanel
         seqCanvas.repaint();\r
     }\r
 \r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @param e DOCUMENT ME!\r
-     */\r
-    public void doMouseEnteredDefineMode(MouseEvent e)\r
-    {\r
-        if (scrollThread != null)\r
-        {\r
-            scrollThread.running = false;\r
-        }\r
-    }\r
-\r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @param e DOCUMENT ME!\r
-     */\r
-    public void doMouseExitedDefineMode(MouseEvent e)\r
-    {\r
-        if (av.getWrapAlignment())\r
-        {\r
-            return;\r
-        }\r
 \r
-        if (mouseDragging)\r
-        {\r
-            scrollThread = new ScrollThread();\r
-        }\r
-    }\r
 \r
     // this class allows scrolling off the bottom of the visible alignment\r
     class ScrollThread extends Thread\r