sourceforge > tacos
 

Refresh

Creates javascript refresh requests, similar to AjaxDirectLink, only this component creates the javascript without a link. That means that the javascript will literally be printed on the response page and executed immediately. The ProgressBar is the only component using it currently.

See also: AjaxDirectLink, Live Demo

Parameters

Name Type Direction Required Default Description
updateComponents List (or comma-delimited String) in no   List of component id strings to refresh after invoking listener. This causes the contents of any html element on the page with a matching id in this list to be replaced, if the specified element is rendered in the return response.
updateObject String in no   If specified, the string passed in will be used to build a javascript statement which will call updateObject.ajaxUpdate(element, responseElement, elementId).
direct boolean in no false Specifies whether or not components should be invoked directly. This will cause the engine service to only call renderComponent on the targeted components of this request, whereas normal behaviour is to render the whole page and only return the portions of the response that were requested. Use at your own risk, components contained by lists, like Foreach and For will likely not be directly updateable.

Body: discarded

Informal parameters: not allowed

Reserved parameters: href, onclick

Examples

This example is a copy of the ProgressBar example, look for the Refresh component insertion in the html template.

HTML template

          
<!-- Task Progress invoker -->
<span jwcid="@Any" id="linkPart" >
<span jwcid="@If" condition="ognl:!importing" >
  <div class="progressStart">
        <a jwcid="progressLink" >Start</a>
    </div>
</span>
<span jwcid="@If" condition="ognl:importing" >
  <div class="progressStart">
        <a jwcid="progressCancel" >Cancel</a>
    </div>
</span>
</span>
<!-- End Task Progress invoker -->

<!-- Task progress -->
<span jwcid="progress" />
<!-- End Task progress -->

<script type="text/javascript">
    dojo.require("dojo.event.*");
    dojo.require("dojo.animation.*");
    dojo.require("dojo.math.*");

    var linkString = "<span jwcid="@Insert" raw="true" value="ognl:components.progress.linkString" />";
    var startObject = new Object();
    startObject.responseComplete = function(responseElements) {
        if (!document.progAnim) {
            var progNode = document.getElementById("progress");
            dojo.fx.html.fadeShow(progNode, 1000);
            document.progAnim = new dojo.animation.Animation(
                    new dojo.math.curves.Line([1],[20]),
                    2000, //2 second pauses
                    0, //no acceleration
                    -1 //repeat forever
                    );
            dojo.event.connect(document.progAnim, "onAnimate", function(e) {
                    tacos.defaultLinkAction({url: linkString, processScripts:true, useSync: true});
            });
            document.progAnim.play(true);
        }
    }

    var cancelObject = new Object();
    cancelObject.ajaxUpdate = function(ajaxElement, responseElement, elementId) {
        if (document.progAnim) {
            document.progAnim.stop();
            document.progAnim = null;
            var progNode = document.getElementById("progress");
            dojo.fx.html.fadeHide(progNode, 1000);
            <span jwcid="@tacos:Refresh" updateComponents="ognl:{'linkPart'}" />
        }
    }

    var progressComplete = new Object();
    progressComplete.progressFinished = function(elementId) {
        if (document.progAnim) {
            document.progAnim.stop();
            document.progAnim = null;
            var progNode = document.getElementById("progress");
            dojo.fx.html.fadeHide(progNode, 1000);
            <span jwcid="@tacos:Refresh" updateComponents="ognl:{'linkPart'}" />
        }
    }
</script>
          
        

