Cut and paste implemented
authoramwaterhouse <Andrew Waterhouse>
Thu, 10 Feb 2005 17:52:35 +0000 (17:52 +0000)
committeramwaterhouse <Andrew Waterhouse>
Thu, 10 Feb 2005 17:52:35 +0000 (17:52 +0000)
src/jalview/gui/AlignFrame.java
src/jalview/jbgui/GAlignFrame.java

index f3d0d99..83f7ba0 100755 (executable)
@@ -23,7 +23,8 @@ import java.awt.print.*;
 import javax.swing.*;\r
 import javax.swing.event.*;\r
 import java.util.*;\r
-import jalview.datamodel.SequenceI;\r
+import  java.awt.datatransfer.*;\r
+\r
 \r
 public class AlignFrame extends GAlignFrame\r
 {\r
@@ -38,7 +39,20 @@ public class AlignFrame extends GAlignFrame
     fontNameMenuItem.setText(viewport.getFont().getName());\r
     fontSizeMenuItem.setText(viewport.getFont().getSize()+"");\r
 \r
-    AlignmentUtil.fetchSequenceFeatures(al, alignPanel);\r
+    addInternalFrameListener(new InternalFrameAdapter()\r
+   {\r
+     public void internalFrameActivated(InternalFrameEvent evt)\r
+     {\r
+          javax.swing.SwingUtilities.invokeLater(new Runnable()\r
+          {\r
+            public void run()\r
+            {      alignPanel.requestFocus();    }\r
+          });\r
+\r
+     }\r
+   });\r
+\r
+   AlignmentUtil.fetchSequenceFeatures(al, alignPanel);\r
   }\r
 \r
   protected void saveAs_actionPerformed(ActionEvent e)\r
@@ -183,6 +197,110 @@ public class AlignFrame extends GAlignFrame
       alignPanel.RefreshPanels();\r
   }\r
 \r
+  protected void copy_actionPerformed(ActionEvent e)\r
+  {\r
+   if(viewport.getRubberbandGroup()==null)\r
+     return;\r
+\r
+   SequenceGroup sg = viewport.getRubberbandGroup();\r
+\r
+     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();\r
+     StringBuffer buffer= new StringBuffer();\r
+\r
+       for(int i=0; i<sg.getSize(); i++)\r
+       {\r
+         SequenceI seq = sg.getSequenceAt(i);\r
+         buffer.append( seq.getName()+"\t"+seq.getStart() +"\t"+seq.getEnd()+"\t"\r
+                        +sg.getSequenceAt(i).getSequence(sg.getStartRes(), sg.getEndRes()+1)+"\n");\r
+       }\r
+     c.setContents( new StringSelection( buffer.toString()) , null ) ;\r
+\r
+  }\r
+\r
+\r
+  protected void pasteNew_actionPerformed(ActionEvent e)\r
+  {\r
+    paste(true);\r
+  }\r
+\r
+  protected void pasteThis_actionPerformed(ActionEvent e)\r
+  {\r
+    paste(false);\r
+  }\r
+\r
+  void paste(boolean newAlignment)\r
+  {\r
+    try{\r
+      Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();\r
+      Transferable contents = c.getContents(this);\r
+      if (contents == null)\r
+        return;\r
+\r
+      String str = (String) contents.getTransferData(DataFlavor.stringFlavor);\r
+      StringTokenizer st = new StringTokenizer(str);\r
+      ArrayList seqs = new ArrayList();\r
+      while (st.hasMoreElements())\r
+      {\r
+        String name = st.nextToken();\r
+        int start = Integer.parseInt(st.nextToken());\r
+        int end = Integer.parseInt(st.nextToken());\r
+\r
+        Sequence sequence = new Sequence(name, st.nextToken(), start, end);\r
+        if(!newAlignment)\r
+          viewport.alignment.addSequence(sequence);\r
+        else\r
+          seqs.add(sequence);\r
+      }\r
+\r
+      if(newAlignment)\r
+      {\r
+        SequenceI[] newSeqs = new SequenceI[seqs.size()];\r
+        seqs.toArray(newSeqs);\r
+        AlignFrame af = new AlignFrame(new Alignment(newSeqs));\r
+        int newHeight = newSeqs.length * af.viewport.getCharHeight() + 200;\r
+        if (newHeight > 500)\r
+          newHeight = 500;\r
+        Desktop.addInternalFrame(af, "Copied sequences", 700, newHeight);\r
+      }\r
+      else\r
+      {\r
+        viewport.setEndSeq(viewport.alignment.getHeight());\r
+        viewport.alignment.getWidth();\r
+        alignPanel.RefreshPanels();\r
+      }\r
+\r
+    }catch(Exception ex){}// could be anything being pasted in here\r
+\r
+  }\r
+\r
+\r
+  protected void cut_actionPerformed(ActionEvent e)\r
+  {\r
+    copy_actionPerformed(null);\r
+    delete_actionPerformed(null);\r
+  }\r
+\r
+  protected void delete_actionPerformed(ActionEvent e)\r
+  {\r
+    if (viewport.getRubberbandGroup() == null)\r
+      return;\r
+\r
+     SequenceGroup sg = viewport.getRubberbandGroup();\r
+     for (int i=0;i < sg.sequences.size(); i++)\r
+             viewport.getAlignment().deleteSequence(sg.getSequenceAt(i));\r
+\r
+     viewport.alignment.deleteGroup(sg);\r
+     viewport.resetSeqLimits( alignPanel.seqPanel.seqCanvas.getHeight());\r
+     if(viewport.getAlignment().getHeight()<1)\r
+     try\r
+     {\r
+       this.setClosed(true);\r
+     }catch(Exception ex){}\r
+     alignPanel.RefreshPanels();\r
+\r
+  }\r
+\r
+\r
 \r
   protected void redoMenuItem_actionPerformed(ActionEvent e)\r
   {\r
@@ -904,13 +1022,4 @@ public class AlignFrame extends GAlignFrame
          g.drawString("Clustal Alignment Web Service running", 30,30);\r
        }\r
   }\r
