Search in sources :

Example 16 with FileUploadField

use of org.apache.wicket.markup.html.form.upload.FileUploadField in project projectforge by micromata.

the class SetupImportForm method init.

@Override
@SuppressWarnings("serial")
protected void init() {
    FeedbackPanel feedbackPanel = createFeedbackPanel();
    feedbackPanel.setOutputMarkupId(true);
    add(feedbackPanel);
    this.setOutputMarkupId(true);
    // set max size
    Bytes maxSize = Bytes.valueOf(configurationService.getMaxFileSizeXmlDumpImport());
    this.setMaxSize(maxSize);
    this.setMultiPart(true);
    final GridBuilder gridBuilder = newGridBuilder(this, "flowform");
    gridBuilder.newFormHeading(getString("import"));
    {
        // Upload dump file
        final FieldsetPanel fs = gridBuilder.newFieldset(getString("administration.setup.dumpFile"));
        fileUploadField = new FileUploadField(FileUploadPanel.WICKET_ID);
        fs.add(new FileUploadPanel(fs.newChildId(), fileUploadField));
    }
    final RepeatingView actionButtons = new RepeatingView("buttons");
    add(actionButtons);
    {
        final Button importButton = new Button(SingleButtonPanel.WICKET_ID, new Model<String>("import")) {

            @Override
            public final void onSubmit() {
                csrfTokenHandler.onSubmit();
                parentPage.upload();
            }
        };
        final SingleButtonPanel importButtonPanel = new SingleButtonPanel(actionButtons.newChildId(), importButton, getString("import"), SingleButtonPanel.DEFAULT_SUBMIT);
        actionButtons.add(importButtonPanel);
    }
    this.fileUploadField.add(new FileFieldSizeCheckBehavior() {

        @Override
        protected void onSubmit(final AjaxRequestTarget target, final FileList fileList) {
            if (fileList.getNumOfFiles() == 1) {
                if (false == (fileList.get(0).getName().endsWith(".xml.gz") || fileList.get(0).getName().endsWith(".xml")))
                    SetupImportForm.this.addError("common.uploadpanel.filewrongtype", ".xml; .xml.gz");
            }
            // return form to remove errors
            target.add(feedbackPanel);
        }

        @Override
        protected void addErrorMsg(AjaxRequestTarget target, FileList fileList) {
            SetupImportForm.this.addError("common.uploadpanel.filetolarge", NumberHelper.formatBytes(maxSize.bytes()));
        }

        @Override
        protected void onError(final AjaxRequestTarget target, final FileList fileList) {
            // clear input field to avoid uploading the image
            SetupImportForm.this.fileUploadField.clearInput();
            // return form
            target.add(SetupImportForm.this);
        }
    });
}
Also used : FileList(org.wicketstuff.html5.fileapi.FileList) RepeatingView(org.apache.wicket.markup.repeater.RepeatingView) FileUploadField(org.apache.wicket.markup.html.form.upload.FileUploadField) GridBuilder(org.projectforge.web.wicket.bootstrap.GridBuilder) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) Bytes(org.apache.wicket.util.lang.Bytes) FeedbackPanel(org.apache.wicket.markup.html.panel.FeedbackPanel) Button(org.apache.wicket.markup.html.form.Button) FileFieldSizeCheckBehavior(org.wicketstuff.html5.fileapi.FileFieldSizeCheckBehavior) Model(org.apache.wicket.model.Model) FieldsetPanel(org.projectforge.web.wicket.flowlayout.FieldsetPanel) FileUploadPanel(org.projectforge.web.wicket.flowlayout.FileUploadPanel) SingleButtonPanel(org.projectforge.web.wicket.components.SingleButtonPanel)

Aggregations

FileUploadField (org.apache.wicket.markup.html.form.upload.FileUploadField)16 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)8 FieldsetPanel (org.projectforge.web.wicket.flowlayout.FieldsetPanel)7 FileUploadPanel (org.projectforge.web.wicket.flowlayout.FileUploadPanel)7 Button (org.apache.wicket.markup.html.form.Button)6 SingleButtonPanel (org.projectforge.web.wicket.components.SingleButtonPanel)6 Model (org.apache.wicket.model.Model)5 DivPanel (org.projectforge.web.wicket.flowlayout.DivPanel)5 List (java.util.List)4 AjaxSubmitButton (com.evolveum.midpoint.web.component.AjaxSubmitButton)3 InputStream (java.io.InputStream)3 Date (java.util.Date)3 AjaxFormSubmitBehavior (org.apache.wicket.ajax.form.AjaxFormSubmitBehavior)3 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)3 Form (org.apache.wicket.markup.html.form.Form)3 AjaxDownloadBehaviorFromStream (com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromStream)2 MidpointForm (com.evolveum.midpoint.web.component.form.MidpointForm)2 VisibleEnableBehaviour (com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2