litesoft
@ 948
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / client / foundation / widget / upload / UploadWidget.java
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 |
package com.temp.client.foundation.widget.upload; import com.google.gwt.dom.client.*; import com.google.gwt.dom.client.Style.*; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import com.temp.shared.utils.*; 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, removeButton; private final Widget sizingWidget; private final int secondsToWait; private final Callback callback; private boolean externallyEnabled, removeButtonExternallyEnabled; private String lastSubmittedFileName; private String servletPath; private Timer timer; private boolean sized = false; public UploadWidget( ButtonBase illusionaryClickWidget, ButtonBase removeButton, int secondsToWait, Callback callback ) { this.illusionaryClickWidget = illusionaryClickWidget; this.removeButton = removeButton; this.secondsToWait = secondsToWait; this.callback = callback; externallyEnabled = illusionaryClickWidget.isEnabled(); sizingWidget = createConstrainer( illusionaryClickWidget ); initWidget( createStylingDiv( "UploadWidget", createConstrainer( createFlowPanel( sizingWidget, viewPort ) ) ) ); formPanel.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler() { @Override public void onSubmitComplete( FormPanel.SubmitCompleteEvent event ) { submitResponse( StringUtils.noEmpty( event.getResults() ) ); } } ); } public void setServletPath( String url ) { servletPath = StringUtils.noEmpty( url ); } public void setRemoveButtonEnabled( boolean enable ) { enableRemoveButton( removeButtonExternallyEnabled = enable ); } public void setEnabled( boolean enable ) { enableWidgets( externallyEnabled = enable ); } private void enableWidgets( boolean enable ) { enableRemoveButton( enable ); enable &= externallyEnabled; illusionaryClickWidget.setEnabled( enable ); upload.setEnabled( enable ); } private void enableRemoveButton( boolean enable ) { removeButton.setEnabled( enable && externallyEnabled && removeButtonExternallyEnabled ); } @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 = StringUtils.noEmpty( upload.getFilename() ); if ( filename != null ) { stopTimer(); submitForm( lastSubmittedFileName = filename ); formPanel.reset(); } } }).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 ); startTimer(); } private void submitForm( String filename ) { System.out.println( "UploadWidget.submitForm: " + filename + " to " + servletPath ); if ( servletPath == null ) { throw new IllegalStateException( "No Servlet Path Set" ); } 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(); if ( (htmlResponse != null) || (lastSubmittedFileName != null) ) { resetUploadWidget(); String fileName = lastSubmittedFileName; lastSubmittedFileName = null; callback.parseResponse( fileName, 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( 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( Position.ABSOLUTE ); style.setLeft( 0, Unit.PX ); style.setTop( 0, Unit.PX ); style.setOverflow( 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( Position.RELATIVE ); style.setLeft( -600, Unit.PX ); style.setTop( -5, 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, Unit.PX ); style.setPadding( 0, 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, Unit.PX ); style.setPadding( 0, Unit.PX ); style.setBorderWidth( 0, Unit.PX ); style.setFontSize( 200, 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( 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( 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( Visibility.VISIBLE ); } int calcSecondsRemaining = secondsToWait - secondsPassed; if ( calcSecondsRemaining < 1 ) { stopTimer(); timeout(); return; } if ( calcSecondsRemaining != secondsRemaining ) { secondsRemaining = calcSecondsRemaining; secondsRemainingLabel.setText( Integer.toString( secondsRemaining ) ); } } }).scheduleRepeating( 100 ); } } } |