001package gwt.material.design.client.base.mixin;
002
003/*
004 * #%L
005 * GwtMaterial
006 * %%
007 * Copyright (C) 2015 GwtMaterialDesign
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.event.logical.shared.AttachEvent;
024import com.google.gwt.event.shared.HandlerRegistration;
025import com.google.gwt.user.client.ui.HasEnabled;
026import com.google.gwt.user.client.ui.UIObject;
027import com.google.gwt.user.client.ui.Widget;
028
029import gwt.material.design.client.base.helper.StyleHelper;
030
031/**
032 * @author Ben Dol
033 */
034public class EnabledMixin<T extends Widget & HasEnabled> extends AbstractMixin<T> implements HasEnabled {
035    private static final String DISABLED = "disabled";
036
037    private HandlerRegistration handler;
038
039    public EnabledMixin(final T widget) {
040        super(widget);
041    }
042
043    @Override
044    public void setUiObject(T uiObject) {
045        super.setUiObject(uiObject);
046
047        // Clean up previous handler
048        if(handler != null) {
049            handler.removeHandler();
050            handler = null;
051        }
052    }
053
054    @Override
055    public boolean isEnabled() {
056        return !StyleHelper.containsStyle(uiObject.getStyleName(), "disabled");
057    }
058
059    @Override
060    public void setEnabled(boolean enabled) {
061        setEnabled(uiObject, enabled);
062    }
063
064    private void setEnabled(final Widget widget, final boolean enabled) {
065        if(!widget.isAttached() && handler == null) {
066            handler = widget.addAttachHandler(new AttachEvent.Handler() {
067                @Override
068                public void onAttachOrDetach(AttachEvent event) {
069                    if(event.isAttached()) {
070                        applyEnabled(enabled, widget);
071                    } else if(handler != null) {
072                        handler.removeHandler();
073                        handler = null;
074                    }
075                }
076            });
077        } else {
078            applyEnabled(enabled, widget);
079        }
080    }
081
082    private void applyEnabled(boolean enabled, UIObject obj) {
083        if(enabled) {
084            obj.removeStyleName("disabled");
085            obj.getElement().removeAttribute(DISABLED);
086        } else {
087            obj.addStyleName("disabled");
088            obj.getElement().setAttribute(DISABLED, "");
089        }
090    }
091}