001    package org.gwtbootstrap3.extras.typeahead.client.events;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 2014 GwtBootstrap3
008     * %%
009     * Licensed under the Apache License, Version 2.0 (the "License");
010     * you may not use this file except in compliance with the License.
011     * You may obtain a copy of the License at
012     * 
013     *      http://www.apache.org/licenses/LICENSE-2.0
014     * 
015     * Unless required by applicable law or agreed to in writing, software
016     * distributed under the License is distributed on an "AS IS" BASIS,
017     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018     * See the License for the specific language governing permissions and
019     * limitations under the License.
020     * #L%
021     */
022    
023    
024    import com.google.gwt.event.shared.GwtEvent;
025    import com.google.gwt.user.client.Event;
026    import org.gwtbootstrap3.extras.typeahead.client.base.Suggestion;
027    import org.gwtbootstrap3.extras.typeahead.client.ui.Typeahead;
028    
029    /**
030     * Triggered when a suggestion from the dropdown menu is selected.
031     *
032     * @author Florian Kremser <florian.kremser@sage.com>
033     */
034    public class TypeaheadSelectedEvent<T> extends GwtEvent<TypeaheadSelectedHandler<T>> {
035    
036        public static <T> void fire(final Typeahead<T> source, final Suggestion<T> suggestion, final Event nativeEvent) {
037            TypeaheadSelectedEvent<T> event = new TypeaheadSelectedEvent<T>(source, suggestion, nativeEvent);
038            source.fireEvent(event);
039        }
040    
041        private static final Type<TypeaheadSelectedHandler<?>> TYPE = new Type<TypeaheadSelectedHandler<?>>();
042    
043        private final Typeahead<T> typeahead;
044        private final Suggestion<T> suggestion;
045        private final Event nativeEvent;
046    
047        public static Type<TypeaheadSelectedHandler<?>> getType() {
048            return TYPE;
049        }
050    
051        private TypeaheadSelectedEvent(final Typeahead<T> typeahead, final Suggestion<T> suggestion, final Event nativeEvent) {
052            this.typeahead = typeahead;
053            this.suggestion = suggestion;
054            this.nativeEvent = nativeEvent;
055        }
056    
057        public Typeahead<T> getTypeahead() {
058            return typeahead;
059        }
060    
061        public Suggestion<T> getSuggestion() {
062            return suggestion;
063        }
064    
065        public Event getNativeEvent() {
066            return nativeEvent;
067        }
068    
069        @SuppressWarnings({"unchecked"})
070        @Override
071        public Type<TypeaheadSelectedHandler<T>> getAssociatedType() {
072            return (Type) TYPE;
073        }
074    
075        @Override
076        protected void dispatch(final TypeaheadSelectedHandler<T> handler) {
077            handler.onSelected(this);
078        }
079    }