private static String writeFileBytes(String path, byte[] data) { try { if (data.length>=524288 && !path.endsWith("JmolApplet.jar") ){ //gzip it path += ".gz"; GZIPOutputStream gzFile = new GZIPOutputStream(new FileOutputStream(path)); gzFile.write(data); LogPanel.log(" ..." + GT._("compressing large data file to") + "\n"); gzFile.flush(); gzFile.close(); } else { FileOutputStream os = new FileOutputStream(path); os.write(data); os.flush(); os.close(); } } catch (IOException e) { LogPanel.log(e.getMessage()); } return path; }
private static String copyBinaryFile(String fullPathName, String dataPath) { String name = fullPathName.substring(fullPathName.lastIndexOf('/') + 1); name = dataPath + "/" + name; String gzname = name + ".gz"; File outFile = new File(name); File gzoutFile = new File(gzname); if (outFile.exists()) return name; if (gzoutFile.exists()) return gzname; try { LogPanel.log(" ..." + GT._("copying\n{0}\n to",fullPathName)); byte[] data = getFileAsBytes(fullPathName); if (data == null) LogPanel.log(GT._("Could not find or open:\n{0}", fullPathName)); else { name = writeFileBytes(name, data); LogPanel.log(name); } } catch (Exception e) { LogPanel.log(e.getMessage()); } return name; }
private static byte[] getFileAsBytes(String path) throws IOException { int len = 0; int totalLen = 0; Object streamOrError = FileManager.getInputStream(path, false, null, null); if (streamOrError instanceof String) { LogPanel.log((String) streamOrError); return null; } byte[] buf = new byte[1024]; byte[] bytes = new byte[4096]; BufferedInputStream bis = new BufferedInputStream((InputStream) streamOrError); while ((len = bis.read(buf)) > 0) { totalLen += len; if (totalLen >= bytes.length) bytes = ArrayUtil.ensureLength(bytes, totalLen * 2); System.arraycopy(buf, 0, bytes, totalLen - len, len); } bis.close(); buf = new byte[totalLen]; System.arraycopy(bytes, 0, buf, 0, totalLen); return buf; }
boolean fileWriter(File file, JList InstanceList) throws IOException { //returns true if successful. useAppletJS = JmolViewer.checkOption(viewer, "webMakerCreateJS"); // JOptionPane.showMessageDialog(null, "Creating directory for data..."); String datadirPath = file.getPath(); String datadirName = file.getName(); String fileName = null; if (datadirName.indexOf(".htm") > 0) { fileName = datadirName; datadirPath = file.getParent(); file = new File(datadirPath); datadirName = file.getName(); } else { fileName = datadirName + ".html"; } datadirPath = datadirPath.replace('\\', '/'); boolean made_datadir = (file.exists() && file.isDirectory() || file.mkdir()); DefaultListModel listModel = (DefaultListModel) InstanceList.getModel(); LogPanel.log(""); if (made_datadir) { LogPanel.log(GT._("Using directory {0}", datadirPath)); LogPanel.log(" " + GT._("adding JmolPopIn.js")); viewer.writeTextFile(datadirPath + "/JmolPopIn.js", WebExport.getResourceString(this, "JmolPopIn.js")); for (int i = 0; i < listModel.getSize(); i++) { JmolInstance thisInstance = (JmolInstance) (listModel.getElementAt(i)); String javaname = thisInstance.javaname; String script = thisInstance.script; LogPanel.log(" ...jmolApplet" + i); LogPanel.log(" ..." + GT._("adding {0}.png", javaname)); try { thisInstance.movepict(datadirPath); } catch (IOException IOe) { throw IOe; } String fileList = ""; fileList += addFileList(script, "/*file*/"); fileList += addFileList(script, "FILE0="); fileList += addFileList(script, "FILE1="); if (localAppletPath.getText().equals(".") || remoteAppletPath.getText().equals(".")) fileList += "Jmol.js\nJmolApplet.jar"; String[] filesToCopy = fileList.split("\n"); String[] copiedFileNames = new String[filesToCopy.length]; String f; int pt; for (int iFile = 0; iFile < filesToCopy.length; iFile++) { if ((pt = (f = filesToCopy[iFile]).indexOf("|")) >= 0) filesToCopy[iFile] = f.substring(0, pt); copiedFileNames[iFile] = copyBinaryFile(filesToCopy[iFile], datadirPath); } script = localizeFileReferences(script, filesToCopy, copiedFileNames); LogPanel.log(" ..." + GT._("adding {0}.spt", javaname)); viewer.writeTextFile(datadirPath + "/" + javaname + ".spt", script); } String html = WebExport.getResourceString(this, panelName + "_template"); html = fixHtml(html); appletInfoDivs = ""; StringBuffer appletDefs = new StringBuffer(); if (!useAppletJS) htmlAppletTemplate = WebExport.getResourceString(this, panelName + "_template2"); for (int i = 0; i < listModel.getSize(); i++) html = getAppletDefs(i, html, appletDefs, (JmolInstance) listModel .getElementAt(i)); html = TextFormat.simpleReplace(html, "@AUTHOR@", GT.escapeHTML(pageAuthorName .getText())); html = TextFormat.simpleReplace(html, "@TITLE@", GT.escapeHTML(webPageTitle.getText())); html = TextFormat.simpleReplace(html, "@REMOTEAPPLETPATH@", remoteAppletPath.getText()); html = TextFormat.simpleReplace(html, "@LOCALAPPLETPATH@", localAppletPath.getText()); html = TextFormat.simpleReplace(html, "@DATADIRNAME@", datadirName); if (appletInfoDivs.length() > 0) appletInfoDivs = "\n<div style='display:none'>\n" + appletInfoDivs + "\n</div>\n"; String str = appletDefs.toString(); if (useAppletJS) str = "<script type='text/javascript'>\n" + str + "\n</script>"; html = TextFormat.simpleReplace(html, "@APPLETINFO@", appletInfoDivs); html = TextFormat.simpleReplace(html, "@APPLETDEFS@", str); html = TextFormat.simpleReplace(html, "@CREATIONDATA@", GT.escapeHTML(WebExport .TimeStamp_WebLink())); html = TextFormat.simpleReplace(html, "@AUTHORDATA@", GT.escapeHTML(GT._("Based on template by A. Herráez as modified by J. Gutow"))); html = TextFormat.simpleReplace(html, "@LOGDATA@", "<pre>\n" + LogPanel.getText() + "\n</pre>\n"); LogPanel.log(" ..." + GT._("creating {0}", fileName)); viewer.writeTextFile(datadirPath + "/" + fileName, html); } else { IOException IOe = new IOException("Error creating directory: " + datadirPath); throw IOe; } LogPanel.log(""); return true; }
public void actionPerformed(ActionEvent e) { if (e.getSource() == remoteAppletPath) {//apparently no events are fired to reach this, maybe "enter" does it String path = remoteAppletPath.getText(); WebExport.setAppletPath(path, true); return; } if (e.getSource() == localAppletPath) {//apparently no events are fired to reach this, maybe "enter" does it String path = localAppletPath.getText(); WebExport.setAppletPath(path, false); return; } //Handle open button action. if (e.getSource() == addInstanceButton) { //make dialog to get name for instance //create an instance with this name. Each instance is just a container for a string with the Jmol state //which contains the full information on the file that is loaded and manipulations done. String label = (instanceList.getSelectedIndices().length != 1 ? "" : getInstanceName(-1)); String name = JOptionPane.showInputDialog( GT._("Give the occurrence of Jmol a name:"), label); if (name == null) return; //need to get the script... String script = viewer.getStateInfo(); if (script == null) { LogPanel.log("Error trying to get Jmol State within pop_in_Jmol."); } DefaultListModel listModel = (DefaultListModel) instanceList.getModel(); int width = 300; int height = 300; if (appletSizeSpinnerH != null) { width = ((SpinnerNumberModel) (appletSizeSpinnerW.getModel())) .getNumber().intValue(); height = ((SpinnerNumberModel) (appletSizeSpinnerH.getModel())) .getNumber().intValue(); } JmolInstance instance = new JmolInstance(viewer, name, script, width, height); if (instance == null) { LogPanel .log(GT._("Error creating new instance containing script(s) and image.")); } int i; for (i = instanceList.getModel().getSize(); --i >= 0;) if (getInstanceName(i).equals(instance.name)) break; if (i < 0) { i = listModel.getSize(); listModel.addElement(instance); LogPanel.log(GT._("added Instance {0}", instance.name)); } else { listModel.setElementAt(instance, i); LogPanel.log(GT._("updated Instance {0}", instance.name)); } instanceList.setSelectedIndex(i); syncLists(); return; } if (e.getSource() == deleteInstanceButton) { DefaultListModel listModel = (DefaultListModel) instanceList.getModel(); //find out which are selected and remove them. int[] todelete = instanceList.getSelectedIndices(); int nDeleted = 0; for (int i = 0; i < todelete.length; i++){ JmolInstance instance = (JmolInstance) listModel.get(todelete[i]); try { instance.delete(); } catch (IOException err) { LogPanel.log(err.getMessage()); } listModel.remove(todelete[i] - nDeleted++); } syncLists(); return; } if (e.getSource() == showInstanceButton) { DefaultListModel listModel = (DefaultListModel) instanceList.getModel(); //find out which are selected and remove them. int[] list = instanceList.getSelectedIndices(); if (list.length != 1) return; JmolInstance instance = (JmolInstance) listModel.get(list[0]); viewer.evalStringQuiet(")" + instance.script); //leading paren disabled history return; } if (e.getSource() == saveButton) { fc.setDialogTitle(GT._("Select a directory to create or an HTML file to save")); int returnVal = fc.showSaveDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) return; File file = fc.getSelectedFile(); boolean retVal = true; try { String path = remoteAppletPath.getText(); WebExport.setAppletPath(path, true); path = localAppletPath.getText(); WebExport.setAppletPath(path, false); String authorName = pageAuthorName.getText(); WebExport.setWebPageAuthor(authorName); retVal = fileWriter(file, instanceList); } catch (IOException IOe) { LogPanel.log(IOe.getMessage()); } if (!retVal) { LogPanel.log(GT._("Call to FileWriter unsuccessful.")); } } if (e.getSource() == helpButton){ HelpDialog webExportHelp = new HelpDialog(WebExport.getFrame(), WebExport.getHtmlResource(this, panelName + "_instructions")); webExportHelp.setVisible(true); webExportHelp.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } }