Form<Void> toolbarForm = new Form<Void>("toolbarForm");
add(toolbarForm);
toolbarForm.add(selectedText = new HiddenField<String>("selectedText", new Model<String>("")));
AjaxSubmitLink dictionary = new AjaxSubmitLink("dictionary") {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
String word = selectedText.getModelObject();
List<Definition> defs = getDefinition(word);
mDefinition.setObject(formatDefinition(word, defs));
if (defs!=null && !defs.isEmpty()) {
String canonicalWord = defs.get(0).getWord();
link.setDefaultModelObject(WORDNIK_BASE_URL+canonicalWord);
link.setVisibilityAllowed(true);
} else {
link.setVisibilityAllowed(false);
}
target.addComponent(definition);
target.addComponent(link);
}
@Override
protected IAjaxCallDecorator getAjaxCallDecorator() {
return new AjaxCallDecorator() {
private static final long serialVersionUID = 1L;
// Several jobs for the Javascript:
// 1. Get the text selection and put it into a form field for transmission to server
// 2. Remove old definition that may be in modal window, replace with "Please wait"
// 3. Open modal
// 4. Do normal script, which means AJAX-submitting form to server.
// FIXME: Please wait should be a string property
// FIXME: does not properly get text selection from inside IFrame (e.g. TinyMCE)
@Override
public CharSequence decorateScript(final CharSequence script) {
return "$('#selectedText').val(getSelectedText());"
+ "$('#" + dictionaryModal.getMarkupId() + " .definitionContainer').text('Please wait...');"
+ dictionaryModal.getOpenString()
+ script
;
}
};
}
};
dictionary.setMarkupId("thtDictionary"); // id expected by CSS
toolbarForm.add (dictionary);
}