Search in sources :

Example 1 with FileWidgetUpload

use of com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload in project wcomponents by BorderTech.

the class WMultiFileWidgetRenderer method handleFileUploadRequest.

/**
 * Paint the response for the file uploaded in this request.
 *
 * @param widget the file widget to render
 * @param xml the XML string builder
 * @param uploadId the file id uploaded in this request
 */
protected void handleFileUploadRequest(final WMultiFileWidget widget, final XmlStringBuilder xml, final String uploadId) {
    FileWidgetUpload file = widget.getFile(uploadId);
    if (file == null) {
        throw new SystemException("Invalid file id [" + uploadId + "] to render uploaded response.");
    }
    int idx = widget.getFiles().indexOf(file);
    FileWidgetRendererUtil.renderFileElement(widget, xml, file, idx);
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) FileWidgetUpload(com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload)

Example 2 with FileWidgetUpload

use of com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload in project wcomponents by BorderTech.

the class WMultiFileWidgetExample method appendFileDetails.

/**
 * Appends details of the uploaded files to the given string buffer.
 *
 * @param buf the buffer to append file details to.
 * @param fileWidget the WFieldWidget to obtain the files from.
 */
private void appendFileDetails(final StringBuffer buf, final WMultiFileWidget fileWidget) {
    List<FileWidgetUpload> files = fileWidget.getFiles();
    if (files != null) {
        for (FileWidgetUpload file : files) {
            String streamedSize;
            try {
                InputStream in = file.getFile().getInputStream();
                int size = 0;
                while (in.read() >= 0) {
                    size++;
                }
                streamedSize = String.valueOf(size);
            } catch (IOException e) {
                streamedSize = e.getMessage();
            }
            buf.append("Name: ").append(file.getFile().getName());
            buf.append("\nSize: ").append(streamedSize).append(" bytes\n\n");
        }
    }
}
Also used : InputStream(java.io.InputStream) FileWidgetUpload(com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload) IOException(java.io.IOException)

Example 3 with FileWidgetUpload

use of com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload in project wcomponents by BorderTech.

the class WMultiFileWidgetRenderer method doRender.

