package org.realityforge.gwt.webpoller.example.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import javax.annotation.Nonnull;
import org.realityforge.gwt.webpoller.client.WebPoller;
import org.realityforge.gwt.webpoller.client.WebPoller.RequestFactory;
import org.realityforge.gwt.webpoller.client.event.ErrorEvent;
import org.realityforge.gwt.webpoller.client.event.MessageEvent;
import org.realityforge.gwt.webpoller.client.event.StartEvent;
import org.realityforge.gwt.webpoller.client.event.StopEvent;

public final class Example
  implements EntryPoint, RequestFactory
{
  private HTML _messages;
  private ScrollPanel _scrollPanel;
  private Button _stop;
  private Button _start;
  private CheckBox _longPoll;

  public void onModuleLoad()
  {
    final WebPoller webPoller = WebPoller.newWebPoller( this );
    registerListeners( webPoller );

    _start = new Button( "Start", new ClickHandler()
    {
      @Override
      public void onClick( final ClickEvent event )
      {
        _start.setEnabled( false );
        _longPoll.setEnabled( false );
        webPoller.setLongPoll( _longPoll.getValue() == Boolean.TRUE );
        webPoller.start();
      }
    } );
    _stop = new Button( "Stop", new ClickHandler()
    {
      @Override
      public void onClick( ClickEvent event )
      {
        webPoller.stop();
        _stop.setEnabled( false );
      }
    } );
    _stop.setEnabled( false );

    _longPoll = new CheckBox( "Long Poll" );

    _messages = new HTML();
    _scrollPanel = new ScrollPanel();
    _scrollPanel.setHeight( "250px" );
    _scrollPanel.add( _messages );
    RootPanel.get().add( _scrollPanel );

    {
      final FlowPanel controls = new FlowPanel();
      controls.add( _longPoll );
      controls.add( _start );
      controls.add( _stop );
      RootPanel.get().add( controls );
    }
  }

  @Nonnull
  @Override
  public RequestBuilder getRequestBuilder( @Nonnull final RequestCallback callback )
  {
    final RequestBuilder rb = new RequestBuilder( RequestBuilder.GET, getPollURL() );
    rb.setCallback( new RequestCallback()
    {
      @Override
      public void onResponseReceived( final Request request, final Response response )
      {
        if ( Response.SC_OK == response.getStatusCode() )
        {
          callback.onResponseReceived( request, response );
        }
        else
        {
          callback.onError( request, new Exception( "Bad response code" ) );
        }
      }

      @Override
      public void onError( final Request request, final Throwable exception )
      {
        callback.onError( request, exception );
      }
    } );
    return rb;
  }

  private String getPollURL()
  {
    final String moduleBaseURL = GWT.getModuleBaseURL();
    final String moduleName = GWT.getModuleName();
    final String suffix = _longPoll.getValue() == Boolean.TRUE ? "/long" : "";
    return moduleBaseURL.substring( 0, moduleBaseURL.length() - moduleName.length() - 1 ) + "api/time" + suffix;
  }

  private void registerListeners( final WebPoller webPoller )
  {
    webPoller.addStartHandler( new StartEvent.Handler()
    {
      @Override
      public void onStartEvent( @Nonnull final StartEvent event )
      {
        appendText( "start", "silver" );
        _stop.setEnabled( true );
      }
    } );
    webPoller.addStopHandler( new StopEvent.Handler()
    {
      @Override
      public void onStopEvent( @Nonnull final StopEvent event )
      {
        appendText( "stop", "silver" );
        _start.setEnabled( true );
        _longPoll.setEnabled( true );
        _stop.setEnabled( false );
      }
    } );
    webPoller.addErrorHandler( new ErrorEvent.Handler()
    {
      @Override
      public void onErrorEvent( @Nonnull final ErrorEvent event )
      {
        appendText( "error", "red" );
        _start.setEnabled( false );
        _longPoll.setEnabled( false );
        _stop.setEnabled( false );
      }
    } );
    webPoller.addMessageHandler( new MessageEvent.Handler()
    {
      @Override
      public void onMessageEvent( @Nonnull final MessageEvent event )
      {
        appendText( "message: " + event.getData(), "black" );
      }
    } );
  }

  private void appendText( final String text, final String color )
  {
    final DivElement div = Document.get().createDivElement();
    div.setInnerText( text );
    div.setAttribute( "style", "color:" + color );
    _messages.getElement().appendChild( div );
    _scrollPanel.scrollToBottom();
  }
}

@@ -371,153 +371,15 @@
 uest
-;%0Aimport com.google.gwt.http.client.RequestBuilder;%0Aimport com.google.gwt.http.client.RequestCallback;%0Aimport com.google.gwt.http.client.Response
+Builder
 ;%0Aim
@@ -735,25 +735,42 @@
 .client.
-WebPoller
+AbstractHttpRequestFactory
 ;%0Aimport
@@ -821,23 +821,8 @@
 ller
-.RequestFactory
 ;%0Aim
@@ -1100,16 +1100,53 @@
 Example%0A
+  extends AbstractHttpRequestFactory%0A
   implem
@@ -1164,24 +1164,8 @@
 oint
-, RequestFactory
 %0A%7B%0A 
@@ -2431,19 +2431,8 @@
  %7D%0A%0A
-  @Nonnull%0A
   @O
@@ -2442,21 +2442,24 @@
 ride%0A  p
-ublic
+rotected
  Request
@@ -2488,737 +2488,79 @@
 der(
- @Nonnull final RequestCallback callback )%0A  %7B%0A    final RequestBuilder rb = new RequestBuilder( RequestBuilder.GET, getPollURL() );%0A    rb.setCallback( new RequestCallback()%0A    %7B%0A      @Override%0A      public void onResponseReceived( final Request request, final Response response )%0A      %7B%0A        if ( Response.SC_OK == response.getStatusCode() )%0A        %7B%0A          callback.onResponseReceived( request, response );%0A        %7D%0A        else%0A        %7B%0A          callback.onError( request, new Exception( %22Bad response code%22 ) );%0A        %7D%0A      %7D%0A%0A      @Override%0A      public void onError( final Request request, final Throwable exception )%0A      %7B%0A        callback.onError( request, exception );%0A      %7D%0A    %7D );%0A    return rb
+)%0A  %7B%0A    return new RequestBuilder( RequestBuilder.GET, getPollURL() )
 ;%0A  
@@ -2729,16 +2729,37 @@
 suffix =
+ null != _longPoll &&
  _longPo

Update to the latest WebPoller API