All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gwtplatform.dispatch.client.actionhandler.DefaultClientActionHandlerRegistry Maven / Gradle / Ivy

There is a newer version: 1.6
Show newest version
/**
 * Copyright 2013 ArcBees Inc.
 *
 * 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 com.gwtplatform.dispatch.client.actionhandler;

import java.util.Map;

import javax.inject.Provider;

import com.google.common.collect.Maps;
import com.google.gwt.inject.client.AsyncProvider;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtplatform.common.client.CodeSplitBundleProvider;
import com.gwtplatform.common.client.IndirectProvider;
import com.gwtplatform.common.client.ProviderBundle;

/**
 * The default implementation that {@link ClientActionHandlerRegistry} that if bound will not load any client-side
 * action handlers.
 * 

* To register client-side action handlers, extend this class and call {@link #register} in the constructor. *

*

Example

*

*

 * 
 * public class MyActionHandlerRegistry extends DefaultClientActionHandlerRegistry {
 *   {@literal}@Inject
 *   public DefaultClientRestActionHandlerRegistry(
 *       RetrieveFooClientActionHandler handler,
 *       Provider<ListFooClientActionHandler> provider,
 *       AsyncProvider<UpdateFooClientActionHandler> asyncProvider,
 *       AsyncProvider<CreateFooBundle> fooCreateBundle) {
 *
 *     register(handler);
 *     register(ListFooClientAction.class, provider);
 *     register(UpdateFooClientAction.class, asyncProvider);
 *     register(CreateFooClientAction.class, fooCreateBundle, CreateFooBundle.ID_CreateFooClientActionHandler);
 * }
 *
 * // Provider Bundle that will try to combine the presenter and client action handler into the same split point.
 * public class CreateFooBundle extends ProviderBundle {
 *   public static final int ID_CreateFooPresenter = 0;
 *   public static final int ID_CreateFooClientActionHandler = 1;
 *
 *   {@literal}@Inject
 *   public CreateFooBundle(
 *       Provider<CreateFooPresenterImpl> presenter,
 *       Provider<CreateFooClientActionHandler> clientActionHandler) {
 *     super(2);
 *     providers[ID_CreateFooPresenter] = presenter;
 *     providers[ID_CreateFooClientActionHandler] = clientActionHandler;
 *   }
 * }
 * 
 * 
*/ public class DefaultClientActionHandlerRegistry implements ClientActionHandlerRegistry { private Map, IndirectProvider>> clientActionHandlers; /** * Register a instance of a client-side action handler. * * @param handler The {@link ClientActionHandler}; */ protected void register(final ClientActionHandler handler) { register(handler.getActionType(), new IndirectProvider>() { @Override public void get(AsyncCallback> callback) { callback.onSuccess(handler); } }); } /** * Register a {@link javax.inject.Provider} of a client-side action handler. * * @param actionType The type of action that the client-side action handler supports. * @param handlerProvider The {@link com.google.inject.Provider} of the handler. */ protected void register(Class actionType, final Provider> handlerProvider) { register(actionType, new IndirectProvider>() { @Override public void get(AsyncCallback> callback) { callback.onSuccess(handlerProvider.get()); } }); } /** * Register an {@link com.google.gwt.inject.client.AsyncProvider} of a client-side action handler. * * @param actionType The type of that the client-side action handler supports. * @param handlerProvider The {@link com.google.gwt.inject.client.AsyncProvider} of the handler. */ protected void register(Class actionType, final AsyncProvider> handlerProvider) { register(actionType, new IndirectProvider>() { @Override public void get(AsyncCallback> callback) { handlerProvider.get(callback); } }); } /** * Register a client-side action handler that is part of a {@link com.gwtplatform.common.client.ProviderBundle}. * * @param actionType The type of that the client-side action handler supports. * @param bundleProvider The {@link javax.inject.Provider} of the * {@link com.gwtplatform.common.client.ProviderBundle}. * @param providerId The id of the client-side action handler provider. */ protected void register(Class actionType, AsyncProvider bundleProvider, int providerId) { register(actionType, new CodeSplitBundleProvider, B>(bundleProvider, providerId)); } /** * Register an {@link com.gwtplatform.common.client.IndirectProvider} of a client-side action handler. * * @param handlerProvider The {@link com.gwtplatform.common.client.IndirectProvider}. */ protected void register(Class actionType, IndirectProvider> handlerProvider) { if (clientActionHandlers == null) { clientActionHandlers = Maps.newHashMap(); } clientActionHandlers.put(actionType, handlerProvider); } @Override public IndirectProvider> find(Class actionClass) { if (clientActionHandlers == null) { return null; } else { return clientActionHandlers.get(actionClass); } } }