JSNI access to non-static inner class?

410 views
Skip to first unread message

chris.f.jones

unread,
Jan 24, 2008, 2:51:34 PM1/24/08
to Google Web Toolkit
Anyone had luck with calling methods on a non-static inner class via
JSNI?

I can get access to the member variable 'suggestionPopup' within
SuggestBox via:
suggestBox. @com.google.gwt.user.client.ui.SuggestBox::suggestionPopup

but I can't figure out how to describe its type (which is a non-static
named inner class within SuggestBox called SuggestionPopup).
neither
@com.google.gwt.user.client.ui.SuggestBox.SuggestionPopup::isAttached()
()
nor
@com.google.gwt.user.client.ui.SuggestBox$SuggestionPopup::isAttached()
()
seem to work

My use case here is that I just want to show all possible suggestions
when the user presses down in an empty suggest box (but not if the
popup is already showing), so there may be an easier way to solve that
instead.

chris.f.jones

unread,
Jan 24, 2008, 2:54:50 PM1/24/08
to Google Web Toolkit
Partial Answer:
I was able to get around this by describing the popup instance as the
well-known Widget class instead, since thats where the isAttached()
method lives anyway.
suggestBox.
@com.google.gwt.user.client.ui.SuggestBox::suggestionPopup.@com.google.gwt.user.client.ui.Widget::isAttached()
()

I still have no idea how to call the methods defined only in the non-
static inner class, but I was able to get the desired functionality by
referencing it as a widget.

walden

unread,
Jan 24, 2008, 3:38:25 PM1/24/08
to Google Web Toolkit
Chris,

A couple of comments:

1. One beauty of GWT is that if you need to write code to enhance
existing widgets, you can do it in Java. Why are you taking the JSNI
approach to this problem?

2. If showing all possible results in your SuggestBox is a viable
option, I would say you're better off using a ListBox. I'm not sure
what you mean by "user presses down in an empty suggest box", but I
think you're talking about no characters typed yet. If you would show
all possible results on nothing but a "press", why not just keep all
possible results there in a list? Or am I missing something?

Walden


On Jan 24, 2:54 pm, "chris.f.jones" <chris.f.jo...@gmail.com> wrote:
> Partial Answer:
> I was able to get around this by describing the popup instance as the
> well-known Widget class instead, since thats where the isAttached()
> method lives anyway.
> suggestBox.
> @com.google.gwt.user.client.ui.SuggestBox::suggestionPop...@com.google.gwt.user.client.ui.Widget::isAttached()

lexaux

unread,
Jan 24, 2008, 3:45:27 PM1/24/08
to Google Web Toolkit
Hi Chris!

I'm just wondering if this works, and my answer is rather method to
find the answer than method... :)
But if you want to figure out how static inner class is called inside
js, you can just recompile your project with -PRETTY style, in order
to achieve the viewable and debuggable js.
Then you can use FF with FireBug, place a breakpoint on the line where
host object is visible and inspect its properties via firebug inspect
menu.

I found this method very useful when searching for non-trivial
solutions with GWT.

Alex

chris.f.jones

unread,
Jan 24, 2008, 5:21:29 PM1/24/08
to Google Web Toolkit
Walden:

1. I'd love to subclass or otherwise enhance SuggestBox in java, but
the problem is its only points of flexibility are in the SuggestOracle
and TextBoxBase (SuggestBox itself is declared final and pretty much
everything is private, see
http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/c4a3796e57525301/50ff11d736eb1bb9).
The functionality I'd like to extend is with the SuggestBox itself. So
the only solution I've found is to override SuggestBox's behavior by
breaking its encapsulation to get it to show suggestions and wrapping
MultiWordSuggestOracle to override its behavior in the class of blank
queries (thanks to the suggestion by Nathan Williams in the above
post).

