Advertisement
Guest User

Untitled

a guest
May 28th, 2010
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package com.eloquentix.soundpure.web.page.migration;
  2.  
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.Future;
  6.  
  7. import org.apache.wicket.AttributeModifier;
  8. import org.apache.wicket.Component;
  9. import org.apache.wicket.ajax.AjaxRequestTarget;
  10. import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
  11. import org.apache.wicket.ajax.IAjaxIndicatorAware;
  12. import org.apache.wicket.ajax.markup.html.AjaxLink;
  13. import org.apache.wicket.extensions.ajax.markup.html.AjaxIndicatorAppender;
  14. import org.apache.wicket.markup.html.basic.Label;
  15. import org.apache.wicket.markup.html.panel.Panel;
  16. import org.apache.wicket.model.AbstractReadOnlyModel;
  17. import org.apache.wicket.model.IModel;
  18. import org.apache.wicket.model.Model;
  19. import org.apache.wicket.util.time.Duration;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22.  
  23. /**
  24.  * A kind of button used to execute long running process & notifying about the progress.
  25.  *
  26.  * @author Alex Objelean
  27.  */
  28. @SuppressWarnings("serial")
  29. public abstract class ProcessExecutorPanel
  30.   extends Panel implements IAjaxIndicatorAware {
  31.   private static final Logger LOG = LoggerFactory.getLogger(ProcessExecutorPanel.class);
  32.  
  33.   /**
  34.    * Indicates if the process is still running.
  35.    */
  36.   private boolean everStarted = false;
  37.   private Component status;
  38.   private transient Future<?> future;
  39.   private AjaxIndicatorAppender ajaxIndicator;
  40.   public ProcessExecutorPanel(final String id) {
  41.     super(id);
  42.     addComponents();
  43.   }
  44.  
  45.   private void addComponents() {
  46.     add(getStatus());
  47.     add(getButton());
  48.     add(ajaxIndicator = new AjaxIndicatorAppender());
  49.   }
  50.  
  51.   /**
  52.    * {@inheritDoc}
  53.    */
  54.   @Override
  55.   public String getAjaxIndicatorMarkupId() {
  56.     return ajaxIndicator.getMarkupId();
  57.   }
  58.  
  59.   private Component getButton() {
  60.     final Component button = new AjaxLink<Void>("button") {
  61.       @Override
  62.       public void onClick(final AjaxRequestTarget target) {
  63.         target.addComponent(getStatus());
  64.         if (isProcessRunning()) {
  65.           target.appendJavascript("alert('still in progress')");
  66.         } else {
  67.           status.add(getSelfUpdatingBehavior());
  68.           everStarted = true;
  69.           future = start();
  70.         }
  71.       }
  72.     };
  73.     button.add(new AttributeModifier("value", getButtonText()));
  74.     return button;
  75.   }
  76.  
  77.   protected IModel<String> getButtonText() {
  78.       return Model.of("Execute");
  79.   }
  80.  
  81.   /**
  82.    * Start execution, immediately returning a future.
  83.    */
  84.   protected abstract Future<?> start();
  85.  
  86.   /**
  87.    * @return if the process is still running.
  88.    */
  89.   private boolean isProcessRunning() {
  90.     return future != null && !future.isDone();
  91.   }
  92.  
  93.   /**
  94.    * @return component displaying the current status of the process.
  95.    */
  96.   private Component getStatus() {
  97.     if (status == null) {
  98.       final IModel<String> model = new AbstractReadOnlyModel<String>() {
  99.         @Override
  100.         public String getObject() {
  101.           return isProcessRunning() ? "in progress" : "complete";
  102.         }
  103.       };
  104.       status = new Label("status", model) {
  105.         @Override
  106.         public boolean isVisible() {
  107.           return everStarted;
  108.         }
  109.       };
  110.       status.setOutputMarkupPlaceholderTag(true);
  111.       status.setOutputMarkupId(true);
  112.     }
  113.     return status;
  114.   }
  115.  
  116.   private AjaxSelfUpdatingTimerBehavior getSelfUpdatingBehavior() {
  117.     return new AjaxSelfUpdatingTimerBehavior(Duration.seconds(2)) {
  118.       @Override
  119.       protected void onPostProcessTarget(final AjaxRequestTarget target) {
  120.         if (!isProcessRunning()) {
  121.           stop();
  122.           status.remove(this);
  123.         }
  124.       }
  125.     };
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement