001 /*
002 * Copyright 2013 Erik Kuefler
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014 package com.ekuefler.supereventbus;
015
016 import com.ekuefler.supereventbus.impl.EventHandlerMethod;
017 import com.google.web.bindery.event.shared.Event;
018 import com.google.web.bindery.event.shared.Event.Type;
019 import com.google.web.bindery.event.shared.HandlerRegistration;
020
021 /**
022 * An adapter allowing SuperEventBus to be referenced via GWT's built-in
023 * {@link com.google.web.bindery.event.shared.EventBus} interface. Calls to methods on this class
024 * are forwarded to an underlying {@link EventBus}, effectively allowing an {@link EventBus} to be
025 * passed to existing methods that expect a legacy
026 * {@link com.google.web.bindery.event.shared.EventBus}. Since only a small subset of
027 * {@link EventBus}'s functionality is exposed here, this should only be used for existing/legacy
028 * code that can't be refactored to use {@link EventBus} directly.
029 *
030 * @author ekuefler@gmail.com (Erik Kuefler)
031 */
032 public class EventBusAdapter extends com.google.web.bindery.event.shared.EventBus {
033
034 private final EventBus eventBus;
035
036 /** Creates a new adapter wrapping the given event bus. */
037 public EventBusAdapter(EventBus eventBus) {
038 this.eventBus = eventBus;
039 }
040
041 /** Invokes {@link EventBus#post} on the underlying event bus with the given event. */
042 @Override
043 public void fireEvent(Event<?> event) {
044 eventBus.post(event);
045 }
046
047 /**
048 * Emulates the behavior of registering a single handler method on the underlying event bus. The
049 * handler is unfiltered and behaves as if it has priority zero.
050 */
051 @Override
052 public <H> HandlerRegistration addHandler(final Type<H> type, final H handler) {
053 eventBus.addHandlerMethod(handler, new EventHandlerMethod<Object, Event<H>>() {
054 @Override
055 public void invoke(Object instance, Event<H> arg) {
056 dispatchEvent(arg, handler);
057 }
058
059 @Override
060 public boolean acceptsArgument(Object arg) {
061 return arg instanceof Event && ((Event<?>) arg).getAssociatedType() == type;
062 }
063
064 @Override
065 public int getDispatchOrder() {
066 return 0;
067 }
068 });
069 return new HandlerRegistration() {
070 @Override
071 public void removeHandler() {
072 eventBus.unregister(handler);
073 }
074 };
075 }
076
077 /** Not supported. */
078 @Override
079 public <H> HandlerRegistration addHandlerToSource(Type<H> type, Object source, H handler) {
080 throw new UnsupportedOperationException();
081 }
082
083 /** Not supported. */
084 @Override
085 public void fireEventFromSource(Event<?> event, Object source) {
086 throw new UnsupportedOperationException();
087 }
088 }