JAL-845 linked protein/dna 'slave' further PoC functionality
[jalview.git] / src / jalview / gui / AlignViewport.java
index 705f53a..f00e4d3 100644 (file)
@@ -45,7 +45,7 @@ import jalview.analysis.NJTree;
 import jalview.api.AlignViewportI;
 import jalview.bin.Cache;
 import jalview.commands.CommandI;
-import jalview.commands.EditCommand;
+import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.PDBEntry;
@@ -70,13 +70,11 @@ import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Deque;
 import java.util.Hashtable;
-import java.util.List;
 import java.util.Set;
 import java.util.Vector;
 
 import javax.swing.JInternalFrame;
 import javax.swing.JOptionPane;
-import javax.swing.JSplitPane;
 
 /**
  * DOCUMENT ME!
@@ -1115,6 +1113,9 @@ public class AlignViewport extends AlignmentViewport implements
 
   boolean showSeqFeaturesHeight;
 
+  /**
+   * Send the current selection to be broadcast to any selection listeners.
+   */
   public void sendSelection()
   {
     jalview.structure.StructureSelectionManager
@@ -1325,23 +1326,30 @@ public class AlignViewport extends AlignmentViewport implements
    */
   @Override
   public void mirrorCommand(CommandI command, boolean undo,
-          StructureSelectionManager ssm)
+          StructureSelectionManager ssm, VamsasSource source)
   {
     /*
-     * Only EditCommand is currently handled by listeners.
+     * ...work in progress... do nothing unless we are a 'slave' of the source
+     * May replace this with direct calls not via SSM.
      */
-    if (!(command instanceof EditCommand))
+    if (source instanceof AlignViewportI
+            && ((AlignViewportI) source).getSlave() == this)
+    {
+      // ok to continue;
+    }
+    else
     {
       return;
     }
-    EditCommand edit = (EditCommand) command;
 
-    List<SequenceI> seqs = getAlignment().getSequences();
-    EditCommand mappedCommand = ssm.mapEditCommand(edit, undo, seqs,
+    CommandI mappedCommand = ssm.mapCommand(command, undo, getAlignment(),
             getGapCharacter());
-    AlignmentI[] views = getAlignPanel().alignFrame.getViewAlignments();
-    mappedCommand.performEdit(0, views);
-    getAlignPanel().alignmentChanged();
+    if (mappedCommand != null)
+    {
+      AlignmentI[] views = getAlignPanel().alignFrame.getViewAlignments();
+      mappedCommand.doCommand(views);
+      getAlignPanel().alignmentChanged();
+    }
   }
 
   @Override
@@ -1416,8 +1424,8 @@ public class AlignViewport extends AlignmentViewport implements
 
   /**
    * Add the sequences from the given alignment to this viewport. Optionally,
-   * may give the user the option to open a new frame or panel linking cDNA and
-   * protein.
+   * may give the user the option to open a new frame, or split panel, with cDNA
+   * and protein linked.
    * 
    * @param al
    * @param title
@@ -1459,9 +1467,8 @@ public class AlignViewport extends AlignmentViewport implements
 
   /**
    * Show a dialog with the option to open and link (cDNA <-> protein) as a new
-   * alignment. Returns true if the new alignment was opened, false if not -
-   * either because the user declined the offer, or because no mapping could be
-   * made.
+   * alignment. Returns true if the new alignment was opened, false if not,
+   * because the user declined the offer.
    * 
    * @param title
    */
@@ -1477,11 +1484,6 @@ public class AlignViewport extends AlignmentViewport implements
             MessageManager.getString("label.open_linked_alignment"),
             JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
             options, options[0]);
