Keyboard editing
authoramwaterhouse <Andrew Waterhouse>
Tue, 4 Apr 2006 09:31:52 +0000 (09:31 +0000)
committeramwaterhouse <Andrew Waterhouse>
Tue, 4 Apr 2006 09:31:52 +0000 (09:31 +0000)
src/jalview/gui/SeqPanel.java

index 3f49cc4..536d178 100755 (executable)
@@ -66,6 +66,8 @@ public class SeqPanel extends JPanel implements MouseListener,
     boolean remove = false;\r
 \r
     boolean mouseWheelPressed = false;\r
+    StringBuffer keyboardNo1;\r
+    StringBuffer keyboardNo2;\r
 \r
     /**\r
      * Creates a new SeqPanel object.\r
@@ -171,35 +173,90 @@ public class SeqPanel extends JPanel implements MouseListener,
      seqEditOccurred = false;\r
      editingSeqs = false;\r
      groupEditing = false;\r
-     keyboardGaps = null;\r
+     keyboardNo1 = null;\r
+     keyboardNo2 = null;\r
     }\r
 \r
+    void setCursorRow()\r
+    {\r
+      seqCanvas.cursorY = getKeyboardNo(keyboardNo1)-1;\r
+      scrollToVisible();\r
+    }\r
+\r
+    void setCursorColumn()\r
+    {\r
+      seqCanvas.cursorX = getKeyboardNo(keyboardNo1)-1;\r
+      scrollToVisible();\r
+    }\r
+\r
+    void setCursorRowAndColumn()\r
+    {\r
+      if(keyboardNo2==null)\r
+      {\r
+        keyboardNo2 = new StringBuffer();\r
+      }\r
+      else\r
+      {\r
+        seqCanvas.cursorX = getKeyboardNo(keyboardNo1) - 1;\r
+        seqCanvas.cursorY = getKeyboardNo(keyboardNo2) - 1;\r
+        scrollToVisible();\r
+      }\r
+    }\r
+\r
+    void setCursorPosition()\r
+    {\r
+      SequenceI sequence =\r
+          (Sequence) av.getAlignment().getSequenceAt(seqCanvas.cursorY);\r
+\r
+      seqCanvas.cursorX = sequence.findIndex(\r
+          getKeyboardNo(keyboardNo1)-1\r
+          );\r
+      scrollToVisible();\r
+    }\r
 \r
     void moveCursor(int dx, int dy)\r
     {\r
       seqCanvas.cursorX += dx;\r
       seqCanvas.cursorY += dy;\r
+      scrollToVisible();\r
+    }\r
 \r
-      if(seqCanvas.cursorX<0)\r
+    void scrollToVisible()\r
+    {\r
+      if (seqCanvas.cursorX < 0)\r
         seqCanvas.cursorX = 0;\r
-      else if(seqCanvas.cursorX>av.alignment.getWidth()-1)\r
-        seqCanvas.cursorX = av.alignment.getWidth()-1;\r
-      if(seqCanvas.cursorX<av.startRes)\r
-        ap.scrollRight(false);\r
-      else if(seqCanvas.cursorX>av.endRes)\r
-        ap.scrollRight(true);\r
-\r
-      if(seqCanvas.cursorY<0)\r
-        seqCanvas.cursorY=0;\r
-      else if(seqCanvas.cursorY>av.alignment.getHeight()-1)\r
-        seqCanvas.cursorY = av.alignment.getHeight()-1;\r
-      if(seqCanvas.cursorY<av.startSeq)\r
+      else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)\r
+        seqCanvas.cursorX = av.alignment.getWidth() - 1;\r
+\r
+      if (seqCanvas.cursorY < 0)\r
+        seqCanvas.cursorY = 0;\r
+      else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)\r
+        seqCanvas.cursorY = av.alignment.getHeight() - 1;\r
+\r
+\r
+      endEditing();\r
+      while (seqCanvas.cursorY < av.startSeq)\r
+      {\r
         ap.scrollUp(true);\r
-      else if(seqCanvas.cursorY+1>av.endSeq)\r
+      }\r
+      while (seqCanvas.cursorY + 1 > av.endSeq)\r
+      {\r
         ap.scrollUp(false);\r
+      }\r
+      while (seqCanvas.cursorX < av.startRes)\r
+      {\r
+        if(!ap.scrollRight(false))\r
+          break;\r
+      }\r
+      while (seqCanvas.cursorX > av.endRes)\r
+      {\r
+        if(!ap.scrollRight(true))\r
+          break;\r
+      }\r
 \r
       setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),\r
                        seqCanvas.cursorX, seqCanvas.cursorY);\r
+\r
       seqCanvas.repaint();\r
     }\r
 \r
@@ -277,7 +334,7 @@ public class SeqPanel extends JPanel implements MouseListener,
       groupEditing = group;\r
       startseq = seqCanvas.cursorY;\r
       lastres = seqCanvas.cursorX;\r
-      editSequence(true, seqCanvas.cursorX+getKeyboardGaps());\r
+      editSequence(true, seqCanvas.cursorX+getKeyboardNo(keyboardNo1));\r
       editOccurred();\r
     }\r
 \r
@@ -287,26 +344,28 @@ public class SeqPanel extends JPanel implements MouseListener,
                                                    av.alignment, HistoryItem.EDIT));\r
       groupEditing = group;\r
       startseq = seqCanvas.cursorY;\r
-      lastres = seqCanvas.cursorX+getKeyboardGaps();\r
+      lastres = seqCanvas.cursorX+getKeyboardNo(keyboardNo1);\r
       editSequence(false, seqCanvas.cursorX);\r
       editOccurred();\r
     }\r
 \r
     void numberPressed(char value)\r
     {\r
-      if(keyboardGaps==null)\r
-        keyboardGaps = new StringBuffer();\r
+      if(keyboardNo1==null)\r
+        keyboardNo1 = new StringBuffer();\r
 \r
-      keyboardGaps.append(value);\r
+      if(keyboardNo2!=null)\r
+        keyboardNo2.append(value);\r
+      else\r
+        keyboardNo1.append(value);\r
     }\r
 \r
-    StringBuffer keyboardGaps;\r
-    int getKeyboardGaps()\r
+    int getKeyboardNo(StringBuffer kb)\r
     {\r
-      if(keyboardGaps==null)\r
+      if(kb==null)\r
         return 1;\r
       else\r
-        return Integer.parseInt(keyboardGaps.toString());\r
+        return Integer.parseInt(kb.toString());\r
     }\r
 \r
 \r
@@ -558,40 +617,31 @@ public class SeqPanel extends JPanel implements MouseListener,
 \r
     }\r
 \r
-    void editSequence(boolean insertGap, int startres)\r
+    synchronized void editSequence(boolean insertGap, int startres)\r
     {\r
       int fixedLeft = -1;\r
       int fixedRight = -1;\r
       boolean fixedColumns = false;\r
+      SequenceGroup sg = av.getSelectionGroup();\r
 \r
-       if(insertGap && av.hasHiddenColumns)\r
-        {\r
-          //Stop editing if the user has dragged beyond hiddenBoundary\r
-          fixedRight = av.getColumnSelection().getHiddenRegionBoundary(lastres);\r
-          if( fixedRight < lastres)\r
-          {\r
-            if(lastres!=fixedRight)\r
-            {\r
-              endEditing();\r
-              return;\r
-            }\r
-          }\r
-        }\r
+      if(groupEditing && sg==null)\r
+        return;\r
 \r
         if (!groupEditing && av.hasHiddenRows)\r
         {\r
+          //This needs to check all the sequences in a group edit,m\r
+          // not just the startseq\r
           if (av.alignment.getSequenceAt(startseq).getHiddenSequences() != null)\r
           {\r
             groupEditing = true;\r
           }\r
         }\r
 \r
-\r
         SequenceI seq = av.alignment.getSequenceAt(startseq);\r
         StringBuffer message = new StringBuffer();\r
         if (groupEditing)\r
-          message.append("Edit group:");\r
-       else\r
+           message.append("Edit group:");\r
+        else\r
             message.append("Edit sequence: "+seq.getName());\r
 \r
        if(insertGap)\r
@@ -605,13 +655,12 @@ public class SeqPanel extends JPanel implements MouseListener,
 \r
         //Are we editing within a selection group?\r
         if (groupEditing\r
-            || (av.getSelectionGroup() != null &&\r
-                av.getSelectionGroup().sequences.contains(seq)))\r
+            || (sg != null && sg.sequences.contains(seq)))\r
         {\r
           fixedColumns = true;\r
 \r
-          fixedLeft = av.getSelectionGroup().getStartRes();\r
-          fixedRight = av.getSelectionGroup().getEndRes();\r
+          fixedLeft = sg.getStartRes();\r
+          fixedRight = sg.getEndRes();\r
 \r
           if (   (startres < fixedLeft && lastres >= fixedLeft)\r
               || (startres >= fixedLeft && lastres < fixedLeft)\r
@@ -634,17 +683,31 @@ public class SeqPanel extends JPanel implements MouseListener,
           }\r
         }\r
 \r
+\r
+\r
+        if(av.hasHiddenColumns)\r
+        {\r
+          fixedColumns = true;\r
+          int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);\r
+          int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);\r
+\r
+          if(    ( insertGap && startres>y1 && lastres<y1)\r
+              || (!insertGap && startres<y2 && lastres>y2) )\r
+          {\r
+            endEditing();\r
+            return;\r
+          }\r
+\r
+          if(fixedRight<y2 && fixedRight==-1 && y2!=startres)\r
+            fixedRight = y2 -1;\r
+          if(y1>fixedLeft && fixedLeft==-1)\r
+            fixedLeft = y1;\r
+        }\r
+\r
         if (groupEditing)\r
         {\r
-          SequenceGroup sg = av.getSelectionGroup();\r
-          if(fixedLeft>sg.getEndRes()\r
-          ||fixedRight<sg.getStartRes())\r
-       {\r
-         endEditing();\r
-         return;\r
-       }\r
 \r
-          if (av.hasHiddenRows)\r
+          /*if (av.hasHiddenRows)\r
           {\r
             //sg might be null as the user may only see 1 sequence\r
             if (sg == null)\r
@@ -669,67 +732,67 @@ public class SeqPanel extends JPanel implements MouseListener,
             }\r
 \r
             sg = tmp;\r
-          }\r
+          }*/\r
+          // int blankColumn = -1;\r
 \r
