package org.latexlab.clsi.client.local;
import org.latexlab.clsi.client.AppletUnavailableException;
import org.latexlab.clsi.client.async.ReadyCallback;
import com.google.gwt.user.client.ui.NamedFrame;
import com.google.gwt.user.client.ui.RootPanel;
public class ClsiLocalEngine {
private static ClsiLocalEngine instance;
public static ClsiLocalEngine get(String publicPath) {
if (instance == null) {
instance = new ClsiLocalEngine(publicPath);
}
return instance;
}
private ReadyCallback readyHandler;
private NamedFrame frame;
private String publicPath, servicePath, asyncPath;
private boolean locked = false, running = false;
/**
* The default constructor.
*/
protected ClsiLocalEngine(String publicPath){
this.publicPath = publicPath;
this.setupJavascriptInterface(this);
this.frame = new NamedFrame("Engine");
this.frame.setPixelSize(0, 0);
this.frame.setUrl(publicPath + "empty.html");
this.frame.getElement().setPropertyString("frameborder", "0");
RootPanel.get().add(this.frame);
}
/** Retrieves a value indicating whether the Java applet has started successfully.
* @return whether the applet has started
*/
public boolean isStarted(){
return this.running;
}
/** Retrieves a value indicating whether the Java applet is running inside a security sandbox.
* @return whether the applet is running inside a security sandbox
*/
public boolean isLocked(){
return this.locked;
}
public String getServicePath() {
return servicePath;
}
public String getAsyncPath() {
return asyncPath;
}
/**
* Starts the Java applet component of this widget.
*/
public void start(ReadyCallback readyCallback){
if(this.running){
if (this.locked) {
readyCallback.onFailure(new AppletUnavailableException(
"The Java applet was not allowed to execute."));
} else {
readyCallback.onSuccess();
}
} else {
this.readyHandler = readyCallback;
this.running = true;
this.frame.setUrl(publicPath + "applet.html");
}
}
/**
* Stops the Java applet component of this widget.
*/
public void stop(){
if(this.running){
this.running = false;
this.frame.setUrl(publicPath + "empty.html");
}
}
/**
* Signals that the Java sandbox has been detected.
*/
public void onSandboxDetected(){
this.locked = true;
this.stop();
if (this.readyHandler != null) {
this.readyHandler.onFailure(
new AppletUnavailableException(
"The Java applet is running within a sandbox."));
this.readyHandler = null;
}
}
public void onReady(String servicePath, String asyncPath) {
this.servicePath = servicePath;
this.asyncPath = asyncPath;
if (this.readyHandler != null) {
this.readyHandler.onSuccess();
this.readyHandler = null;
}
}
/**
* Signals that a download item has been opened.
* @param id the download id
*/
public void onError(String error){
if (this.readyHandler != null) {
this.readyHandler.onFailure(new Exception(error));
this.readyHandler = null;
}
}
/**
* Exposes this widget's API to the HTML page for direct use by the Java applet component.
* @param eng the ClsiEngine widget which will handle the JavaScript side of the Java-JavaScript communication.
*/
public native void setupJavascriptInterface(ClsiLocalEngine eng) /*-{
$wnd.clsiEngine = { };
$wnd.clsiEngine.onReady = function(svcPath, asyncPath) {
return @org.latexlab.clsi.client.local.ClsiLocalEngine::instance.@org.latexlab.clsi.client.local.ClsiLocalEngine::onReady(Ljava/lang/String;Ljava/lang/String;)(svcPath, asyncPath);
}
$wnd.clsiEngine.onError = function(error) {
return @org.latexlab.clsi.client.local.ClsiLocalEngine::instance.@org.latexlab.clsi.client.local.ClsiLocalEngine::onError(Ljava/lang/String;)(error);
}
$wnd.clsiEngine.onSandboxDetected = function(){
return @org.latexlab.clsi.client.local.ClsiLocalEngine::instance.@org.latexlab.clsi.client.local.ClsiLocalEngine::onSandboxDetected()();
}
}-*/;
}