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 classWMultiFileWidget.FileWidgetUploadHolds the uploaded file and extra details.static classWMultiFileWidget.MultiFileWidgetModelHolds 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 StringFILE_UPLOAD_ID_KEYFile id for AJAX action.static StringFILE_UPLOAD_MULTI_PART_ID_KEYFile id for file content included in multi part request.static StringFILE_UPLOAD_THUMB_NAIL_KEYFile 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 voidaddFile(WMultiFileWidget.FileWidgetUpload file)Add a file item to this widget.protected voidafterPaint(RenderContext renderContext)Subclasses may override this method to output content after the component has been painted.protected booleanbeforeHandleRequest(Request request)Handle before handle request processing.voidclearFiles()Clear uploaded files from this component.voidclearThumbnails()Clear the thumbnails currently set on the files.protected ImagecreateThumbNail(File file)protected voiddoHandleFileAjaxActionRequest(Request request)Handle a file action AJAX request.protected voiddoHandleFileContentRequest(WMultiFileWidget.FileWidgetUpload file)Handle the file content request.protected booleandoHandleRequest(Request request)Specific handle request processing for an input component is provided here.protected voiddoHandleTargetedRequest(Request request)Handle a targeted request.protected voiddoHandleThumbnailRequest(WMultiFileWidget.FileWidgetUpload file)Handle the thumb nail request.protected voiddoHandleUploadRequest(Request request)The request is a targeted file upload request.IntegergetColumns()protected WMultiFileWidget.MultiFileWidgetModelgetComponentModel()Returns the effective component model for this component.DropZonegetDropzone()Return the dropzone associated with this file input.WImageEditorgetEditor()Return the image editor associated with this file input.WMultiFileWidget.FileWidgetUploadgetFile(String fileId)Retrieves the File at the given index.ActiongetFileAjaxAction()The AJAX action used when an uploaded file has been selected.List<WMultiFileWidget.FileWidgetUpload>getFiles()Gets all the File items that have been uploaded.StringgetFileThumbnailUrl(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.StringgetFileUploadRequestId()This method is used by the renderer to return the successful upload response.StringgetFileUrl(String fileId)Retrieves a URL for the uploaded file content.intgetMaxFiles()Return the maximum number of files that can be accepted by this file input.longgetMaxFileSize()Return the maximum file size (in bytes) that can be accepted by this file input.StringgetMimeType(String fileId)Retrieves an mime type of the uploaded file's contents.protected WMultiFileWidget.MultiFileWidgetModelgetOrCreateComponentModel()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.StringgetTargetId()The target id returned must be unique across all targetable WComponents within the application.WLink.ImagePositiongetThumbnailPosition()DimensiongetThumbnailSize()Retrieve the thumbnail size.List<WMultiFileWidget.FileWidgetUpload>getValue()Provide the value of the component returned byDataBound.getData()in the correct format.StringgetValueAsString()Retrieves a String representation of the input field's value.booleanhasFileTypes()booleanhasMaxFileSize()protected booleanisNewUpload()Used in handle request processing to trigger on change processing.booleanisSelected(File upload)Deprecated.no longer required as unselected files are now removed.booleanisUseThumbnails()protected WMultiFileWidget.MultiFileWidgetModelnewComponentModel()Creates a new model appropriate for this component.voidremoveFile(WMultiFileWidget.FileWidgetUpload file)Remove the file.voidsetColumns(Integer cols)Sets the layout of uploaded files to be a certain number of columns.voidsetDropzone(DropZone dropzone)Register a component to receive drag and dropped files on behalf of this input.voidsetEditor(WImageEditor editor)Registers an image editor with this file upload widget so that the user will be prompted to edit (crop, rotate etc).voidsetFileAjaxAction(Action action)The AJAX action used when an uploaded file has been selected.voidsetFileTypes(String[] types)Set each file type to be accepted by the WMultiFileWidget.voidsetFileTypes(Collection<String> types)Determines the file types accepted by this widget.voidsetMaxFiles(int maxFiles)Set the maximum number of files that will be accepted by the file input.voidsetMaxFileSize(long bytes)Set the maximum file size (in bytes) that will be accepted by the file input.voidsetNewUpload(boolean newUpload)voidsetThumbnailPosition(WLink.ImagePosition thumbnailPosition)The position of the thumbnail image on the file link.voidsetThumbnailSize(Dimension thumbnailSize)Set the thumbnail size.voidsetUseThumbnails(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:
getValueAsStringin interfaceInput- Overrides:
getValueAsStringin 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:
getValuein interfaceInput- Overrides:
getValuein 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 anIndexOutOfBoundsExceptionifindexis 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, applicationetc, and subtype isplain, jpeg, *etc. Some example MIME types are:text/*- indicates that all text files MIME types are accepted,text/html, text/plainetc.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 extensiontxtare accepted..jpg- indicates any files with extensionjpgare 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:
trueif 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:
doHandleRequestin 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:
getRequestValuein 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:
afterPaintin classAbstractWComponent- Parameters:
renderContext- the context to render to.
-
beforeHandleRequest
protected boolean beforeHandleRequest(Request request)
Handle before handle request processing.- Overrides:
beforeHandleRequestin 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:
getTargetIdin 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:AbstractInputCreates a new model appropriate for this component.- Overrides:
newComponentModelin 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:
getComponentModelin 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:
getOrCreateComponentModelin classAbstractInput- Returns:
- the model for this component
-
-