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