/**
 * Paints the given WMultiFileWidget.
 *
 * @param component the WMultiFileWidget to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WMultiFileWidget widget = (WMultiFileWidget) component;
    XmlStringBuilder xml = renderContext.getWriter();
    // Check if rendering a file upload response
    String uploadId = widget.getFileUploadRequestId();
    if (uploadId != null) {
        handleFileUploadRequest(widget, xml, uploadId);
        return;
    }
    boolean readOnly = widget.isReadOnly();
    xml.appendTagOpen(TAG_NAME);
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("hidden", widget.isHidden(), "true");
    if (readOnly) {
        xml.appendAttribute("readOnly", "true");
    } else {
        long maxFileSize = widget.getMaxFileSize();
        int maxFiles = widget.getMaxFiles();
        WComponent dropzone = widget.getDropzone();
        WImageEditor editor = widget.getEditor();
        xml.appendOptionalAttribute("disabled", widget.isDisabled(), "true");
        xml.appendOptionalAttribute("required", widget.isMandatory(), "true");
        xml.appendOptionalAttribute("toolTip", widget.getToolTip());
        xml.appendOptionalAttribute("accessibleText", widget.getAccessibleText());
        xml.appendOptionalAttribute("acceptedMimeTypes", typesToString(widget.getFileTypes()));
        xml.appendOptionalAttribute("maxFileSize", maxFileSize > 0, maxFileSize);
        xml.appendOptionalAttribute("maxFiles", maxFiles > 0, maxFiles);
        if (dropzone != null) {
            xml.appendAttribute("dropzone", dropzone.getId());
        }
        if (editor != null) {
            xml.appendAttribute("editor", editor.getId());
            if (editor.getUseCamera()) {
                xml.appendAttribute("camera", true);
            }
        }
    }
    if (widget.getColumns() != null) {
        xml.appendAttribute("cols", widget.getColumns());
    }
    if (widget.getFileAjaxAction() != null) {
        xml.appendAttribute("ajax", "true");
    }
    List<Diagnostic> diags = readOnly ? null : widget.getDiagnostics(Diagnostic.ERROR);
    if (widget.getFiles().isEmpty()) {
        if (readOnly || diags == null || diags.isEmpty()) {
            xml.appendEnd();
            return;
        }
        xml.appendClose();
    } else {
        xml.appendClose();
        // Render files
        int i = 0;
        for (FileWidgetUpload file : widget.getFiles()) {
            FileWidgetRendererUtil.renderFileElement(widget, xml, file, i++);
        }
    }
    if (!readOnly && diags != null && !diags.isEmpty()) {
        DiagnosticRenderUtil.renderDiagnostics(widget, renderContext);
    }
    xml.appendEndTag(TAG_NAME);
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WImageEditor(com.github.bordertech.wcomponents.WImageEditor) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) WMultiFileWidget(com.github.bordertech.wcomponents.WMultiFileWidget) FileWidgetUpload(com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 4 with FileWidgetUpload

use of com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload in project wcomponents by BorderTech.

the class WMultiFileWidgetRenderer_Test method testDoPaint.

@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
    WMultiFileWidget fileUpload = new WMultiFileWidget();
    assertSchemaMatch(fileUpload);
    assertXpathEvaluatesTo(fileUpload.getId(), "//ui:multifileupload/@id", fileUpload);
    assertXpathEvaluatesTo("10240000", "//ui:multifileupload/@maxFileSize", fileUpload);
    assertXpathNotExists("//ui:multifileupload/@disabled", fileUpload);
    assertXpathNotExists("//ui:multifileupload/@hidden", fileUpload);
    assertXpathNotExists("//ui:multifileupload/@required", fileUpload);
    assertXpathNotExists("//ui:multifileupload/@readOnly", fileUpload);
    assertXpathNotExists("//ui:multifileupload/@toolTip", fileUpload);
    assertXpathNotExists("//ui:multifileupload/@accessibleText", fileUpload);
    assertXpathNotExists("//ui:multifileupload/@acceptedMimeTypes", fileUpload);
    assertXpathNotExists("//ui:multifileupload/@maxFiles", fileUpload);
    assertXpathNotExists("//ui:multifileupload/ui:file", fileUpload);
    fileUpload.setDisabled(true);
    assertSchemaMatch(fileUpload);
    assertXpathEvaluatesTo("true", "//ui:multifileupload/@disabled", fileUpload);
    setFlag(fileUpload, ComponentModel.HIDE_FLAG, true);
    assertSchemaMatch(fileUpload);
    assertXpathEvaluatesTo("true", "//ui:multifileupload/@hidden", fileUpload);
    fileUpload.setMandatory(true);
    assertSchemaMatch(fileUpload);
    assertXpathEvaluatesTo("true", "//ui:multifileupload/@required", fileUpload);
    fileUpload.setToolTip("tooltip");
    assertSchemaMatch(fileUpload);
    assertXpathEvaluatesTo(fileUpload.getToolTip(), "//ui:multifileupload/@toolTip", fileUpload);
    fileUpload.setAccessibleText("accessible");
    assertSchemaMatch(fileUpload);
    assertXpathEvaluatesTo(fileUpload.getAccessibleText(), "//ui:multifileupload/@accessibleText", fileUpload);
    fileUpload.setFileTypes(new String[] { "a/b", "c/d" });
    assertXpathEvaluatesTo("a/b,c/d", "//ui:multifileupload/@acceptedMimeTypes", fileUpload);
    fileUpload.setMaxFileSize(12345);
    assertXpathEvaluatesTo("12345", "//ui:multifileupload/@maxFileSize", fileUpload);
    fileUpload.setMaxFiles(11);
    assertXpathEvaluatesTo("11", "//ui:multifileupload/@maxFiles", fileUpload);
    // Test file rendering
    MockFileItem fileItem = new MockFileItem();
    fileItem.setName("test.bin");
    fileItem.setContentType("application/octet-stream");
    fileItem.set(new byte[123]);
    FileWidgetUpload file = new FileWidgetUpload("X", new FileItemWrap(fileItem));
    fileUpload.setData(Arrays.asList(file));
    assertSchemaMatch(fileUpload);
    assertXpathEvaluatesTo("1", "count(//ui:multifileupload/ui:file)", fileUpload);
    assertXpathEvaluatesTo("X", "//ui:multifileupload/ui:file/@id", fileUpload);
    assertXpathEvaluatesTo(fileItem.getName(), "//ui:multifileupload/ui:file/@name", fileUpload);
    assertXpathEvaluatesTo(fileItem.getContentType(), "//ui:multifileupload/ui:file/@type", fileUpload);
    assertXpathEvaluatesTo(String.valueOf(fileItem.getSize()), "//ui:multifileupload/ui:file/@size", fileUpload);
}
Also used : WMultiFileWidget(com.github.bordertech.wcomponents.WMultiFileWidget) FileWidgetUpload(com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload) MockFileItem(com.github.bordertech.wcomponents.util.mock.MockFileItem) FileItemWrap(com.github.bordertech.wcomponents.file.FileItemWrap) Test(org.junit.Test)

Example 5 with FileWidgetUpload

use of com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload in project wcomponents by BorderTech.

the class WMultiFileWidget_Test method testGetFile.

@Test
public void testGetFile() {
    WMultiFileWidget widget = new WMultiFileWidget();
    widget.setLocked(true);
    setActiveContext(createUIContext());
    // Null
    FileWidgetUpload file = widget.getFile("X");
    Assert.assertNull("Shuld be null for invalid file id", file);
    // Set file1, file2 as uploaded
    widget.setData(UPLOADED_1_2);
    Assert.assertEquals("File2 should be returned for index 1", TEST_FILE_ITEM_WRAP2, widget.getFile("2"));
}
Also used : FileWidgetUpload(com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload) Test(org.junit.Test)

Aggregations

FileWidgetUpload (com.github.bordertech.wcomponents.WMultiFileWidget.FileWidgetUpload)5 WMultiFileWidget (com.github.bordertech.wcomponents.WMultiFileWidget)2 Test (org.junit.Test)2 WComponent (com.github.bordertech.wcomponents.WComponent)1 WImageEditor (com.github.bordertech.wcomponents.WImageEditor)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1 FileItemWrap (com.github.bordertech.wcomponents.file.FileItemWrap)1 SystemException (com.github.bordertech.wcomponents.util.SystemException)1 MockFileItem (com.github.bordertech.wcomponents.util.mock.MockFileItem)1 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1