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.core.client.JavaScriptObject;
024import com.google.gwt.core.client.JsArray;
025import com.google.gwt.event.shared.GwtEvent;
026
027/**
028 * The {@link SummernoteImageUploadEvent} is fired when inserting images into the
029 * summernote editor.
030 *
031 * @author Xiaodong Sun
032 */
033public class SummernoteImageUploadEvent extends GwtEvent<SummernoteImageUploadHandler> {
034
035    /**
036     * JavaScript overlay type of image file.
037     */
038    public static class ImageFile extends JavaScriptObject {
039
040        protected ImageFile() {
041        }
042
043        public final native String getName() /*-{
044            return this.name;
045        }-*/;
046
047        public final native double getSize() /*-{
048            return this.size;
049        }-*/;
050
051        public final native String getType() /*-{
052            return this.type;
053        }-*/;
054
055        public final String getMetadata() {
056            return new StringBuilder("ImageFile [")
057                .append("name = ").append(getName()).append(", ")
058                .append("size = ").append(getSize()).append(", ")
059                .append("type = ").append(getType())
060                .append("]").toString();
061        }
062
063    }
064
065    private static Type<SummernoteImageUploadHandler> TYPE;
066
067    private final JsArray<ImageFile> images;
068
069    /**
070     * Fires a summernote image upload event on all registered handlers in the
071     * handler manager. If no such handlers exist, this method will do nothing.
072     *
073     * @param source the source of the handlers
074     */
075    public static void fire(final HasSummernoteImageUploadHandlers source, JsArray<ImageFile> images) {
076        if (TYPE != null) {
077            SummernoteImageUploadEvent event = new SummernoteImageUploadEvent(images);
078            source.fireEvent(event);
079        }
080    }
081
082    /**
083     * Gets the type associated with this event.
084     *
085     * @return returns the handler type
086     */
087    public static Type<SummernoteImageUploadHandler> getType() {
088        if (TYPE == null) {
089            TYPE = new Type<SummernoteImageUploadHandler>();
090        }
091        return TYPE;
092    }
093
094    @Override
095    public Type<SummernoteImageUploadHandler> getAssociatedType() {
096        return TYPE;
097    }
098
099    @Override
100    protected void dispatch(final SummernoteImageUploadHandler handler) {
101        handler.onSummernoteImageUpload(this);
102    }
103
104    /**
105     * Creates a summernote image upload event.
106     */
107    protected SummernoteImageUploadEvent(JsArray<ImageFile> images) {
108        this.images = images;
109    }
110
111    @Override
112    public String toDebugString() {
113        return super.toDebugString() + " with " + images.length() + " images";
114    }
115
116    /**
117     * Returns the JavaScript array of the {@link ImageFile}s to be
118     * inserted.
119     *
120     * @return the JavaScript array of the {@link ImageFile}s to be
121     *         inserted.
122     */
123    public JsArray<ImageFile> getImages() {
124        return images;
125    }
126}