blob: 6c3a91f3b0088e5ed3615dfc5cc92c8509210749 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package com.vaadin.ui;
import java.io.OutputStream;
import java.io.Serializable;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.terminal.Receiver;
import com.vaadin.terminal.ReceiverOwner.ReceivingEndedEvent;
import com.vaadin.terminal.ReceiverOwner.ReceivingFailedEvent;
import com.vaadin.terminal.ReceiverOwner.ReceivingProgressedEvent;
import com.vaadin.terminal.ReceiverOwner.ReceivingStartedEvent;
/**
* {@link DragAndDropWrapper} can receive also files from client computer if
* appropriate HTML 5 features are supported on client side. This class wraps
* information about dragged file on server side.
*/
public class Html5File implements Serializable {
final class ProxyReceiver implements Receiver {
public OutputStream receiveUpload(String filename, String MIMEType) {
if (receiver == null) {
return null;
}
return receiver.receiveUpload(filename, MIMEType);
}
Html5File getFile() {
return Html5File.this;
}
}
private String name;
private long size;
private Receiver receiver;
private String type;
Html5File(String name, long size, String mimeType) {
this.name = name;
this.size = size;
type = mimeType;
}
/**
* The receiver that is registered to the terminal. Wraps the actual
* Receiver set later by Html5File user.
*/
private ProxyReceiver proxyReceiver = new ProxyReceiver();
private boolean interrupted = false;
private Html5FileUploadListener listener;
public String getFileName() {
return name;
}
public long getFileSize() {
return size;
}
public String getType() {
return type;
}
/**
* Sets the {@link Receiver} that into which the file contents will be
* written. Usage of Receiver is similar to {@link Upload} component.
* <p>
* If the {@link Receiver} is not set in the {@link DropHandler} the file
* contents will not be sent to server.
* <p>
* <em>Note!</em> receiving file contents is experimental feature depending
* on HTML 5 API's. It is supported only by modern web browsers like Firefox
* 3.6 and above and recent webkit based browsers (Safari 5, Chrome 6) at
* this time.
*
* @param receiver
* the callback that returns stream where the implementation
* writes the file contents as it arrives.
*/
public void setReceiver(Receiver receiver) {
this.receiver = receiver;
}
public Receiver getReceiver() {
return receiver;
}
ProxyReceiver getProxyReceiver() {
return proxyReceiver;
}
/**
* Gets the {@link Html5FileUploadListener} that is used to track the
* progress of streaming the file contents to given {@link Receiver}.
*
* @return
*/
public Html5FileUploadListener getUploadListener() {
return listener;
}
/**
* Sets the {@link Html5FileUploadListener} that can be used to track the
* progress of streaming the file contents to given {@link Receiver}.
*
* @param listener
* @see #setReceiver(Receiver)
*/
public void setUploadListener(Html5FileUploadListener listener) {
this.listener = listener;
}
public boolean isInterrupted() {
return interrupted;
}
/**
* Interrupts uploading this file.
*
* @param interrupted
*/
public void setInterrupted(boolean interrupted) {
this.interrupted = interrupted;
}
public interface Html5FileUploadListener extends Serializable {
void onProgress(ReceivingProgressedEvent event);
void uploadStarted(ReceivingStartedEvent event);
void uploadFinished(ReceivingEndedEvent event);
void uploadFailed(ReceivingFailedEvent event);
}
}
|