001package gwt.material.design.addins.client.camera.events;
002
003import com.google.gwt.event.shared.GwtEvent;
004import gwt.material.design.addins.client.camera.MaterialCameraCapture;
005import gwt.material.design.addins.client.camera.base.HasCameraCaptureHandlers;
006
007/*
008 * #%L
009 * GwtMaterial
010 * %%
011 * Copyright (C) 2015 - 2017 GwtMaterialDesign
012 * %%
013 * Licensed under the Apache License, Version 2.0 (the "License");
014 * you may not use this file except in compliance with the License.
015 * You may obtain a copy of the License at
016 * 
017 *      http://www.apache.org/licenses/LICENSE-2.0
018 * 
019 * Unless required by applicable law or agreed to in writing, software
020 * distributed under the License is distributed on an "AS IS" BASIS,
021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
022 * See the License for the specific language governing permissions and
023 * limitations under the License.
024 * #L%
025 */
026
027/**
028 * <p>
029 * Event that holds informartion about the state of a {@link HasCameraCaptureHandlers}, such
030 * as {@link MaterialCameraCapture}.
031 * </p>
032 * <p>
033 * When the stream has started, after the user allows the browser to use the camera, an event
034 * with {@link CaptureStatus#STARTED} is raised. If the stream is paused, an event with
035 * {@link CaptureStatus#PAUSED} is raised. If the user doesn't allow the browser to use the camera,
036 * or if any other error occurs, an event with  {@link CaptureStatus#ERRORED} is raised, with the
037 * {@link #getErrorMessage()} set.
038 * </p>
039 *
040 * @author gilberto-torrezan
041 */
042public class CameraCaptureEvent extends GwtEvent<CameraCaptureHandler> {
043
044    private static Type<CameraCaptureHandler> TYPE = new Type<>();
045
046    public enum CaptureStatus {
047        STARTED, PAUSED, ERRORED;
048    }
049
050    private final CaptureStatus captureStatus;
051    private final String errorMessage;
052
053    private CameraCaptureEvent(CaptureStatus captureStatus, String errorMessage) {
054        this.captureStatus = captureStatus;
055        this.errorMessage = errorMessage;
056    }
057
058    /**
059     * Fires an event with the {@link CaptureStatus}.
060     */
061    public static <T> void fire(HasCameraCaptureHandlers source, CaptureStatus status) {
062        CameraCaptureEvent event = new CameraCaptureEvent(status, null);
063        source.fireEvent(event);
064    }
065
066    /**
067     * Fires an event with the {@link CaptureStatus#ERRORED} status and the error message.
068     */
069    public static <T> void fire(HasCameraCaptureHandlers source, String errorMessage) {
070        CameraCaptureEvent event = new CameraCaptureEvent(CaptureStatus.ERRORED, errorMessage);
071        source.fireEvent(event);
072    }
073
074    /**
075     * Returns the {@link CaptureStatus} that raised this event.
076     */
077    public CaptureStatus getCaptureStatus() {
078        return captureStatus;
079    }
080
081    /**
082     * Returns the error message when the {@link #getCaptureStatus()} is {@link CaptureStatus#ERRORED}.
083     */
084    public String getErrorMessage() {
085        return errorMessage;
086    }
087
088    @Override
089    public com.google.gwt.event.shared.GwtEvent.Type<CameraCaptureHandler> getAssociatedType() {
090        return TYPE;
091    }
092
093    public static Type<CameraCaptureHandler> getType() {
094        return TYPE;
095    }
096
097    @Override
098    protected void dispatch(CameraCaptureHandler handler) {
099        handler.onCameraCaptureChange(this);
100    }
101
102}