001package com.github.gwtbootstrap.client.ui;
002
003import com.github.gwtbootstrap.client.ui.base.DivWidget;
004import com.github.gwtbootstrap.client.ui.base.Style;
005import com.github.gwtbootstrap.client.ui.base.StyleHelper;
006import com.github.gwtbootstrap.client.ui.constants.Constants;
007import com.google.gwt.dom.client.Style.Unit;
008
009/**
010 * The Bar of ProgressBar.
011 * 
012 * @since 2.2.1.0
013 * @author ohashi keisuke
014 * 
015 * @see StackProgressBar
016 */
017public class Bar extends DivWidget {
018
019    /**
020     * Create an Empty widget.
021     */
022    public Bar() {
023        super(Constants.BAR);
024    }
025    
026    /**
027     * The Color of {@link Bar}
028     * 
029     * @since 2.2.1.0
030     * @author ohashi keisuke
031     *
032     */
033    public enum Color implements Style {
034        /** DEFAULT Color */
035        DEFAULT,
036        /** INFO Color */
037        INFO,
038        /** SUCCESS Color */
039        SUCCESS,
040        /** DANGER Color */
041        DANGER,
042        /** WARNING Color */
043        WARNING
044        ;
045        
046        /**
047         * {@inheritDoc}
048         */
049        @Override
050        public String get() {
051            return DEFAULT.equals(this) ? "" : "bar-" +name().toLowerCase();
052        }
053    }
054    
055    /**
056     * Set bar width as a percent unit
057     * @param percent percent
058     */
059    public void setPercent(double percent) {
060        this.getElement().getStyle().setWidth(percent, Unit.PCT);
061    }
062    
063    /**
064     * Get bar width as a percent unit
065     * @return percent
066     */
067    public double getPercent() {
068        String width = this.getElement().getStyle().getWidth();
069        if (width == null)
070            return 0;
071        else
072            return Double.valueOf(width.substring(0, width.indexOf("%")));
073    }
074    
075    /**
076     * Set bar color
077     * @param color color
078     */
079    public void setColor(Color color) {
080        StyleHelper.changeStyle(this, color, Color.class);
081    }
082
083    /**
084     * Set bar text
085     * @param text bar text
086     */
087    public void setText(String text) {
088        getElement().setInnerText(text);
089    }
090}
091