Search in sources :

Example 1 with FileInputConfig

use of de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.FileInputConfig in project ocvn by devgateway.

the class FileInputBootstrapFormComponentWrapper method addBootstrapFileInputComponent.

private void addBootstrapFileInputComponent() {
    // this is where the newly uploaded files are saved
    final IModel<List<FileUpload>> internalUploadModel = new ListModel<>();
    /*
         * some customization of the BootstrapFileInput Component
         */
    FileInputConfig fileInputConfig = new FileInputConfig();
    fileInputConfig.put(new Key<String>("browseLabel"), new StringResourceModel("browseLabel", FileInputBootstrapFormComponentWrapper.this, null).getString());
    fileInputConfig.put(new Key<String>("uploadClass"), "btn btn-blue");
    fileInputConfig.put(new Key<String>("browseClass"), "btn btn-blue");
    bootstrapFileInput = new BootstrapFileInput("bootstrapFileInput", internalUploadModel, fileInputConfig) {

        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        @Override
        protected void onSubmit(final AjaxRequestTarget target) {
            super.onSubmit(target);
            List<FileUpload> fileUploads = internalUploadModel.getObject();
            if (fileUploads != null) {
                // check if we uploaded too many files
                if (maxFiles > 0 && filesModel.size() + fileUploads.size() > maxFiles) {
                    if (maxFiles == 1) {
                        FileInputBootstrapFormComponentWrapper.this.fatal(new StringResourceModel("OneUpload", FileInputBootstrapFormComponentWrapper.this, null).getString());
                    } else {
                        FileInputBootstrapFormComponentWrapper.this.fatal(new StringResourceModel("tooManyFiles", FileInputBootstrapFormComponentWrapper.this, Model.of(maxFiles)).getString());
                    }
                    FileInputBootstrapFormComponentWrapper.this.invalid();
                } else {
                    // and update the model
                    for (FileUpload upload : fileUploads) {
                        FileMetadata fileMetadata = new FileMetadata();
                        fileMetadata.setName(upload.getClientFileName());
                        fileMetadata.setContentType(upload.getContentType());
                        fileMetadata.setSize(upload.getSize());
                        FileContent fileContent = new FileContent();
                        fileContent.setBytes(upload.getBytes());
                        fileMetadata.setContent(fileContent);
                        filesModel.add(fileMetadata);
                    // don't display the success notification
                    // FileInputBootstrapFormComponentWrapper.this.success(new
                    // StringResourceModel("successUpload",
                    // FileInputBootstrapFormComponentWrapper.this,
                    // null, new
                    // Model(upload.getClientFileName())).getString());
                    }
                }
            }
            FileInputBootstrapFormComponentWrapper.this.getModel().setObject((T) filesModel);
            target.add(fileUploadFeedback);
            target.add(pendingFiles);
        }
    };
    add(bootstrapFileInput);
    /**
     * due to an upgrade of FormGroup in wicket7/wicket-bootrap-0.10, the
     * visitor that finds inner FormComponentS, will now find two instead of
     * one: the FileInputBootstrapFormComponentWrapper and also the
     * BootstrapFileInputField. This is the RIGHT result, previously in
     * wicket 6.x it only got the first level of children, hence only one
     * FormComponent (the FileInputBootstrapFormComponentWrapper). It would
     * then read the label from FileInputBootstrapFormComponentWrapper and
     * use it for displaying the label of the FormGroup. In
     * wicket7/wicket-bootstrap-0.10 this will result in reading the label
     * of BootstrapFileInputField which is null. So you will notice no
     * labels for FormGroupS. We fix this by forcing the label of the
     * underlying fileInput element to the same model as the label used by
     * FileInputBootstrapFormComponentWrapper
     */
    FormComponent<?> fileInput = (FormComponent<?>) bootstrapFileInput.get("fileInputForm").get("fileInput");
    fileInput.setLabel(this.getLabel());
    // only to admins
    if (visibleOnlyToAdmin) {
        MetaDataRoleAuthorizationStrategy.authorize(bootstrapFileInput, Component.RENDER, SecurityConstants.Roles.ROLE_ADMIN);
    }
    // want to read only
    if (disableDeleteButton) {
        MetaDataRoleAuthorizationStrategy.authorize(bootstrapFileInput, Component.RENDER, MetaDataRoleAuthorizationStrategy.NO_ROLE);
    }
}
Also used : FormComponent(org.apache.wicket.markup.html.form.FormComponent) FileMetadata(org.devgateway.toolkit.persistence.dao.FileMetadata) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) FileContent(org.devgateway.toolkit.persistence.dao.FileContent) ListModel(org.apache.wicket.model.util.ListModel) BootstrapFileInput(de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.BootstrapFileInput) ArrayList(java.util.ArrayList) List(java.util.List) FileInputConfig(de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.FileInputConfig) StringResourceModel(org.apache.wicket.model.StringResourceModel) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Example 2 with FileInputConfig

use of de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.FileInputConfig in project oc-explorer by devgateway.

the class FileInputBootstrapFormComponentWrapper method addBootstrapFileInputComponent.

