passing in parameters to an applet

19 views
Skip to first unread message

vilo

unread,
Jul 7, 2011, 3:53:52 AM7/7/11
to gwtai, a.bu...@gmail.com
I react to a discussion "passing in parameters to an applet" (http://
groups.google.com/group/gwtai/browse_thread/thread/2cd18db28953f5b4),
where kilkenny suggests to create method createAppletWidget with a Map
parameter, where dynamic parameters could be supplied. I implemented
this method and want to share the code, so it could be incorporated
into next version of gwtai.

Here is the source code with modified 3.0 version:

/*
* Copyright 2010 Adrian Buerki
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
may not
* use this file except in compliance with the License. You may obtain
a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the
* License for the specific language governing permissions and
limitations under
* the License.
*/

package com.google.gwt.gwtai.applet.client;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

/**
* GWT-side utility class.
*
* @author Adrian Buerki <a.bu...@gmail.com>
*/
public class AppletJSUtil {

static {
CallbackUtil.defineBridgeMethod();
}

/**
* Constructs an <code>HTML</code> element which contains an applet
tag. The HTML specification
* states that the <code>applet</code> tag is deprecated. But the
browser support for the <code>object</code>
* and <code>embed</code> tag is currently inconsistent. So using the
<code>applet</code> tag is the only
* consistent way to deploy a Java Applet across browsers on all
platforms.
*
* @param applet The <code>Applet</code> to take the information
from.
* @return An <code>HTML</code> element which contains an applet tag.
*/
public static Widget createAppletWidget(Applet applet) {
return createAppletWidget(applet, (Map<String, String>) null);
}

/**
* Constructs an <code>HTML</code> element which contains an applet
tag. The HTML specification
* states that the <code>applet</code> tag is deprecated. But the
browser support for the <code>object</code>
* and <code>embed</code> tag is currently inconsistent. So using the
<code>applet</code> tag is the only
* consistent way to deploy a Java Applet across browsers on all
platforms.
*
* @param applet The <code>Applet</code> to take the information
from.
* @param dynamicParams TODO
* @return An <code>HTML</code> element which contains an applet tag.
*/
public static Widget createAppletWidget(Applet applet, Map<String,
String> dynamicParams) {
String htmlCode = createAppletHTML(applet, dynamicParams);

if (htmlCode != null) {
return new HTML(htmlCode);
}

return null;
}

/**
* Constructs an applet tag. The HTML specification states that the
<code>applet</code> tag is deprecated.
* But the browser support for the <code>object</code> and
<code>embed</code> tag is currently
* inconsistent. So using the <code>applet</code> tag is the only
consistent way to deploy a Java Applet
* across browsers on all platforms.
*
* @param applet The <code>Applet</code> to take the information
from.
* @return The applet tag.
*/
public static String createAppletHTML(Applet applet) {
return createAppletHTML(applet, null);
}

/**
* Constructs an applet tag. The HTML specification states that the
<code>applet</code> tag is deprecated.
* But the browser support for the <code>object</code> and
<code>embed</code> tag is currently
* inconsistent. So using the <code>applet</code> tag is the only
consistent way to deploy a Java Applet
* across browsers on all platforms.
*
* @param applet The <code>Applet</code> to take the information
from.
* @param dynamicParams Additional parameters for this applet
instance.
* @return The applet tag.
*/
public static String createAppletHTML(Applet applet, Map<String,
String> dynamicParams) {
if (applet instanceof AppletAccomplice) {
AppletAccomplice aapplet = (AppletAccomplice) applet;

String htmlCode = "<applet mayscript='true'"
+ " code='"
+ escapeXmlAttr(aapplet.getCode())
+ "' width='"
+ aapplet.getWidth()
+ "' height='"
+ aapplet.getHeight()
+ "' name='"
+ escapeXmlAttr(aapplet.getName())
+ "' id='"
+ escapeXmlAttr(aapplet.getName())
+ "' alt='Java Runtime Environment is not working on your
system'";

if (null != aapplet.getCodebase()) {
htmlCode += "codebase='" + escapeXmlAttr(aapplet.getCodebase()) +
"'";
}

if (null != aapplet.getArchive()) {
htmlCode += "archive='" + escapeXmlAttr(aapplet.getArchive()) +
"'";
}

if (null != aapplet.getAlign()) {
htmlCode += "align='" + aapplet.getAlign() + "'";
}

htmlCode += ">";

Map<String, String> parameters = aapplet.getParameters();

if (parameters != null && !parameters.isEmpty()) {
for (String name : parameters.keySet()) {
htmlCode += createParamTag(name, parameters.get(name));
}
}

if (dynamicParams != null) {
for (String name : dynamicParams.keySet()) {
htmlCode += createParamTag(name, dynamicParams.get(name));
}
}

htmlCode += createParamTag("java_version",
aapplet.getJavaVersion());
htmlCode += createParamTag("java_arguments",
aapplet.getJavaArguments());
htmlCode += createParamTag("separate_jvm",
aapplet.hasSeparateJVM());
htmlCode += createParamTag("image", aapplet.getLoadingImage());
htmlCode += createParamTag("applet_name", aapplet.getName());

htmlCode += "</applet>";

return htmlCode;
}

return null;
}

public static Widget createAppletWidget(Applet applet,
String forceJavaVersion) {
if (applet instanceof AppletAccomplice) {
AppletAccomplice aapplet = (AppletAccomplice) applet;

String codebase = GWT.getModuleBaseURL();

String htmlCode = "<p style='text-align: center;'><script
src='http://java.com/js/deployJava.js'></script>"
+ "<script>deployJava.runApplet({codebase:'"
+ codebase
+ "', ";

if (null != aapplet.getArchive()) {
htmlCode += "archive:'" + aapplet.getArchive() + "', ";
}

htmlCode += "code:'" + aapplet.getCode() + "', width:'"
+ aapplet.getWidth() + "', Height:'" + aapplet.getHeight()
+ "'}, null, '" + forceJavaVersion + "');"
+ "</script></p>";

return new HTML(htmlCode);
}

return null;
}

/**
* Registers an instance of your <code>AppletCallback</code>
* implementation to listen for callbacks coming from the given
* <code>Applet</code>. The actual <code>Applet</code> implementation
* can check whether an <code>AppletCallback</code> handler is
installed,
* if so it can notify the given <code>AppletCallback</code> instance
* about updates or events.
*
* <p>Example:
*
* <p>First, register a listening <code>AppletCallback</code>
object:<br>
* <code>AppletJSUtil.registerAppletCallback(stopWatchApplet,
* new StopWatchCallback(panelLaps));</code>
*
* <p>Then callback to GWT from your <code>Applet</code>
implementation
* (Java):<br>
* <code>AppletUtil.callback(StopWatchAppletImpl.this,
_swLabel.getText());</code>
*
* @param applet -
* The <code>Applet</code> instance to listen to.
* @param appletCallback -
* The <code>AppletCallback</code> instance to notify once
a
* callback is coming.
*/
public static void registerAppletCallback(Applet applet,
AppletCallback<? extends Object> appletCallback) {
if (applet instanceof AppletAccomplice) {
AppletAccomplice aapplet = (AppletAccomplice) applet;

CallbackUtil.registerCallback(aapplet.getName(), appletCallback);
}
}

/**
* Calls the method with the given name on the <code>Applet</code>
* instance.
*
* @param applet -
* The <code>Applet</code> instance to call the method on.
* @param methodName -
* The name of the method to call.
*/
public static void call(Applet applet, String methodName) {
if (applet instanceof AppletAccomplice) {
String id = ((AppletAccomplice) applet).getId();
Element elem = DOM.getElementById(id);

call(elem, methodName);
}
}

/**
* Calls the method with the given name on the <code>Applet</code>
* instance.
*
* @param applet -
* The <code>Applet</code> instance to call the method on.
* @param methodName -
* The name of the method to call.
* @param args -
* The method arguments.
*/
public static void call(Applet applet, String methodName, Object[]
args) {
if (applet instanceof AppletAccomplice) {
String id = ((AppletAccomplice) applet).getId();
Element elem = DOM.getElementById(id);

call(elem, methodName, args);
}
}

/**
* Helper-method to create a param tag.
*/
private static String createParamTag(String name, Object value) {
if (value != null) {
return "<param name='" + escapeXmlAttr(name) + "' value='" +
escapeXmlAttr(value.toString()) + "'>";
}

return "";
}

/**
* Helper-method to do the actual calling.
*/
private static native Object call(Element elem, String methodName) /*-
{
var theFunc = elem[methodName];
return theFunc();
}-*/;

/**
* Helper-method to do the actual calling.
*/
private static native Object call(Element elem, String methodName,
Object args) /*-{
var theFunc = elem[methodName];
return theFunc(args);
}-*/;

public static String escapeXmlAttr(String s) {
int i=0, l=s.length();
char ch;
while (i < l) {
ch = s.charAt(i);
if (ch == '"' || ch == '\'' || ch == '&' || ch == '<')
break;
i++;
}

if (i == l)
return s;

StringBuilder res = new StringBuilder(s.length() + 64);
res.append(s.subSequence(0, i));

while (i < l) {
ch = s.charAt(i);
switch (ch) {
case '"':
res.append("&quot;");
break;
case '\'':
res.append("&#39;");
break;
case '&':
res.append("&amp;");
break;
case '<':
res.append("&lt;");
break;
default:
res.append(ch);
}
i++;
}
return res.toString();
}

}
Reply all
Reply to author
Forward
0 new messages