| 
001 /*002  * Copyright 2008-2015 the original author or authors.
 003  *
 004  * Licensed under the Apache License, Version 2.0 (the "License");
 005  * you may not use this file except in compliance with the License.
 006  * You may obtain a copy of the License at
 007  *
 008  *     http://www.apache.org/licenses/LICENSE-2.0
 009  *
 010  * Unless required by applicable law or agreed to in writing, software
 011  * distributed under the License is distributed on an "AS IS" BASIS,
 012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 013  * See the License for the specific language governing permissions and
 014  * limitations under the License.
 015  */
 016 package org.codehaus.griffon.runtime.core.event;
 017
 018 import griffon.core.CallableWithArgs;
 019 import griffon.core.event.Event;
 020 import griffon.core.event.EventPublisher;
 021 import griffon.core.event.EventRouter;
 022
 023 import javax.annotation.Nonnull;
 024 import javax.annotation.Nullable;
 025 import javax.inject.Inject;
 026 import java.util.List;
 027 import java.util.Map;
 028
 029 import static griffon.util.GriffonNameUtils.requireNonBlank;
 030 import static java.util.Objects.requireNonNull;
 031
 032 /**
 033  * @author Andres Almiray
 034  * @since 2.0.0
 035  */
 036 public abstract class AbstractEventPublisher implements EventPublisher {
 037     private static final String ERROR_EVENT_NAME_BLANK = "Argument 'eventName' must not be blank";
 038     private static final String ERROR_LISTENER_NULL = "Argument 'listener' must not be null";
 039     private static final String ERROR_EVENT_CLASS_NULL = "Argument 'eventClass' must not be null";
 040     private static final String ERROR_EVENT_NULL = "Argument 'event' must not be null";
 041
 042     private EventRouter eventRouter;
 043
 044     @Inject
 045     public void setEventRouter(@Nonnull EventRouter eventRouter) {
 046         this.eventRouter = requireNonNull(eventRouter, "Argument 'eventRouter' must not be null");
 047     }
 048
 049     public boolean isEventPublishingEnabled() {
 050         return eventRouter.isEventPublishingEnabled();
 051     }
 052
 053     public <E extends Event> void removeEventListener(@Nonnull Class<E> eventClass, @Nonnull CallableWithArgs<?> listener) {
 054         requireNonNull(eventClass, ERROR_EVENT_CLASS_NULL);
 055         requireNonNull(listener, ERROR_LISTENER_NULL);
 056         eventRouter.removeEventListener(eventClass, listener);
 057     }
 058
 059     public void addEventListener(@Nonnull Object listener) {
 060         requireNonNull(listener, ERROR_LISTENER_NULL);
 061         eventRouter.addEventListener(listener);
 062     }
 063
 064     public void addEventListener(@Nonnull String eventName, @Nonnull CallableWithArgs<?> listener) {
 065         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
 066         requireNonNull(listener, ERROR_LISTENER_NULL);
 067         eventRouter.addEventListener(eventName, listener);
 068     }
 069
 070     public void publishEvent(@Nonnull String eventName) {
 071         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
 072         eventRouter.publishEvent(eventName);
 073     }
 074
 075     public void publishEventOutsideUI(@Nonnull Event event) {
 076         requireNonNull(event, ERROR_EVENT_NULL);
 077         eventRouter.publishEventOutsideUI(event);
 078     }
 079
 080     public void removeEventListener(@Nonnull Map<String, CallableWithArgs<?>> listener) {
 081         requireNonNull(listener, ERROR_LISTENER_NULL);
 082         eventRouter.removeEventListener(listener);
 083     }
 084
 085     public <E extends Event> void addEventListener(@Nonnull Class<E> eventClass, @Nonnull CallableWithArgs<?> listener) {
 086         requireNonNull(eventClass, ERROR_EVENT_CLASS_NULL);
 087         requireNonNull(listener, ERROR_LISTENER_NULL);
 088         eventRouter.addEventListener(eventClass, listener);
 089     }
 090
 091     public void publishEventOutsideUI(@Nonnull String eventName) {
 092         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
 093         eventRouter.publishEventOutsideUI(eventName);
 094     }
 095
 096     public void publishEventOutsideUI(@Nonnull String eventName, @Nullable List<?> params) {
 097         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
 098         eventRouter.publishEventOutsideUI(eventName, params);
 099     }
 100
 101     public void publishEvent(@Nonnull String eventName, @Nullable List<?> params) {
 102         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
 103         eventRouter.publishEvent(eventName, params);
 104     }
 105
 106     public void publishEventAsync(@Nonnull String eventName, @Nullable List<?> params) {
 107         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
 108         eventRouter.publishEventAsync(eventName, params);
 109     }
 110
 111     public void removeEventListener(@Nonnull String eventName, @Nonnull CallableWithArgs<?> listener) {
 112         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
 113         requireNonNull(listener, ERROR_LISTENER_NULL);
 114         eventRouter.removeEventListener(eventName, listener);
 115     }
 116
 117     public void removeEventListener(@Nonnull Object listener) {
 118         requireNonNull(listener, ERROR_LISTENER_NULL);
 119         eventRouter.removeEventListener(listener);
 120     }
 121
 122     public void addEventListener(@Nonnull Map<String, CallableWithArgs<?>> listener) {
 123         requireNonNull(listener, ERROR_LISTENER_NULL);
 124         eventRouter.addEventListener(listener);
 125     }
 126
 127     public void setEventPublishingEnabled(boolean enabled) {
 128         eventRouter.setEventPublishingEnabled(enabled);
 129     }
 130
 131     public void publishEvent(@Nonnull Event event) {
 132         requireNonNull(event, ERROR_EVENT_NULL);
 133         eventRouter.publishEvent(event);
 134     }
 135
 136     public void publishEventAsync(@Nonnull String eventName) {
 137         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
 138         eventRouter.publishEventAsync(eventName);
 139     }
 140
 141     public void publishEventAsync(@Nonnull Event event) {
 142         requireNonNull(event, ERROR_EVENT_NULL);
 143         eventRouter.publishEventAsync(event);
 144     }
 145 }
 |