JAL-1264 adding show/hide annotation options to applet
[jalview.git] / src / jalview / appletgui / CutAndPasteTransfer.java
index 0c717b2..036bbe3 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.appletgui;
 
-import jalview.datamodel.Alignment;
-import jalview.datamodel.PDBEntry;
-import jalview.datamodel.Sequence;
-import jalview.io.AnnotationFile;
-import jalview.io.AppletFormatAdapter;
-import jalview.io.IdentifyFile;
-import jalview.io.NewickFile;
-import jalview.io.TCoffeeScoreFile;
-import jalview.schemes.TCoffeeColourScheme;
-import jalview.util.MessageManager;
-
 import java.awt.BorderLayout;
 import java.awt.Button;
 import java.awt.Dialog;
 import java.awt.Font;
 import java.awt.Frame;
+import java.awt.Label;
 import java.awt.Panel;
 import java.awt.TextArea;
 import java.awt.event.ActionEvent;
@@ -43,6 +33,20 @@ import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 
+import jalview.analysis.AlignmentUtils;
+import jalview.bin.JalviewLite;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.io.AnnotationFile;
+import jalview.io.AppletFormatAdapter;
+import jalview.io.IdentifyFile;
+import jalview.io.NewickFile;
+import jalview.io.TCoffeeScoreFile;
+import jalview.schemes.TCoffeeColourScheme;
+import jalview.util.MessageManager;
+
 public class CutAndPasteTransfer extends Panel implements ActionListener,
         MouseListener
 {
@@ -52,7 +56,7 @@ public class CutAndPasteTransfer extends Panel implements ActionListener,
 
   boolean annotationImport = false;
 
-  Sequence seq;
+  SequenceI seq;
 
   AlignFrame alignFrame;
 
@@ -84,7 +88,7 @@ public class CutAndPasteTransfer extends Panel implements ActionListener,
     textarea.setText(text);
   }
 
-  public void setPDBImport(Sequence seq)
+  public void setPDBImport(SequenceI seq)
   {
     this.seq = seq;
     accept.setLabel(MessageManager.getString("action.accept"));
@@ -225,7 +229,11 @@ public class CutAndPasteTransfer extends Panel implements ActionListener,
 
     if (al != null)
     {
-      al.setDataset(null);
+      al.setDataset(null); // set dataset on alignment/sequences
+      if (openSplitFrame(al, format))
+      {
+        return;
+      }
       if (newWindow)
       {
         AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
@@ -244,6 +252,69 @@ public class CutAndPasteTransfer extends Panel implements ActionListener,
   }
 
   /**
+   * Check whether the new alignment could be mapped to the current one as
+   * cDNA/protein, if so offer the option to open as split frame view. Returns
+   * true if a split frame view is opened, false if not.
+   * 
+   * @param al
+   * @return
+   */
+  protected boolean openSplitFrame(Alignment al, String format)
+  {
+    final AlignmentI thisAlignment = this.alignFrame.getAlignViewport().getAlignment();
+    if (thisAlignment.isNucleotide() == al.isNucleotide())
+    {
+      // both nucleotide or both protein
+      return false;
+    }
+    AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
+    AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
+    boolean mapped = AlignmentUtils.mapProteinToCdna(protein, dna);
+    if (!mapped)
+    {
+      return false;
+    }
+
+    /*
+     * A mapping is possible; ask user if they want a split frame.
+     */
+    String title = MessageManager.getString("label.open_split_window");
+    final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
+            true, 100, 400);
+    dialog.ok.setLabel(MessageManager.getString("action.yes"));
+    dialog.cancel.setLabel(MessageManager.getString("action.no"));
+    Panel question = new Panel(new BorderLayout());
+    final String text = MessageManager
+            .getString("label.open_split_window?");
+    question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
+    dialog.setMainPanel(question);
+    dialog.setVisible(true);
+    dialog.toFront();
+    
+    if (!dialog.accept)
+    {
+      return false;
+    }
+
+    /*
+     * Open SplitFrame with DNA above and protein below, including the alignment
+     * from textbox and a copy of the original.
+     */
+    final JalviewLite applet = this.alignFrame.viewport.applet;
+    AlignFrame copyFrame = new AlignFrame(
+            this.alignFrame.viewport.getAlignment(), applet,
+            alignFrame.getTitle(), false, false);
+    AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
+            "Cut & Paste input - " + format, false, false);
+    AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
+    AlignFrame proteinFrame = al.isNucleotide() ? copyFrame
+            : newFrame;
+    SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
+    sf.addToDisplay(false, applet);
+    return true;
+  }
+
+  /**
    * Parse the text as a TCoffee score file, if successful add scores as
    * alignment annotations.
    */
@@ -324,12 +395,12 @@ public class CutAndPasteTransfer extends Panel implements ActionListener,
 
     if (alignFrame.alignPanel.av.applet.jmolAvailable)
     {
-      new jalview.appletgui.AppletJmol(pdb, new Sequence[]
+      new jalview.appletgui.AppletJmol(pdb, new SequenceI[]
       { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
     }
     else
     {
-      new MCview.AppletPDBViewer(pdb, new Sequence[]
+      new MCview.AppletPDBViewer(pdb, new SequenceI[]
       { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
     }
   }