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
package org.litesoft.sandbox.csapp.client.widgets;

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

public class UploadWidget extends Composite implements IsWidget
{
    private static int instance = 1;

    private FileUpload upload = new FileUpload();
    private String servletPath;
    private Timer timer;

    public UploadWidget()
    {
        initWidget( new FormPanel() );
        FormPanel form = getFormPanel();
        form.setWidget( upload );
        upload.setName( "UploadWidget" + instance++ );

        // 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 );
        form.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler()
        {
            @Override
            public void onSubmitComplete( FormPanel.SubmitCompleteEvent event )
            {
                submitResponse( event.getResults() );
            }
        } );
    }

    protected FormPanel getFormPanel()
    {
        return (FormPanel) getWidget();
    }

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

    public void setSize( String width )
    {
        if ( null != (width = noEmpty( width )) )
        {
            upload.getElement().setAttribute( "size", width );
        }
    }

    @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()
            {
                String filename = noEmpty( upload.getFilename() );
                if ( filename != null )
                {
                    upload.setEnabled( false );
                    stopTimer();
                    submitForm( filename );
                }
            }
        }).scheduleRepeating( 250 );
    }

    private void resetUploadWidget()
    {
        upload.setEnabled( true );
        getFormPanel().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;
        }
        FormPanel form = getFormPanel();
        form.setAction( servletPath );
        form.submit();
    }

    private void submitResponse( String pHtmlResponse )
    {
        // Truly Bad Url:
        // null

        // No Servlet:
        // <h2>HTTP ERROR: 404</h2><pre>NOT_FOUND</pre>
        // <p>RequestURI=/v2/TestSubmit</p><p><i><small><a href="http://jetty.mortbay.org/">Powered by Jetty://</a></small></i></p><br>
        // <br>
        // <br>
        // <br>
        // ...


        System.out.println( "UploadWidget.onSubmitComplete:\n" + pHtmlResponse );
        resetUploadWidget();
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
518 GeorgeS picture GeorgeS Tue 27 Sep, 2011 04:06:12 +0000