com.gwtm.ui.client.widgets.ProgressBar.java Source code

Java tutorial

Introduction

Here is the source code for com.gwtm.ui.client.widgets.ProgressBar.java

Source

/*
 * Copyright 2011 Vancouver Ywebb Consulting Ltd
 * 
 * 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.
 */
package com.gwtm.ui.client.widgets;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
import com.gwtm.ui.client.core.Utils;
import com.gwtm.ui.client.core.widgets.CssFactory.BoxOrient;
import com.gwtm.ui.client.themes.ThemeManager;

/**
 * XProgress depicts the progress of a task over time. The current progress is
 * represented by a floating-point value between 0.0 and 1.0, inclusive, where
 * 1.0 indicates the completion of the task. Values less than 0.0 and greater
 * than 1.0 are pinned to those limits.
 * 
 * <p>
 * <img class='ai' src='../../../../resources/XProgress.png' />
 * </p>
 */
public class ProgressBar extends BoxPanel {

    public enum LabelPosition {
        top, right, bottom, left
    };

    private double value = 0;
    //private FlowPanel panel = new FlowPanel();
    private HTML label = new HTML("");
    private HTML progress = new HTML("<div class=right></div><div class=left></div>");
    private int transistionTiming = 500;
    private State state = State.NOT_RUNNING;
    private Style progressStyle = null;
    private LabelPosition labelPosition = LabelPosition.top;
    private String labelText = "";
    private boolean labelVisible = true;

    private enum State {
        NOT_RUNNING, RUNNING, COMPLETED
    }

    public ProgressBar() {
        Utils.Widgets.setPrimaryCssClass(this, ThemeManager.getTheme().progressbar());

        label.setStylePrimaryName("labeltext");
        progress.setStylePrimaryName("bar");

        reset();
    }

    public ProgressBar(int initialValue) {
        this();
        setValue(initialValue);
    }

    public void reset() {

        this.clear();
        if (labelPosition == LabelPosition.top || labelPosition == LabelPosition.left) {
            add(label);
            add(progress);
        } else {
            add(progress);
            add(label);
        }
        if (labelPosition == LabelPosition.left || labelPosition == LabelPosition.right) {
            this.setBoxOrient(BoxOrient.horizontal);
        } else {
            this.setBoxOrient(BoxOrient.vertical);
        }

        // FIXME doesn't remove -webkit-transition-property: width;
        // causing progress to animate backwards on restart
        css_().setProperty("display", "none");
        css_().clearProperty("webkitTransitionProperty");
        css_().setProperty("webkitTransitionProperty", "height");
        double currentValue = getValue(); // store value temp
        setValue(0);
        css_().setProperty("webkitTransitionProperty", "width");
        state = State.NOT_RUNNING;
        setValue(currentValue); // restore value

        label.setHTML(formatLabel(getPercentValue()));
    }

    public LabelPosition getLabelPosition() {
        return labelPosition;
    }

    public void setLabelPosition(LabelPosition labelPosition) {
        this.labelPosition = labelPosition;
        reset();
    }

    public String getLabelText() {
        return labelText;
    }

    public void setLabelText(String labelText) {
        this.labelText = labelText;
        reset();
    }

    public void setValue(double v) {
        if (value != v) {

            if (state != State.RUNNING) {
                css_().setProperty("display", "inline");
                state = State.RUNNING;
            }

            if (v < 0.0) {
                value = 0;

            } else if (v >= 1.0) {
                value = 1;

            } else {
                value = v;
            }
            updatePosition_();
        }
    }

    public void setTransistionTiming(int transistionTiming) {
        this.transistionTiming = transistionTiming;
    }

    public int getTransistionTiming() {
        return transistionTiming;
    }

    public double getValue() {
        return value;
    }

    public int getPercentValue() {
        return (int) Math.round(value * 100.0);
    }

    public void setLabelVisible(boolean labelVisible) {
        this.labelVisible = labelVisible;
        label.setVisible(labelVisible);
    }

    public boolean isLabelVisible() {
        return labelVisible;
    }

    public boolean isRunning() {
        return state == State.RUNNING;
    }

    private void updatePosition_() {
        css_().setWidth((value * 100), Unit.PCT);
        final int pctVal = getPercentValue();

        if (pctVal >= 100) {
            state = State.COMPLETED;
        }

        if (transistionTiming > -1) {
            css_().setProperty("webkitTransitionDuration", transistionTiming + "ms");
            new Timer() {
                public void run() {
                    label.setHTML(formatLabel(pctVal));
                }
            }.schedule(transistionTiming);
        } else {
            new Timer() {
                public void run() {
                    label.setHTML(formatLabel(pctVal));
                }
            }.schedule(500/* defined at css */);
        }

    }

    private Style css_() {
        if (progressStyle != null) {
            return progressStyle;
        }
        progressStyle = ((Element) progress.getElement().getChild(1)).getStyle();
        return progressStyle;
    }

    private String formatLabel(int val) {
        return getLabelText().replaceAll("%", val + "%");
    }

}