001 /* 002 * Copyright 2012 GWT-Bootstrap 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package com.github.gwtbootstrap.client.ui; 017 018 import com.github.gwtbootstrap.client.ui.base.DivWidget; 019 import com.github.gwtbootstrap.client.ui.base.StyleHelper; 020 import com.github.gwtbootstrap.client.ui.resources.Bootstrap; 021 import com.google.gwt.dom.client.Style.Unit; 022 023 //@formatter:off 024 /** 025 * The progress bar. 026 * 027 * @since 2.0.4.0 028 * @author Dominik Mayer 029 */ 030 //@formatter:on 031 public class ProgressBar extends DivWidget { 032 033 public enum Style implements com.github.gwtbootstrap.client.ui.base.Style { 034 035 DEFAULT(""), 036 037 STRIPED(Bootstrap.progress_striped), 038 039 ANIMATED(Bootstrap.progress_animated); 040 041 private final String styleName; 042 043 private Style(String styleName) { 044 this.styleName = styleName; 045 046 } 047 048 public String get() { 049 return this.styleName; 050 } 051 } 052 053 public enum Color implements com.github.gwtbootstrap.client.ui.base.Style { 054 DEFAULT(""), 055 INFO("progress-info"), 056 SUCCESS("progress-success"), 057 DANGER("progress-danger"), 058 WARNING("progress-warning") 059 ; 060 061 private final String styleName; 062 063 private Color(String styleName) { 064 this.styleName = styleName; 065 } 066 067 @Override 068 public String get() { 069 return this.styleName; 070 } 071 072 } 073 074 private DivWidget bar = new DivWidget(); 075 076 public ProgressBar() { 077 setStylePrimaryName(Bootstrap.progress); 078 addStyleName(Bootstrap.progress); 079 bar.addStyleName(Bootstrap.bar); 080 setColor(Color.DEFAULT); 081 add(bar); 082 } 083 084 public ProgressBar(Style style) { 085 this(); 086 setType(style); 087 } 088 089 public void setType(Style style) { 090 StyleHelper.changeStyle(this, style, Style.class); 091 } 092 093 public void setPercent(int percent) { 094 bar.getElement().getStyle().setWidth(percent, Unit.PCT); 095 } 096 097 public int getPercent() { 098 String width = bar.getElement().getStyle().getWidth(); 099 if (width == null) 100 return 0; 101 else 102 return Integer.valueOf(width.substring(0, width.indexOf("%"))); 103 } 104 105 public void setColor(Color color) { 106 StyleHelper.changeStyle(this, color, Color.class); 107 } 108 109 }