Example usage for org.apache.wicket.ajax.markup.html.form AjaxSubmitLink setMarkupId

List of usage examples for org.apache.wicket.ajax.markup.html.form AjaxSubmitLink setMarkupId

Introduction

In this page you can find the example usage for org.apache.wicket.ajax.markup.html.form AjaxSubmitLink setMarkupId.

Prototype

final void setMarkupId(Component comp) 

Source Link

Document

Copy markupId

Usage

From source file:org.cast.isi.panel.FreeToolbar.java

License:Open Source License

public FreeToolbar(String id) {
    super(id);//from  w  w  w .  jav a2  s.c  o m

    dictionaryModal = new SidebarDialog("dictionaryModal", "Dictionary", "thtDictionary");
    add(dictionaryModal);

    // I'm a little puzzled as to why this extra WMC is necessary, but without it the definition can 
    // only be ajax-updated once, after which it loses its markupId.
    WebMarkupContainer body = new WebMarkupContainer("body");
    dictionaryModal.getBodyContainer().add(body);

    definition = new Label("definition", mDefinition);
    definition.setOutputMarkupId(true);
    definition.setEscapeModelStrings(false);
    body.add(definition);

    link = new ExternalLink("wordnikLink", new Model<String>(""));
    link.setOutputMarkupId(true);
    body.add(link);

    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.add(definition);
            target.add(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(Component c, 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);
}