Miguel NoTeimporta
Miguel NoTeimporta

Reputation: 175

Refresh panel in Wicket with BootstrapDownloadLink

My problem is simple but I have no clue how to solve it. I have a feedbackPanel and I want to show an error message if the BootstrapDownloadLink fails. With a submit I could easily do:

protected void onSubmit(AjaxRequestTarget target) {
    ...
    error("File_not_found");  //Wicket will print this on the feedback panel
    target.add(getModalPanel().getFeedbackPanel()); //But i need to refresh it first
}

But the button is inside a panel which I fill with a populateItem and is the only way I know to insert Bootstrap Styles to it. The code of the button:

BootstrapDownloadLink downloadDocument = new BootstrapDownloadLink(IDITEMREPEATER, file) {                                  

    @Override
    public void onClick() {
        File file = (File)getModelObject();
        if(file.exists()) {
            IResourceStream resourceStream = new FileResourceStream(new org.apache.wicket.util.file.File(file));
            getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream, file.getName()));
        } else {
            error(getString("error_fichero_no_existe"));
            /// ???? need to refresh-> getModalPanel().getFeedbackPanel()
        }
    }


};
downloadDocument.setIconType(GlyphIconType.clouddownload);
downloadDocument.add(new AttributeModifier("title", getString("documentos.descargar")));
downloadDocument.add(new AttributeModifier("class", " btn btn-info negrita btn-xs center-block"));
downloadDocument.setVisible(Boolean.TRUE);
list.add(downloadDocument);

Upvotes: 3

Views: 212

Answers (2)

Rob Audenaerde
Rob Audenaerde

Reputation: 20099

You could create or extend from an AjaxDownloadLink, for example like here.

The main idea is to have an AjaxBehavior that does the download, and you get a public void onClick(AjaxRequestTarget target) in which you can add the FeedbackPanel

 downloadBehavior = new AbstractAjaxBehavior()
        {
            private static final long serialVersionUID = 3472918725573624819L;

            @Override
            public void onRequest()
            {
                [...]                   
                ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(
                        AjaxDownloadLink.this.getModelObject(), name);
                handler.setContentDisposition(ContentDisposition.ATTACHMENT);
                getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
            }
        };

And use that behavior in the onclick:

   @Override
    public void onClick(AjaxRequestTarget aTarget)
    {
        String url = downloadBehavior.getCallbackUrl().toString();

        if (addAntiCache) {
            url = url + (url.contains("?") ? "&" : "?");
            url = url + "antiCache=" + System.currentTimeMillis();
        }

        // the timeout is needed to let Wicket release the channel
        aTarget.appendJavaScript("setTimeout(\"window.location.href='" + url + "'\", 100);");
    }

Upvotes: 1

martin-g
martin-g

Reputation: 17533

You can use target.addChildren(getPage(), IFeedback.class). This will add all instances of IFeedback interface in the page to the AjaxRequestTarget.

You can also use FeedbackPanel.class instead of the interface.

Or use getPage().visit(new IVisitor() {...}) to find a specific feedback panel if you don't want to add others which are not related.

Upvotes: 0

Related Questions