Merge commit
[jalview.git] / src / jalview / gui / Desktop.java
index 007fefd..70c8355 100644 (file)
@@ -26,6 +26,7 @@ import jalview.io.FormatAdapter;
 import jalview.io.IdentifyFile;
 import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
+import jalview.jbgui.GStructureViewer;
 import jalview.structure.StructureSelectionManager;
 import jalview.util.ImageMaker;
 import jalview.util.MessageManager;
@@ -113,6 +114,8 @@ public class Desktop extends jalview.jbgui.GDesktop implements
    */
   private BlogReader jvnews = null;
 
+  private File projectFile;
+
   /**
    * @param listener
    * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.beans.PropertyChangeListener)
@@ -631,38 +634,85 @@ public class Desktop extends jalview.jbgui.GDesktop implements
    * Adds and opens the given frame to the desktop
    * 
    * @param frame
-   *          DOCUMENT ME!
+   *          Frame to show
    * @param title
-   *          DOCUMENT ME!
+   *          Visible Title
    * @param w
-   *          DOCUMENT ME!
+   *          width
    * @param h
-   *          DOCUMENT ME!
+   *          height
    */
   public static synchronized void addInternalFrame(
           final JInternalFrame frame, String title, int w, int h)
   {
-    addInternalFrame(frame, title, w, h, true);
+    addInternalFrame(frame, title, true, w, h, true);
   }
 
+
   /**
-   * DOCUMENT ME!
+   * Add an internal frame to the Jalview desktop
    * 
    * @param frame
-   *          DOCUMENT ME!
+   *          Frame to show
    * @param title
-   *          DOCUMENT ME!
+   *          Visible Title
+   * @param makeVisible
+   *          When true, display frame immediately, otherwise, caller must call
+   *          setVisible themselves.
    * @param w
-   *          DOCUMENT ME!
+   *          width
    * @param h
-   *          DOCUMENT ME!
+   *          height
+   */
+  public static synchronized void addInternalFrame(
+          final JInternalFrame frame, String title, boolean makeVisible,
+          int w, int h)
+  {
+    addInternalFrame(frame, title, makeVisible, w, h, true);
+  }
+
+  /**
+   * Add an internal frame to the Jalview desktop and make it visible
+   * 
+   * @param frame
+   *          Frame to show
+   * @param title
+   *          Visible Title
+   * @param w
+   *          width
+   * @param h
+   *          height
    * @param resizable
-   *          DOCUMENT ME!
+   *          Allow resize
    */
   public static synchronized void addInternalFrame(
           final JInternalFrame frame, String title, int w, int h,
           boolean resizable)
   {
+    addInternalFrame(frame, title, true, w, h, resizable);
+  }
+
+  /**
+   * Add an internal frame to the Jalview desktop
+   * 
+   * @param frame
+   *          Frame to show
+   * @param title
+   *          Visible Title
+   * @param makeVisible
+   *          When true, display frame immediately, otherwise, caller must call
+   *          setVisible themselves.
+   * @param w
+   *          width
+   * @param h
+   *          height
+   * @param resizable
+   *          Allow resize
+   */
+  public static synchronized void addInternalFrame(
+          final JInternalFrame frame, String title, boolean makeVisible,
+          int w, int h, boolean resizable)
+  {
 
     // TODO: allow callers to determine X and Y position of frame (eg. via
     // bounds object).
@@ -687,7 +737,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
     openFrameCount++;
 
-    frame.setVisible(true);
+    frame.setVisible(makeVisible);
     frame.setClosable(true);
     frame.setResizable(resizable);
     frame.setMaximizable(resizable);
@@ -1064,7 +1114,9 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   {
     CutAndPasteTransfer cap = new CutAndPasteTransfer();
     cap.setForInput(viewport);
-    Desktop.addInternalFrame(cap, MessageManager.getString("label.cut_paste_alignmen_file"), 600, 500);
+    Desktop.addInternalFrame(cap,
+            MessageManager.getString("label.cut_paste_alignmen_file"),
+            true, 600, 500);
   }
 
   /*
@@ -1413,6 +1465,11 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     {
       final Desktop me = this;
       final java.io.File choice = chooser.getSelectedFile();
+      setProjectFile(choice);
+
+      // TODO or move inside the new Thread?
+      saveChimeraSessions(choice.getAbsolutePath());
+
       new Thread(new Runnable()
       {
         public void run()
@@ -1426,7 +1483,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
           // TODO prevent user from messing with the Desktop whilst we're saving
           try
           {
-            new Jalview2XML().SaveState(choice);
+            new Jalview2XML().saveState(choice);
           } catch (OutOfMemoryError oom)
           {
             new OOMWarning("Whilst saving current state to "
@@ -1449,6 +1506,42 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   }
 
   /**
+   * Request any open, linked Chimera sessions to save their state.
+   * 
+   * @param jalviewProjectFilename
+   *          the filename of the Jalview project; Chimera session files should
+   *          be given distinct, but obviously related, names.
+   */
+  public void saveChimeraSessions(String jalviewProjectFilename)
+  {
+    int i = 0;
+    for (JInternalFrame frame : getAllFrames())
+    {
+      if (frame instanceof ChimeraViewFrame)
+      {
+        /*
+         * Construct a filename for the Chimera session by append _chimera<n>.py
+         * to the Jalview project file name.
+         */
+        String chimeraPath = jalviewProjectFilename + "_chimera_" + i
+                + ".py";
+        ((ChimeraViewFrame) frame).saveSession(chimeraPath);
+        i++;
+      }
+    }
+  }
+
+  private void setProjectFile(File choice)
+  {
+    this.projectFile = choice;
+  }
+
+  public File getProjectFile()
+  {
+    return this.projectFile;
+  }
+
+  /**
    * DOCUMENT ME!
    * 
    * @param e
@@ -1468,9 +1561,10 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
     if (value == JalviewFileChooser.APPROVE_OPTION)
     {
-      final String choice = chooser.getSelectedFile().getAbsolutePath();
-      jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
-              .getSelectedFile().getParent());
+      final File selectedFile = chooser.getSelectedFile();
+      setProjectFile(selectedFile);
+      final String choice = selectedFile.getAbsolutePath();
+      jalview.bin.Cache.setProperty("LAST_DIRECTORY", selectedFile.getParent());
       new Thread(new Runnable()
       {
         public void run()
@@ -1479,7 +1573,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
                   choice.hashCode());
           try
           {
-            new Jalview2XML().LoadJalviewAlign(choice);
+            new Jalview2XML().loadJalviewAlign(choice);
           } catch (OutOfMemoryError oom)
           {
             new OOMWarning("Whilst loading project from " + choice, oom);
@@ -2263,7 +2357,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     return afs;
   }
 
-  public AppJmol[] getJmols()
+  public GStructureViewer[] getJmols()
   {
     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
 
@@ -2279,7 +2373,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
       {
         if (frames[i] instanceof AppJmol)
         {
-          AppJmol af = (AppJmol) frames[i];
+          GStructureViewer af = (GStructureViewer) frames[i];
           avp.addElement(af);
         }
       }
@@ -2291,10 +2385,10 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     {
       return null;
     }
-    AppJmol afs[] = new AppJmol[avp.size()];
+    GStructureViewer afs[] = new GStructureViewer[avp.size()];
     for (int i = 0, j = avp.size(); i < j; i++)
     {
-      afs[i] = (AppJmol) avp.elementAt(i);
+      afs[i] = (GStructureViewer) avp.elementAt(i);
     }
     avp.clear();
     return afs;