SequenceGroup parsing added to annotation file
[jalview.git] / src / jalview / appletgui / APopupMenu.java
index 853574b..8f1836c 100755 (executable)
@@ -26,6 +26,7 @@ import java.util.Vector;
 import jalview.analysis.*;\r
 import jalview.datamodel.*;\r
 import jalview.schemes.*;\r
+import jalview.commands.ChangeCaseCommand;\r
 \r
 public class APopupMenu\r
     extends java.awt.PopupMenu implements ActionListener, ItemListener\r
@@ -64,9 +65,15 @@ public class APopupMenu
   MenuItem pdb = new MenuItem();\r
   MenuItem hideSeqs = new MenuItem();\r
   MenuItem repGroup = new MenuItem();\r
+  MenuItem sequenceName = new MenuItem("Edit Name/Description");\r
 \r
   Sequence seq;\r
   MenuItem revealAll = new MenuItem();\r
+  Menu menu1 = new Menu();\r
+\r
+  Dialog editNameDialog;\r
+  Button okDialog = new Button("Accept");\r
+  Button cancelDialog = new Button("Cancel");;\r
 \r
   public APopupMenu(AlignmentPanel apanel, final Sequence seq, Vector links)\r
   {\r
@@ -99,7 +106,7 @@ public class APopupMenu
 \r
     SequenceGroup sg = ap.av.getSelectionGroup();\r
 \r
-    if (sg != null && sg.getSize(false)>0)\r
+    if (sg != null && sg.getSize()>0)\r
     {\r
       showText.setState(sg.getDisplayText());\r
       showColourText.setState(sg.getColourText());\r
@@ -216,6 +223,14 @@ public class APopupMenu
     else if (source == unGroupMenuItem)\r
       unGroupMenuItem_actionPerformed();\r
 \r
+    else if(source == sequenceName)\r
+      editName();\r
+    else if (source==okDialog || source==cancelDialog)\r
+    {\r
+      editNameDialog.setVisible(false);\r
+      if(source==cancelDialog)\r
+        editNameDialog = null;\r
+    }\r
     else if(source == pdb)\r
       addPDB();\r
     else if(source == hideSeqs)\r
@@ -233,43 +248,75 @@ public class APopupMenu
       ap.alignFrame.cut_actionPerformed();\r
     else if(source==toUpper || source==toLower || source==toggleCase)\r
     {\r
-      SequenceGroup sg = ap.av.getSelectionGroup();\r
-      if (sg != null)\r
+    SequenceGroup sg = ap.av.getSelectionGroup();\r
+    Vector regions = new Vector();\r
+    if (sg != null)\r
+    {\r
+      int start = sg.getStartRes();\r
+      int end = sg.getEndRes() + 1;\r
+\r
+      do\r
       {\r
-        for (int g = 0; g < sg.getSize(true); g++)\r
+        if (ap.av.hasHiddenColumns)\r
+        {\r
+          if(start==0)\r
+            start = ap.av.colSel.adjustForHiddenColumns(start);\r
+\r
+          end = ap.av.colSel.getHiddenBoundaryRight(start);\r
+          if (start == end)\r
+            end = sg.getEndRes() + 1;\r
+          if (end > sg.getEndRes())\r
+            end = sg.getEndRes() + 1;\r
+        }\r
+\r
+        regions.addElement(new int[]\r
+                           {start, end});\r
+\r
+        if (ap.av.hasHiddenColumns)\r
         {\r
-          int start = sg.getStartRes();\r
-          int end = sg.getEndRes() + 1;\r
-\r
-          do\r
-          {\r
-            if (ap.av.hasHiddenColumns)\r
-            {\r
-              end = ap.av.colSel.getHiddenBoundaryRight(start);\r
-              if (start == end)\r
-                end = sg.getEndRes() + 1;\r
-              if (end > sg.getEndRes())\r
-                end = sg.getEndRes() + 1;\r
-            }\r
-\r
-            if (source == toggleCase)\r
-              ( (SequenceI) sg.getSequences(true).elementAt(g))\r
-                  .toggleCase(start, end);\r
-            else\r
-              ( (SequenceI) sg.getSequences(true).elementAt(g))\r
-                  .changeCase(source == toUpper, start, end);\r
-\r
-            if (ap.av.hasHiddenColumns)\r
-            {\r
-              start = ap.av.colSel.adjustForHiddenColumns(end);\r
-              start = ap.av.colSel.getHiddenBoundaryLeft(start) + 1;\r
-            }\r
-\r
-          }\r
-        while (end < sg.getEndRes());\r
+          start = ap.av.colSel.adjustForHiddenColumns(end);\r
+          start = ap.av.colSel.getHiddenBoundaryLeft(start) + 1;\r
         }\r
-        ap.seqPanel.seqCanvas.repaint();\r
       }\r
+      while (end < sg.getEndRes());\r
+\r
+      int[][] startEnd = new int[regions.size()][2];\r
+      for (int i = 0; i < regions.size(); i++)\r
+      {\r
+        startEnd[i] = (int[]) regions.elementAt(i);\r
+      }\r
+\r
+\r
+      String description;\r
+      int caseChange;\r
+\r
+      if(source==toggleCase)\r
+      {\r
+        description = "Toggle Case";\r
+        caseChange = ChangeCaseCommand.TOGGLE_CASE;\r
+      }\r
+      else if(source==toUpper)\r
+      {\r
+        description = "To Upper Case";\r
+        caseChange = ChangeCaseCommand.TO_UPPER;\r
+      }\r
+      else\r
+      {\r
+        description = "To Lower Case";\r
+        caseChange = ChangeCaseCommand.TO_LOWER;\r
+      }\r
+\r
+      ChangeCaseCommand caseCommand = new ChangeCaseCommand(\r
+          description, sg.getSequencesAsArray(ap.av.hiddenRepSequences), startEnd, caseChange\r
+          );\r
+\r
+      ap.alignFrame.addHistoryItem(caseCommand);\r
+\r
+\r
+      ap.av.firePropertyChange("alignment", null,\r
+                                ap.av.getAlignment().getSequences());\r
+\r
+    }\r
     }\r
     else\r
       outputText(evt);\r
@@ -278,23 +325,7 @@ public class APopupMenu
 \r
   void outputText(ActionEvent e)\r
   {\r
-    CutAndPasteTransfer cap = new CutAndPasteTransfer(false, ap.alignFrame);\r
-    Vector vseqs = new Vector();\r
-\r
-      String [] selection = ap.av.getViewAsString(true);\r
-      SequenceI [] seqs = ap.av.getSelectionAsNewSequence();\r
-      if (selection != null)\r
-      {\r
-        for (int i = 0; i < selection.length; i++)\r
-        {\r
-          Sequence seq = new Sequence(\r
-              seqs[i].getName(),\r
-              selection[i],\r
-              seqs[i].getStart(), seqs[i].getEnd());\r
-          seq.setDescription(seqs[i].getDescription());\r
-          vseqs.addElement( seq );\r
-      }\r
-    }\r
+    CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);\r
 \r
     Frame frame = new Frame();\r
     frame.add(cap);\r
@@ -304,11 +335,54 @@ public class APopupMenu
 \r
     cap.setText(new jalview.io.AppletFormatAdapter().formatSequences(\r
         e.getActionCommand(),\r
-        vseqs,\r
+      new Alignment( ap.av.getSelectionAsNewSequence() ),\r
         ap.av.showJVSuffix));\r
 \r
   }\r
 \r
+  void editName()\r
+  {\r
+    TextField id = new TextField(seq.getName(), 40);\r
+    TextField description = new TextField(seq.getDescription(), 40);\r
+    Panel panel = new Panel(new BorderLayout());\r
+    Panel panel2 = new Panel(new BorderLayout());\r
+    panel2.add(new Label("       Sequence Name "), BorderLayout.WEST);\r
+    panel2.add(id, BorderLayout.CENTER);\r
+    panel.add(panel2, BorderLayout.NORTH);\r
+    panel2 = new Panel(new BorderLayout());\r
+    panel2.add(new Label("Sequence Description "), BorderLayout.WEST);\r
+    panel2.add(description, BorderLayout.CENTER);\r
+    panel.add(panel2, BorderLayout.CENTER);\r
+\r
+    panel2 = new Panel(new FlowLayout());\r
+\r
+    panel2.add(okDialog);\r
+    panel2.add(cancelDialog);\r
+\r
+    panel.add(panel2, BorderLayout.SOUTH);\r
+\r
+    editNameDialog = new Dialog(ap.alignFrame,\r
+      "Edit Sequence Name / Description",\r
+      true);\r
+\r
+    editNameDialog.add(panel, BorderLayout.NORTH);\r
+\r
+    editNameDialog.setBounds(ap.alignFrame.getBounds().x\r
+                             +(ap.alignFrame.getSize().width-500)/2 ,\r
+                             ap.alignFrame.getBounds().y\r
+                             +(ap.alignFrame.getSize().height-120)/2,\r
+                             500, 120);\r
+\r
+    editNameDialog.show();\r
+\r
+    if (editNameDialog != null)\r
+    {\r
+      seq.setName(id.getText());\r
+      seq.setDescription(description.getText());\r
+      ap.repaint();\r
+    }\r
+  }\r
+\r
   void addPDB()\r
   {\r
     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);\r
@@ -336,6 +410,7 @@ public class APopupMenu
     showBoxes.setLabel("Boxes");\r
     showBoxes.setState(true);\r
     showBoxes.addItemListener(this);\r
+    sequenceName.addActionListener(this);\r
 \r
     showText.setLabel("Text");\r
     showText.addItemListener(this);\r
@@ -347,19 +422,14 @@ public class APopupMenu
     hideSeqs.setLabel("Hide Sequences");\r
     repGroup.setLabel("Represent Group with");\r
     revealAll.setLabel("Reveal All");\r
-\r
+    menu1.setLabel("Group");\r
     add(groupMenu);\r
     this.add(seqMenu);\r
     this.add(hideSeqs);\r
     this.add(revealAll);\r
     groupMenu.add(editMenu);\r
     groupMenu.add(outputmenu);\r
-    groupMenu.addSeparator();\r
-    groupMenu.add(unGroupMenuItem);\r
-    groupMenu.add(colourMenu);\r
-    groupMenu.add(showBoxes);\r
-    groupMenu.add(showText);\r
-    groupMenu.add(showColourText);\r
+    groupMenu.add(menu1);\r
     colourMenu.add(noColourmenuItem);\r
     colourMenu.add(clustalColour);\r
     colourMenu.add(BLOSUM62Colour);\r
@@ -415,13 +485,21 @@ public class APopupMenu
     editMenu.add(toLower);\r
     toLower.addActionListener(this);\r
     editMenu.add(toggleCase);\r
+    seqMenu.add(sequenceName);\r
     seqMenu.add(pdb);\r
     seqMenu.add(repGroup);\r
+    menu1.add(unGroupMenuItem);\r
+    menu1.add(colourMenu);\r
+    menu1.add(showBoxes);\r
+    menu1.add(showText);\r
+    menu1.add(showColourText);\r
     toggleCase.addActionListener(this);\r
     pdb.addActionListener(this);\r
     hideSeqs.addActionListener(this);\r
     repGroup.addActionListener(this);\r
     revealAll.addActionListener(this);\r
+    okDialog.addActionListener(this);\r
+    cancelDialog.addActionListener(this);\r
 \r
   }\r
 \r