2. By 'press down', I mean press the 'down' button. My use case here
is that sometimes the user might have a small list of things (and they
don't know exactly what they want until they see it) and other times
they have a very large list of things (and they usually know what
they're looking for). A better widget would be gwt-ext's combo box,
but I'd rather stick to the core gwt widgets in this case.


lexaux:

Unfortunately in this case, the special JSNI syntax with the @'s and
Ljava/lang/String's is replaced with real javascript during the
compile process, so its tough to devine the proper syntax from the
compiled result. Ultimately, I guess I'm feeling to lazy too walk
through the compilation process to try and figure it out ;)

Peter Blazejewicz

unread,
Jan 24, 2008, 7:50:48 PM1/24/08
to Google Web Toolkit
hi Chris,

Hello World in rather complicated way:


class:

/**
*
*/
package com.mycompany.project.client;

import com.google.gwt.user.client.Window;

/**
* @author PeterBlazejewicz
*
*/
public class MyClass {
public class MyInnerClass {
public void alert(String msg) {
Window.alert(msg);
}
}

private MyInnerClass alert = new MyInnerClass();

public MyInnerClass getAlert() {
return alert;
}
}


usage:

package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class ImageViewer implements EntryPoint {
private Button clickMeButton;
private MyClass myClass;

public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();

clickMeButton = new Button();
rootPanel.add(clickMeButton);
clickMeButton.setText("Click me!");
clickMeButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
test();
}
});
myClass = new MyClass();
}
private native void test()/*-{
var myClass =
this.@com.mycompany.project.client.ImageViewer::myClass;
var alert =
myClass.@com.mycompany.project.client.MyClass::getAlert()();
alert.@com.mycompany.project.client.MyClass
$MyInnerClass::alert(Ljava/lang/String;)("Hello World!");
}-*/;
}


regards,
Peter

walden

unread,
Jan 25, 2008, 8:02:24 AM1/25/08
to Google Web Toolkit


On Jan 24, 5:21 pm, "chris.f.jones" <chris.f.jo...@gmail.com> wrote:
> Walden:
>
> 1. I'd love to subclass or otherwise enhance SuggestBox in java, but
> the problem is its only points of flexibility are in the SuggestOracle
> and TextBoxBase (SuggestBox itself is declared final and pretty much
> everything is private, seehttp://groups.google.com/group/Google-Web-Toolkit/browse_thread/threa...).
> The functionality I'd like to extend is with the SuggestBox itself. So
> the only solution I've found is to override SuggestBox's behavior by
> breaking its encapsulation to get it to show suggestions and wrapping
> MultiWordSuggestOracle to override its behavior in the class of blank
> queries (thanks to the suggestion by Nathan Williams in the above
> post).

I had a similar problem with TabBar a while back. I ended up copying
a couple of the GWT classes to my own package and modifying the copies
to my taste. Is that an option for you? I understand that copying
code is not a best practice under normal circumstances, but in this
case it may be a "better practice" in that mods to existing Suggest*
classes and extensions to same (in Java) could have a future once
these classes in GWT have been made extensible, while a bunch of JSNI
code to work around existing constraints is destined for the garbage
bin.

But I'm not trying to tweak SuggestBox, so I may not fully understand.

Walden

chris.f.jones

unread,
Jan 25, 2008, 6:41:10 PM1/25/08
to Google Web Toolkit
Peter:
Thanks, I guess the class$innerClass is the right syntax after all.

Walden:
To get the modified behavior I want out of SuggestBox, all I had to
do in JSNI was this:
<code>
private static final native void pleaseShowSuggestions(SuggestBox
suggestBox) /*-{
if (!
suggestBox.@com.google.gwt.user.client.ui.SuggestBox::suggestionPopup.@com.google.gwt.user.client.ui.Widget::isAttached()
()) {

suggestBox.@com.google.gwt.user.client.ui.SuggestBox::showSuggestions(Ljava/
lang/String;)("*");
}
}-*/;
</code>
... which is preferable to copying SuggestBox and its dependents for
my case. Its susceptible to changes in the SuggestBox implementation,
but I've got a nice test around it so that is ok with me ;)

Reply all
Reply to author
Forward
0 new messages