There are a couple of approaches to do this, since tapestry does not
support ajax push (aka reverse ajax) out-of-the box, a simple solution
would be to have the client poll the server for the current percentage
complete.

In order to display a progress bar to the client, you must do the actual
work for the task on a separate thread to the Ajax request thread. It is
probably best to have a singleton ExecutorService (using
java.util.concurrent.Executors.newFixedThreadPool(threadCount) or similar)

The Ajax request thread should:
1. Create a runnable and give it a unique taskId
2. Put the runnable in a serverside map keyed on taskId (map could be a
singleton or perhaps stored in each user's session)

3. Call executorService.submit(myRunnable)
4. Use the AjaxResponseRenderer to fire the "updateProgressBar" event
passing the taskId in the context
5. Return the progress bar zone's getBody() with a percentageComplete of 0

Your runnable would be something like

public class MyWorker implements Runnable {
   private int taskId;
   private ConcurrentMap workers;
   private AtomicInteger percentComplete = new AtomicInteger(0);
   public MyWorker(ConcurrentMap workers, String taskId) {
      this.workers = workers;
      this.taskId = taskId;
   }
   public int getPercentComplete() {
      return percentComplete.get();
   }
   public void run() {
      try {
         doSomeWork();
         percentComplete.set(10);
         doSomeMoreWork();
         percentComplete.set(50);
         doEvenMoreWork();
         percentComplete.set(100);
      } finally {
         workers.remove(taskId);
      }
   }
}


In your serverside event handler [onUpdateProgressBar(String taskId)]
lookup the runnable in the map and call getPercentComplete() then return
the updated progress zone's getBody(). If the percentageComplete() is less
than 100, use the AjaxResponseRenderer to fire the "updateProgressBar"
event again.

@see http://tawus.wordpress.com/2011/10/01/tapestry-5-3-new-features-part-2/for
AjaxResponseRenderer usage

Cheers,
Lance.

On Monday, 26 December 2011, angelochen <angelochen...@yahoo.com.hk> wrote:
> Hi,
>
> would like to have some ideas about how to implement following:
>
> I have a t:grid, every row has a link, when click, it will do a process
> which might take a while, once done, it will either disable the link or
> re-display the link depending on the result. I'd like that, when clicked,
> the link is replaced with a progress bar in place, until it is done. any
> hints? Thanks,
>
> Angelo
>
> --
> View this message in context:
http://tapestry.1045711.n5.nabble.com/T5-progress-bar-in-a-grid-tp5101565p5101565.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
>
>

Reply via email to