選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VLoadingIndicator.java 7.8KB

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