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

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import javax.annotation.Nonnull;
import org.realityforge.gwt.appcache.client.ApplicationCache;
import org.realityforge.gwt.appcache.client.event.CachedEvent;
import org.realityforge.gwt.appcache.client.event.CachedEvent.Handler;
import org.realityforge.gwt.appcache.client.event.CheckingEvent;
import org.realityforge.gwt.appcache.client.event.DownloadingEvent;
import org.realityforge.gwt.appcache.client.event.ErrorEvent;
import org.realityforge.gwt.appcache.client.event.NoUpdateEvent;
import org.realityforge.gwt.appcache.client.event.ObsoleteEvent;
import org.realityforge.gwt.appcache.client.event.ProgressEvent;
import org.realityforge.gwt.appcache.client.event.UpdateReadyEvent;

public final class Example
  implements EntryPoint
{
  public void onModuleLoad()
  {
    final VerticalPanel panel = new VerticalPanel();
    final ApplicationCache cache = ApplicationCache.get();
    if ( null == cache )
    {
      Window.alert( "ApplicationCache not available!" );
    }
    else
    {
      final VerticalPanel textPanel = new VerticalPanel();

      cache.addCachedHandler( new Handler()
      {
        @Override
        public void onCachedEvent( @Nonnull final CachedEvent event )
        {
          appendText( textPanel, "Cached", "blue" );
        }
      } );
      cache.addCheckingHandler( new CheckingEvent.Handler()
      {
        @Override
        public void onCheckingEvent( @Nonnull final CheckingEvent event )
        {
          appendText( textPanel, "Checking", "yellow" );
        }
      } );
      cache.addDownloadingHandler( new DownloadingEvent.Handler()
      {
        @Override
        public void onDownloadingEvent( @Nonnull final DownloadingEvent event )
        {
          appendText( textPanel, "Downloading", "orange" );
        }
      } );
      cache.addErrorHandler( new ErrorEvent.Handler()
      {
        @Override
        public void onErrorEvent( @Nonnull final ErrorEvent event )
        {
          appendText( textPanel, "Error", "red" );
        }
      } );
      cache.addNoUpdateHandler( new NoUpdateEvent.Handler()
      {
        @Override
        public void onNoUpdateEvent( @Nonnull final NoUpdateEvent event )
        {
          appendText( textPanel, "NoUpdate", "green" );
        }
      } );
      cache.addObsoleteHandler( new ObsoleteEvent.Handler()
      {
        @Override
        public void onObsoleteEvent( @Nonnull final ObsoleteEvent event )
        {
          appendText( textPanel, "Obsolete", "yellow" );
        }
      } );
      cache.addProgressHandler( new ProgressEvent.Handler()
      {
        @Override
        public void onProgressEvent( @Nonnull final ProgressEvent event )
        {
          appendText( textPanel, "Progress[" + event.getLoaded() + " of " + event.getTotal() + "]", "orange" );
        }
      } );
      cache.addUpdateReadyHandler( new UpdateReadyEvent.Handler()
      {
        @Override
        public void onUpdateReadyEvent( @Nonnull final UpdateReadyEvent event )
        {
          appendText( textPanel, "UpdateReady", "green" );
        }
      } );

      final Button button = new Button( "Request cache update" );
      button.addClickHandler( new ClickHandler()
      {
        @Override
        public void onClick( final ClickEvent event )
        {
          cache.update();
        }
      } );
      panel.add( button );
      final Button button2 = new Button( "Request removal of cache" );
      button2.addClickHandler( new ClickHandler()
      {
        @Override
        public void onClick( final ClickEvent event )
        {
          cache.removeCache();
        }
      } );
      panel.add( button2 );
      panel.add( textPanel );
      RootPanel.get().add( panel );
    }
  }

  private void appendText( final VerticalPanel panel, final String eventName, final String color )
  {
    panel.add( new InlineHTML( "<span style=\"color:" + color + "\">" + eventName + "</span><br />" ) );
  }
}

@@ -3514,17 +3514,26 @@
    %7D );%0A
-%0A
+      %7B%0A  
       fi
@@ -3592,24 +3592,26 @@
 e%22 );%0A      
+  
 button.addCl
@@ -3633,32 +3633,34 @@
  ClickHandler()%0A
+  
       %7B%0A        
@@ -3651,32 +3651,34 @@
       %7B%0A        
+  
 @Override%0A      
@@ -3663,32 +3663,34 @@
       @Override%0A
+  
         public v
@@ -3727,34 +3727,38 @@
 event )%0A        
-%7B%0A
+  %7B%0A  
           cache.
@@ -3767,34 +3767,38 @@
 date();%0A        
-%7D%0A
+  %7D%0A  
       %7D );%0A     
@@ -3790,32 +3790,34 @@
      %7D );%0A      
+  
 panel.add( butto
@@ -3817,24 +3817,43 @@
 ( button );%0A
+      %7D%0A%0A      %7B%0A  
       final 
@@ -3913,24 +3913,26 @@
 e%22 );%0A      
+  
 button2.addC
@@ -3961,34 +3961,38 @@
 Handler()%0A      
+ 
+ 
 %7B%0A
+  
         @Overrid
@@ -3985,32 +3985,34 @@
       @Override%0A
+  
         public v
@@ -4041,32 +4041,34 @@
 ckEvent event )%0A
+  
         %7B%0A      
@@ -4067,24 +4067,26 @@
 %7B%0A          
+  
 cache.remove
@@ -4094,34 +4094,38 @@
 ache();%0A        
-%7D%0A
+  %7D%0A  
       %7D );%0A     
@@ -4117,32 +4117,34 @@
      %7D );%0A      
+  
 panel.add( butto
@@ -4145,24 +4145,352 @@
  button2 );%0A
+      %7D%0A%0A      %7B%0A        final Button button3 = new Button( %22Request cache swap%22 );%0A        button3.addClickHandler( new ClickHandler()%0A        %7B%0A          @Override%0A          public void onClick( final ClickEvent event )%0A          %7B%0A            cache.swapCache();%0A          %7D%0A        %7D );%0A        panel.add( button3 );%0A      %7D%0A
       panel.

Move button creates into separate scopes and add a button to swap the cache