You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProgressBar.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui;
  17. import org.jsoup.nodes.Element;
  18. import com.vaadin.shared.ui.progressindicator.ProgressBarState;
  19. import com.vaadin.ui.declarative.DesignAttributeHandler;
  20. import com.vaadin.ui.declarative.DesignContext;
  21. /**
  22. * A component for displaying progress.
  23. * <p>
  24. * The default mode is to show the current progress internally represented by a
  25. * floating point value between 0 and 1 (inclusive). The progress bar can also
  26. * be in an indeterminate mode showing an animation indicating that the task is
  27. * running but without providing any information about the current progress.
  28. *
  29. * @since 8.0
  30. * @author Vaadin Ltd
  31. */
  32. public class ProgressBar extends AbstractComponent {
  33. private static final float DEFAULT_VALUE = 0f;
  34. /**
  35. * Creates a new progress bar initially set to 0% progress.
  36. */
  37. public ProgressBar() {
  38. this(DEFAULT_VALUE);
  39. }
  40. /**
  41. * Creates a new progress bar with the given initial value.
  42. *
  43. * @param progress
  44. * the initial progress value
  45. */
  46. public ProgressBar(float progress) {
  47. setValue(Float.valueOf(progress));
  48. }
  49. /**
  50. * Gets the value of this progress bar. The value is a <code>float</code>
  51. * between 0 and 1 where 0 represents no progress at all and 1 represents
  52. * fully completed.
  53. *
  54. * @return the current progress value
  55. */
  56. public float getValue() {
  57. return getState(false).state;
  58. }
  59. /**
  60. * Sets the value of this progress bar. The value is a <code>float</code>
  61. * between 0 and 1 where 0 represents no progress at all and 1 represents
  62. * fully completed.
  63. *
  64. * @param newValue
  65. * the current progress value
  66. * @since 8.0
  67. */
  68. public void setValue(float newValue) {
  69. getState().state = newValue;
  70. }
  71. @Override
  72. protected ProgressBarState getState() {
  73. return (ProgressBarState) super.getState();
  74. }
  75. @Override
  76. protected ProgressBarState getState(boolean markAsDirty) {
  77. return (ProgressBarState) super.getState(markAsDirty);
  78. }
  79. /**
  80. * Sets whether or not this progress indicator is indeterminate. In
  81. * indeterminate mode there is an animation indicating that the task is
  82. * running but without providing any information about the current progress.
  83. *
  84. * @param indeterminate
  85. * <code>true</code> to set to indeterminate mode; otherwise
  86. * <code>false</code>
  87. */
  88. public void setIndeterminate(boolean indeterminate) {
  89. getState().indeterminate = indeterminate;
  90. }
  91. /**
  92. * Gets whether or not this progress indicator is indeterminate. In
  93. * indeterminate mode there is an animation indicating that the task is
  94. * running but without providing any information about the current progress.
  95. *
  96. * @return <code>true</code> if set to indeterminate mode; otherwise
  97. * <code>false</code>
  98. */
  99. public boolean isIndeterminate() {
  100. return getState(false).indeterminate;
  101. }
  102. @Override
  103. public void readDesign(Element design, DesignContext designContext) {
  104. super.readDesign(design, designContext);
  105. if (design.hasAttr("value") && !design.attr("value").isEmpty()) {
  106. setValue(DesignAttributeHandler.readAttribute("value",
  107. design.attributes(), Float.class));
  108. }
  109. }
  110. @Override
  111. public void writeDesign(Element design, DesignContext designContext) {
  112. super.writeDesign(design, designContext);
  113. Float defaultValue = ((ProgressBar) designContext
  114. .getDefaultInstance(this)).getValue();
  115. DesignAttributeHandler.writeAttribute("value", design.attributes(),
  116. getValue(), defaultValue, Float.class, designContext);
  117. }
  118. /**
  119. * Resets the value of this component, effectively displaying zero progress.
  120. *
  121. * @since 8.0
  122. */
  123. public void reset() {
  124. setValue(DEFAULT_VALUE);
  125. }
  126. }