001package gwt.material.design.client.ui;
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.dom.client.Style.Unit;
024import gwt.material.design.client.base.HasType;
025import gwt.material.design.client.constants.ProgressType;
026import gwt.material.design.client.ui.html.Div;
027
028//@formatter:off
029
030/**
031* Material Progress indicator to define intermediate and determinate progress bars
032* <h3>UiBinder Usage:</h3>
033* 
034* <pre>
035* {@code 
036* <m:MaterialProgress />
037}
038* </pre>
039* @see <a href="http://gwt-material-demo.herokuapp.com/#loaders">Material Progress</a>
040* @author kevzlou7979
041*/
042public class MaterialProgress extends Div implements HasType<ProgressType> {
043
044    private Div div = new Div();
045    private double percent = 0;
046    private String color;
047    private ProgressType type;
048
049    public MaterialProgress() {
050        super("progress");
051        getElement().getStyle().setMargin(0, Unit.PX);
052        add(div);
053        setType(ProgressType.INDETERMINATE);
054    }
055
056    @Override
057    public void setType(ProgressType type) {
058        if(this.type != null) {
059            div.removeStyleName(this.type.getCssName());
060        }
061        this.type = type;
062        div.addStyleName(type.getCssName());
063    }
064
065    @Override
066    public ProgressType getType() {
067        return type;
068    }
069
070    /**
071     * Get progress width as percent unit
072     */
073    public double getPercent() {
074        return percent;
075    }
076
077    /**
078     * Set progress width as percent unit.
079     */
080    public void setPercent(double percent) {
081        this.percent = percent;
082        div.getElement().getStyle().setWidth(percent, Unit.PCT);
083    }
084
085    /**
086     * Get the progress bar color.
087     */
088    public String getColor() {
089        return color;
090    }
091
092    /**
093     * Set the color of the progress bar.
094     * @param color String value of the color.
095     */
096    public void setColor(String color) {
097        if(this.color != null) {
098            div.removeStyleName(this.color);
099        }
100        this.color = color;
101        div.addStyleName(color);
102    }
103}