001 /*
002 * This file is part of the Echo Point Project. This project is a
003 * collection of Components that have extended the Echo Web Application
004 * Framework Version 3.
005 *
006 * Version: MPL 1.1
007 *
008 * The contents of this file are subject to the Mozilla Public License Version
009 * 1.1 (the "License"); you may not use this file except in compliance with
010 * the License. You may obtain a copy of the License at
011 * http://www.mozilla.org/MPL/
012 *
013 * Software distributed under the License is distributed on an "AS IS" basis,
014 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015 * for the specific language governing rights and limitations under the
016 * License.
017 */
018 package echopoint.tucana;
019
020 import nextapp.echo.app.ApplicationInstance;
021 import nextapp.echo.app.Command;
022 import nextapp.echo.app.RenderIdSupport;
023
024 import java.io.Serializable;
025
026 import echopoint.tucana.event.DownloadEvent;
027 import echopoint.tucana.event.DownloadStartEvent;
028 import echopoint.tucana.event.DownloadFailEvent;
029 import echopoint.tucana.event.DownloadCallback;
030 import echopoint.tucana.event.DownloadFinishEvent;
031
032 /**
033 * A command used to enqueue a file (or similar) to the client for downlaoding
034 * from the server.
035 *
036 * <p>The following code shows the simplest form of using this command:</p>
037 * <pre>
038 * import java.io.File;
039 * import echopoint.tucana.DownloadButton
040 *
041 * ...
042 * final File file = new File( "/tmp/test.txt" );
043 * final DownloadButton button = new DownloadButton( file );
044 * button.setText( "Download file" );
045 * button.setStyleName( "mystyle" );
046 * parent.add( button );
047 * </pre>
048 *
049 * @see DownloadButton
050 * @author Echo File Transfer Library, Rakesh 2008-11-10
051 * @version $Id: DownloadCommand.java 100 2008-11-12 19:17:24Z sptrakesh $
052 */
053 public class DownloadCommand implements Command, RenderIdSupport, Serializable
054 {
055 private static final long serialVersionUID = 1L;
056
057 /** The render id for the command. */
058 private String id;
059
060 /** The download provider implementation for the command. */
061 private DownloadProvider provider;
062
063 /**
064 * The callback handler that will be notified of the progress of the file
065 * download process.
066 */
067 private DownloadCallback callback = null;
068
069 /** Constructs a new download command. */
070 public DownloadCommand() {}
071
072 /**
073 * Constructs a new download command, whose data will be taken from the
074 * passed {@link DownloadProvider}.
075 *
076 * @param provider the provider from which to get the data.
077 */
078 public DownloadCommand( final DownloadProvider provider )
079 {
080 this.provider = provider;
081 }
082
083 /**
084 * Returns the download provider.
085 *
086 * @return the download provider.
087 */
088 public DownloadProvider getProvider()
089 {
090 return provider;
091 }
092
093 /**
094 * Sets the download provider from which to get the data.
095 *
096 * @param newValue the download provider from which to get the data.
097 */
098 public void setProvider( final DownloadProvider newValue )
099 {
100 this.provider = newValue;
101 }
102
103 /**
104 * Returns the render id.
105 *
106 * @return the render id.
107 */
108 public String getRenderId()
109 {
110 if ( id == null )
111 {
112 id = ApplicationInstance.generateSystemId();
113 }
114 return id;
115 }
116
117 /**
118 * Accessor for property 'callback'.
119 *
120 * @return Value for property 'callback'.
121 */
122 public DownloadCallback getCallback()
123 {
124 return callback;
125 }
126
127 /**
128 * Mutator for property 'callback'.
129 *
130 * @param callback Value to set for property 'callback'.
131 */
132 public void setCallback( final DownloadCallback callback )
133 {
134 this.callback = callback;
135 }
136
137 /**
138 * Notify the download progress callback handler of updates to the download
139 * process.
140 *
141 * @param event The progress event.
142 */
143 protected void notifyCallback( final DownloadEvent event )
144 {
145 if ( callback == null ) return;
146
147 if ( event instanceof DownloadStartEvent )
148 {
149 callback.downloadStarted( (DownloadStartEvent) event );
150 }
151 if ( event instanceof DownloadFinishEvent )
152 {
153 callback.downloadFinished( (DownloadFinishEvent) event );
154 }
155 else if ( event instanceof DownloadFailEvent )
156 {
157 callback.downloadFailed( (DownloadFailEvent) event );
158 }
159 }
160 }