-          if (sg == null)\r
-          {\r
-            endEditing();\r
-            return;\r
-          }\r
-\r
-         // int blankColumn = -1;\r
 \r
           // drag to right\r
           if (insertGap)\r
           {\r
-            // Is it valid??\r
+              //If the user has selected the whole sequence, and is dragging to\r
+              // the right, we can still extend the alignment and selectionGroup\r
+              if(sg.getStartRes() == 0 && sg.getEndRes() + 1 == av.alignment.getWidth())\r
+              {\r
+                sg.setEndRes(av.alignment.getWidth() + startres - lastres);\r
+                fixedRight = sg.getEndRes();\r
+              }\r
+\r
+            // Is it valid with fixed columns??\r
             // Find the next gap before the end\r
             // of the visible region boundary\r
-            if (av.hasHiddenColumns)\r
+            boolean blank = false;\r
+            for (fixedRight = fixedRight;\r
+                 fixedRight > lastres;\r
+                 fixedRight--)\r
             {\r
-             // int lastCol = av.getColumnSelection().\r
-             //     getHiddenRegionBoundary(res);\r
-\r
-            }\r
-\r
-              if (fixedRight != startres && fixedRight<av.alignment.getWidth()-1)\r
+              blank = true;\r
+              for (int s = 0; s < sg.getSize(); s++)\r
               {\r
-                for (fixedRight = fixedRight;\r
-                     fixedRight > lastres;\r
-                     fixedRight --)\r
+                seq = sg.getSequenceAt(s);\r
+                for (int j = 0; j < startres - lastres; j++)\r
                 {\r
-                  boolean blank = true;\r
-                  for (int s = 0; s < sg.getSize(); s++)\r
+                  if (!jalview.util.Comparison.isGap(\r
+                      seq.getCharAt(fixedRight - j)))\r
                   {\r
-                    seq = sg.getSequenceAt(s);\r
-\r
-                    if (seq.getSequence().length() <= fixedRight)\r
-                    {\r
-                      continue;\r
-                    }\r
-\r
-                    if (!jalview.util.Comparison.isGap(\r
-                        seq.getCharAt(fixedRight)))\r
-                    {\r
-                      blank = false;\r
-                      continue;\r
-                    }\r
-                  }\r
-                  if (blank)\r
+                    blank = false;\r
                     break;\r
+                  }\r
                 }\r
               }\r
