Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Communicate from applet to a custom widget
Communicate from applet to a custom widget [message #124270] Tue, 10 March 2009 17:46 Go to next message
Johannes is currently offline JohannesFriend
Messages: 6
Registered: July 2009
Junior Member
Hi,
we are trying to communicate from an applet to a custom widget.
At the custom Widget creation we tell our applet the widget id. this works
fine and looks like this:

load : function() {

var adr = this.getAddress();
if(document.myApplet==null)
{
document.getElementById(this._id).innerHTML = '<APPLET
CODE="TestApplet.class"'+
'NAME="myApplet" MAYSCRIPT'+
'HEIGHT=200 WIDTH=200
CODEBASE="http://www.eineHompage.com/HTML/">'+
'</APPLET>';

var wm = org.eclipse.swt.WidgetManager.getInstance();
var widgetId = wm.findIdByWidget( this );
document.myApplet.setWidgetId(widgetId);
}


document.myApplet.setValue(adr);

},

but we cant communicate from the applet to the widget. code looks like
this:

String js = "javascript:";
js += " var wm =
org.eclipse.swt.WidgetManager.getInstance();";
js += " var widget = wm.findWidgetById(" + widgetId + ");";
js += " widget.setAddress('" + value + "');";


getAppletContext().showDocument(new URL(js));

perhaps someone had the same problem and could deliver a snippet or
something

kind regards
Johannes
Re: Communicate from applet to a custom widget [message #124283 is a reply to message #124270] Wed, 11 March 2009 08:39 Go to previous messageGo to next message
Michael Haendel is currently offline Michael HaendelFriend
Messages: 5
Registered: July 2009
Junior Member
Hi Johannes,

what is the exact problem with the JavaScript? I've got a similar scenario
running and see 2 diffenrences:

1. Try adding a "parent" before calling the the WidgetManager (var wm =
parent.org.eclipse.swt.WidgetManager.getInstance())
2. If that still doesn't help pack the JS in a method and simply call the
method from the Applet with showDocument(new
URL("javascript:YourMethod()"),"_self")

Cheers,
Michael.
Re: Communicate from applet to a custom widget [message #124308 is a reply to message #124270] Wed, 11 March 2009 12:55 Go to previous messageGo to next message
Stefan Hansel is currently offline Stefan HanselFriend
Messages: 103
Registered: July 2009
Senior Member
If the applet doesn't send the SessionID (typically stored in a cookie,
sometimes in the RAP URL), the server-side is not able to match the
correct widget-instance.
That was our main problem.
Re: Communicate from applet to a custom widget [message #124320 is a reply to message #124270] Wed, 11 March 2009 16:54 Go to previous messageGo to next message
Johannes is currently offline JohannesFriend
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
Re: Communicate from applet to a custom widget [message #124726 is a reply to message #124320] Mon, 16 March 2009 12:36 Go to previous message
Johannes is currently offline JohannesFriend
Messages: 6
Registered: July 2009
Junior Member
hi,

i still have a problem with the widget i implemented :( iam able to set a
value for my widget and listen for changes but something strange happens:
if i send a value to my widget the widget does something sends a answer to
the serverside i listen to the answer and set a label text this works so
far. but if the widget changes itself and send this change to the server
then i can listen for the change but if i try to set the text for the
label i dosent see that the label change.....

hopefully you could understand my problem :D sorry for my bad english

kind regards
johannes
Previous Topic:Session timeout
Next Topic:TableEditors don't scroll with table content
Goto Forum:
  


Current Time: Sat Dec 04 06:01:16 GMT 2021

Powered by FUDForum. Page generated in 0.02227 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top