protected WebPanel(JmolViewer viewer, JFileChooser fc, WebPanel[] webPanels, int panelIndex) { this.viewer = viewer; this.fc = fc; this.webPanels = webPanels; this.panelIndex = panelIndex; //Create the text fields for the path to the Jmol applet, page author(s) name(s) and web page title. remoteAppletPath = new JTextField(20); remoteAppletPath.addActionListener(this); remoteAppletPath.setText(WebExport.getAppletPath(true)); localAppletPath = new JTextField(20); localAppletPath.addActionListener(this); localAppletPath.setText(WebExport.getAppletPath(false)); pageAuthorName= new JTextField(20); pageAuthorName.addActionListener(this); pageAuthorName.setText(WebExport.getPageAuthorName()); webPageTitle = new JTextField(20); webPageTitle.addActionListener(this); webPageTitle.setText(GT._("A web page containing Jmol applets")); }
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); } }
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; }
private JPanel getLeftPanel(int w, int h) { helpButton = new JButton(GT._("Help/Instructions")); helpButton.addActionListener(this); String templateImage = panelName + ".png"; URL pageCartoon = WebExport.getResource(this, templateImage); ImageIcon pageImage = null; if (pageCartoon != null) { pageImage = new ImageIcon(pageCartoon, GT._("Cartoon of Page")); } else { System.err.println("Error Loading Page Cartoon Image " + templateImage); } JLabel pageCartoonLabel = new JLabel(pageImage); JPanel pageCartoonPanel = new JPanel(); pageCartoonPanel.setLayout(new BorderLayout()); pageCartoonPanel.setBorder(BorderFactory.createTitledBorder(GT ._("Cartoon of Page")+":")); pageCartoonPanel.add(pageCartoonLabel); // editorScrollPane = getInstructionPane(w, h); //Create the save button. saveButton = new JButton(GT._("Save HTML as...")); saveButton.addActionListener(this); JPanel savePanel = new JPanel(); savePanel.add(saveButton); //Path to applet panel JPanel pathPanel = new JPanel(); pathPanel.setLayout(new BorderLayout()); pathPanel.setBorder(BorderFactory.createTitledBorder(GT ._("Relative server path to jar files:"))); pathPanel.add(remoteAppletPath, BorderLayout.NORTH); JPanel pathPanel2 = new JPanel(); pathPanel2.setLayout(new BorderLayout()); pathPanel2.setBorder(BorderFactory.createTitledBorder(GT ._("Relative local path to jar files:"))); pathPanel2.add(localAppletPath, BorderLayout.NORTH); //Page Author Panel JPanel authorPanel = new JPanel(); authorPanel.setBorder(BorderFactory.createTitledBorder(GT ._("Author (your name):"))); authorPanel.add(pageAuthorName, BorderLayout.NORTH); //Page Title Panel JPanel titlePanel = new JPanel(); titlePanel.setLayout(new BorderLayout()); titlePanel.setBorder(BorderFactory.createTitledBorder(GT ._("Browser window title for this web page:"))); titlePanel.add(webPageTitle, BorderLayout.NORTH); titlePanel.add(savePanel, BorderLayout.SOUTH); JPanel pathPanels = new JPanel(); pathPanels.setLayout(new BorderLayout()); pathPanels.add(pathPanel, BorderLayout.NORTH); pathPanels.add(pathPanel2, BorderLayout.SOUTH); JPanel settingsPanel = new JPanel(); settingsPanel.setLayout(new BorderLayout()); settingsPanel.add(pathPanels, BorderLayout.NORTH); settingsPanel.add(authorPanel, BorderLayout.CENTER); settingsPanel.add(titlePanel, BorderLayout.SOUTH); //Combine previous three panels into one JPanel leftpanel = new JPanel(); leftpanel.setLayout(new BorderLayout()); // leftpanel.add(editorScrollPane, BorderLayout.CENTER); leftpanel.add(helpButton, BorderLayout.NORTH); leftpanel.add(pageCartoonPanel, BorderLayout.CENTER); leftpanel.add(settingsPanel, BorderLayout.SOUTH); return leftpanel; }