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 echopoint.tucana.event.DownloadCallback;
021 import echopoint.tucana.event.DownloadCallbackAdapter;
022 import nextapp.echo.app.ApplicationInstance;
023 import nextapp.echo.app.Button;
024 import nextapp.echo.app.event.ActionEvent;
025 import nextapp.echo.app.event.ActionListener;
026
027 import java.io.File;
028 import java.io.InputStream;
029
030 /**
031 * A convenience component to display a {@link DownloadCommand} as a button
032 * with a pre-configured {@link nextapp.echo.app.event.ActionListener}.
033 *
034 * <p>The following shows sample use of this component:</p>
035 * <pre>
036 * import echopoint.Strut;
037 * import echopoint.tucana.DownloadButton;
038 * import nextapp.echo.app.Row;
039 * import java.io.File;
040 *
041 * ...
042 * final File file = new File( "/path/tofile" );
043 * final Row row = new Row();
044 * final DownloadButton button = new DownloadButton( file );
045 * button.setText( "Download File" );
046 * row.add( new Strut() );
047 * row.add( button );
048 * row.add( new Strut() );
049 * </pre>
050 *
051 * @author Rakesh 2008-11-11
052 * @version $Id: DownloadButton.java 248 2009-10-19 14:20:53Z sptrakesh $
053 */
054 public class DownloadButton extends Button
055 {
056 private static final long serialVersionUID = 1l;
057
058 /** The download command instance that is to be enqueued. */
059 private DownloadProvider provider;
060
061 /**
062 * The callback handler to use with the download command. Defaults to
063 * {@link echopoint.tucana.event.DownloadCallbackAdapter}.
064 */
065 private DownloadCallback callback;
066
067 /**
068 * Create a new instance for the specified download provider. This is the
069 * designated constructor.
070 *
071 * @param provider The download provider to use with the {@link DownloadCommand}
072 */
073 public DownloadButton( final DownloadProvider provider )
074 {
075 setProvider( provider );
076 setText( "Download" );
077 setDownloadCallback( new DownloadCallbackAdapter() );
078 super.addActionListener( new DownloadListener() );
079 }
080
081 /**
082 * Create a new instance for the specified file (using a {@link
083 * FileDownloadProvider}.
084 *
085 * @param file The file to be enqueued for download.
086 */
087 public DownloadButton( final File file )
088 {
089 this( new FileDownloadProvider( file ) );
090 }
091
092 /**
093 * Create a new instance using the input stream to feed a {@link
094 * InputStreamDownloadProvider}.
095 *
096 * @param stream The input stream to use as the data source.
097 */
098 public DownloadButton( final InputStream stream )
099 {
100 this( new InputStreamDownloadProvider( stream ) );
101 }
102
103 /**
104 * Accessor for property 'provider'.
105 *
106 * @return Value for property 'provider'.
107 */
108 public DownloadProvider getProvider()
109 {
110 return provider;
111 }
112
113 /**
114 * Over-ridden to not allow any other listeners than the default. Does not
115 * throw any exception, just ignores the call.
116 *
117 * @param listener The listener to add.
118 */
119 @Override
120 public void addActionListener( final ActionListener listener )
121 {
122 // noop
123 }
124
125 /**
126 * Mutator for property 'provider'.
127 *
128 * @param provider Value to set for property 'provider'.
129 */
130 public void setProvider( final DownloadProvider provider )
131 {
132 this.provider = provider;
133 }
134
135 /**
136 * Mutator for property 'provider'.
137 *
138 * @param file The file to be enqueued for download.
139 */
140 public void setProvider( final File file )
141 {
142 setProvider( new FileDownloadProvider( file ) );
143 }
144
145 /**
146 * Accessor for property 'callback'.
147 *
148 * @return Value for property 'callback'.
149 */
150 public DownloadCallback getDownloadCallback()
151 {
152 return callback;
153 }
154
155 /**
156 * Mutator for property 'callback'.
157 *
158 * @param callback Value to set for property 'callback'.
159 */
160 public void setDownloadCallback( final DownloadCallback callback )
161 {
162 this.callback = callback;
163 }
164
165 /**
166 * The standard action listener to associate with the download button.
167 */
168 private class DownloadListener implements ActionListener
169 {
170 private static final long serialVersionUID = 1l;
171
172 /**
173 * Create a new {@link DownloadCommand} and enqueue it for download.
174 *
175 * @param event The event object that was triggered.
176 */
177 public void actionPerformed( final ActionEvent event )
178 {
179 final DownloadCommand command = new DownloadCommand( provider );
180 command.setCallback( callback );
181 ApplicationInstance.getActive().enqueueCommand( command );
182 }
183 }
184 }