001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
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.mixin;
021
022import com.google.gwt.user.client.ui.UIObject;
023import gwt.material.design.client.base.HasProgress;
024import gwt.material.design.client.constants.CssName;
025import gwt.material.design.client.constants.Display;
026import gwt.material.design.client.constants.ProgressType;
027import gwt.material.design.client.ui.MaterialCollapsibleBody;
028import gwt.material.design.client.ui.MaterialCollapsibleItem;
029import gwt.material.design.client.ui.MaterialNavBar;
030import gwt.material.design.client.ui.MaterialProgress;
031
032/**
033 * @author kevzlou7979
034 */
035public class ProgressMixin<T extends UIObject & HasProgress> extends AbstractMixin<T> implements HasProgress {
036
037    private MaterialProgress progress = new MaterialProgress();
038
039    public ProgressMixin(T uiObject) {
040        super(uiObject);
041    }
042
043    @Override
044    public void showProgress(ProgressType type) {
045        if (uiObject instanceof MaterialCollapsibleItem) {
046            applyCollapsibleProgress(true);
047        } else if (uiObject instanceof MaterialNavBar) {
048            ((MaterialNavBar) uiObject).add(progress);
049        }
050    }
051
052    @Override
053    public void setPercent(double percent) {
054        progress.setPercent(percent);
055    }
056
057    @Override
058    public void hideProgress() {
059        if (uiObject instanceof MaterialCollapsibleItem) {
060            applyCollapsibleProgress(false);
061        } else {
062            progress.removeFromParent();
063        }
064    }
065
066    @Override
067    public MaterialProgress getProgress() {
068        return progress;
069    }
070
071    protected void applyCollapsibleProgress(boolean isShow) {
072        MaterialCollapsibleItem item = (MaterialCollapsibleItem) uiObject;
073        MaterialCollapsibleBody body = (MaterialCollapsibleBody) item.getWidget(1);
074        if (uiObject.getElement().getClassName().contains(CssName.ACTIVE)) {
075            if (isShow) {
076                body.setDisplay(Display.NONE);
077                item.add(progress);
078            } else {
079                body.setDisplay(Display.BLOCK);
080                progress.removeFromParent();
081            }
082        }
083    }
084}