/**
    Copyright 2017 Andrea "Stock" Stocchero

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

	    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package org.pepstock.charba.client.resources;

import org.pepstock.charba.client.Injector;

import com.google.gwt.resources.client.ResourceCallback;
import com.google.gwt.resources.client.ResourceException;
import com.google.gwt.resources.client.TextResource;

/**
 * Utility to start an entry point leveraging on deferred resources mode.<br>
 * This helps when the GWT application is leveraging on code splitting.<br>
 * <br>
 * 
 * <pre>
 * GWT.runAsync(new RunAsyncCallback() {
 * 
 * 	&#64;Override
 * 	public void onFailure(Throwable throwable) {
 * 		Window.alert("Code download failed");
 * 	}
 * 
 * 	&#64;Override
 * 	public void onSuccess() {
 * 		EntryPointStarter.run(new Runnable() {
 * 
 * 			&#64;Override
 * 			public void run() {
 * 				// CHARBA charts and api...
 * 			}
 * 		});
 * 	}
 * });
 * </pre>
 * 
 * @author Andrea "Stock" Stocchero
 *
 */
public final class EntryPointStarter {

	/**
	 * To avoid any instantiation
	 */
	private EntryPointStarter() {
		// do nothing
	}

	/**
	 * Start an entry point as a runnable. This runnable instance must contains all calls to chart.<br>
	 * This helps when the GWT application is leveraging on code splitting.
	 * 
	 * @param runnable the entry point instance as runnable
	 */
	public static void run(final Runnable runnable) {
		// checks if the entry point is consistent
		if (runnable == null) {
			// if not, exception
			throw new IllegalArgumentException("Entry point runnable is null");
		}
		// sets deferred resources
		ResourcesType.setClientBundle(DeferredResources.INSTANCE);
		try {
			// loads CHART.JS in async
			DeferredResources.INSTANCE.chartJs().getText(new ResourceCallback<TextResource>() {

				/*
				 * (non-Javadoc)
				 * 
				 * @see
				 * com.google.gwt.resources.client.ResourceCallback#onError(com.google.gwt.resources.client.ResourceException)
				 */
				@Override
				public void onError(ResourceException e) {
					throw new IllegalArgumentException("Unable to load CHART.JS resource, reason: " + e.getMessage());
				}

				/*
				 * (non-Javadoc)
				 * 
				 * @see
				 * com.google.gwt.resources.client.ResourceCallback#onSuccess(com.google.gwt.resources.client.ResourcePrototype)
				 */
				@Override
				public void onSuccess(TextResource resource) {
					// injects the CHART.JS
					Injector.ensureInjected(resource);
					// executes the entry point
					runnable.run();
				}
			});
		} catch (ResourceException e) {
			// if here, there is an error on resource callback
			throw new IllegalArgumentException("Unable to load CHART.JS resource, reason: " + e.getMessage());
		}
	}
}

@@ -1964,20 +1964,19 @@
 unnable)
- 
 %7B%0D%0A
+
 %09%09// che
@@ -2615,39 +2615,31 @@
 %09%09throw new 
-IllegalArgument
+Runtime
 Exception(%22U
@@ -2673,36 +2673,12 @@
 urce
-, reason: %22 + e.getMessage()
+%22, e
 );%0D%0A
@@ -3173,39 +3173,31 @@
 %09%09throw new 
-IllegalArgument
+Runtime
 Exception(%22U
@@ -3231,36 +3231,12 @@
 urce
-, reason: %22 + e.getMessage()
+%22, e
 );%0D%0A

change exception