@@ -435,7 +513,8 @@ public class APopupMenu
   protected void clustalColour_actionPerformed()\r
   {\r
     SequenceGroup sg = getGroup();\r
-    sg.cs = new ClustalxColourScheme(sg.getSequences(true), ap.av.alignment.getWidth());\r
+    sg.cs = new ClustalxColourScheme(sg.getSequences(ap.av.hiddenRepSequences),\r
+                                     ap.av.alignment.getWidth());\r
     refresh();\r
   }\r
 \r
@@ -495,7 +574,7 @@ public class APopupMenu
 \r
     if (abovePIDColour.getState())\r
     {\r
-      sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(true), 0,\r
+      sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av.hiddenRepSequences), 0,\r
                                                ap.av.alignment.getWidth()));\r
       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs,\r
           getGroup().getName());\r
@@ -523,7 +602,7 @@ public class APopupMenu
   {\r
     SequenceGroup sg = getGroup();\r
     sg.cs = new PIDColourScheme();\r
-    sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(true), 0,\r
+    sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av.hiddenRepSequences), 0,\r
                                              ap.av.alignment.getWidth()));\r
     refresh();\r
   }\r
@@ -534,7 +613,7 @@ public class APopupMenu
 \r
     sg.cs = new Blosum62ColourScheme();\r
 \r