-\r
-\r
-  public void doKeyPressed(KeyEvent evt)\r
-  {\r
-    if(evt.isControlDown() && evt.getKeyChar()=='f')\r
-      findMenuItem_actionPerformed(null);\r
-  }\r
-\r
-\r
 }\r
index 764f5cf..5c6494e 100755 (executable)
@@ -83,6 +83,12 @@ public class GAlignFrame extends JInternalFrame
   protected JCheckBoxMenuItem sequenceFeatures = new JCheckBoxMenuItem();\r
   protected JRadioButtonMenuItem nucleotideColour = new JRadioButtonMenuItem();\r
   JMenuItem deleteGroups = new JMenuItem();\r
+  JMenuItem delete = new JMenuItem();\r
+  JMenuItem copy = new JMenuItem();\r
+  JMenuItem cut = new JMenuItem();\r
+  JMenu jMenu1 = new JMenu();\r
+  JMenuItem pasteNew = new JMenuItem();\r
+  JMenuItem pasteThis = new JMenuItem();\r
   public GAlignFrame()\r
   {\r
 \r
@@ -619,6 +625,52 @@ public class GAlignFrame extends JInternalFrame
         deleteGroups_actionPerformed(e);\r
       }\r
     });\r
+    copy.setText("Copy");\r
+    copy.setMnemonic(KeyEvent.VK_C);\r
+\r
+    copy.addActionListener(new java.awt.event.ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent e)\r
+      {\r
+        copy_actionPerformed(e);\r
+      }\r
+    });\r
+   // pasteNew.setText("Paste (as new alignment)");\r
+    cut.setText("Cut");\r
+    cut.setMnemonic(KeyEvent.VK_X);\r
+    cut.addActionListener(new java.awt.event.ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent e)\r
+      {\r
+        cut_actionPerformed(e);\r
+      }\r
+    });\r
+    delete.setText("Delete");\r
+    delete.addActionListener(new java.awt.event.ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent e)\r
+      {\r
+        delete_actionPerformed(e);\r
+      }\r
+    });\r
+    jMenu1.setMnemonic('V');\r
+    jMenu1.setText("Paste");\r
+    pasteNew.setText("as new alignment");\r
+    pasteNew.addActionListener(new java.awt.event.ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent e)\r
+      {\r
+        pasteNew_actionPerformed(e);\r
+      }\r
+    });\r
+    pasteThis.setText("add to this alignment");\r
+    pasteThis.addActionListener(new java.awt.event.ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent e)\r
+      {\r
+        pasteThis_actionPerformed(e);\r
+      }\r
+    });\r
     alignFrameMenuBar.add(fileMenu);\r
     alignFrameMenuBar.add(editMenu);\r
     alignFrameMenuBar.add(searchMenu);\r
@@ -633,6 +685,11 @@ public class GAlignFrame extends JInternalFrame
     fileMenu.add(printMenuItem);\r
     fileMenu.addSeparator();\r
     fileMenu.add(closeMenuItem);\r
+    editMenu.add(copy);\r
+    editMenu.add(jMenu1);\r
+    editMenu.add(cut);\r
+    editMenu.add(delete);\r
+    editMenu.addSeparator();\r
     editMenu.add(undoMenuItem);\r
     editMenu.add(redoMenuItem);\r
     editMenu.addSeparator();\r
@@ -693,6 +750,8 @@ public class GAlignFrame extends JInternalFrame
     alignMenu.add(clustalAlignMenuItem);\r
     alignMenu.add(pdbTest);\r
     this.getContentPane().add(statusBar, BorderLayout.SOUTH);\r
+    jMenu1.add(pasteNew);\r
+    jMenu1.add(pasteThis);\r
   }\r
 \r
   protected void saveAs_actionPerformed(ActionEvent e)\r
@@ -1007,5 +1066,32 @@ public class GAlignFrame extends JInternalFrame
 \r
   }\r
 \r
+ protected void copy_actionPerformed(ActionEvent e)\r
+  {\r
+\r
+  }\r
+\r
+\r
+  protected void cut_actionPerformed(ActionEvent e)\r
+  {\r
+\r
+  }\r
+\r
+  protected void delete_actionPerformed(ActionEvent e)\r
+  {\r
+\r
+  }\r
+\r
+  protected void pasteNew_actionPerformed(ActionEvent e)\r
+  {\r
+\r
+  }\r
+\r
+  protected void pasteThis_actionPerformed(ActionEvent e)\r
+  {\r
+\r
+  }\r
+\r
+\r
 \r
 }\r