private void addBootstrapFileInputComponent() {
    // this is where the newly uploaded files are saved
    final IModel<List<FileUpload>> internalUploadModel = new ListModel<>();
    /*
         * some customization of the BootstrapFileInput Component
         */
    FileInputConfig fileInputConfig = new FileInputConfig();
    fileInputConfig.put(new Key<String>("browseLabel"), new StringResourceModel("browseLabel", FileInputBootstrapFormComponentWrapper.this, null).getString());
    fileInputConfig.put(new Key<String>("uploadClass"), "btn btn-blue");
    fileInputConfig.put(new Key<String>("browseClass"), "btn btn-blue");
    bootstrapFileInput = new BootstrapFileInput("bootstrapFileInput", internalUploadModel, fileInputConfig) {

        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        @Override
        protected void onSubmit(final AjaxRequestTarget target) {
            super.onSubmit(target);
            List<FileUpload> fileUploads = internalUploadModel.getObject();
            if (fileUploads != null) {
                // check if we uploaded too many files
                if (maxFiles > 0 && filesModel.size() + fileUploads.size() > maxFiles) {
                    if (maxFiles == 1) {
                        FileInputBootstrapFormComponentWrapper.this.fatal(new StringResourceModel("OneUpload", FileInputBootstrapFormComponentWrapper.this, null).getString());
                    } else {
                        FileInputBootstrapFormComponentWrapper.this.fatal(new StringResourceModel("tooManyFiles", FileInputBootstrapFormComponentWrapper.this, Model.of(maxFiles)).getString());
                    }
                    FileInputBootstrapFormComponentWrapper.this.invalid();
                } else {
                    // and update the model
                    for (FileUpload upload : fileUploads) {
                        FileMetadata fileMetadata = new FileMetadata();
                        fileMetadata.setName(upload.getClientFileName());
                        fileMetadata.setContentType(upload.getContentType());
                        fileMetadata.setSize(upload.getSize());
                        FileContent fileContent = new FileContent();
                        fileContent.setBytes(upload.getBytes());
                        fileMetadata.setContent(fileContent);
                        filesModel.add(fileMetadata);
                    // don't display the success notification
                    // FileInputBootstrapFormComponentWrapper.this.success(new
                    // StringResourceModel("successUpload",
                    // FileInputBootstrapFormComponentWrapper.this,
                    // null, new
                    // Model(upload.getClientFileName())).getString());
                    }
                }
            }
            FileInputBootstrapFormComponentWrapper.this.getModel().setObject((T) filesModel);
            target.add(fileUploadFeedback);
            target.add(pendingFiles);
        }
    };
    add(bootstrapFileInput);
    /**
     * due to an upgrade of FormGroup in wicket7/wicket-bootrap-0.10, the
     * visitor that finds inner FormComponentS, will now find two instead of
     * one: the FileInputBootstrapFormComponentWrapper and also the
     * BootstrapFileInputField. This is the RIGHT result, previously in
     * wicket 6.x it only got the first level of children, hence only one
     * FormComponent (the FileInputBootstrapFormComponentWrapper). It would
     * then read the label from FileInputBootstrapFormComponentWrapper and
     * use it for displaying the label of the FormGroup. In
     * wicket7/wicket-bootstrap-0.10 this will result in reading the label
     * of BootstrapFileInputField which is null. So you will notice no
     * labels for FormGroupS. We fix this by forcing the label of the
     * underlying fileInput element to the same model as the label used by
     * FileInputBootstrapFormComponentWrapper
     */
    FormComponent<?> fileInput = (FormComponent<?>) bootstrapFileInput.get("fileInputForm").get("fileInput");
    fileInput.setLabel(this.getLabel());
    // only to admins
    if (visibleOnlyToAdmin) {
        MetaDataRoleAuthorizationStrategy.authorize(bootstrapFileInput, Component.RENDER, SecurityConstants.Roles.ROLE_ADMIN);
    }
    // want to read only
    if (disableDeleteButton) {
        MetaDataRoleAuthorizationStrategy.authorize(bootstrapFileInput, Component.RENDER, MetaDataRoleAuthorizationStrategy.NO_ROLE);
    }
}
Also used : FormComponent(org.apache.wicket.markup.html.form.FormComponent) FileMetadata(org.devgateway.toolkit.persistence.dao.FileMetadata) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) FileContent(org.devgateway.toolkit.persistence.dao.FileContent) ListModel(org.apache.wicket.model.util.ListModel) BootstrapFileInput(de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.BootstrapFileInput) ArrayList(java.util.ArrayList) List(java.util.List) FileInputConfig(de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.FileInputConfig) StringResourceModel(org.apache.wicket.model.StringResourceModel) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Aggregations

BootstrapFileInput (de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.BootstrapFileInput)2 FileInputConfig (de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.FileInputConfig)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)2 FormComponent (org.apache.wicket.markup.html.form.FormComponent)2 FileUpload (org.apache.wicket.markup.html.form.upload.FileUpload)2 StringResourceModel (org.apache.wicket.model.StringResourceModel)2 ListModel (org.apache.wicket.model.util.ListModel)2 FileContent (org.devgateway.toolkit.persistence.dao.FileContent)2 FileMetadata (org.devgateway.toolkit.persistence.dao.FileMetadata)2