|
@@ -5,10 +5,8 @@ |
5 |
5 |
|
import com.google.gwt.user.client.*; |
6 |
6 |
|
import com.google.gwt.user.client.ui.*; |
7 |
7 |
|
|
8 |
|
- |
public class UploadWidget extends Composite implements IsWidget |
9 |
|
- |
{ |
10 |
|
- |
public interface Callback |
11 |
|
- |
{ |
|
8 |
+ |
public class UploadWidget extends Composite implements IsWidget { |
|
9 |
+ |
public interface Callback { |
12 |
10 |
|
public Image createBusyImage(); |
13 |
11 |
|
|
14 |
12 |
|
public Widget createWaitingMessageWidget( Widget secondsRemaining ); |
|
@@ -38,8 +36,7 @@ |
38 |
36 |
|
private Timer timer; |
39 |
37 |
|
private boolean sized = false; |
40 |
38 |
|
|
41 |
|
- |
public UploadWidget( ButtonBase illusionaryClickWidget, ButtonBase removeButton, int secondsToWait, Callback callback ) |
42 |
|
- |
{ |
|
39 |
+ |
public UploadWidget( ButtonBase illusionaryClickWidget, ButtonBase removeButton, int secondsToWait, Callback callback ) { |
43 |
40 |
|
this.removeButton = removeButton; |
44 |
41 |
|
this.secondsToWait = secondsToWait; |
45 |
42 |
|
this.callback = callback; |
|
@@ -49,31 +46,27 @@ |
49 |
46 |
|
|
50 |
47 |
|
//<div style="display: block; width: 100px; height: 20px; overflow: hidden;"> |
51 |
48 |
|
//<button style="width: 110px; height: 30px; position: relative; top: -5px; left: -5px;"><a href="javascript: void(0)">Upload File</a></button> |
52 |
|
- |
//<input type="file" id="upload_input" name="upload" style="font-size: 50px; width: 120px; opacity: 0; filter:alpha(opacity: 0); position: relative; top: -40px;; left: -20px" /> |
|
49 |
+ |
//<input type="file" id="upload_input" name="upload" style="font-size: 50px; width: 120px; opacity: 0; filter:alpha(opacity: 0); position: relative; |
|
50 |
+ |
// top: -40px;; left: -20px" /> |
53 |
51 |
|
//</div> |
54 |
52 |
|
|
55 |
|
- |
formPanel.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler() |
56 |
|
- |
{ |
|
53 |
+ |
formPanel.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler() { |
57 |
54 |
|
@Override |
58 |
|
- |
public void onSubmitComplete( FormPanel.SubmitCompleteEvent event ) |
59 |
|
- |
{ |
|
55 |
+ |
public void onSubmitComplete( FormPanel.SubmitCompleteEvent event ) { |
60 |
56 |
|
submitResponse( event.getResults() ); |
61 |
57 |
|
} |
62 |
58 |
|
} ); |
63 |
59 |
|
} |
64 |
60 |
|
|
65 |
|
- |
public void setServletPath( String url ) |
66 |
|
- |
{ |
|
61 |
+ |
public void setServletPath( String url ) { |
67 |
62 |
|
servletPath = noEmpty( url ); |
68 |
63 |
|
} |
69 |
64 |
|
|
70 |
|
- |
public void setEnabled( boolean enable ) |
71 |
|
- |
{ |
|
65 |
+ |
public void setEnabled( boolean enable ) { |
72 |
66 |
|
enableWidgets( externallyEnabled = enable ); |
73 |
67 |
|
} |
74 |
68 |
|
|
75 |
|
- |
private void enableWidgets( boolean enable ) |
76 |
|
- |
{ |
|
69 |
+ |
private void enableWidgets( boolean enable ) { |
77 |
70 |
|
enable &= externallyEnabled; |
78 |
71 |
|
removeButton.setEnabled( enable ); |
79 |
72 |
|
illusionaryClickWidget.setEnabled( enable ); |
|
@@ -81,40 +74,32 @@ |
81 |
74 |
|
} |
82 |
75 |
|
|
83 |
76 |
|
@Override |
84 |
|
- |
protected void onAttach() |
85 |
|
- |
{ |
|
77 |
+ |
protected void onAttach() { |
86 |
78 |
|
super.onAttach(); |
87 |
79 |
|
startTimer(); |
88 |
80 |
|
} |
89 |
81 |
|
|
90 |
82 |
|
@Override |
91 |
|
- |
protected void onDetach() |
92 |
|
- |
{ |
|
83 |
+ |
protected void onDetach() { |
93 |
84 |
|
stopTimer(); |
94 |
85 |
|
super.onDetach(); |
95 |
86 |
|
} |
96 |
87 |
|
|
97 |
|
- |
private void stopTimer() |
98 |
|
- |
{ |
99 |
|
- |
if ( timer != null ) |
100 |
|
- |
{ |
|
88 |
+ |
private void stopTimer() { |
|
89 |
+ |
if ( timer != null ) { |
101 |
90 |
|
timer.cancel(); |
102 |
91 |
|
timer = null; |
103 |
92 |
|
} |
104 |
93 |
|
} |
105 |
94 |
|
|
106 |
|
- |
private void startTimer() |
107 |
|
- |
{ |
|
95 |
+ |
private void startTimer() { |
108 |
96 |
|
stopTimer(); |
109 |
|
- |
(timer = new Timer() |
110 |
|
- |
{ |
|
97 |
+ |
(timer = new Timer() { |
111 |
98 |
|
@Override |
112 |
|
- |
public void run() |
113 |
|
- |
{ |
|
99 |
+ |
public void run() { |
114 |
100 |
|
sized = sized || size(); |
115 |
101 |
|
String filename = noEmpty( upload.getFilename() ); |
116 |
|
- |
if ( filename != null ) |
117 |
|
- |
{ |
|
102 |
+ |
if ( filename != null ) { |
118 |
103 |
|
stopTimer(); |
119 |
104 |
|
submitForm( lastSubmittedFileName = filename ); |
120 |
105 |
|
formPanel.reset(); |
|
@@ -123,41 +108,33 @@ |
123 |
108 |
|
}).scheduleRepeating( 250 ); |
124 |
109 |
|
} |
125 |
110 |
|
|
126 |
|
- |
private boolean size() |
127 |
|
- |
{ |
|
111 |
+ |
private boolean size() { |
128 |
112 |
|
int width = sizingWidget.getOffsetWidth(); |
129 |
113 |
|
int height = sizingWidget.getOffsetHeight(); |
130 |
|
- |
if ( (width < 16) || (height < 16) ) |
131 |
|
- |
{ |
|
114 |
+ |
if ( (width < 16) || (height < 16) ) { |
132 |
115 |
|
return false; |
133 |
116 |
|
} |
134 |
117 |
|
viewPort.setSize( width + "px", height + "px" ); |
135 |
118 |
|
return true; |
136 |
119 |
|
} |
137 |
120 |
|
|
138 |
|
- |
private void resetUploadWidget() |
139 |
|
- |
{ |
|
121 |
+ |
private void resetUploadWidget() { |
140 |
122 |
|
enableWidgets( true ); |
141 |
123 |
|
startTimer(); |
142 |
124 |
|
} |
143 |
125 |
|
|
144 |
|
- |
private String noEmpty( String string ) |
145 |
|
- |
{ |
146 |
|
- |
if ( string != null ) |
147 |
|
- |
{ |
148 |
|
- |
if ( (string = string.trim()).length() != 0 ) |
149 |
|
- |
{ |
|
126 |
+ |
private String noEmpty( String string ) { |
|
127 |
+ |
if ( string != null ) { |
|
128 |
+ |
if ( (string = string.trim()).length() != 0 ) { |
150 |
129 |
|
return string; |
151 |
130 |
|
} |
152 |
131 |
|
} |
153 |
132 |
|
return null; |
154 |
133 |
|
} |
155 |
134 |
|
|
156 |
|
- |
private void submitForm( String filename ) |
157 |
|
- |
{ |
|
135 |
+ |
private void submitForm( String filename ) { |
158 |
136 |
|
System.out.println( "UploadWidget.submitForm: " + filename + " to " + servletPath ); |
159 |
|
- |
if ( servletPath == null ) |
160 |
|
- |
{ |
|
137 |
+ |
if ( servletPath == null ) { |
161 |
138 |
|
Window.alert( "No Servlet Path Set" ); |
162 |
139 |
|
return; |
163 |
140 |
|
} |
|
@@ -168,37 +145,32 @@ |
168 |
145 |
|
showBusy(); |
169 |
146 |
|
} |
170 |
147 |
|
|
171 |
|
- |
private void timeout() |
172 |
|
- |
{ |
|
148 |
+ |
private void timeout() { |
173 |
149 |
|
hideBusy(); |
174 |
150 |
|
resetUploadWidget(); |
175 |
151 |
|
callback.timeout( lastSubmittedFileName ); |
176 |
152 |
|
} |
177 |
153 |
|
|
178 |
|
- |
private void submitResponse( String htmlResponse ) |
179 |
|
- |
{ |
|
154 |
+ |
private void submitResponse( String htmlResponse ) { |
180 |
155 |
|
hideBusy(); |
181 |
156 |
|
resetUploadWidget(); |
182 |
157 |
|
callback.parseResponse( lastSubmittedFileName, htmlResponse ); |
183 |
158 |
|
} |
184 |
159 |
|
|
185 |
|
- |
private static Widget createConstrainer( Widget widget ) |
186 |
|
- |
{ |
|
160 |
+ |
private static Widget createConstrainer( Widget widget ) { |
187 |
161 |
|
HorizontalPanel constrainer = new HorizontalPanel(); |
188 |
162 |
|
constrainer.add( widget ); |
189 |
163 |
|
return constrainer; |
190 |
164 |
|
} |
191 |
165 |
|
|
192 |
|
- |
private static Widget createStylingDiv( String styleName, Widget widget ) |
193 |
|
- |
{ |
|
166 |
+ |
private static Widget createStylingDiv( String styleName, Widget widget ) { |
194 |
167 |
|
SimplePanel divBasePanel = new SimplePanel(); |
195 |
168 |
|
divBasePanel.addStyleName( styleName ); |
196 |
169 |
|
divBasePanel.setWidget( widget ); |
197 |
170 |
|
return divBasePanel; |
198 |
171 |
|
} |
199 |
172 |
|
|
200 |
|
- |
private static Widget createFlowPanel( Widget widget1, Widget widget2 ) |
201 |
|
- |
{ |
|
173 |
+ |
private static Widget createFlowPanel( Widget widget1, Widget widget2 ) { |
202 |
174 |
|
FlowPanel panel = new FlowPanel(); |
203 |
175 |
|
panel.getElement().getStyle().setPosition( Style.Position.RELATIVE ); |
204 |
176 |
|
panel.add( widget1 ); |
|
@@ -206,8 +178,7 @@ |
206 |
178 |
|
return panel; |
207 |
179 |
|
} |
208 |
180 |
|
|
209 |
|
- |
private static SimplePanel createViewPort( Widget widget ) |
210 |
|
- |
{ |
|
181 |
+ |
private static SimplePanel createViewPort( Widget widget ) { |
211 |
182 |
|
SimplePanel viewPort = new SimplePanel(); |
212 |
183 |
|
Element element = viewPort.getElement(); |
213 |
184 |
|
Style style = element.getStyle(); |
|
@@ -220,8 +191,7 @@ |
220 |
191 |
|
return viewPort; |
221 |
192 |
|
} |
222 |
193 |
|
|
223 |
|
- |
private static SimplePanel createFormFloater( Widget widget ) |
224 |
|
- |
{ |
|
194 |
+ |
private static SimplePanel createFormFloater( Widget widget ) { |
225 |
195 |
|
SimplePanel panel = new SimplePanel(); |
226 |
196 |
|
panel.setSize( "1000px", "400px" ); |
227 |
197 |
|
Style style = panel.getElement().getStyle(); |
|
@@ -232,8 +202,7 @@ |
232 |
202 |
|
return panel; |
233 |
203 |
|
} |
234 |
204 |
|
|
235 |
|
- |
private static FormPanel createFormPanel( FileUpload upload ) |
236 |
|
- |
{ |
|
205 |
+ |
private static FormPanel createFormPanel( FileUpload upload ) { |
237 |
206 |
|
FormPanel form = new FormPanel(); |
238 |
207 |
|
form.add( upload ); |
239 |
208 |
|
Style style = form.getElement().getStyle(); |
|
@@ -246,8 +215,7 @@ |
246 |
215 |
|
return form; |
247 |
216 |
|
} |
248 |
217 |
|
|
249 |
|
- |
private static FileUpload createFileUploadWidget() |
250 |
|
- |
{ |
|
218 |
+ |
private static FileUpload createFileUploadWidget() { |
251 |
219 |
|
FileUpload upload = new FileUpload(); |
252 |
220 |
|
upload.setName( "UploadWidget" + instance++ ); |
253 |
221 |
|
upload.getElement().setAttribute( "size", "1" ); |
|
@@ -261,23 +229,18 @@ |
261 |
229 |
|
return upload; |
262 |
230 |
|
} |
263 |
231 |
|
|
264 |
|
- |
protected void hideBusy() |
265 |
|
- |
{ |
266 |
|
- |
if ( busyPopup != null ) |
267 |
|
- |
{ |
|
232 |
+ |
protected void hideBusy() { |
|
233 |
+ |
if ( busyPopup != null ) { |
268 |
234 |
|
busyPopup.hide(); |
269 |
235 |
|
busyPopup = null; |
270 |
236 |
|
} |
271 |
237 |
|
} |
272 |
238 |
|
|
273 |
|
- |
protected void showBusy() |
274 |
|
- |
{ |
|
239 |
+ |
protected void showBusy() { |
275 |
240 |
|
hideBusy(); |
276 |
|
- |
(busyPopup = new OurBusyPopup()).setPopupPositionAndShow( new PopupPanel.PositionCallback() |
277 |
|
- |
{ |
|
241 |
+ |
(busyPopup = new OurBusyPopup()).setPopupPositionAndShow( new PopupPanel.PositionCallback() { |
278 |
242 |
|
@Override |
279 |
|
- |
public void setPosition( int offsetWidth, int offsetHeight ) |
280 |
|
- |
{ |
|
243 |
+ |
public void setPosition( int offsetWidth, int offsetHeight ) { |
281 |
244 |
|
int left = (Window.getClientWidth() - offsetWidth) / 2; |
282 |
245 |
|
int top = (Window.getClientHeight() - offsetHeight) / 2; |
283 |
246 |
|
busyPopup.setPopupPosition( left, top ); |
|
@@ -287,8 +250,7 @@ |
287 |
250 |
|
|
288 |
251 |
|
private OurBusyPopup busyPopup; |
289 |
252 |
|
|
290 |
|
- |
private class OurBusyPopup extends PopupPanel |
291 |
|
- |
{ |
|
253 |
+ |
private class OurBusyPopup extends PopupPanel { |
292 |
254 |
|
private Widget messageWidget; |
293 |
255 |
|
private boolean messageShowing = false; |
294 |
256 |
|
private int secondsRemaining = secondsToWait; |
|
@@ -297,8 +259,7 @@ |
297 |
259 |
|
private float currentOpacity = 0; |
298 |
260 |
|
private Timer timer; |
299 |
261 |
|
|
300 |
|
- |
private OurBusyPopup() |
301 |
|
- |
{ |
|
262 |
+ |
private OurBusyPopup() { |
302 |
263 |
|
setModal( true ); |
303 |
264 |
|
setGlassEnabled( true ); |
304 |
265 |
|
getGlassElement().getStyle().setBackgroundColor( "#fff" ); |
|
@@ -309,8 +270,7 @@ |
309 |
270 |
|
outerpanel.setStyleName( "UploadWidgetBusyContainer" ); |
310 |
271 |
|
outerpanel.setHorizontalAlignment( HasHorizontalAlignment.ALIGN_CENTER ); |
311 |
272 |
|
Image busyImage = callback.createBusyImage(); |
312 |
|
- |
if ( busyImage != null ) |
313 |
|
- |
{ |
|
273 |
+ |
if ( busyImage != null ) { |
314 |
274 |
|
outerpanel.add( busyImage ); |
315 |
275 |
|
} |
316 |
276 |
|
outerpanel.add( createStylingDiv( "UploadWidgetBusyMessage", callback.createWaitingMessageWidget( secondsRemainingLabel ) ) ); |
|
@@ -320,48 +280,38 @@ |
320 |
280 |
|
} |
321 |
281 |
|
|
322 |
282 |
|
@Override |
323 |
|
- |
public void hide() |
324 |
|
- |
{ |
|
283 |
+ |
public void hide() { |
325 |
284 |
|
stopTimer(); |
326 |
285 |
|
super.hide(); |
327 |
286 |
|
} |
328 |
287 |
|
|
329 |
|
- |
private void stopTimer() |
330 |
|
- |
{ |
331 |
|
- |
if ( timer != null ) |
332 |
|
- |
{ |
|
288 |
+ |
private void stopTimer() { |
|
289 |
+ |
if ( timer != null ) { |
333 |
290 |
|
timer.cancel(); |
334 |
291 |
|
timer = null; |
335 |
292 |
|
} |
336 |
293 |
|
} |
337 |
294 |
|
|
338 |
|
- |
private void startTimer() |
339 |
|
- |
{ |
|
295 |
+ |
private void startTimer() { |
340 |
296 |
|
stopTimer(); |
341 |
|
- |
(timer = new Timer() |
342 |
|
- |
{ |
|
297 |
+ |
(timer = new Timer() { |
343 |
298 |
|
@Override |
344 |
|
- |
public void run() |
345 |
|
- |
{ |
346 |
|
- |
if ( currentOpacity < 0.75 ) |
347 |
|
- |
{ |
|
299 |
+ |
public void run() { |
|
300 |
+ |
if ( currentOpacity < 0.75 ) { |
348 |
301 |
|
getGlassElement().getStyle().setOpacity( currentOpacity += 0.025 ); |
349 |
302 |
|
} |
350 |
303 |
|
int secondsPassed = (int) ((System.currentTimeMillis() - started) / 1000); |
351 |
|
- |
if ( (secondsPassed > 1) && !messageShowing ) |
352 |
|
- |
{ |
|
304 |
+ |
if ( (secondsPassed > 1) && !messageShowing ) { |
353 |
305 |
|
messageShowing = true; |
354 |
306 |
|
messageWidget.getElement().getStyle().setVisibility( Style.Visibility.VISIBLE ); |
355 |
307 |
|
} |
356 |
308 |
|
int calcSecondsRemaining = secondsToWait - secondsPassed; |
357 |
|
- |
if ( calcSecondsRemaining < 1 ) |
358 |
|
- |
{ |
|
309 |
+ |
if ( calcSecondsRemaining < 1 ) { |
359 |
310 |
|
stopTimer(); |
360 |
311 |
|
timeout(); |
361 |
312 |
|
return; |
362 |
313 |
|
} |
363 |
|
- |
if ( calcSecondsRemaining != secondsRemaining ) |
364 |
|
- |
{ |
|
314 |
+ |
if ( calcSecondsRemaining != secondsRemaining ) { |
365 |
315 |
|
secondsRemainingLabel.setText( (secondsRemaining = calcSecondsRemaining) + "" ); |
366 |
316 |
|
} |
367 |
317 |
|
} |