-    sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(true), 0,\r
+    sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av.hiddenRepSequences), 0,\r
                                              ap.av.alignment.getWidth()));\r
 \r
     refresh();\r
@@ -557,7 +636,7 @@ public class APopupMenu
 \r
       Conservation c = new Conservation("Group",\r
                                         ResidueProperties.propHash, 3,\r
-                                        sg.getSequences(true), 0,\r
+                                        sg.getSequences(ap.av.hiddenRepSequences), 0,\r
                                         ap.av.alignment.getWidth());\r
 \r
       c.calculate();\r
@@ -617,34 +696,33 @@ public class APopupMenu
   void hideSequences(boolean representGroup)\r
   {\r
     SequenceGroup sg = ap.av.getSelectionGroup();\r
-    if(sg==null || sg.getSize(false)<1)\r
+    if(sg==null || sg.getSize()<1)\r
     {\r
       ap.av.hideSequence(new SequenceI[]{seq});\r
       return;\r
     }\r
 \r
-    int gsize = sg.getSize(false);\r
-    SequenceI [] hseqs;\r
+    ap.av.setSelectionGroup(null);\r
 \r
-    hseqs = new SequenceI[ representGroup ? gsize-1 : gsize ];\r
+    if (representGroup)\r
+    {\r
+      ap.av.hideRepSequences(seq, sg);\r
 \r
-      int index = 0;\r
-      for(int i=0; i<gsize; i++)\r
-      {\r
-        if(representGroup && sg.getSequenceAt(i)!=seq)\r
-        {\r
-          seq.addHiddenSequence(sg.getSequenceAt(i));\r
-          hseqs[index++] = sg.getSequenceAt(i);\r
-        }\r
-        else if(!representGroup)\r
-        {\r
-          hseqs[index++] = sg.getSequenceAt(i);\r
-        }\r
-        }\r
+      return;\r
+    }\r
+\r
+    int gsize = sg.getSize();\r
+    SequenceI[] hseqs;\r
 \r
-      ap.av.hideSequence(hseqs);\r
+    hseqs = new SequenceI[gsize];\r
+\r
+    int index = 0;\r
+    for (int i = 0; i < gsize; i++)\r
+    {\r
+      hseqs[index++] = sg.getSequenceAt(i);\r
+    }\r
 \r
-      ap.av.setSelectionGroup(null);\r
+    ap.av.hideSequence(hseqs);\r
     }\r
 \r
 }\r