001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.examples.features;
039:
040: import java.io.ByteArrayInputStream;
041: import java.io.ByteArrayOutputStream;
042: import java.io.InputStream;
043: import java.io.OutputStream;
044:
045: import org.millstone.base.terminal.StreamResource;
046: import org.millstone.base.ui.Component;
047: import org.millstone.base.ui.Label;
048: import org.millstone.base.ui.Link;
049: import org.millstone.base.ui.OrderedLayout;
050: import org.millstone.base.ui.Panel;
051: import org.millstone.base.ui.Upload;
052: import org.millstone.base.ui.Upload.FinishedEvent;
053:
054: public class FeatureUpload extends Feature implements
055: Upload.FinishedListener {
056:
057: Buffer buffer = new Buffer();
058:
059: Panel status = new Panel("Uploaded file:");
060:
061: public FeatureUpload() {
062: super ();
063: }
064:
065: protected Component getDemoComponent() {
066:
067: OrderedLayout l = new OrderedLayout();
068:
069: // Example panel
070: Panel show = new Panel("Upload component");
071:
072: Upload up = new Upload("Upload a file:", buffer);
073: up.addListener(this );
074:
075: show.addComponent(up);
076: status.setVisible(false);
077: l.addComponent(status);
078: l.addComponent(show);
079:
080: // Properties
081: PropertyPanel p = new PropertyPanel(up);
082: l.addComponent(p);
083:
084: return l;
085: }
086:
087: protected String getExampleSrc() {
088: return "Upload u = new Upload(\"Upload a file:\", uploadReceiver);\n\n"
089: + "public class uploadReceiver \n"
090: + "implements Upload.receiver, Upload.FinishedListener { \n"
091: + "\n"
092: + " java.io.File file;\n"
093: + " java.io.FileOutputStream fos;\n"
094: + " public uploadReceiver() {\n" + " }";
095:
096: }
097:
098: protected String getDescriptionXHTML() {
099: return "This demonstrates the use of the Upload component together with the Link component. "
100: + "This implementation does not actually store the file to disk, it only keeps it in a buffer. "
101: + "The example given on the example-tab on the other hand stores the file to disk and binds the link to that file.<br/>"
102: + "<br/>On the demo tab you can try out how the different properties affect the presentation of the component.";
103: }
104:
105: protected String getImage() {
106: return "filetransfer.jpg";
107: }
108:
109: protected String getTitle() {
110: return "Upload";
111: }
112:
113: public void uploadFinished(FinishedEvent event) {
114: status.removeAllComponents();
115: if (buffer.getStream() == null)
116: status.addComponent(new Label(
117: "Upload finished, but output buffer is null!!"));
118: else {
119: status.addComponent(new Label("<b>Name:</b> "
120: + event.getFilename(), Label.CONTENT_XHTML));
121: status.addComponent(new Label("<b>Mimetype:</b> "
122: + event.getMIMEType(), Label.CONTENT_XHTML));
123: status.addComponent(new Label("<b>Size:</b> "
124: + event.getLength() + " bytes.",
125: Label.CONTENT_XHTML));
126:
127: status.addComponent(new Link("Download "
128: + buffer.getFileName(), new StreamResource(buffer,
129: buffer.getFileName(), getApplication())));
130:
131: status.setVisible(true);
132: }
133: }
134:
135: public class Buffer implements StreamResource.StreamSource,
136: Upload.Receiver {
137: ByteArrayOutputStream outputBuffer = null;
138: String mimeType;
139: String fileName;
140:
141: public Buffer() {
142:
143: }
144:
145: public InputStream getStream() {
146: if (outputBuffer == null)
147: return null;
148: return new ByteArrayInputStream(outputBuffer.toByteArray());
149: }
150:
151: /**
152: * @see org.millstone.base.ui.Upload.Receiver#receiveUpload(String, String)
153: */
154: public OutputStream receiveUpload(String filename,
155: String MIMEType) {
156: fileName = filename;
157: mimeType = MIMEType;
158: outputBuffer = new ByteArrayOutputStream();
159: return outputBuffer;
160: }
161:
162: /**
163: * Returns the fileName.
164: * @return String
165: */
166: public String getFileName() {
167: return fileName;
168: }
169:
170: /**
171: * Returns the mimeType.
172: * @return String
173: */
174: public String getMimeType() {
175: return mimeType;
176: }
177:
178: }
179: }
|