AbstractEventPublisher.java
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.RunnableWithArgs;
020 import griffon.core.event.Event;
021 import griffon.core.event.EventPublisher;
022 import griffon.core.event.EventRouter;
023 
024 import javax.annotation.Nonnull;
025 import javax.annotation.Nullable;
026 import javax.inject.Inject;
027 import java.util.List;
028 import java.util.Map;
029 
030 import static griffon.util.GriffonNameUtils.requireNonBlank;
031 import static java.util.Objects.requireNonNull;
032 
033 /**
034  @author Andres Almiray
035  @since 2.0.0
036  */
037 public abstract class AbstractEventPublisher implements EventPublisher {
038     private static final String ERROR_EVENT_NAME_BLANK = "Argument 'eventName' must not be blank";
039     private static final String ERROR_LISTENER_NULL = "Argument 'listener' must not be null";
040     private static final String ERROR_EVENT_CLASS_NULL = "Argument 'eventClass' must not be null";
041     private static final String ERROR_EVENT_NULL = "Argument 'event' must not be null";
042 
043     private EventRouter eventRouter;
044 
045     @Inject
046     public void setEventRouter(@Nonnull EventRouter eventRouter) {
047         this.eventRouter = requireNonNull(eventRouter, "Argument 'eventRouter' must not be null");
048     }
049 
050     public boolean isEventPublishingEnabled() {
051         return eventRouter.isEventPublishingEnabled();
052     }
053 
054     public void setEventPublishingEnabled(boolean enabled) {
055         eventRouter.setEventPublishingEnabled(enabled);
056     }
057 
058     public <E extends Event> void removeEventListener(@Nonnull Class<E> eventClass, @Nonnull CallableWithArgs<?> listener) {
059         requireNonNull(eventClass, ERROR_EVENT_CLASS_NULL);
060         requireNonNull(listener, ERROR_LISTENER_NULL);
061         eventRouter.removeEventListener(eventClass, listener);
062     }
063 
064     public <E extends Event> void removeEventListener(@Nonnull Class<E> eventClass, @Nonnull RunnableWithArgs listener) {
065         requireNonNull(eventClass, ERROR_EVENT_CLASS_NULL);
066         requireNonNull(listener, ERROR_LISTENER_NULL);
067         eventRouter.removeEventListener(eventClass, listener);
068     }
069 
070     public void addEventListener(@Nonnull Object listener) {
071         requireNonNull(listener, ERROR_LISTENER_NULL);
072         eventRouter.addEventListener(listener);
073     }
074 
075     public void addEventListener(@Nonnull String eventName, @Nonnull CallableWithArgs<?> listener) {
076         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
077         requireNonNull(listener, ERROR_LISTENER_NULL);
078         eventRouter.addEventListener(eventName, listener);
079     }
080 
081     public void addEventListener(@Nonnull String eventName, @Nonnull RunnableWithArgs listener) {
082         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
083         requireNonNull(listener, ERROR_LISTENER_NULL);
084         eventRouter.addEventListener(eventName, listener);
085     }
086 
087     public void publishEvent(@Nonnull String eventName) {
088         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
089         eventRouter.publishEvent(eventName);
090     }
091 
092     public void publishEventOutsideUI(@Nonnull Event event) {
093         requireNonNull(event, ERROR_EVENT_NULL);
094         eventRouter.publishEventOutsideUI(event);
095     }
096 
097     public void removeEventListener(@Nonnull Map<String, Object> listener) {
098         requireNonNull(listener, ERROR_LISTENER_NULL);
099         eventRouter.removeEventListener(listener);
100     }
101 
102     public <E extends Event> void addEventListener(@Nonnull Class<E> eventClass, @Nonnull CallableWithArgs<?> listener) {
103         requireNonNull(eventClass, ERROR_EVENT_CLASS_NULL);
104         requireNonNull(listener, ERROR_LISTENER_NULL);
105         eventRouter.addEventListener(eventClass, listener);
106     }
107 
108     public <E extends Event> void addEventListener(@Nonnull Class<E> eventClass, @Nonnull RunnableWithArgs listener) {
109         requireNonNull(eventClass, ERROR_EVENT_CLASS_NULL);
110         requireNonNull(listener, ERROR_LISTENER_NULL);
111         eventRouter.addEventListener(eventClass, listener);
112     }
113 
114     public void publishEventOutsideUI(@Nonnull String eventName) {
115         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
116         eventRouter.publishEventOutsideUI(eventName);
117     }
118 
119     public void publishEventOutsideUI(@Nonnull String eventName, @Nullable List<?> params) {
120         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
121         eventRouter.publishEventOutsideUI(eventName, params);
122     }
123 
124     public void publishEvent(@Nonnull String eventName, @Nullable List<?> params) {
125         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
126         eventRouter.publishEvent(eventName, params);
127     }
128 
129     public void publishEventAsync(@Nonnull String eventName, @Nullable List<?> params) {
130         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
131         eventRouter.publishEventAsync(eventName, params);
132     }
133 
134     public void removeEventListener(@Nonnull String eventName, @Nonnull CallableWithArgs<?> listener) {
135         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
136         requireNonNull(listener, ERROR_LISTENER_NULL);
137         eventRouter.removeEventListener(eventName, listener);
138     }
139 
140     public void removeEventListener(@Nonnull String eventName, @Nonnull RunnableWithArgs listener) {
141         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
142         requireNonNull(listener, ERROR_LISTENER_NULL);
143         eventRouter.removeEventListener(eventName, listener);
144     }
145 
146     public void removeEventListener(@Nonnull Object listener) {
147         requireNonNull(listener, ERROR_LISTENER_NULL);
148         eventRouter.removeEventListener(listener);
149     }
150 
151     public void addEventListener(@Nonnull Map<String, Object> listener) {
152         requireNonNull(listener, ERROR_LISTENER_NULL);
153         eventRouter.addEventListener(listener);
154     }
155 
156     public void publishEvent(@Nonnull Event event) {
157         requireNonNull(event, ERROR_EVENT_NULL);
158         eventRouter.publishEvent(event);
159     }
160 
161     public void publishEventAsync(@Nonnull String eventName) {
162         requireNonBlank(eventName, ERROR_EVENT_NAME_BLANK);
163         eventRouter.publishEventAsync(eventName);
164     }
165 
166     public void publishEventAsync(@Nonnull Event event) {
167         requireNonNull(event, ERROR_EVENT_NULL);
168         eventRouter.publishEventAsync(event);
169     }
170 }