|
EchoPoint API - 3.0.0b5 App Webcontainer |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnextapp.echo.app.Component
echopoint.internal.AbstractContainer
echopoint.tucana.FileUploadSelector
public class FileUploadSelector
The file upload selector component. This component is a re-implementation of the original tucana file upload selector component for Echo2.
Note: It is critical that this component be set absolute height and width properties. Percentage based values can cause problems if using a progress bar.
The following code shows sample usage of this component:
import nextapp.echo.app.Border;
import nextapp.echo.app.Color;
import echopoint.tucana.ButtonMode;
import echopoint.tucana.ButtonDisplay;
import echopoint.tucana.FileUploadSelector;
import echopoint.tucana.ProgressBar;
import static echopoint.tucana.UploadSPI;
import echopoint.tucana.event.DefaultUploadCallback;
...
final FileUploadSelector selector = new FileUploadSelector();
selector.setButtonMode( ButtonMode.image );
selector.setButtonDisplayMode( ButtonDisplay.right );
selector.setInputSize( 20 );
selector.setUploadSizeLimit( NO_SIZE_LIMIT );
selector.setBackground( new Color( 0xa1a1a1 ) );
selector.setBorder( new Border( 1, Color.BLUE, Border.STYLE_GROOVE ) );
selector.setProgressBar( new ProgressBar() );
selector.setUploadCallback( new DefaultUploadCallback( new File( "/tmp" ) ) );
selector.addActionListener( ... );
// Allow only the following types of files to be uploaded.
final HashSet<String> set = new HashSet<String>();
set.add( "image/gif" );
set.add( "image/jpeg" );
set.add( "image/png" );
selector.setContentTypeFilter( set );
parent.add( selector );
There are two different (equivalent) ways to process a completed upload:
UploadCallback based. It is best
to use a sub-class of either DefaultUploadCallback
or UploadCallbackAdapter since they
ensure removal of the task queue used to enqueue processing from the
call back methods to the UI thread. If you sub-class please note that
calls to super.uploadXxx methods should be invoked at the end
of your over-ridden implementation and not at the top. The super class
implementations destroys the queue and sets it to null.
If you implement your own handler
please make sure that you invoke removeTaskQueue()
at the end of your handler methods. The queue will be automatically
cleaned up when the component is removed from the hierarchy, so it may
be acceptable to not invoke removeTaskQueue depending upon how
your application logic works.ActionListener based. Two types of
events are recieved by the event handler:
START_ACTION - The action command that indicates that
the file upload has commenced.COMPLETE_ACTION - The action command that indicates that
the file upload has finished.Please note that it is safest to check on the command value rather than check one and default action for the other value.
The following callback class and associated runnable may be used to update the UI after an upload.
import nextapp.echo.app.ApplicationInstance;
import nextapp.echo.app.Component;
import nextapp.echo.app.TaskQueueHandle;
import echopoint.DirectHtml;
import echopoint.tucana.event.UploadCallbackAdapter;
public class UploadCallbackImpl extends UploadCallbackAdapter
{
private static final long serialVersionUID = 1l;
private final Component parent;
private UploadCallbackImpl( final Component parent )
{
this.parent = parent;
}
@Override
public void uploadSucceeded( final UploadFinishEvent event )
{
final StringBuilder builder = new StringBuilder( 128 );
builder.append( "Upload of file: <b>" );
builder.append( event.getFileName() );
builder.append( "</b> succeeded. File size is: <i>");
builder.append( event.getFileSize() / 1000 );
builder.append( "</i> kilobytes." );
final DirectHtml html = new DirectHtml( builder.toString() );
parent.add( child );
super.uploadSucceeded( event );
}
@Override
public void uploadFailed( final UploadFailEvent event )
{
final StringBuilder builder = new StringBuilder( 128 );
builder.append( "File upload failed." );
if ( event.getFileName() != null )
{
builder.append( " Failed file: <i>" );
builder.append( event.getFileName() );
builder.append( "</i>." );
}
if ( event.getException() != null )
{
builder.append( "Exception: <p><pre>" );
builder.append( event.getException().toString() );
builder.append( "</pre></p>" );
}
final DirectHtml html = new DirectHtml( builder.toString() );
parent.add( child );
super.uploadFailed( event );
}
Processing UI updates after upload completion is much simpler (and more network friendly) when using an action listener. A simple action listener may be configured as follows:
import nextapp.echo.app.Component;
import nextapp.echo.app.event.ActionEvent;
import nextapp.echo.app.event.ActionListener;
import echopoint.tucana.FileUploadSelector;
import echopoint.tucana.event.UploadCallback;
import echopoint.tucana.event.UploadFinishEvent;
import echopoint.DirectHtml;
public class FinishListener implements ActionListener
{
private static final long serialVersionUID = 1l;
public void actionPerformed( final ActionEvent event )
{
final FileUploadSelector upload = ( FileUploadSelector) event.getSource();
final UploadCallback callback = upload.getUploadCallback();
if ( callback != null )
{
final StringBuilder builder = new StringBuilder( 128 );
final boolean success = ( callback.getEvent() instanceof UploadFinishEvent );
if ( success )
{
builder.append( "Upload of file: <b>" );
builder.append( callback.getEvent().getFileName() );
builder.append( "</b> succeeded. File size is: <i>");
builder.append( callback.getEvent().getFileSize() / 1000 );
builder.append( "</i> kilobytes." );
}
else
{
builder.append( "Upload " );
if ( callback.getEvent() != null )
{
builder.append( " of file: <b>" );
builder.append( callback.getEvent().getFileName() );
builder.append( "</b>" );
}
builder.append( " failed/cancelled." );
}
upload.getParent().add( new DirectHtml( builder.toString() ) );
if ( upload.getProgressBar() != null )
{
upload.getProgressBar().setText( ( success ) ?
"Finished upload!" : "Cancelled upload!" );
}
}
}
}
Note: Development of this component was sponsored by TCN Broadcasting. We are grateful for their support and sponsorship.
| Field Summary | |
|---|---|
static String |
COMPLETE_ACTION
The name of the action that is fired by the client upon completion (regardless of complete or cancel) of the upload process. |
static String |
PROPERTY_BUTTON_DISPLAY
|
static String |
PROPERTY_BUTTON_IMAGE_CANCEL
|
static String |
PROPERTY_BUTTON_IMAGE_UPLOAD
|
static String |
PROPERTY_BUTTON_IMAGE_WAIT
|
static String |
PROPERTY_BUTTON_MODE
|
static String |
PROPERTY_BUTTON_TEXT_CANCEL
|
static String |
PROPERTY_BUTTON_TEXT_UPLOAD
|
static String |
PROPERTY_BUTTON_TEXT_WAIT
|
static String |
PROPERTY_CANCEL_ENABLED
|
static String |
PROPERTY_INPUT_SIZE
The size (indicates number of characters) for the input field. |
static String |
PROPERTY_POLLING_INTERVAL
The interval (in milliseconds) at which the progress service is to be polled for updates. |
static String |
PROPERTY_UPLOAD_SIZE_LIMIT
The maximum size of file that is allowed to be uploaded. |
static String |
START_ACTION
The name of the action that is fired by the client upon start of the upload process. |
| Fields inherited from class echopoint.internal.AbstractContainer |
|---|
ACTION_COMMAND_PROPERTY, ACTION_LISTENERS_CHANGED_PROPERTY, INPUT_ACTION, PROPERTY_ALIGNMENT, PROPERTY_BACKGROUND_IMAGE, PROPERTY_BORDER, PROPERTY_HEIGHT, PROPERTY_INSETS, PROPERTY_WIDTH |
| Constructor Summary | |
|---|---|
FileUploadSelector()
Default constructor. |
|
| Method Summary | |
|---|---|
void |
addActionListener(ActionListener listener)
Add the specified action listener to this component. |
void |
dispose()
Over-ridden to clean up taskQueue if not already cleaned up. |
ImageReference |
getButtonCancelImage()
Gets the current cancel button image. |
String |
getButtonCancelText()
Gets the current cancel button text. |
ButtonDisplay |
getButtonDisplayMode()
Get the configuration for display of the submit button. |
ButtonMode |
getButtonMode()
Get the current upload button display mode. |
ImageReference |
getButtonUploadImage()
Gets the current upload button image. |
String |
getButtonUploadText()
Gets the current upload button text. |
ImageReference |
getButtonWaitImage()
Gets the current wait button image. |
String |
getButtonWaitText()
Gets the current wait button text. |
Set<String> |
getContentTypeFilter()
Accessor for property 'contentTypeFilter'. |
int |
getInputSize()
Return the value of the PROPERTY_INPUT_SIZE property. |
int |
getPollingInterval()
Return the value of the PROPERTY_POLLING_INTERVAL property. |
ProgressBar |
getProgressBar()
Return the progress bar component configured for this component. |
TaskQueueHandle |
getTaskQueue()
Return the task queue that may be used to enqueue asynchronous tasks (usually from callback handlers). |
UploadCallback |
getUploadCallback()
Get the callback used when a file is uploaded. |
long |
getUploadSizeLimit()
Return the value of the PROPERTY_UPLOAD_SIZE_LIMIT property. |
boolean |
isCancelEnabled()
Accessor for property 'cancelEnabled'. |
boolean |
isValidChild(Component child)
The only allowed sub-component is a ProgressBar. |
protected void |
notifyCallback(UploadEvent e)
Notifies the listener that the given event has occurred. |
void |
processInput(String name,
Object value)
Over-ridden to fire action events with action commands that are set to START_ACTION or COMPLETE_ACTION depending upon whether
the event represents start of the upload or completion of the upload. |
void |
removeActionListener(ActionListener listener)
Remove the specified action listener from the component. |
void |
removeTaskQueue()
Remove the current task queue. |
void |
setButtonCancelImage(ImageReference image)
Sets the cancel button image. |
void |
setButtonCancelText(String text)
Sets the cancel button text. |
void |
setButtonDisplayMode(ButtonDisplay mode)
Set the button display mode for the input submit button. |
void |
setButtonMode(ButtonMode mode)
Sets the upload button display mode |
void |
setButtonUploadImage(ImageReference image)
Sets the upload button image. |
void |
setButtonUploadText(String text)
Sets the upload button text. |
void |
setButtonWaitImage(ImageReference image)
Sets the wait button image. |
void |
setButtonWaitText(String text)
Sets the wait button text. |
void |
setCancelEnabled(boolean enabled)
Mutator for property 'cancelEnabled'. |
void |
setContentTypeFilter(Set<String> contentTypeFilter)
Mutator for property 'contentTypeFilter'. |
void |
setInputSize(int size)
Set the value of the PROPERTY_INPUT_SIZE property. |
void |
setPollingInterval(int interval)
Set the value of the PROPERTY_POLLING_INTERVAL property. |
void |
setProgressBar(ProgressBar progressBar)
Add the specified progress bar to this component. |
void |
setTaskQueue(TaskQueueHandle taskQueue)
Set the task queue for the component. |
void |
setUploadCallback(UploadCallback callback)
Set the callback used when a file is uploaded. |
void |
setUploadSizeLimit(long limit)
Set the value of the PROPERTY_UPLOAD_SIZE_LIMIT property. |
| Methods inherited from class echopoint.internal.AbstractContainer |
|---|
fireActionPerformed, getAlignment, getBackgroundImage, getBorder, getHeight, getInsets, getWidth, hasActionListeners, setAlignment, setBackgroundImage, setBorder, setHeight, setInsets, setWidth |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String PROPERTY_BUTTON_TEXT_UPLOAD
public static final String PROPERTY_BUTTON_TEXT_CANCEL
public static final String PROPERTY_BUTTON_TEXT_WAIT
public static final String PROPERTY_BUTTON_IMAGE_UPLOAD
public static final String PROPERTY_BUTTON_IMAGE_CANCEL
public static final String PROPERTY_BUTTON_IMAGE_WAIT
public static final String PROPERTY_BUTTON_MODE
public static final String PROPERTY_BUTTON_DISPLAY
public static final String PROPERTY_CANCEL_ENABLED
public static final String PROPERTY_INPUT_SIZE
PROPERTY_WIDTH_SIZE.
This property is best styled.
public static final String PROPERTY_POLLING_INTERVAL
public static final String PROPERTY_UPLOAD_SIZE_LIMIT
public static final String COMPLETE_ACTION
public static final String START_ACTION
| Constructor Detail |
|---|
public FileUploadSelector()
DefaultUploadListener
as a listener for this component to initialise the taskQueue.
| Method Detail |
|---|
public void setButtonUploadImage(ImageReference image)
image - The new upload button image.public ImageReference getButtonUploadImage()
public void setButtonCancelImage(ImageReference image)
image - The new cancel button image.public ImageReference getButtonCancelImage()
public void setButtonWaitImage(ImageReference image)
image - The new wait button image.public ImageReference getButtonWaitImage()
public void setButtonUploadText(String text)
text - The new upload button text.public String getButtonUploadText()
public void setButtonCancelText(String text)
text - The new cancel button text.public String getButtonCancelText()
public void setButtonWaitText(String text)
text - The new wait button text.public String getButtonWaitText()
public void setButtonMode(ButtonMode mode)
mode - The new upload button display modepublic ButtonMode getButtonMode()
ButtonMode.submit if not set.public void setButtonDisplayMode(ButtonDisplay mode)
mode - Set to configure the location of the submit button.public ButtonDisplay getButtonDisplayMode()
ButtonDisplay.auto if not set.public void setCancelEnabled(boolean enabled)
enabled - Value to set for property 'cancelEnabled'.public boolean isCancelEnabled()
public int getInputSize()
PROPERTY_INPUT_SIZE property.
public void setInputSize(int size)
PROPERTY_INPUT_SIZE property.
size - The size of the input text field.public int getPollingInterval()
PROPERTY_POLLING_INTERVAL property.
public void setPollingInterval(int interval)
PROPERTY_POLLING_INTERVAL property.
interval - The time interval at which the progress service will be polledpublic long getUploadSizeLimit()
PROPERTY_UPLOAD_SIZE_LIMIT property.
If not set the implementation defaults to
echopoint.tucana.AbstractFileUploadProvider#DEFAULT_UPLOAD_SIZE_LIMIT.
public void setUploadSizeLimit(long limit)
PROPERTY_UPLOAD_SIZE_LIMIT property.
Specify echopoint.tucana.UploadSPI#NO_SIZE_LIMIT to prevent
size checking.
limit - The maximum size in bytes that is allowed.public void setUploadCallback(UploadCallback callback)
callback - The upload callback.public UploadCallback getUploadCallback()
public ProgressBar getProgressBar()
null.public void setProgressBar(ProgressBar progressBar)
progressBar - The progress bar component to add.public Set<String> getContentTypeFilter()
public void setContentTypeFilter(Set<String> contentTypeFilter)
contentTypeFilter - Value to set for property 'contentTypeFilter'.public boolean isValidChild(Component child)
ProgressBar.
isValidChild in class Componentchild - The child component that is to be added if allowed.
true if child is an instance of ProgressBar and a progress bar has not already been
assigned.public TaskQueueHandle getTaskQueue()
public void setTaskQueue(TaskQueueHandle taskQueue)
taskQueue - Value to set for property 'taskQueue'.public void removeTaskQueue()
dispose().
protected void notifyCallback(UploadEvent e)
e - the eventpublic void addActionListener(ActionListener listener)
addActionListener in class AbstractContainerlistener - The action listener to add.Component.firePropertyChange(String, Object, Object)public void removeActionListener(ActionListener listener)
removeActionListener in class AbstractContainerlistener - The listener that is to be removed.Component.firePropertyChange(String, Object, Object)
public void processInput(String name,
Object value)
START_ACTION or COMPLETE_ACTION depending upon whether
the event represents start of the upload or completion of the upload.
processInput in class ComponentComponent.processInput(String, Object)public void dispose()
taskQueue if not already cleaned up.
dispose in class ComponentremoveTaskQueue(),
Component.dispose()
|
EchoPoint API - 3.0.0b5 App Webcontainer |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||