+              if (blank)\r
+                break;\r
+            }\r
 \r
-              if (fixedRight <= startres)\r
+            if (!blank)\r
+            {\r
+              if(sg.getSize() == av.alignment.getHeight())\r
+              {\r
+                //We can still insert gaps if the selectionGroup\r
+                //contains all the sequences\r
+                sg.setEndRes(sg.getEndRes()+1+startres-lastres);\r
+                fixedRight = av.alignment.getWidth()+startres-lastres;\r
+              }\r
+              else\r
               {\r
                 endEditing();\r
                 return;\r
               }\r
             }\r
+          }\r
 \r
 \r
           // drag to left\r
-          else\r
+          else if(!insertGap)\r
           {\r
             /// Are we able to delete?\r
             // ie are all columns blank?\r
@@ -793,14 +856,26 @@ public class SeqPanel extends JPanel implements MouseListener,
         }\r
         else /////Editing a single sequence///////////\r
         {\r
-\r
           if (insertGap)\r
           {\r
             // dragging to the right\r
             for (int j = lastres; j < startres; j++)\r
             {\r
               if (fixedColumns && fixedRight != -1)\r
-                insertChar(j, seq, fixedRight);\r
+              {\r
+                if (sg.getStartRes() == 0\r
+                    && sg.getEndRes() + 1 == av.alignment.getWidth()\r
+                    && !jalview.util.Comparison.isGap(seq.getCharAt(fixedRight)))\r
+                {\r
+                  //Single sequence edit, whole sequence selected,\r
+                  //extend the selection group\r
+                  sg.setEndRes(av.alignment.getWidth() -1 + startres - lastres);\r
+                  fixedColumns = false;\r
+                  insertChar(j, seq);\r
+                }\r
+                else\r
+                  insertChar(j, seq, fixedRight);\r
+              }\r
               else\r
                 insertChar(j, seq);\r
             }\r
@@ -847,7 +922,7 @@ public class SeqPanel extends JPanel implements MouseListener,
       for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)\r
       {\r
         if (jalview.util.Comparison.isGap(seq.getCharAt(blankColumn)))\r
-    {\r
+        {\r
           //Theres a space, so break and insert the gap\r
           break;\r
         }\r
@@ -1178,6 +1253,10 @@ public class SeqPanel extends JPanel implements MouseListener,
         {\r
           y = av.alignment.getHeight() -1;\r
         }\r
+        if(res> av.alignment.getWidth())\r
+        {\r
+          res = av.alignment.getWidth()-1;\r
+        }\r
 \r
         if (stretchGroup.getEndRes() == res)\r
         {\r
@@ -1263,6 +1342,27 @@ public class SeqPanel extends JPanel implements MouseListener,
         seqCanvas.repaint();\r
     }\r
 \r
+    void scrollCanvas(MouseEvent evt)\r
+    {\r
+      if(evt==null)\r
+      {\r
+        if(scrollThread!=null)\r
+        {\r
+          scrollThread.running = false;\r
+          scrollThread = null;\r
+        }\r
+        mouseDragging = false;\r
+      }\r
+      else\r
+      {\r
+        if (scrollThread == null)\r
+          scrollThread = new ScrollThread();\r
+\r
+        mouseDragging = true;\r
+        scrollThread.setEvent(evt);\r
+      }\r
+\r
+    }\r
 \r
 \r
     // this class allows scrolling off the bottom of the visible alignment\r