MarvinSketch Example - Retrieving a structure

In Macintosh systems, there are several browsers that do not support standard JavaScript to Java communication (LiveConnect). The following example is a workaround for this problem. Press Submit as Molfile or Submit as SMILES button to get the molecule from MarvinSketch in the specified format and post it to another page to display the structure in MarvinView.

If LiveConnect is available the document.MSketch.getMol(format) statement can be used to get the structure from the MSketch applet. Otherwise a workaround is needed. See the else if(!isjs2java) branch in the code of the getMol method.

In this case, instead of document.MSketch.getMol the following code should be used:

	setMethod("MSketch.getMol","java.lang.String");
	addMethodParam(format);
	setPostJsMethod("parent.submitMethod()");
	runMethod();

where

submitMethod() sets the molecule field of the form to the received molecule and submit the form. Since this method will be called after the applet sent back the result, you can already refer to the result of the applet method by getResult(). When the form is submited, the molecule is posted to the display-result.jsp page.

<script language="JavaScript" src="../../../marvin.js"></script>
<script language="JavaScript" src="../../../js2java.js"></script>
<p>

<form name="molform" method="post" action="display-result.jsp">
	<input type="button" value="Submit molfile"
	    onClick="getMol('mol')">
	<input type="button" value="Submit SMILES"
		onClick="getMol('smiles')">
	<input type="hidden" name="molecule">
</form>
</p>

<script language="JavaScript">
<!--
var isJs2Java = isLiveConnect(); // Does JavaScript - Java communication exist?
setPath("../../.."); // sets relative path of the Marvin directory

/* Sets form variable and submit the form.*/
function submitMethod() {
	document.molform.molecule.value = getResult();
	document.molform.submit();
}

/*Gets the structure from the sketcher and
calls 'submitMethod()'.
The 'runMethod' function should be the last statement in this function.*/
function getMol(format) {
	if((document.MSketch != null) && isJs2Java) {
		var s = document.MSketch.getMol(format);
		s = unix2local(s); // Convert "\n" to local line separator
		document.molform.molecule.value = s;
		document.molform.submit();
	} else if(!isJs2Java) {
		setMethod("MSketch.getMol","java.lang.String");
		addMethodParam(format);
		setPostJsMethod("parent.submitMethod()");
		runMethod();
	} else {
		alert("Cannot import molecule:\n" +
		"no JavaScript to Java communication in your browser.\n");
	}
}

msketch_name="MSketch";
msketch_begin("../../..",540,480);
msketch_param("mol","../../../mols-2d/caffeine.mol");
msketch_end();
//--></script>

The limitation of this workaround is the value of the result string. The maximum is 2K characters. Because of the limitied transfer size, the data is sent in compressed (gzip) format with Base 64 encoding. Marvin supports both molecule import and export in base64:gzip format.