+ 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(QUOTE)
+ .append(Platform.escapeString(s)).append(QUOTE);
+ }
+ 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);
+ ex.printStackTrace();
+ return;
+ }
+ }
+ 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);
+ return;
+ } catch (Exception ex)
+ {
+ Cache.log.error("Couldn't add files to Jmol viewer!", ex);
+ ex.printStackTrace();
+ return;
+ }
+ }
+
+ // 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.getStructureFiles() != null
+ && jmb.getStructureFiles().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.getStructureFiles();
+ break;
+ }
+ }
+
+ // refresh the sequence colours for the new structure(s)
+ for (AlignmentPanel ap : _colourwith)
+ {
+ jmb.updateColours(ap);
+ }
+ // do superposition if asked to
+ if (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();
+ }
+ }
+ });
+
+ }
+
+ /**
+ * 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 pdbid = "";
+ try
+ {
+ String[] filesInViewer = jmb.getStructureFiles();
+ // TODO: replace with reference fetching/transfer code (validate PDBentry
+ // as a DBRef?)
+ Pdb pdbclient = new Pdb();
+ for (int pi = 0; pi < jmb.getPdbCount(); pi++)
+ {
+ String file = jmb.getPdbEntry(pi).getFile();
+ if (file == null)
+ {
+ // todo: extract block as method and pull up (also ChimeraViewFrame)
+ // retrieve the pdb and store it locally
+ AlignmentI pdbseq = null;
+ pdbid = jmb.getPdbEntry(pi).getId();
+ long hdl = pdbid.hashCode() - System.currentTimeMillis();
+ if (progressBar != null)
+ {
+ progressBar.setProgressBar(MessageManager
+ .formatMessage("status.fetching_pdb", new String[]
+ { pdbid }), hdl);
+ }
+ try
+ {
+ pdbseq = pdbclient.getSequenceRecords(pdbid);