GWT and re-Captcha

140 views
Skip to first unread message

vacorda

unread,
Oct 10, 2009, 9:23:08 PM10/10/09
to Google Web Toolkit
This is sort of a refinement to:
http://groups.google.com/group/google-web-toolkit/browse_thread/thread/99c259aad06406c6/f7fb27b8fa47760d?lnk=gst&q=GWT+recaptcha#f7fb27b8fa47760d

The idea here is I wanted a captcha control that I can insert in my
GWT Dialogs. I have pasted the composite below to share. A little bit
of a hack because of the creation of "Label dummyLable = new Label();"
and manipulating the Element to set the ID attribute. Prerequisite to
using this control is to load the reCaptcha script:

<script type="text/javascript" src="http://api.recaptcha.net/js/
recaptcha_ajax.js"></script>

public class CaptchaComposite extends Composite {
private Grid grid = new Grid(1, 1);
private static final String DIV_NAME = "recaptcha_div";

public CaptchaComposite() {

this.initWidget(grid);

Label dummyLable = new Label();
grid.setWidget(0, 0, dummyLable);
Element divElement = dummyLable.getElement();
divElement.setAttribute("id", DIV_NAME);
}

private void createChallenge() {
createNewChallengeJSNI();
}

public String getChallengeField() {
String value = getChallengeJSNI();
PMLogger.debug("challenge=" + value);
return value;
}
public String getResponse() {
String value = getResponseJSNI();
PMLogger.debug("response=" + value);
return value;
}


private native String getResponseJSNI()
/*-{
return $wnd.Recaptcha.get_response();
}-*/;

private native String getChallengeJSNI()
/*-{
return $wnd.Recaptcha.get_challenge();
}-*/;

private native void createNewChallengeJSNI()
/*-{
$wnd.Recaptcha.create("<your public key here>",
"recaptcha_div", {
theme: "red",
callback: $wnd.Recaptcha.focus_response_field
});
}-*/;

private native void reloadChallengeJSNI()
/*-{
$wnd.Recaptcha.reload();
}-*/;

private native void destroyJSNI()
/*-{
$wnd.Recaptcha.destroy();
}-*/;

public void createNewChallenge() {
reloadChallengeJSNI();
}

public void destroyCaptcha() {
destroyJSNI();
}

@Override
protected void onAttach() {
super.onAttach();
createChallenge();
}

}
Reply all
Reply to author
Forward
0 new messages