Yes, you can do it very easy using two nice libraries I contribute with:
gwtquery and gwtexporter
1.- Use gquery to find widgets in the DOM using css selectors
2.- Use gwtexporter to expose widget methods and and gquery finders
I've committed a maven project [1] to the gwtexporter repository with a
very simple example so as you can use it as reference (I have used the
gquery archetype btw) .
As you can see in the complete code [2] I use a class to export some static
methods, which will be exposed to javascript using gwtexporter generator:
@ExportPackage("")
@Export("js")
public static class Exposer implements Exportable {
public static Widget[] getWidgets(String selector) {
return $(selector).widgets().toArray(new Widget[0]);
}
}
Then it is necessary to expose the methods you want from your widgets, I
use the ExportOverlay feature in gwtexporter so as I do not modify the
original widget (gwt Button in this case)
@ExportPackage("js")
@Export
public static interface ButtonOverLay extends ExportOverlay<Button> {
void setText(String s);
void click();
}
Finally you can use js in this way to find widgets and call theirs methods
<script>
var widget = js.getWidgets(".gwt-Button");
var button = new js.Button(widget);
button.click();
</script>
[1] svn checkout
http://gwt-exporter.googlecode.com/svn/examples/export-widgets
[2]
http://code.google.com/p/gwt-exporter/source/browse/examples/export-widgets/src/main/java/org/gquery/ewidget/client/ExportWidget.java
- Manolo
On Fri, May 4, 2012 at 2:15 PM, Alexander Kozyncev <[email protected]>wrote:
> General Aim: find widget's javascript object and call it's exported
> java method
>
> Tried: gwt-2.4.0 (with and without gwt-exporter 2.3.0)
>
> Description:
> To find gwt's widget on DOM in Java Applet code and call it's java
> method(non static)
> <pre><code>
> JSObject win = JSObject.getWindow(this);
> JSObject doc = (JSObject) win.getMember("document");
> JSObject gwtWidget = (JSObject) doc.call("getElementsByName",
> widgetName);
>
> //todo: have possibility to call `exported` java method over here,
> smth like:
> //Object[] params = new Object[1];
> //params[0] = widgetName;
> //Object result = gwtWidget.call("exportedJavaMethod", params);
>
> //todo: or just call global ($wnd) static JSNI bridge method:
> //Object result = win.call("exportedJavaMethod", params);
> //
> </code></pre>
>
> The problem is: I can find by widget's id not the widget, but it's
> DivElement which does not have any exported instanced methods.
>
> My widget class is Exportable (gwt-export):
> <pre><code>
> @Export(value="somewidget")
> public class SomeWidget extends SimplePanel implements ..., Exportable
> {
> private final String id = "id_some_widget_" + count++;
> private static int count = 0;
>
> public SomeWidget () {
> getElement().setAttribute("name", id);
> getElement().setId(id);
> }
> ...
>
> public static Element getInstanceById(String elementId) {
> Element someWidget= DOM.getElementById(elementId);
> return someWidget;
> }
>
> public String getSomeInstancedData() {
> return "useful_inner_data" + this.id;
> }
> </code></pre>
>
> So, for example I'd like to find the concrete widget added to DOM and
> call nonstatic method `getSomeInstancedData()` in javascript. Is it
> possible at all?
>
> Suppose smth like:
> <pre><code>
> var someWidget = document.getElementById(widgetId);
> alert(someWidget.getSomeInstancedData());
>
> //or:
> var someWidgetExported =
> com.mypackage.widgets.somewidget.getInstanceById(listenerId);
> alert(someWidgetExported.getSomeInstancedData());
> </code></pre>
>
> In Base module I write:
> <pre><code>
> ExporterUtil.exportAll();
> </code></pre>
>
> There is a View(ViewWithSomeWidget.ui.xml) that contains this widget:
> <pre>
> ...
> base:Form
> base:SomeWidget ui:field="someWidget" ...
> ...
> /base:SomeWidget
> ...
> </pre>
>
> When SomeWidget does not implement Exportable, project runs well, but
> I couldn't call nonstatic methods of found widget's DIV element.
>
> By the time, to solve the problem SomeWidget implements Exportable,
> but progect doesn't show View with the SomeWidget well because of
> ClassCastException using deferred binding:
> <pre><code>
> ClassCastException: com.mypackage.widgets.SomeWidgetExporterImpl
> cannot be cast to com.mypackage.widgets.SomeWidget
> </code></pre>
>
> So, probably there are any other methods to find widget's javascript
> object and call it's exported java method? In any ways, any idea is
> much appreciated.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.