Class WMultiFileWidget
- java.lang.Object
-
- com.github.bordertech.wcomponents.AbstractWComponent
-
- com.github.bordertech.wcomponents.WBeanComponent
-
- com.github.bordertech.wcomponents.AbstractInput
-
- com.github.bordertech.wcomponents.WMultiFileWidget
-
- All Implemented Interfaces:
AjaxInternalTrigger
,AjaxTarget
,AjaxTrigger
,BeanAware
,BeanBound
,BeanProviderBound
,DataBound
,Diagnosable
,Disableable
,Input
,Labelable
,Mandatable
,MultiInputComponent
,SubordinateTarget
,Targetable
,WComponent
,WebComponent
,Serializable
public class WMultiFileWidget extends AbstractInput implements Targetable, AjaxInternalTrigger, AjaxTrigger, AjaxTarget, SubordinateTarget, MultiInputComponent
The WMultiFileWidget component allows multiple file input elements to be uploaded, without requiring an entire page reload for each item. After a file is uploaded to the server the client displays the file information with a checkbox adjacent to it. The file information is a link that pops up the file content. Use
getFiles()
to retrieve all files uploaded by the client, usegetSelectedFiles()
to retrieve only the selected file items.If one or more file types is set
setFileTypes(java.util.Collection)
, then each uploaded file will be validated against the accepted list. If accepted list contains any extension(s) then uploaded files will be first checked against them. Developers can choose to have custom validation of file by retrieving it's MIME type, seegetMimeType(java.lang.String)
.The maximum number of allowed files, maximum individual file size, and allowed file types can be configured.
- Since:
- 1.0.0
- Author:
- Christina Harris, Jonathan Austin, Rick Brown, Aswin Kandula
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
WMultiFileWidget.FileWidgetUpload
Holds the uploaded file and extra details.static class
WMultiFileWidget.MultiFileWidgetModel
Holds the extrinsic state information of a WMultiFileWidget.-
Nested classes/interfaces inherited from class com.github.bordertech.wcomponents.AbstractInput
AbstractInput.InputModel
-
Nested classes/interfaces inherited from class com.github.bordertech.wcomponents.AbstractWComponent
AbstractWComponent.WComponentRef
-
-
Field Summary
Fields Modifier and Type Field Description static String
FILE_UPLOAD_ID_KEY
File id for AJAX action.static String
FILE_UPLOAD_MULTI_PART_ID_KEY
File id for file content included in multi part request.static String
FILE_UPLOAD_THUMB_NAIL_KEY
File id thumb nail content request.-
Fields inherited from interface com.github.bordertech.wcomponents.WComponent
DEFAULT_APPLICATION_ID, DEFAULT_INTERNAL_ID, DEFAULT_NO_ID, ID_CONTEXT_SEPERATOR, ID_FRAMEWORK_ASSIGNED_SEPERATOR, ID_VALIDATION_PATTERN
-
-
Constructor Summary
Constructors Constructor Description WMultiFileWidget()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addFile(WMultiFileWidget.FileWidgetUpload file)
Add a file item to this widget.protected void
afterPaint(RenderContext renderContext)
Subclasses may override this method to output content after the component has been painted.protected boolean
beforeHandleRequest(Request request)
Handle before handle request processing.void
clearFiles()
Clear uploaded files from this component.void
clearThumbnails()
Clear the thumbnails currently set on the files.protected Image
createThumbNail(File file)
protected void
doHandleFileAjaxActionRequest(Request request)
Handle a file action AJAX request.protected void
doHandleFileContentRequest(WMultiFileWidget.FileWidgetUpload file)
Handle the file content request.protected boolean
doHandleRequest(Request request)
Specific handle request processing for an input component is provided here.protected void
doHandleTargetedRequest(Request request)
Handle a targeted request.protected void
doHandleThumbnailRequest(WMultiFileWidget.FileWidgetUpload file)
Handle the thumb nail request.protected void
doHandleUploadRequest(Request request)
The request is a targeted file upload request.Integer
getColumns()
protected WMultiFileWidget.MultiFileWidgetModel
getComponentModel()
Returns the effective component model for this component.DropZone
getDropzone()
Return the dropzone associated with this file input.WImageEditor
getEditor()
Return the image editor associated with this file input.WMultiFileWidget.FileWidgetUpload
getFile(String fileId)
Retrieves the File at the given index.Action
getFileAjaxAction()
The AJAX action used when an uploaded file has been selected.List<WMultiFileWidget.FileWidgetUpload>
getFiles()
Gets all the File items that have been uploaded.String
getFileThumbnailUrl(String fileId)
Retrieves a URL for the thumbnail of an uploaded file.List<String>
getFileTypes()
Returns a list of file types accepted by the file input.String
getFileUploadRequestId()
This method is used by the renderer to return the successful upload response.String
getFileUrl(String fileId)
Retrieves a URL for the uploaded file content.int
getMaxFiles()
Return the maximum number of files that can be accepted by this file input.long
getMaxFileSize()
Return the maximum file size (in bytes) that can be accepted by this file input.String
getMimeType(String fileId)
Retrieves an mime type of the uploaded file's contents.protected WMultiFileWidget.MultiFileWidgetModel
getOrCreateComponentModel()
Retrieves the model for this component so that it can be modified.List<WMultiFileWidget.FileWidgetUpload>
getRequestValue(Request request)
Provide the value of the component on the Request.List<WMultiFileWidget.FileWidgetUpload>
getSelectedFiles()
Deprecated.no longer required as unselected files are now removed.String
getTargetId()
The target id returned must be unique across all targetable WComponents within the application.WLink.ImagePosition
getThumbnailPosition()
Dimension
getThumbnailSize()
Retrieve the thumbnail size.List<WMultiFileWidget.FileWidgetUpload>
getValue()
Provide the value of the component returned byDataBound.getData()
in the correct format.String
getValueAsString()
Retrieves a String representation of the input field's value.boolean
hasFileTypes()
boolean
hasMaxFileSize()
protected boolean
isNewUpload()
Used in handle request processing to trigger on change processing.boolean
isSelected(File upload)
Deprecated.no longer required as unselected files are now removed.boolean
isUseThumbnails()
protected WMultiFileWidget.MultiFileWidgetModel
newComponentModel()
Creates a new model appropriate for this component.void
removeFile(WMultiFileWidget.FileWidgetUpload file)
Remove the file.void
setColumns(Integer cols)
Sets the layout of uploaded files to be a certain number of columns.void
setDropzone(DropZone dropzone)
Register a component to receive drag and dropped files on behalf of this input.void
setEditor(WImageEditor editor)
Registers an image editor with this file upload widget so that the user will be prompted to edit (crop, rotate etc).void
setFileAjaxAction(Action action)
The AJAX action used when an uploaded file has been selected.void
setFileTypes(String[] types)
Set each file type to be accepted by the WMultiFileWidget.void
setFileTypes(Collection<String> types)
Determines the file types accepted by this widget.void
setMaxFiles(int maxFiles)
Set the maximum number of files that will be accepted by the file input.void
setMaxFileSize(long bytes)
Set the maximum file size (in bytes) that will be accepted by the file input.void
setNewUpload(boolean newUpload)
void
setThumbnailPosition(WLink.ImagePosition thumbnailPosition)
The position of the thumbnail image on the file link.void
setThumbnailSize(Dimension thumbnailSize)
Set the thumbnail size.void
setUseThumbnails(boolean useThumbnails)
If enabled then uploaded files will display to the user as thumbnails.-
Methods inherited from class com.github.bordertech.wcomponents.AbstractInput
addValidator, createMandatoryDiagnostic, doHandleChanged, getActionCommand, getActionObject, getActionOnChange, getDefaultSubmitButton, getDiagnostics, getValidators, handleRequest, isChangedInLastRequest, isCurrentAjaxTrigger, isDisabled, isEmpty, isMandatory, isPresent, isReadOnly, setActionObject, setActionOnChange, setChangedInLastRequest, setDefaultSubmitButton, setDisabled, setMandatory, setMandatory, setReadOnly, showErrorIndicatorsForComponent, showIndicatorsForComponent, showWarningIndicatorsForComponent, toString, validateComponent
-
Methods inherited from class com.github.bordertech.wcomponents.WBeanComponent
addBeanToScratchMap, doUpdateBeanValue, getBean, getBeanFromScratchMap, getBeanId, getBeanProperty, getBeanProvider, getBeanScratchMap, getBeanValue, getData, isBeanInScratchMap, isChanged, isSearchAncestors, isUseRequestScopeScratchMap, removeBeanFromScratchMap, resetData, setBean, setBeanId, setBeanProperty, setBeanProvider, setData, setSearchAncestors, updateBeanValue
-
Methods inherited from class com.github.bordertech.wcomponents.AbstractWComponent
addHtmlClass, addHtmlClass, addNotify, assertAddSupported, beforePaint, createErrorDiagnostic, createErrorDiagnostic, forward, getAccessibleText, getAttribute, getBaseUrl, getDefaultModel, getEnvironment, getHeaders, getHtmlClass, getHtmlClasses, getId, getIdName, getInternalId, getLabel, getName, getParent, getScratchMap, getTabIndex, getTag, getTemplate, getTemplateMarkUp, getToolTip, hasNoComponentModel, hasTabIndex, initialiseComponentModel, invokeLater, invokeLaters, isDebugStructure, isDefaultState, isFlagSet, isHidden, isInitialised, isLocked, isTracking, isTrackingEnabled, isValidate, isVisible, paint, paintComponent, preparePaint, preparePaintComponent, removeAttribute, removeComponentModel, removeHtmlClass, removeHtmlClass, removeNotify, replaceWComponent, reset, serviceRequest, setAccessibleText, setAttribute, setEnvironment, setFlag, setFocussed, setHidden, setHtmlClass, setHtmlClass, setIdName, setInitialised, setLocked, setTag, setToolTip, setTrackingEnabled, setValidate, setVisible, showErrorIndicators, showWarningIndicators, tidyUpUIContext, tidyUpUIContextForTree, validate, writeReplace
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.github.bordertech.wcomponents.BeanAware
getBean, getBeanProperty, getBeanValue, setBeanProperty
-
Methods inherited from interface com.github.bordertech.wcomponents.BeanBound
isSearchAncestors, setBean
-
Methods inherited from interface com.github.bordertech.wcomponents.BeanProviderBound
getBeanId, setBeanId, setBeanProvider
-
Methods inherited from interface com.github.bordertech.wcomponents.WComponent
addHtmlClass, addHtmlClass, forward, getAccessibleText, getAttribute, getBaseUrl, getEnvironment, getHeaders, getHtmlClass, getHtmlClasses, getId, getIdName, getInternalId, getLabel, getName, getParent, getTabIndex, getTag, getToolTip, handleRequest, hasTabIndex, invokeLater, isDefaultState, isHidden, isInitialised, isLocked, isTracking, isTrackingEnabled, isValidate, isVisible, paint, preparePaint, removeAttribute, removeHtmlClass, removeHtmlClass, reset, serviceRequest, setAccessibleText, setAttribute, setEnvironment, setFocussed, setHtmlClass, setHtmlClass, setIdName, setInitialised, setLocked, setTag, setToolTip, setTrackingEnabled, setValidate, setVisible, showErrorIndicators, showWarningIndicators, tidyUpUIContextForTree, validate
-
-
-
-
Field Detail
-
FILE_UPLOAD_ID_KEY
public static final String FILE_UPLOAD_ID_KEY
File id for AJAX action.- See Also:
- Constant Field Values
-
FILE_UPLOAD_MULTI_PART_ID_KEY
public static final String FILE_UPLOAD_MULTI_PART_ID_KEY
File id for file content included in multi part request.- See Also:
- Constant Field Values
-
FILE_UPLOAD_THUMB_NAIL_KEY
public static final String FILE_UPLOAD_THUMB_NAIL_KEY
File id thumb nail content request.- See Also:
- Constant Field Values
-
-
Method Detail
-
getValueAsString
public String getValueAsString()
Retrieves a String representation of the input field's value.- Specified by:
getValueAsString
in interfaceInput
- Overrides:
getValueAsString
in classAbstractInput
- Returns:
- the String representation of the input field's value
-
getValue
public List<WMultiFileWidget.FileWidgetUpload> getValue()
Provide the value of the component returned byDataBound.getData()
in the correct format.If required, this method can convert the data into the correct type and also do any validation before the value is used.
- Specified by:
getValue
in interfaceInput
- Overrides:
getValue
in classAbstractInput
- Returns:
- the value of the component returned by
DataBound.getData()
in the correct format.
-
getFiles
public List<WMultiFileWidget.FileWidgetUpload> getFiles()
Gets all the File items that have been uploaded.- Returns:
- A list of all File items uploaded by the client.
-
addFile
public void addFile(WMultiFileWidget.FileWidgetUpload file)
Add a file item to this widget.- Parameters:
file
- the file item
-
removeFile
public void removeFile(WMultiFileWidget.FileWidgetUpload file)
Remove the file.- Parameters:
file
- the file to remove
-
getFile
public WMultiFileWidget.FileWidgetUpload getFile(String fileId)
Retrieves the File at the given index. Will throw anIndexOutOfBoundsException
ifindex
is not in the range of the list of items.- Parameters:
fileId
- the file id- Returns:
- The FileWidgetUpload for the file id or null
-
getMimeType
public String getMimeType(String fileId)
Retrieves an mime type of the uploaded file's contents. This is not the content type passed by the browser.- Parameters:
fileId
- the file id- Returns:
- an file's mime type, or null if no file has been uploaded
-
getSelectedFiles
@Deprecated public List<WMultiFileWidget.FileWidgetUpload> getSelectedFiles()
Deprecated.no longer required as unselected files are now removed. So all files are "selected".Returns only the selected file items. The file is selected if the checkbox adjacent to the uploaded file name is checked by the user.- Returns:
- The uploaded file items that have been selected by the user. If no files have been selected then an empty list is returned.
-
isSelected
@Deprecated public boolean isSelected(File upload)
Deprecated.no longer required as unselected files are now removed. So all files are "selected".Indicates whether the given upload has been selected by the user.- Parameters:
upload
- the uploaded file- Returns:
- true if the upload has been selected, false otherwise.
-
clearFiles
public void clearFiles()
Clear uploaded files from this component.
-
setFileTypes
public void setFileTypes(String[] types)
Set each file type to be accepted by the WMultiFileWidget.- Parameters:
types
- The file types that will be accepted by the file input.- See Also:
for the file types
-
setFileTypes
public void setFileTypes(Collection<String> types)
Determines the file types accepted by this widget. Note that duplicates are not allowed and these are not case sensitive.
The file type(s) can be either:- MIME type
- Extension
text, image, application
etc, and subtype isplain, jpeg, *
etc. Some example MIME types are:text/*
- indicates that all text files MIME types are accepted,text/html, text/plain
etc.image/jpeg
- indicates that only jpeg image files are accepted.
Extension: A string whose first character is a "." (U+002E) character (Indicates that files with the specified file extension are accepted). Some example extensions are:.txt
- indicates any files with extensiontxt
are accepted..jpg
- indicates any files with extensionjpg
are accepted.
- Parameters:
types
- The file types that will be accepted by the file input. Note that this is not additive, it will overwrite any previously set fileTypes. Pass null or and empty collection to clear all file types.
-
getFileTypes
public List<String> getFileTypes()
Returns a list of file types accepted by the file input.- Returns:
- The file types accepted by this file input e.g. "image/*", ".vis", "text/plain", "text/html", "application/pdf".
- See Also:
for a description of what constitutes an allowable file types If no types have been added an empty list is returned. An empty list indicates that all file types are accepted.
-
hasFileTypes
public boolean hasFileTypes()
- Returns:
- true if one or more file type is supplied.
- See Also:
setFileTypes(java.util.Collection)
,setFileTypes(java.lang.String[])
-
setMaxFileSize
public void setMaxFileSize(long bytes)
Set the maximum file size (in bytes) that will be accepted by the file input. If the user selects a file larger than this value the client script will tell the user it cannot be uploaded.- Parameters:
bytes
- The maximum size (in bytes) that can be uploaded by this input.
-
getMaxFileSize
public long getMaxFileSize()
Return the maximum file size (in bytes) that can be accepted by this file input.- Returns:
- The maximum size (in bytes) that can be uploaded by this component.
-
hasMaxFileSize
public boolean hasMaxFileSize()
- Returns:
true
if max file size is supplied.- See Also:
setMaxFileSize(long)
-
getMaxFiles
public int getMaxFiles()
Return the maximum number of files that can be accepted by this file input.- Returns:
- The maximum number of files that can be uploaded by this component.
-
setMaxFiles
public void setMaxFiles(int maxFiles)
Set the maximum number of files that will be accepted by the file input.- Parameters:
maxFiles
- The maximum number of files that can be uploaded by this input.
-
setDropzone
public void setDropzone(DropZone dropzone)
Register a component to receive drag and dropped files on behalf of this input. It is recommended that you register the WApplication as the dropzone. This allows users to drop files anywhere on the page and eliminates the risk of them dropping files outside of the dropzone (which causes the browser to attempt to render the dropped files).- Parameters:
dropzone
- The dropzone.
-
getDropzone
public DropZone getDropzone()
Return the dropzone associated with this file input.- Returns:
- The dropzone or null if not set.
-
setEditor
public void setEditor(WImageEditor editor)
Registers an image editor with this file upload widget so that the user will be prompted to edit (crop, rotate etc). This obviously only makes sense if this widget is configured to only allow image file types. It is probably also a logical idea to set max files to one.- Parameters:
editor
- The image editor.
-
getEditor
public WImageEditor getEditor()
Return the image editor associated with this file input.- Returns:
- The editor or null if not set.
-
setFileAjaxAction
public void setFileAjaxAction(Action action)
The AJAX action used when an uploaded file has been selected.Setting this action causes the uploaded file links to act as AJAX triggers. The file id of the selected file is set as the action object.
- Parameters:
action
- the file AJAX action
-
getFileAjaxAction
public Action getFileAjaxAction()
The AJAX action used when an uploaded file has been selected.- Returns:
- the file AJAX action
-
setColumns
public void setColumns(Integer cols)
Sets the layout of uploaded files to be a certain number of columns. Null uses the theme default.- Parameters:
cols
- the number of columns.
-
getColumns
public Integer getColumns()
- Returns:
- the number of columns for layout of uploaded files. Null uses the theme default.
-
isNewUpload
protected boolean isNewUpload()
Used in handle request processing to trigger on change processing.- Returns:
- true if a new file has been uploaded
-
setNewUpload
public void setNewUpload(boolean newUpload)
- Parameters:
newUpload
- true if a new file has been uploaded
-
isUseThumbnails
public boolean isUseThumbnails()
- Returns:
- true if generate thumb nails for the file links.
-
setUseThumbnails
public void setUseThumbnails(boolean useThumbnails)
If enabled then uploaded files will display to the user as thumbnails. While this can be used for any file types it is only recommended when the WMultiFileWidget is set to accept image types only, e.g. withsetFileTypes(new String[] { "image/*" });
- Parameters:
useThumbnails
- true if generate thumb nails for the file links.
-
getThumbnailPosition
public WLink.ImagePosition getThumbnailPosition()
- Returns:
- the position of the thumbnail image on the file link
-
setThumbnailPosition
public void setThumbnailPosition(WLink.ImagePosition thumbnailPosition)
The position of the thumbnail image on the file link.If no position is set then the text is not shown.
- Parameters:
thumbnailPosition
- the position of the image
-
getThumbnailSize
public Dimension getThumbnailSize()
Retrieve the thumbnail size. If null, the default size is used.- Returns:
- the thumbnail size or null for default
-
setThumbnailSize
public void setThumbnailSize(Dimension thumbnailSize)
Set the thumbnail size. Null uses the default size.To scale thumbnails to a certain height or width, use -1 on the scalable dimension. For example, to scale thumbnails to 64 pixels high but maintain the correct width ration, set Height to 64 and Width to -1.
- Parameters:
thumbnailSize
- the thumbnail size or null for default
-
clearThumbnails
public void clearThumbnails()
Clear the thumbnails currently set on the files. This will cause them to be generated again when requested. This can be used if the thumbnail size has changed.
-
doHandleRequest
protected boolean doHandleRequest(Request request)
Specific handle request processing for an input component is provided here.Input components are required to determine if the component has changed in the request, set the component data to the new value (if changed) and return the changed flag.
- Specified by:
doHandleRequest
in classAbstractInput
- Parameters:
request
- the request being responded to.- Returns:
- true if the input component has changed, otherwise return false
-
getRequestValue
public List<WMultiFileWidget.FileWidgetUpload> getRequestValue(Request request)
Provide the value of the component on the Request.If the component is not on the request, the components current value will be provided.
- Specified by:
getRequestValue
in interfaceInput
- Parameters:
request
- the request being responded to.- Returns:
- the value of this component on the Request, or its current state if it is not on the request.
-
afterPaint
protected void afterPaint(RenderContext renderContext)
Subclasses may override this method to output content after the component has been painted. When overriding this method, it is good practice to call the superclass implementation before emitting any additional content.- Overrides:
afterPaint
in classAbstractWComponent
- Parameters:
renderContext
- the context to render to.
-
beforeHandleRequest
protected boolean beforeHandleRequest(Request request)
Handle before handle request processing.- Overrides:
beforeHandleRequest
in classAbstractInput
- Parameters:
request
- the request being responded to.- Returns:
- true to continue
-
doHandleFileAjaxActionRequest
protected void doHandleFileAjaxActionRequest(Request request)
Handle a file action AJAX request.- Parameters:
request
- the request being processed
-
doHandleTargetedRequest
protected void doHandleTargetedRequest(Request request)
Handle a targeted request. Can be a file upload, thumbnail request or file content request.- Parameters:
request
- the request being processed
-
doHandleUploadRequest
protected void doHandleUploadRequest(Request request)
The request is a targeted file upload request. Upload the file and respond with the file information.- Parameters:
request
- the request being processed.
-
doHandleThumbnailRequest
protected void doHandleThumbnailRequest(WMultiFileWidget.FileWidgetUpload file)
Handle the thumb nail request.- Parameters:
file
- the file to process
-
doHandleFileContentRequest
protected void doHandleFileContentRequest(WMultiFileWidget.FileWidgetUpload file)
Handle the file content request.- Parameters:
file
- the file to process
-
createThumbNail
protected Image createThumbNail(File file)
- Parameters:
file
- the file to create a thumbnail for- Returns:
- the thumbnail
-
getFileUrl
public String getFileUrl(String fileId)
Retrieves a URL for the uploaded file content.- Parameters:
fileId
- the file id- Returns:
- the URL to access the uploaded file.
-
getFileThumbnailUrl
public String getFileThumbnailUrl(String fileId)
Retrieves a URL for the thumbnail of an uploaded file.- Parameters:
fileId
- the file id- Returns:
- the URL to access the thumbnail for an uploaded file.
-
getTargetId
public String getTargetId()
The target id returned must be unique across all targetable WComponents within the application. In a portal environment, the target ids must also be unique across portlets. Most components should just return their component id.- Specified by:
getTargetId
in interfaceTargetable
- Returns:
- the target id for this targetable.
-
getFileUploadRequestId
public String getFileUploadRequestId()
This method is used by the renderer to return the successful upload response.- Returns:
- the file id that has been successfully uploaded, or null
-
newComponentModel
protected WMultiFileWidget.MultiFileWidgetModel newComponentModel()
Description copied from class:AbstractInput
Creates a new model appropriate for this component.- Overrides:
newComponentModel
in classAbstractInput
- Returns:
- a new
WMultiFileWidget.MultiFileWidgetModel
.
-
getComponentModel
protected WMultiFileWidget.MultiFileWidgetModel getComponentModel()
Returns the effective component model for this component. Subclass may override this method to narrow the return type to their specific model type.- Overrides:
getComponentModel
in classAbstractInput
- Returns:
- the effective component model
-
getOrCreateComponentModel
protected WMultiFileWidget.MultiFileWidgetModel getOrCreateComponentModel()
Retrieves the model for this component so that it can be modified. If this method is called during request processing, and a session specific model does not yet exist, then a new model is created. Subclasses may override this method to narrow the return type to their specific model type.- Overrides:
getOrCreateComponentModel
in classAbstractInput
- Returns:
- the model for this component
-
-