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.

VLoadingIndicator.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright 2000-2013 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.client;
  17. import com.google.gwt.dom.client.Style.Display;
  18. import com.google.gwt.dom.client.Style.Position;
  19. import com.google.gwt.user.client.DOM;
  20. import com.google.gwt.user.client.Timer;
  21. /**
  22. * Class representing the loading indicator for Vaadin applications. The loading
  23. * indicator has four states: "triggered", "first", "second" and "third". When
  24. * {@link #trigger()} is called the indicator moves to its "triggered" state and
  25. * then transitions from one state to the next when the timeouts specified using
  26. * the set*StateDelay methods occur.
  27. *
  28. * @author Vaadin Ltd
  29. * @since 7.1
  30. */
  31. public class VLoadingIndicator {
  32. private static final String PRIMARY_STYLE_NAME = "v-loading-indicator";
  33. private ApplicationConnection connection;
  34. private int firstDelay = 300;
  35. private int secondDelay = 1500;
  36. private int thirdDelay = 5000;
  37. private Timer firstTimer = new Timer() {
  38. @Override
  39. public void run() {
  40. show();
  41. }
  42. };
  43. private Timer secondTimer = new Timer() {
  44. @Override
  45. public void run() {
  46. getElement().setClassName(PRIMARY_STYLE_NAME);
  47. getElement().addClassName("second");
  48. // For backwards compatibility only
  49. getElement().addClassName(PRIMARY_STYLE_NAME + "-delay");
  50. }
  51. };
  52. private Timer thirdTimer = new Timer() {
  53. @Override
  54. public void run() {
  55. getElement().setClassName(PRIMARY_STYLE_NAME);
  56. getElement().addClassName("third");
  57. // For backwards compatibility only
  58. getElement().addClassName(PRIMARY_STYLE_NAME + "-wait");
  59. }
  60. };
  61. private com.google.gwt.user.client.Element element;
  62. /**
  63. * Returns the delay (in ms) which must pass before the loading indicator
  64. * moves into the "first" state and is shown to the user
  65. *
  66. * @return The delay (in ms) until moving into the "first" state. Counted
  67. * from when {@link #trigger()} is called.
  68. */
  69. public int getFirstDelay() {
  70. return firstDelay;
  71. }
  72. /**
  73. * Sets the delay (in ms) which must pass before the loading indicator moves
  74. * into the "first" state and is shown to the user
  75. *
  76. * @param firstDelay
  77. * The delay (in ms) until moving into the "first" state. Counted
  78. * from when {@link #trigger()} is called.
  79. */
  80. public void setFirstDelay(int firstDelay) {
  81. this.firstDelay = firstDelay;
  82. }
  83. /**
  84. * Returns the delay (in ms) which must pass before the loading indicator
  85. * moves to its "second" state.
  86. *
  87. * @return The delay (in ms) until the loading indicator moves into its
  88. * "second" state. Counted from when {@link #trigger()} is called.
  89. */
  90. public int getSecondDelay() {
  91. return secondDelay;
  92. }
  93. /**
  94. * Sets the delay (in ms) which must pass before the loading indicator moves
  95. * to its "second" state.
  96. *
  97. * @param secondDelay
  98. * The delay (in ms) until the loading indicator moves into its
  99. * "second" state. Counted from when {@link #trigger()} is
  100. * called.
  101. */
  102. public void setSecondDelay(int secondDelay) {
  103. this.secondDelay = secondDelay;
  104. }
  105. /**
  106. * Returns the delay (in ms) which must pass before the loading indicator
  107. * moves to its "third" state.
  108. *
  109. * @return The delay (in ms) until the loading indicator moves into its
  110. * "third" state. Counted from when {@link #trigger()} is called.
  111. */
  112. public int getThirdDelay() {
  113. return thirdDelay;
  114. }
  115. /**
  116. * Sets the delay (in ms) which must pass before the loading indicator moves
  117. * to its "third" state.
  118. *
  119. * @param thirdDelay
  120. * The delay (in ms) from the event until changing the loading
  121. * indicator into its "third" state. Counted from when
  122. * {@link #trigger()} is called.
  123. */
  124. public void setThirdDelay(int thirdDelay) {
  125. this.thirdDelay = thirdDelay;
  126. }
  127. /**
  128. * Triggers displaying of this loading indicator. The loading indicator will
  129. * actually be shown by {@link #show()} when the "first" delay (as specified
  130. * by {@link #getFirstDelay()}) has passed.
  131. * <p>
  132. * The loading indicator will be hidden if shown when calling this method.
  133. * </p>
  134. */
  135. public void trigger() {
  136. hide();
  137. firstTimer.schedule(getFirstDelay());
  138. }
  139. /**
  140. * Shows the loading indicator in its standard state and triggers timers for
  141. * transitioning into the "second" and "third" states.
  142. */
  143. public void show() {
  144. // Reset possible style name and display mode
  145. getElement().setClassName(PRIMARY_STYLE_NAME);
  146. getElement().addClassName("first");
  147. getElement().getStyle().setDisplay(Display.BLOCK);
  148. // Schedule the "second" loading indicator
  149. int secondTimerDelay = getSecondDelay() - getFirstDelay();
  150. if (secondTimerDelay >= 0) {
  151. secondTimer.schedule(secondTimerDelay);
  152. }
  153. // Schedule the "third" loading indicator
  154. int thirdTimerDelay = getThirdDelay() - getFirstDelay();
  155. if (thirdTimerDelay >= 0) {
  156. thirdTimer.schedule(thirdTimerDelay);
  157. }
  158. }
  159. /**
  160. * Returns the {@link ApplicationConnection} which uses this loading
  161. * indicator
  162. *
  163. * @return The ApplicationConnection for this loading indicator
  164. */
  165. public ApplicationConnection getConnection() {
  166. return connection;
  167. }
  168. /**
  169. * Sets the {@link ApplicationConnection} which uses this loading indicator.
  170. * Only used internally.
  171. *
  172. * @param connection
  173. * The ApplicationConnection for this loading indicator
  174. */
  175. void setConnection(ApplicationConnection connection) {
  176. this.connection = connection;
  177. }
  178. /**
  179. * Hides the loading indicator (if visible). Cancels any possibly running
  180. * timers.
  181. */
  182. public void hide() {
  183. firstTimer.cancel();
  184. secondTimer.cancel();
  185. thirdTimer.cancel();
  186. getElement().getStyle().setDisplay(Display.NONE);
  187. }
  188. /**
  189. * Returns whether or not the loading indicator is showing.
  190. *
  191. * @return true if the loading indicator is visible, false otherwise
  192. */
  193. public boolean isVisible() {
  194. if (getElement().getStyle().getDisplay()
  195. .equals(Display.NONE.getCssName())) {
  196. return false;
  197. }
  198. return true;
  199. }
  200. /**
  201. * Returns the root element of the loading indicator
  202. *
  203. * @return The loading indicator DOM element
  204. */
  205. public com.google.gwt.user.client.Element getElement() {
  206. if (element == null) {
  207. element = DOM.createDiv();
  208. element.getStyle().setPosition(Position.ABSOLUTE);
  209. getConnection().getUIConnector().getWidget().getElement()
  210. .appendChild(element);
  211. }
  212. return element;
  213. }
  214. }