|
|
|
Re: Communicate from applet to a custom widget [message #124320 is a reply to message #124270] |
Wed, 11 March 2009 16:54 |
Johannes Messages: 6 Registered: July 2009 |
Junior Member |
|
|
Hi,
we made it working somehow but there is still a lot room for improvement
especially the SessionID Problem. Have you some demo code how you solved
it?
Here is our code feel free to comment how we could improve it:
Applet looks like this:
public class TestApplet extends JApplet {
public static Point COMPONENT_SIZE = new Point(200, 100);
private JButton inc;
private JButton dec;
private JLabel label;
private JLabel labelId;
private int i = 0;
private String widgetId;
private ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == inc) {
increment();
} else if (source == dec) {
decrement();
}
}
};
@Override
public void init() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
// super.init();
inc = new JButton("Increment");
inc.setSize(COMPONENT_SIZE.x, COMPONENT_SIZE.y);
cp.add(inc);
dec = new JButton("Decrement");
dec.setSize(COMPONENT_SIZE.x, COMPONENT_SIZE.y);
cp.add(dec);
label = new JLabel("0");
label.setSize(COMPONENT_SIZE.x, COMPONENT_SIZE.y);
cp.add(label);
labelId = new JLabel("hallo");
labelId.setSize(COMPONENT_SIZE.x, COMPONENT_SIZE.y);
cp.add(labelId);
inc.addActionListener(listener);
dec.addActionListener(listener);
}
public void increment() {
label.setText("" + (Integer.parseInt(label.getText()) + 1));
try {
writeToServer(label.getText(), getWidgetId());
} catch (MalformedURLException e) {
}
}
public void decrement() {
label.setText("" + (Integer.parseInt(label.getText()) - 1));
try {
writeToServer(label.getText(), getWidgetId());
} catch (MalformedURLException e) {
}
}
private void writeToServer(String appletResponse, String widgetId)
throws MalformedURLException {
String js = "javascript:";
js += "var req = org.eclipse.swt.Request.getInstance();";
js += "req.addParameter( \"" + widgetId + ".appletCommand\",'" +
appletResponse + "' );";
js += "req.send();";
getAppletContext().showDocument(new URL(js));
}
public void setValue(String value) {
try {
label.setText("" + Integer.parseInt(value));
} catch (NumberFormatException e) {
}
}
public void processAppletCommand(String appletCommand) {
i++;
setValue(appletCommand);
labelId.setText("processAppletCommand" + i);
}
public void setWidgetId(String widgetId) {
this.widgetId = widgetId;
labelId.setText(widgetId);
}
public String getWidgetId() {
return this.widgetId;
}
}
the widget server side:
package org.eclipse.rap.appletwidgets;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Layout;
public class AppletWidget extends Composite {
public AppletWidget(Composite parent, int style) {
super(parent, style);
}
private String appletCommand = "";
public String getAppletCommand() {
return appletCommand;
}
public void setAppletCommand(String appletCommand) {
if (appletCommand == null)
appletCommand = "";
this.appletCommand = appletCommand;
System.err.println(appletCommand);
}
/*
* Intentionally commented out as a map cannot have a layout
*/
public void setLayout(final Layout layout) {
}
}
the resource:
package org.eclipse.rap.appletwidgets;
import org.eclipse.rwt.internal.util.HTML;
import org.eclipse.rwt.resources.IResource;
import org.eclipse.rwt.resources.IResourceManager.RegisterOptions;
public class AppletWidgetResource implements IResource {
public String getCharset() {
return HTML.CHARSET_NAME_ISO_8859_1;
}
public ClassLoader getLoader() {
return this.getClass().getClassLoader();
}
public RegisterOptions getOptions() {
return RegisterOptions.VERSION_AND_COMPRESS;
}
public String getLocation() {
return "org/eclipse/rap/appletwidgets/AppletWidget.js";
}
public boolean isJSLibrary() {
return true;
}
public boolean isExternal() {
return false;
}
}
clientside implementation based on googlemap example:
generally i have difficulties to understand the javascript part.
qx.Class.define( "org.eclipse.rap.appletwidgets.AppletWidget", {
extend: qx.ui.layout.CanvasLayout,
construct: function( id ) {
this.base( arguments );
this.setHtmlAttribute( "id", id );
this._id = id;
// dont realy know what this stuff does but it dosent work without
it
// could someone explain
this.addEventListener( "changeHeight", this._doResize, this );
this.addEventListener( "changeWidth", this._doResize, this );
},
properties : {
appletCommand : {
init : "",
apply : "send"
}
},
members : {
_doActivate : function() {
var shell = null;
var parent = this.getParent();
while( shell == null && parent != null ) {
if( parent.classname == "org.eclipse.swt.widgets.Shell" ) {
shell = parent;
}
parent = parent.getParent();
}
if( shell != null ) {
shell.setActiveChild( this );
}
},
initialize : function() {
if(document.myApplet==null)
{
document.getElementById(this._id).innerHTML = '<APPLET
CODE="TestApplet.class"'+
'NAME="myApplet" MAYSCRIPT'+
'HEIGHT=200 WIDTH=200
CODEBASE="http://www.somenicepagethatdosentexist.com/HTML/">'+
'</APPLET>';
//var wm = org.eclipse.swt.WidgetManager.getInstance();
//var widgetId = wm.findIdByWidget( this );
document.myApplet.setWidgetId(this._id);
var req = org.eclipse.swt.Request.getInstance();
req.addParameter( this._id + ".appletCommand",
this._id);
}
},
send : function() {
this.initialize();
var adr = this.getAppletCommand();
document.myApplet.processAppletCommand(adr);
},
_doResize : function() {
qx.ui.core.Widget.flushGlobalQueues();
}
}
} );
the LCA based on googlemaps example:
package org.eclipse.rap.internal.appletwidgets.appletwidgetkit;
import java.io.IOException;
import org.eclipse.rap.appletwidgets.AppletWidget;
import org.eclipse.rwt.lifecycle.AbstractWidgetLCA;
import org.eclipse.rwt.lifecycle.ControlLCAUtil;
import org.eclipse.rwt.lifecycle.IWidgetAdapter;
import org.eclipse.rwt.lifecycle.JSWriter;
import org.eclipse.rwt.lifecycle.WidgetLCAUtil;
import org.eclipse.rwt.lifecycle.WidgetUtil;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Widget;
public class AppletWidgetLCA extends AbstractWidgetLCA {
private static final String JS_PROP_APPLETCOMMAND = "appletCommand";
private static final String PROP_APPLETCOMMAND = "appletCommand";
public void preserveValues(final Widget widget) {
ControlLCAUtil.preserveValues((Control)widget);
IWidgetAdapter adapter = WidgetUtil.getAdapter(widget);
adapter.preserve(PROP_APPLETCOMMAND,
((AppletWidget)widget).getAppletCommand());
// only needed for custom variants (theming)
WidgetLCAUtil.preserveCustomVariant(widget);
}
/*
* Read the parameters transfered from the client
*/
public void readData(final Widget widget) {
AppletWidget applet = (AppletWidget)widget;
String appletCommand = WidgetLCAUtil.readPropertyValue(applet,
PROP_APPLETCOMMAND);
applet.setAppletCommand(appletCommand);
}
/*
* Initial creation procedure of the widget
*/
public void renderInitialization(final Widget widget) throws
IOException {
JSWriter writer = JSWriter.getWriterFor(widget);
String id = WidgetUtil.getId(widget);
writer.newWidget("org.eclipse.rap.appletwidgets.AppletWidget ", new
Object[] {id });
writer.set("appearance", "composite");
writer.set("overflow", "hidden");
ControlLCAUtil.writeStyleFlags((AppletWidget)widget);
}
public void renderChanges(final Widget widget) throws IOException {
AppletWidget applet = (AppletWidget)widget;
ControlLCAUtil.writeChanges(applet);
JSWriter writer = JSWriter.getWriterFor(widget);
writer.set(PROP_APPLETCOMMAND, JS_PROP_APPLETCOMMAND,
applet.getAppletCommand());
// only needed for custom variants (theming)
WidgetLCAUtil.writeCustomVariant(widget);
}
public void renderDispose(final Widget widget) throws IOException {
JSWriter writer = JSWriter.getWriterFor(widget);
writer.dispose();
}
public void createResetHandlerCalls(String typePoolId) throws
IOException {
}
public String getTypePoolId(Widget widget) {
return null;
}
}
kind regards
Johannes
|
|
|
|
Powered by
FUDForum. Page generated in 0.03412 seconds