-    // int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
-    // question,
-    // MessageManager.getString("label.open_linked_alignment"),
-    // JOptionPane.YES_NO_OPTION,
-    // JOptionPane.QUESTION_MESSAGE);
 
     if (response != 1 && response != 2)
     {
@@ -1494,13 +1496,19 @@ public class AlignViewport extends AlignmentViewport implements
      * Create the AlignFrame first (which creates the new alignment's datasets),
      * before attempting sequence mapping.
      */
-    AlignFrame alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
+    AlignFrame newAlignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
             AlignFrame.DEFAULT_HEIGHT);
 
-    final AlignmentI protein = al.isNucleotide() ? getAlignment() : al;
-    final AlignmentI cdna = al.isNucleotide() ? al : getAlignment();
+    /*
+     * Identify protein and dna alignments. Make a copy of this one if opening
+     * in a new split pane.
+     */
+    AlignmentI thisAlignment = openSplitPane ? new Alignment(getAlignment())
+            : getAlignment();
+    final AlignmentI protein = al.isNucleotide() ? thisAlignment : al;
+    final AlignmentI cdna = al.isNucleotide() ? al : thisAlignment;
 
-    alignFrame.statusBar.setText(MessageManager.formatMessage(
+    newAlignFrame.statusBar.setText(MessageManager.formatMessage(
             "label.successfully_loaded_file", new Object[]
             { title }));
 
@@ -1510,12 +1518,11 @@ public class AlignViewport extends AlignmentViewport implements
     // {
     // alignFrame.setFileName(file, format);
     // }
+
     if (openInNewWindow)
     {
-      /*
-       * open in new window
-       */
-      Desktop.addInternalFrame(alignFrame, title, AlignFrame.DEFAULT_WIDTH,
+      Desktop.addInternalFrame(newAlignFrame, title,
+              AlignFrame.DEFAULT_WIDTH,
               AlignFrame.DEFAULT_HEIGHT);
     }
 
@@ -1523,6 +1530,8 @@ public class AlignViewport extends AlignmentViewport implements
      * Try to find mappings for at least one sequence.
      */
     MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, cdna);
+    final StructureSelectionManager ssm = StructureSelectionManager
+            .getStructureSelectionManager(Desktop.instance);
     if (mapped == MappingResult.Mapped)
     {
 
@@ -1530,15 +1539,7 @@ public class AlignViewport extends AlignmentViewport implements
        * Register the mappings (held on the protein alignment) with the
        * StructureSelectionManager (for mouseover linking).
        */
-      final StructureSelectionManager ssm = StructureSelectionManager
-              .getStructureSelectionManager(Desktop.instance);
       ssm.addMappings(protein.getCodonFrames());
-
-      /*
-       * Set the cDNA to listen for edits on the protein.
-       */
-      ssm.addCommandListener(al.isNucleotide() ? alignFrame.getViewport()
-              : this);
     }
     else
     {
@@ -1555,7 +1556,8 @@ public class AlignViewport extends AlignmentViewport implements
 
     try
     {
-      alignFrame.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
+      newAlignFrame.setMaximum(jalview.bin.Cache.getDefault(
+              "SHOW_FULLSCREEN",
               false));
     } catch (java.beans.PropertyVetoException ex)
     {
@@ -1564,22 +1566,32 @@ public class AlignViewport extends AlignmentViewport implements
     if (openSplitPane)
     {
       /*
-       * Open in split pane. Original sequence above, new one below.
+       * Open in split pane. DNA sequence above, protein below.
        */
-      JInternalFrame splitFrame = new JInternalFrame();
-      splitFrame.setSize(AlignFrame.DEFAULT_WIDTH,
-              AlignFrame.DEFAULT_HEIGHT);
-      // TODO not quite right to 'move' AlignPanel from 'this' to the split
-      // pane
-      // TODO probably want linked editing set up here
-      JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
-              getAlignPanel(), alignFrame.alignPanel);
-      splitPane.setDividerLocation(0.5d);
-      splitFrame.setSize(AlignFrame.DEFAULT_WIDTH,
-              AlignFrame.DEFAULT_HEIGHT);
-      splitFrame.add(splitPane);
+      AlignFrame copyMe = new AlignFrame(thisAlignment,
+              AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+      copyMe.setTitle(""); // TODO would like this AlignFrame.title here
+      final AlignFrame proteinFrame = al.isNucleotide() ? copyMe
+              : newAlignFrame;
+      final AlignFrame cdnaFrame = al.isNucleotide() ? newAlignFrame
+              : copyMe;
+      newAlignFrame.setTitle(title);
+
+      cdnaFrame.setVisible(true);
+      proteinFrame.setVisible(true);
+      JInternalFrame splitFrame = new SplitFrame(cdnaFrame, proteinFrame);
       Desktop.addInternalFrame(splitFrame, title, AlignFrame.DEFAULT_WIDTH,
               AlignFrame.DEFAULT_HEIGHT);
+
+      /*
+       * Set the cDNA to list for edits on the protein.
+       */
+      ssm.addCommandListener(cdnaFrame.getViewport());
+
+      /*
+       * cDNA is 'slaved' to edits, selection, sorting, show/hide on protein
+       */
+      proteinFrame.getViewport().setSlave(cdnaFrame.getViewport());
     }
 
     return true;