Subversion Repository Public Repository

litesoft

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package org.litesoft.sandbox.csapp.client.widgets;

import com.google.gwt.dom.client.*;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;

public class UploadWidget extends Composite implements IsWidget
{
    public interface Callback
    {
        public Image createBusyImage();

        public Widget createWaitingMessageWidget( Widget secondsRemaining );

        public void requestingUpload( String filename );

        public void timeout( String filename );

        public void parseResponse( String fileName, String pHtmlResponse );
    }

    private static int instance = 1;

    private final FileUpload upload = createFileUploadWidget();
    private final FormPanel formPanel = createFormPanel( upload );
    private final SimplePanel viewPort = createViewPort( createFormFloater( formPanel ) );
    private final ButtonBase illusionaryClickWidget;
    private final Widget sizingWidget;

    private final int secondsToWait;
    private final Callback callback;

    private boolean externallyEnabled;
    private String lastSubmittedFileName;

    private String servletPath;
    private Timer timer;
    private boolean sized = false;

    public UploadWidget( ButtonBase illusionaryClickWidget, int secondsToWait, Callback callback )
    {
        this.secondsToWait = secondsToWait;
        this.callback = callback;
        externallyEnabled = illusionaryClickWidget.isEnabled();
        sizingWidget = createConstrainer( this.illusionaryClickWidget = illusionaryClickWidget );
        initWidget( createStylingDiv( "UploadWidget", createConstrainer( createFlowPanel( sizingWidget, viewPort ) ) ) );

        //<div style="display: block; width: 100px; height: 20px; overflow: hidden;">
        //<button style="width: 110px; height: 30px; position: relative; top: -5px; left: -5px;"><a href="javascript: void(0)">Upload File</a></button>
        //<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" />
        //</div>

        formPanel.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler()
        {
            @Override
            public void onSubmitComplete( FormPanel.SubmitCompleteEvent event )
            {
                submitResponse( event.getResults() );
            }
        } );
    }

    public void setServletPath( String url )
    {
        servletPath = noEmpty( url );
    }

    public void setEnabled( boolean enable )
    {
        enableWidgets( externallyEnabled = enable );
    }

    private void enableWidgets( boolean enable )
    {
        enable &= externallyEnabled;
        illusionaryClickWidget.setEnabled( enable );
        upload.setEnabled( enable );
    }

    @Override
    protected void onAttach()
    {
        super.onAttach();
        startTimer();
    }

    @Override
    protected void onDetach()
    {
        stopTimer();
        super.onDetach();
    }

    private void stopTimer()
    {
        if ( timer != null )
        {
            timer.cancel();
            timer = null;
        }
    }

    private void startTimer()
    {
        stopTimer();
        (timer = new Timer()
        {
            @Override
            public void run()
            {
                sized = sized || size();
                String filename = noEmpty( upload.getFilename() );
                if ( filename != null )
                {
                    stopTimer();
                    submitForm( lastSubmittedFileName = filename );
                }
            }
        }).scheduleRepeating( 250 );
    }

    private boolean size()
    {
        int width = sizingWidget.getOffsetWidth();
        int height = sizingWidget.getOffsetHeight();
        if ( (width < 16) || (height < 16) )
        {
            return false;
        }
        viewPort.setSize( width + "px", height + "px" );
        return true;
    }

    private void resetUploadWidget()
    {
        enableWidgets( true );
        formPanel.reset();
        startTimer();
    }

    private String noEmpty( String string )
    {
        if ( string != null )
        {
            if ( (string = string.trim()).length() != 0 )
            {
                return string;
            }
        }
        return null;
    }

    private void submitForm( String filename )
    {
        System.out.println( "UploadWidget.submitForm: " + filename + " to " + servletPath );
        if ( servletPath == null )
        {
            Window.alert( "No Servlet Path Set" );
            return;
        }
        callback.requestingUpload( filename );
        formPanel.setAction( servletPath );
        formPanel.submit();
        enableWidgets( false );
        showBusy();
    }

    private void timeout()
    {
        hideBusy();
        resetUploadWidget();
        callback.timeout( lastSubmittedFileName );
    }

    private void submitResponse( String htmlResponse )
    {
        hideBusy();
        resetUploadWidget();
        callback.parseResponse( lastSubmittedFileName, htmlResponse );
    }

    private static Widget createConstrainer( Widget widget )
    {
        HorizontalPanel constrainer = new HorizontalPanel();
        constrainer.add( widget );
        return constrainer;
    }

    private static Widget createStylingDiv( String styleName, Widget widget )
    {
        SimplePanel divBasePanel = new SimplePanel();
        divBasePanel.addStyleName( styleName );
        divBasePanel.setWidget( widget );
        return divBasePanel;
    }

    private static Widget createFlowPanel( Widget widget1, Widget widget2 )
    {
        FlowPanel panel = new FlowPanel();
        panel.getElement().getStyle().setPosition( Style.Position.RELATIVE );
        panel.add( widget1 );
        panel.add( widget2 );
        return panel;
    }

    private static SimplePanel createViewPort( Widget widget )
    {
        SimplePanel viewPort = new SimplePanel();
        Element element = viewPort.getElement();
        Style style = element.getStyle();
        style.setPosition( Style.Position.ABSOLUTE );
        style.setLeft( 0, Style.Unit.PX );
        style.setTop( 0, Style.Unit.PX );
        style.setOverflow( Style.Overflow.HIDDEN );
        viewPort.setWidget( widget );
        viewPort.setSize( "1px", "1px" );
        return viewPort;
    }

    private static SimplePanel createFormFloater( Widget widget )
    {
        SimplePanel panel = new SimplePanel();
        panel.setSize( "1000px", "400px" );
        Style style = panel.getElement().getStyle();
        style.setPosition( Style.Position.RELATIVE );
        style.setLeft( -600, Style.Unit.PX );
        style.setTop( -5, Style.Unit.PX );
        panel.setWidget( createConstrainer( widget ) );
        return panel;
    }

    private static FormPanel createFormPanel( FileUpload upload )
    {
        FormPanel form = new FormPanel();
        form.add( upload );
        Style style = form.getElement().getStyle();
        style.setMargin( 0, Style.Unit.PX );
        style.setPadding( 0, Style.Unit.PX );
        // Because we're going to add a FileUpload widget, we'll need to set the
        // form to use the POST method, and multipart MIME encoding.
        form.setEncoding( FormPanel.ENCODING_MULTIPART );
        form.setMethod( FormPanel.METHOD_POST );
        return form;
    }

    private static FileUpload createFileUploadWidget()
    {
        FileUpload upload = new FileUpload();
        upload.setName( "UploadWidget" + instance++ );
        upload.getElement().setAttribute( "size", "1" );
        upload.setSize( "1000px", "400px" );
        Style style = upload.getElement().getStyle();
        style.setMargin( 0, Style.Unit.PX );
        style.setPadding( 0, Style.Unit.PX );
        style.setBorderWidth( 0, Style.Unit.PX );
        style.setFontSize( 200, Style.Unit.PX );
        style.setOpacity( 0 ); // Tested w/ FireFox & IE8!
        return upload;
    }

    protected void hideBusy()
    {
        if ( busyPopup != null )
        {
            busyPopup.hide();
            busyPopup = null;
        }
    }

    protected void showBusy()
    {
        hideBusy();
        (busyPopup = new OurBusyPopup()).setPopupPositionAndShow( new PopupPanel.PositionCallback()
        {
            @Override
            public void setPosition( int offsetWidth, int offsetHeight )
            {
                int left = (Window.getClientWidth() - offsetWidth) / 2;
                int top = (Window.getClientHeight() - offsetHeight) / 2;
                busyPopup.setPopupPosition( left, top );
            }
        } );
    }

    private OurBusyPopup busyPopup;

    private class OurBusyPopup extends PopupPanel
    {
        private Widget messageWidget;
        private boolean messageShowing = false;
        private int secondsRemaining = secondsToWait;
        private Label secondsRemainingLabel = new Label( "" + secondsRemaining );
        private long started = System.currentTimeMillis();
        private float currentOpacity = 0;
        private Timer timer;

        private OurBusyPopup()
        {
            setModal( true );
            setGlassEnabled( true );
            getGlassElement().getStyle().setBackgroundColor( "#fff" );
            getGlassElement().getStyle().setCursor( Style.Cursor.WAIT );
            getGlassElement().getStyle().setOpacity( currentOpacity );

            VerticalPanel outerpanel = new VerticalPanel();
            outerpanel.setStyleName( "UploadWidgetBusyContainer" );
            outerpanel.setHorizontalAlignment( HasHorizontalAlignment.ALIGN_CENTER );
            Image busyImage = callback.createBusyImage();
            if ( busyImage != null )
            {
                outerpanel.add( busyImage );
            }
            outerpanel.add( createStylingDiv( "UploadWidgetBusyMessage", callback.createWaitingMessageWidget( secondsRemainingLabel ) ) );
            outerpanel.getElement().getStyle().setVisibility( Style.Visibility.HIDDEN );
            setWidget( messageWidget = outerpanel );
            startTimer();
        }

        @Override
        public void hide()
        {
            stopTimer();
            super.hide();
        }

        private void stopTimer()
        {
            if ( timer != null )
            {
                timer.cancel();
                timer = null;
            }
        }

        private void startTimer()
        {
            stopTimer();
            (timer = new Timer()
            {
                @Override
                public void run()
                {
                    if ( currentOpacity < 0.75 )
                    {
                        getGlassElement().getStyle().setOpacity( currentOpacity += 0.025 );
                    }
                    int secondsPassed = (int) ((System.currentTimeMillis() - started) / 1000);
                    if ( (secondsPassed > 1) && !messageShowing )
                    {
                        messageShowing = true;
                        messageWidget.getElement().getStyle().setVisibility( Style.Visibility.VISIBLE );
                    }
                    int calcSecondsRemaining = secondsToWait - secondsPassed;
                    if ( calcSecondsRemaining < 1 )
                    {
                        stopTimer();
                        timeout();
                        return;
                    }
                    if ( calcSecondsRemaining != secondsRemaining )
                    {
                        secondsRemainingLabel.setText( (secondsRemaining = calcSecondsRemaining) + "" );
                    }
                }
            }).scheduleRepeating( 100 );
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/Upload/src/org/litesoft/sandbox/csapp/client/widgets/UploadWidget.java

Diff revisions: vs.
Revision Author Commited Message
535 Diff Diff GeorgeS picture GeorgeS Fri 30 Sep, 2011 18:34:03 +0000

!

533 Diff Diff GeorgeS picture GeorgeS Fri 30 Sep, 2011 17:25:49 +0000
531 Diff Diff GeorgeS picture GeorgeS Thu 29 Sep, 2011 20:58:34 +0000

Working...

529 Diff Diff GeorgeS picture GeorgeS Thu 29 Sep, 2011 05:31:12 +0000
528 Diff Diff GeorgeS picture GeorgeS Thu 29 Sep, 2011 04:05:38 +0000

More

527 Diff Diff GeorgeS picture GeorgeS Thu 29 Sep, 2011 01:17:00 +0000
526 Diff Diff GeorgeS picture GeorgeS Thu 29 Sep, 2011 00:55:24 +0000
523 Diff Diff GeorgeS picture GeorgeS Wed 28 Sep, 2011 15:21:49 +0000

Latest Changes

521 Diff Diff GeorgeS picture GeorgeS Tue 27 Sep, 2011 18:24:12 +0000

Upload Widget...

520 GeorgeS picture GeorgeS Tue 27 Sep, 2011 13:01:16 +0000