Subversion Repository Public Repository

litesoft

Diff Revisions 521 vs 523 for /trunk/GWT_Sandbox/Upload/src/org/litesoft/sandbox/csapp/client/widgets/UploadWidget.java

Diff revisions: vs.
  @@ -8,32 +8,41 @@
8 8 {
9 9 private static int instance = 1;
10 10
11 - private FileUpload upload = new FileUpload();
11 + private final FileUpload upload = createFileUploadWidget();
12 + private final FormPanel formPanel = createFormPanel( upload );
13 + private final SimplePanel viewPort = createViewPort( createFormFloater( formPanel ) );
14 + private final Widget sizingWidget;
15 +
12 16 private String servletPath;
13 17 private Timer timer;
14 18
15 19 public UploadWidget()
16 20 {
17 - initWidget( new FormPanel() );
21 + this( new Button( "Pick" ) );
22 + }
23 +
24 + public UploadWidget( ButtonBase illusionaryClickWidget )
25 + {
26 + sizingWidget = createDivConstrainer( illusionaryClickWidget );
27 + initWidget( createStylingDiv( "UploadWidget", createDivConstrainer( createFlowPanel( sizingWidget, viewPort ) ) ) );
28 +
29 + //<div style="display: block; width: 100px; height: 20px; overflow: hidden;">
30 + //<button style="width: 110px; height: 30px; position: relative; top: -5px; left: -5px;"><a href="javascript: void(0)">Upload File</a></button>
31 + //<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" />
32 + //</div>
18 33
19 34 upload.setName( "UploadWidget" + instance++ );
20 35 upload.getElement().setAttribute( "size", "1" );
21 - upload.setSize( "400px", "400px" );
22 - upload.getElement().getStyle().setFontSize( 200, Style.Unit.PX );
23 - upload.getElement().getStyle().setOpacity( 0 );
36 + // upload.setSize( "400px", "400px" );
37 + // upload.getElement().getStyle().setFontSize( 200, Style.Unit.PX );
38 + // upload.getElement().getStyle().setOpacity( 0 ); // Todo: Verify Opacity & IE!
24 39 upload.getElement().getStyle().setCursor( Style.Cursor.POINTER );
25 40
26 - FormPanel form = getFormPanel();
27 - FlowPanel flowPanel = new FlowPanel();
28 - flowPanel.add( upload );
29 - flowPanel.add( new Button( "Pick" ) );
30 - form.setWidget( flowPanel );
31 -
32 41 // Because we're going to add a FileUpload widget, we'll need to set the
33 42 // form to use the POST method, and multipart MIME encoding.
34 - form.setEncoding( FormPanel.ENCODING_MULTIPART );
35 - form.setMethod( FormPanel.METHOD_POST );
36 - form.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler()
43 + formPanel.setEncoding( FormPanel.ENCODING_MULTIPART );
44 + formPanel.setMethod( FormPanel.METHOD_POST );
45 + formPanel.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler()
37 46 {
38 47 @Override
39 48 public void onSubmitComplete( FormPanel.SubmitCompleteEvent event )
  @@ -43,11 +52,6 @@
43 52 } );
44 53 }
45 54
46 - protected FormPanel getFormPanel()
47 - {
48 - return (FormPanel) getWidget();
49 - }
50 -
51 55 public void setServletPath( String url )
52 56 {
53 57 servletPath = noEmpty( url );
  @@ -97,7 +101,7 @@
97 101 private void resetUploadWidget()
98 102 {
99 103 upload.setEnabled( true );
100 - getFormPanel().reset();
104 + formPanel.reset();
101 105 startTimer();
102 106 }
103 107
  @@ -121,9 +125,8 @@
121 125 Window.alert( "No Servlet Path Set" );
122 126 return;
123 127 }
124 - FormPanel form = getFormPanel();
125 - form.setAction( servletPath );
126 - form.submit();
128 + formPanel.setAction( servletPath );
129 + formPanel.submit();
127 130 upload.setEnabled( false );
128 131 }
129 132
  @@ -144,45 +147,61 @@
144 147 resetUploadWidget();
145 148 }
146 149
147 - /**
148 - * Programmatically simulate a User Click Event on an Element located by ID (pID).
149 - *
150 - * @param pID ID of the element to "click".
151 - *
152 - * @return Error text - null/empty means no error.
153 - */
154 - public static native String clickElementByID( String pID ) /*-{
155 - var zDoc = $wnd.document;
156 - var zTarget = zDoc.getElementById( pID );
157 - if ( zTarget )
158 - {
159 - var zStep = 1;
160 - try
161 - {
162 - if ( zDoc.dispatchEvent ) // W3C
163 - {
164 - zStep = 2;
165 - var zEvent = document.createEvent( "MouseEvents" );
166 - zStep = 3;
167 - zEvent.initMouseEvent( "click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, zTarget );
168 - zStep = 4;
169 - zTarget.dispatchEvent( zEvent );
170 - return null;
171 - }
172 - zStep = 5;
173 - if ( document.fireEvent ) // IE
174 - {
175 - zStep = 6;
176 - zTarget.fireEvent( "onclick" );
177 - return null;
178 - }
179 - return "Un-recognized/supported Browser";
180 - }
181 - catch ( e )
182 - {
183 - return "Exception at " + zStep + " in clickElementByID:\n\n" + e;
184 - }
185 - }
186 - return "No Element found w/ ID: " + pID;
187 - }-*/;
150 + private static Widget createDivConstrainer( Widget widget )
151 + {
152 + HorizontalPanel divConstrainer = new HorizontalPanel();
153 + divConstrainer.add( widget );
154 + return divConstrainer;
155 + }
156 +
157 + private static Widget createStylingDiv( String styleName, Widget widget )
158 + {
159 + SimplePanel divBasePanel = new SimplePanel();
160 + divBasePanel.addStyleName( styleName );
161 + divBasePanel.setWidget( widget );
162 + return divBasePanel;
163 + }
164 +
165 + private static Widget createFlowPanel( Widget widget1, Widget widget2 )
166 + {
167 + FlowPanel panel = new FlowPanel();
168 + panel.getElement().getStyle().setPosition( Style.Position.RELATIVE );
169 + panel.add( widget1 );
170 + panel.add( widget2 );
171 + return panel;
172 + }
173 +
174 + private static SimplePanel createViewPort( Widget widget )
175 + {
176 + SimplePanel viewPort = new SimplePanel();
177 + Style style = viewPort.getElement().getStyle();
178 + style.setPosition( Style.Position.ABSOLUTE );
179 + style.setOverflow( Style.Overflow.HIDDEN );
180 + viewPort.setWidget( widget );
181 + return viewPort;
182 + }
183 +
184 + private static SimplePanel createFormFloater( Widget widget )
185 + {
186 + SimplePanel panel = new SimplePanel();
187 + Style style = panel.getElement().getStyle();
188 + style.setRight( 0, Style.Unit.PX );
189 + style.setTop( 0, Style.Unit.PX );
190 + panel.setWidget( widget );
191 + return panel;
192 + }
193 +
194 + private static FormPanel createFormPanel( FileUpload upload )
195 + {
196 + FormPanel form = new FormPanel();
197 + form.getElement().getStyle().setMargin( 0, Style.Unit.PX );
198 + form.add( upload );
199 + return form;
200 + }
201 +
202 +
203 + private static FileUpload createFileUploadWidget()
204 + {
205 + return new FileUpload();
206 + }
188 207 }