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