+ try
+ {
+ List<String> files = fetchPdbFiles();
+ if (files.size() > 0)
+ {
+ showFilesInViewer(files);
+ }
+ } finally
+ {
+ _started = false;
+ worker = null;
+ }
+ }
+
+ /**
+ * Either adds the given files to a structure viewer or opens a new viewer to
+ * show them
+ *
+ * @param files
+ * list of absolute paths to structure files
+ */
+ void showFilesInViewer(List<String> files)
+ {
+ long lastnotify = jmb.getLoadNotifiesHandled();
+ StringBuilder fileList = new StringBuilder();
+ for (String s : files)
+ {
+ fileList.append(SPACE).append(BACKSLASH)
+ .append(Platform.escapeString(s)).append(BACKSLASH);
+ }
+ String filesString = fileList.toString();
+
+ if (!addingStructures)
+ {
+ try
+ {
+ initJmol("load FILES " + filesString);
+ } catch (OutOfMemoryError oomerror)
+ {
+ new OOMWarning("When trying to open the Jmol viewer!", oomerror);
+ Cache.log.debug("File locations are " + filesString);
+ } catch (Exception ex)
+ {
+ Cache.log.error("Couldn't open Jmol viewer!", ex);
+ }
+ }
+ else
+ {
+ StringBuilder cmd = new StringBuilder();
+ cmd.append("loadingJalviewdata=true\nload APPEND ");
+ cmd.append(filesString);
+ cmd.append("\nloadingJalviewdata=null");
+ final String command = cmd.toString();
+ lastnotify = jmb.getLoadNotifiesHandled();
+
+ try
+ {
+ jmb.evalStateCommand(command);
+ } catch (OutOfMemoryError oomerror)
+ {
+ new OOMWarning("When trying to add structures to the Jmol viewer!",
+ oomerror);
+ Cache.log.debug("File locations are " + filesString);
+ } catch (Exception ex)
+ {
+ Cache.log.error("Couldn't add files to Jmol viewer!", ex);
+ }
+ }
+
+ // need to wait around until script has finished
+ int waitMax = JMOL_LOAD_TIMEOUT;
+ int waitFor = 35;
+ int waitTotal = 0;
+ while (addingStructures ? lastnotify >= jmb.getLoadNotifiesHandled()
+ : !(jmb.isFinishedInit() && jmb.getPdbFile() != null && jmb
+ .getPdbFile().length == files.size()))
+ {
+ try
+ {
+ Cache.log.debug("Waiting around for jmb notify.");
+ Thread.sleep(waitFor);
+ waitTotal += waitFor;
+ } catch (Exception e)
+ {
+ }
+ if (waitTotal > waitMax)
+ {
+ System.err
+ .println("Timed out waiting for Jmol to load files after "
+ + waitTotal + "ms");
+// System.err.println("finished: " + jmb.isFinishedInit()
+// + "; loaded: " + Arrays.toString(jmb.getPdbFile())
+// + "; files: " + files.toString());
+ jmb.getPdbFile();
+ break;
+ }
+ }
+
+ // refresh the sequence colours for the new structure(s)
+ for (AlignmentPanel ap : _colourwith)
+ {
+ jmb.updateColours(ap);
+ }
+ // do superposition if asked to
+ if (Cache.getDefault("AUTOSUPERIMPOSE", true) && alignAddedStructures)
+ {
+ alignAddedStructures();
+ }
+ addingStructures = false;
+ }
+
+ /**
+ * Queues a thread to align structures with Jalview alignments
+ */
+ void alignAddedStructures()
+ {
+ javax.swing.SwingUtilities.invokeLater(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ if (jmb.viewer.isScriptExecuting())
+ {
+ SwingUtilities.invokeLater(this);
+ try
+ {
+ Thread.sleep(5);
+ } catch (InterruptedException q)
+ {
+ }
+ return;
+ }
+ else
+ {
+ alignStructs_withAllAlignPanels();
+ }
+ }
+ });
+ alignAddedStructures = false;
+ }
+
+ /**
+ * Retrieves and saves as file any modelled PDB entries for which we do not
+ * already have a file saved. Returns a list of absolute paths to structure
+ * files which were either retrieved, or already stored but not modelled in
+ * the structure viewer (i.e. files to add to the viewer display).
+ *
+ * @return
+ */
+ List<String> fetchPdbFiles()
+ {
+ // todo - record which pdbids were successfully imported.
+ StringBuilder errormsgs = new StringBuilder();
+
+ List<String> files = new ArrayList<String>();