001/*
002 *  Copyright 2012 GWT-Bootstrap
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 */
016package com.github.gwtbootstrap.client.ui.event;
017
018import com.google.gwt.event.shared.GwtEvent;
019
020/**
021 * Represents an event that is fired when a widget has been closed.
022 *
023 * @author Dominik Mayer
024 * @author Danilo Reinert
025 *
026 * @see com.google.gwt.event.logical.shared.CloseEvent
027 *
028 * @since 2.0.4.0
029 */
030public class ClosedEvent<T> extends GwtEvent<ClosedHandler<T>> {
031
032    private static final Type<ClosedHandler<?>> TYPE = new Type<ClosedHandler<?>>();
033
034    public static Type<ClosedHandler<?>> getType() {
035        return TYPE;
036    }
037
038    /**
039     * Fires a closed event on all registered handlers in the handler manager. If
040     * no such handlers exist, this method will do nothing.
041     *
042     * @param <T>    the target type
043     * @param source the source of the handlers
044     * @param target the target
045     */
046    public static <T> void fire(HasCloseHandlers<T> source, T target) {
047        fire(source, target, false);
048    }
049
050    /**
051     * Fires a closed event on all registered handlers in the handler manager.
052     *
053     * @param <T>        the target type
054     * @param source     the source of the handlers
055     * @param target     the target
056     * @param autoClosed was the target closed automatically
057     */
058    public static <T> void fire(HasCloseHandlers<T> source, T target,
059                                boolean autoClosed) {
060        if (TYPE != null) {
061            ClosedEvent<T> event = new ClosedEvent<T>(target, autoClosed);
062            source.fireEvent(event);
063        }
064    }
065
066    private final T target;
067    private final boolean autoClosed;
068
069    public ClosedEvent(T target, boolean autoClosed) {
070        this.target = target;
071        this.autoClosed = autoClosed;
072    }
073
074    // The instance knows its of type T, but the TYPE
075    // field itself does not, so we have to do an unsafe cast here.
076    @SuppressWarnings("unchecked")
077    @Override
078    public final Type<ClosedHandler<T>> getAssociatedType() {
079        return (Type) TYPE;
080    }
081
082    public T getTarget() {
083        return target;
084    }
085
086    public boolean isAutoClosed() {
087        return autoClosed;
088    }
089
090    @Override
091    protected void dispatch(ClosedHandler handler) {
092        handler.onClosed(this);
093    }
094
095}