/WOGWT/Sources/wogwt/translatable/http/PeriodicUpdater.java

http://wogwt.googlecode.com/ · Java · 73 lines · 41 code · 11 blank · 21 comment · 0 complexity · 49a72a9a0e51c541d5e9c65ffaf1e82f MD5 · raw file

  1. package wogwt.translatable.http;
  2. import com.google.gwt.user.client.Timer;
  3. /**
  4. * This class will do an ajax request every X milliseconds to the url specified
  5. * by the updateContainer's "updateUrl" attribute and replace the contents of
  6. * the updateContainer with the response.
  7. *
  8. * To use this, your WOComponent should have something like this:
  9. *
  10. * PeriodicUpdateContainer : WOGenericContainer {
  11. * invokeAction = yourAction;
  12. * updateUrl = context.componentActionURL;
  13. * elementName = "div";
  14. * id = "yourid";
  15. * }
  16. *
  17. */
  18. public class PeriodicUpdater extends Updater {
  19. private Timer timer;
  20. private int frequencyInMilliseconds;
  21. public PeriodicUpdater(String updateContainerID, int frequencyInMilliseconds) {
  22. super(updateContainerID);
  23. this.frequencyInMilliseconds = frequencyInMilliseconds;
  24. setupTimer();
  25. }
  26. public PeriodicUpdater(String updateContainerID, int frequencyInMilliseconds, AfterDOMUpdateCallback callback) {
  27. super(updateContainerID, callback);
  28. this.frequencyInMilliseconds = frequencyInMilliseconds;
  29. setupTimer();
  30. }
  31. protected void setupTimer() {
  32. new Timer() {
  33. public void run() {
  34. timer = new Timer() {
  35. public void run() {
  36. fireUpdate();
  37. }
  38. };
  39. timer.scheduleRepeating(getFrequencyInMilliseconds());
  40. }
  41. }.schedule(getFrequencyInMilliseconds());
  42. }
  43. /**
  44. * Ends the periodic update.
  45. */
  46. public void cancel() {
  47. timer.cancel();
  48. }
  49. /**
  50. * Restarts the periodic update.
  51. */
  52. public void restart() {
  53. timer.cancel();
  54. setupTimer();
  55. }
  56. protected Timer getTimer() {
  57. return timer;
  58. }
  59. protected int getFrequencyInMilliseconds() {
  60. return frequencyInMilliseconds;
  61. }
  62. }