Page specification

          
<page-specification class="net.sf.tacos.demo.pages.ajax.ProgressBarExample">

  <description>Partial ProgressBar example</description>

  <property name="progressWorker" persist="session" />
  <property name="startTime" persist="session" />

    <component id="progressLink" type="tacos:AjaxDirectLink">
    <binding name="listener" value="listener:startTask"/>
    <binding name="updateComponents" value="ognl:{'linkPart'}"/>
    <binding name="updateObject" value="literal:startObject" />
  </component>

    <component id="progressCancel" type="tacos:AjaxDirectLink">
    <binding name="listener" value="ognl:components.progress.listeners.cancelTask"/>
    <binding name="updateComponents" value="ognl:{'linkPart'}"/>
    <binding name="updateObject" value="literal:cancelObject" />
  </component>

  <component id="progress" type="tacos:ProgressBar">
        <binding name="reloadseconds" value="1" />
        <binding name="worker" value="ognl:progressWorker" />
        <binding name="id" value="literal:progress" />
        <binding name="onCompleteObject" value="literal:progressComplete" />
    </component>

</page-specification>
          
        

Java sources

          
public abstract class ProgressCounter extends BasePage implements IDirect {

  /** Logger */
  private static final Log log = LogFactory.getLog(ProgressCounter.class);

  /** Worker doing import */
  public abstract ProgressWorkThread getProgressWorker();
  /** sets worker doing import */
  public abstract void setProgressWorker(ProgressWorkThread worker);
  /** Set time - in milliseconds - that worker started */
  public abstract void setStartTime(long time);
  /** Gets start time */
  public abstract long getStartTime();

  /**
   *
   * @return True if currently importing a casebase file.
   */
  public boolean isImporting()
  {
      return getProgressWorker() != null && !getProgressWorker().isComplete();
  }

  /**
   * Calculates amount of time left, in minutes, for task.
   * @return
   */
  public String getEstimatedTimeLeft()
  {
      if (getProgressWorker() == null)
          return "0 min";

      //Get values so they don't change on us
      double currentProgress = getProgressWorker().getCurrentProgress();
      double totalProgress = getProgressWorker().getTotalProgress();

      double avgDuration =
          (System.currentTimeMillis() - getStartTime()) / currentProgress;
      double remainingDuration =
          (totalProgress - getProgressWorker().getCurrentProgress()) * avgDuration;
      return DurationFormatUtils
      .formatDurationHMS(Math.round(remainingDuration));
  }

  /**
   * {@inheritDoc}
   */
  public void trigger(IRequestCycle cycle)
  {

  }

  /**
   * Starts the progress task.
   * @param cycle
   * @throws Exception on error
   */
  public void startTask(IRequestCycle cycle)
  throws Exception
  {
      if (isImporting())
          return;
      log.debug("startTask");

      setProgressWorker(null);

      //Start task
      ProgressWorkThread worker = new ProgressWorkThread();
      setProgressWorker(worker);
      worker.start();
      setStartTime(System.currentTimeMillis());

  }
}
          
        
          
public class ProgressWorkThread extends Thread
     implements ProgressWorker, Serializable {

  /** serialuid */
  private static final long serialVersionUID = 4915291816082490796L;
  /** logger */
  private static final Log log = LogFactory.getLog(
      ProgressWorkThread.class);

  /** Total progress we are working on */
  private static final double TOTAL = 100;

  /** Current progress */
  private double progress = 0;

  /* Flag for if we are running or not */
  private boolean running = true;

  /** Default constructor */
  public ProgressWorkThread()
  {
  }

  /**
   * {@inheritDoc}
   */
  public double getTotalProgress() {
      return TOTAL;
  }

  /**
   * {@inheritDoc}
   */
  public double getCurrentProgress() {
      return progress;
  }

  /**
   * {@inheritDoc}
   */
  public String getCurrentStatus() {
      return "Processing count of " + progress;
  }

  /**
   * {@inheritDoc}
   */
  public boolean isComplete() {
      return !running;
  }

  /**
   * {@inheritDoc}
   */
  public void cancelTask() {
      if (!running)
          return;

      progress = TOTAL;
      running = false;
      try { this.interrupt(); } catch (Exception et) { }
  }

  /**
   * {@inheritDoc}
   */
  public void run() {
      while (progress < TOTAL) {
          log.debug("progressWorker() " + progress);
          progress += 1;
          try { sleep(100); } catch (Exception e) { }
      }
      running = false;
  }
}