merge commit
[jalview.git] / src / ext / edu / ucsf / rbvi / strucviz2 / ChimeraManager.java
index c0b3a39..a7440e7 100644 (file)
@@ -152,6 +152,20 @@ public class ChimeraManager
 
   public List<ChimeraModel> openModel(String modelPath, ModelType type)
   {
+    return openModel(modelPath, getFileNameFromPath(modelPath), type);
+  }
+
+  /**
+   * Overloaded method to allow Jalview to pass in a model name.
+   * 
+   * @param modelPath
+   * @param modelName
+   * @param type
+   * @return
+   */
+  public List<ChimeraModel> openModel(String modelPath, String modelName,
+          ModelType type)
+  {
     logger.info("chimera open " + modelPath);
     stopListening();
     List<String> response = null;
@@ -190,22 +204,14 @@ public class ChimeraManager
             {
               continue;
             }
-            String modelName = modelPath;
-            // TODO: [Optional] Convert path to name in a better way
-            if (modelPath.lastIndexOf(File.separator) > 0)
-            {
-              modelName = modelPath.substring(modelPath
-                      .lastIndexOf(File.separator) + 1);
-            }
-            else if (modelPath.lastIndexOf("/") > 0)
-            {
-              modelName = modelPath
-                      .substring(modelPath.lastIndexOf("/") + 1);
-            }
             ChimeraModel newModel = new ChimeraModel(modelName, type,
                     modelNumbers[0], modelNumbers[1]);
             currentModelsMap.put(modelNumber, newModel);
             models.add(newModel);
+            // patch for Jalview - set model name in Chimera
+            sendChimeraCommand("setattr M name " + modelName + " #"
+                    + modelNumbers[0], false);
+            // end patch for Jalview
             modelNumbers = null;
           }
         }
@@ -265,6 +271,34 @@ public class ChimeraManager
     return models;
   }
 
+  /**
+   * Refactored method to extract the last (or only) element delimited by file
+   * path separator.
+   * 
+   * @param modelPath
+   * @return
+   */
+  private String getFileNameFromPath(String modelPath)
+  {
+    String modelName = modelPath;
+    if (modelPath == null)
+    {
+      return null;
+    }
+    // TODO: [Optional] Convert path to name in a better way
+    if (modelPath.lastIndexOf(File.separator) > 0)
+    {
+      modelName = modelPath.substring(modelPath
+              .lastIndexOf(File.separator) + 1);
+    }
+    else if (modelPath.lastIndexOf("/") > 0)
+    {
+      modelName = modelPath
+              .substring(modelPath.lastIndexOf("/") + 1);
+    }
+    return modelName;
+  }
+
   public void closeModel(ChimeraModel model)
   {
     // int model = structure.modelNumber();
@@ -450,14 +484,20 @@ public class ChimeraManager
 
   public boolean isChimeraLaunched()
   {
-    // TODO: [Optional] What is the best way to test if chimera is launched?
-
-    // sendChimeraCommand("test", true) !=null
+    boolean launched = false;
     if (chimera != null)
     {
-      return true;
+      try
+      {
+        chimera.exitValue();
+        // if we get here, process has ended
+      } catch (IllegalThreadStateException e)
+      {
+        // ok - not yet terminated
+        launched = true;
+      }
     }
-    return false;
+    return launched;
   }
 
   public boolean launchChimera(List<String> chimeraPaths)
@@ -533,7 +573,7 @@ public class ChimeraManager
     {
       return null;
     }
-    return ChimUtils.parseModelColor((String) colorLines.get(0));
+    return ChimUtils.parseModelColor(colorLines.get(0));
   }
 
   /**
@@ -647,6 +687,7 @@ public class ChimeraManager
     {
       return null;
     }
+    // TODO do we need a maximum wait time before aborting?
     while (busy)
     {
       try
@@ -658,32 +699,38 @@ public class ChimeraManager
       ;
     }
     busy = true;
-    chimeraListenerThreads.clearResponse(command);
-    String text = command.concat("\n");
-    // System.out.println("send command to chimera: " + text);
     try
     {
-      // send the command
-      chimera.getOutputStream().write(text.getBytes());
-      chimera.getOutputStream().flush();
-    } catch (IOException e)
-    {
-      // logger.info("Unable to execute command: " + text);
-      // logger.info("Exiting...");
-      logger.warn("Unable to execute command: " + text);
-      logger.warn("Exiting...");
-      clearOnChimeraExit();
-      busy = false;
-      return null;
-    }
-    if (!reply)
+      chimeraListenerThreads.clearResponse(command);
+      String text = command.concat("\n");
+      // System.out.println("send command to chimera: " + text);
+      try
+      {
+        // send the command
+        chimera.getOutputStream().write(text.getBytes());
+        chimera.getOutputStream().flush();
+      } catch (IOException e)
+      {
+        // logger.info("Unable to execute command: " + text);
+        // logger.info("Exiting...");
+        logger.warn("Unable to execute command: " + text);
+        logger.warn("Exiting...");
+        clearOnChimeraExit();
+        // busy = false;
+        return null;
+      }
+      if (!reply)
+      {
+        // busy = false;
+        return null;
+      }
+      List<String> rsp = chimeraListenerThreads.getResponse(command);
+      // busy = false;
+      return rsp;
+    } finally
     {
       busy = false;
-      return null;
     }
-    List<String> rsp = chimeraListenerThreads.getResponse(command);
-    busy = false;
-    return rsp;
   }
 
   public StructureManager getStructureManager()