JAL-1619 split frame now offered in applet 'input from textbox'
[jalview.git] / src / jalview / appletgui / CutAndPasteTransfer.java
index 0c717b2..248153f 100644 (file)
  */
 package jalview.appletgui;
 
+import jalview.analysis.AlignmentUtils;
+import jalview.analysis.AlignmentUtils.MappingResult;
+import jalview.bin.JalviewLite;
 import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.Sequence;
 import jalview.io.AnnotationFile;
@@ -36,6 +40,7 @@ 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;
@@ -225,7 +230,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 +253,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;
+    MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, dna);
+    if (mapped == MappingResult.NotMapped)
+    {
+      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.
    */