All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gwt.material.design.client.base.mixin.EnabledMixin Maven / Gradle / Ivy

/*
 * #%L
 * GwtMaterial
 * %%
 * Copyright (C) 2015 - 2017 GwtMaterialDesign
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package gwt.material.design.client.base.mixin;

import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import gwt.material.design.client.base.MaterialWidget;
import gwt.material.design.client.constants.CssName;

/**
 * @author Ben Dol
 * @author kevzlou7979
 */
public class EnabledMixin extends AbstractMixin implements HasEnabled {

    private static final String TAB_INDEX = "tabIndex";
    private static final String DISABLED = "disabled";
    private String initialTabIndex;

    private HandlerRegistration handler;

    private boolean propagateToChildren;

    public EnabledMixin(final T widget) {
        super(widget);
    }

    @Override
    public void setUiObject(T uiObject) {
        super.setUiObject(uiObject);

        // Clean up previous handler
        if (handler != null) {
            handler.removeHandler();
            handler = null;
        }

        initialTabIndex = uiObject.getElement().getAttribute("tabIndex");
    }

    @Override
    public boolean isEnabled() {
        return !uiObject.getElement().hasAttribute("disabled");
    }

    @Override
    public void setEnabled(final boolean enabled) {
        if (!uiObject.isAttached()) {
            if (handler != null) {
                handler.removeHandler();
                handler = null;
            }

            handler = uiObject.addAttachHandler(event -> {
                if (event.isAttached()) {
                    applyEnabled(enabled, uiObject);
                } else if (handler != null) {
                    handler.removeHandler();
                    handler = null;
                }
            });
        } else {
            applyEnabled(enabled, uiObject);
        }
    }

    public void setEnabled(MaterialWidget widget, boolean enabled) {
        setEnabled(enabled);

        if (isPropagateToChildren()) {
            for (Widget child : widget.getChildren()) {
                if (child instanceof MaterialWidget) {
                    ((MaterialWidget) child).setEnabled(enabled);
                } else if (child instanceof HasEnabled) {
                    ((HasEnabled) child).setEnabled(enabled);
                }
            }
        }
    }

    private void applyEnabled(boolean enabled, UIObject obj) {
        if (enabled) {
            obj.removeStyleName(CssName.DISABLED);
            obj.getElement().removeAttribute(DISABLED);
        } else {
            obj.addStyleName(CssName.DISABLED);
            obj.getElement().setAttribute(DISABLED, "");
        }

        updateWaves(enabled, obj);
        updateTabIndex(enabled, obj);
    }

    public void updateWaves(boolean enabled, UIObject obj) {
        if (obj instanceof MaterialWidget) {
            MaterialWidget widget = (MaterialWidget) obj;
            if (enabled) {
                if (widget.getWaves() != null) {
                    widget.getElement().addClassName(CssName.WAVES_EFFECT);
                }
            } else {
                widget.getElement().removeClassName(CssName.WAVES_EFFECT);
            }
        }
    }

    public void updateTabIndex(boolean enabled, UIObject obj) {
        if (obj instanceof MaterialWidget) {
            MaterialWidget widget = (MaterialWidget) obj;
            if (enabled) {
                if (initialTabIndex != null) widget.getElement().setAttribute(TAB_INDEX, initialTabIndex);
            } else {
                widget.getElement().setAttribute(TAB_INDEX, "-1");
            }
        }
    }

    public boolean isPropagateToChildren() {
        return propagateToChildren;
    }

    public void setPropagateToChildren(boolean propagateToChildren) {
        this.propagateToChildren = propagateToChildren;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy