001package org.gwtbootstrap3.extras.summernote.client.event;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2015 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
023import com.google.gwt.dom.client.NativeEvent;
024import com.google.gwt.event.shared.GwtEvent;
025
026/**
027 * The {@link SummernoteKeyDownEvent} is fired when a key is pressed within
028 * the summernote editor.
029 *
030 * @author Xiaodong Sun
031 */
032public class SummernoteKeyDownEvent extends GwtEvent<SummernoteKeyDownHandler> {
033
034    private static Type<SummernoteKeyDownHandler> TYPE;
035
036    private final NativeEvent nativeEvent;
037
038    /**
039     * Fires a summernote key down event on all registered handlers in the handler
040     * manager. If no such handlers exist, this method will do nothing.
041     *
042     * @param source the source of the handlers
043     * @param nativeEvent native key down event
044     */
045    public static void fire(final HasSummernoteKeyDownHandlers source, NativeEvent nativeEvent) {
046        if (TYPE != null) {
047            SummernoteKeyDownEvent event = new SummernoteKeyDownEvent(nativeEvent);
048            source.fireEvent(event);
049        }
050    }
051
052    /**
053     * Gets the type associated with this event.
054     *
055     * @return returns the handler type
056     */
057    public static Type<SummernoteKeyDownHandler> getType() {
058        if (TYPE == null) {
059            TYPE = new Type<SummernoteKeyDownHandler>();
060        }
061        return TYPE;
062    }
063
064    @Override
065    public Type<SummernoteKeyDownHandler> getAssociatedType() {
066        return TYPE;
067    }
068
069    @Override
070    protected void dispatch(final SummernoteKeyDownHandler handler) {
071        handler.onSummnernoteKeyDown(this);
072    }
073
074    @Override
075    public String toDebugString() {
076        return super.toDebugString() + " key code is: " + nativeEvent.getKeyCode();
077    }
078
079    /**
080     * Returns the native key down event.
081     *
082     * @return native key down event
083     */
084    public NativeEvent getNativeEvent() {
085        return nativeEvent;
086    }
087
088    /**
089     * Creates a summernote key down event.
090     *
091     * @param nativeEvent native key down event
092     */
093    protected SummernoteKeyDownEvent(NativeEvent nativeEvent) {
094        this.nativeEvent = nativeEvent;
095    }
096}