001/*
002 * #%L
003 * GwtBootstrap3
004 * %%
005 * Copyright (C) 2015 GwtBootstrap3
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.client.base.validator;
021
022import com.google.gwt.event.shared.EventHandler;
023import com.google.gwt.event.shared.GwtEvent;
024import com.google.gwt.event.shared.HasHandlers;
025import com.google.web.bindery.event.shared.HandlerRegistration;
026
027/**
028 * Event fired when validation state changes.
029 *
030 * @author Steven Jardine
031 */
032public class ValidationChangedEvent extends GwtEvent<ValidationChangedEvent.ValidationChangedHandler> {
033
034    /**
035     * HasValidationChangedHandlers.
036     */
037    public interface HasValidationChangedHandlers extends HasHandlers {
038
039        /**
040         * Adds a validation changed handler.
041         *
042         * @param handler the handler
043         * @return the handler registration
044         */
045        HandlerRegistration addValidationChangedHandler(ValidationChangedHandler handler);
046    }
047
048    /**
049     * ValidationChangedHandler.
050     */
051    public interface ValidationChangedHandler extends EventHandler {
052
053        /**
054         * On validation changed.
055         *
056         * @param event the event
057         */
058        void onValidationChanged(ValidationChangedEvent event);
059    }
060
061    protected static final Type<ValidationChangedHandler> TYPE = new Type<>();
062
063    /**
064     * Fire the event.
065     *
066     * @param source the source
067     * @param valid  the valid
068     */
069    public static void fire(HasHandlers source, boolean valid) {
070        source.fireEvent(new ValidationChangedEvent(valid));
071    }
072
073    /**
074     * Fire.
075     *
076     * @param source        the source
077     * @param eventInstance the event instance
078     */
079    public static void fire(HasHandlers source, ValidationChangedEvent eventInstance) {
080        source.fireEvent(eventInstance);
081    }
082
083    /**
084     * Gets the event type.
085     *
086     * @return the type
087     */
088    public static Type<ValidationChangedHandler> getType() {
089        return TYPE;
090    }
091
092    private boolean valid;
093
094    /**
095     * Constructor.
096     */
097    protected ValidationChangedEvent() {
098    }
099
100    /**
101     * Constructor.
102     *
103     * @param valid the validation state.
104     */
105    public ValidationChangedEvent(boolean valid) {
106        this.valid = valid;
107    }
108
109    @Override
110    protected void dispatch(ValidationChangedHandler handler) {
111        handler.onValidationChanged(this);
112    }
113
114    @Override
115    public boolean equals(Object obj) {
116        if (this == obj) return true;
117        if (obj == null) return false;
118        if (getClass() != obj.getClass()) return false;
119        ValidationChangedEvent other = (ValidationChangedEvent) obj;
120        return valid == other.valid;
121    }
122
123    @Override
124    public Type<ValidationChangedHandler> getAssociatedType() {
125        return TYPE;
126    }
127
128    @Override
129    public int hashCode() {
130        int hashCode = 23;
131        hashCode = (hashCode * 37) + Boolean.valueOf(valid).hashCode();
132        return hashCode;
133    }
134
135    /**
136     * Checks if is valid.
137     *
138     * @return true, if is valid
139     */
140    public boolean isValid() {
141        return valid;
142    }
143
144    @Override
145    public String toString() {
146        return "ValidationChangedEvent[" + valid + "]";
147    }
148
149}