home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / indeterminate.pkg < prev    next >
Encoding:
Text File  |  2004-03-24  |  9.8 KB  |  288 lines

  1. <refentry id="{@id}">
  2.  <refnamediv>
  3.   <refname>Using Indeterminate Mode</refname>
  4.   <refpurpose>how to animate a progress bar to show unknown length-task activity</refpurpose>
  5.  </refnamediv>
  6.  <refsynopsisdiv>
  7.   <refsynopsisdivinfo>
  8.    <author>
  9.     by Laurent Laville
  10.     <authorblurb>{@link mailto:pear@laurent-laville.org}</authorblurb>
  11.    </author>
  12.    <copyright>November 2003, Laurent Laville</copyright>
  13.    <releaseinfo>HTML_Progress 1.0+</releaseinfo>
  14.   </refsynopsisdivinfo>
  15.  </refsynopsisdiv>
  16.  {@toc}
  17.  <refsect1 id="{@id intro}">
  18.   <title>Introduction</title>
  19.   <para>
  20.    Sometimes you can't immediately determine the length of a long-running task, 
  21.    or the task might stay stuck at the same state of completion for a long time.
  22.    You can show work without measurable progress by putting the progress bar in 
  23.    indeterminate mode. A progress bar in indeterminate mode displays animation 
  24.    to indicate that work is occurring. As soon as the progress bar can display 
  25.    more meaningful information, you should switch it back into its default, 
  26.    determinate mode. The look and feel of indeterminate progress bars look like this: 
  27.    <para><graphic fileref="../media/screenshots/indeterminate.png"></graphic></para>
  28.   </para>
  29.  </refsect1>
  30.  <refsect1 id="{@id useit}">
  31.   <title>Using Indeterminate Mode</title>
  32.   <para>
  33.    In example that follow, we have created a Task Class that extends the default monitoring
  34.    {@link HTML_Progress_Monitor} class.   
  35.   </para>
  36.   <para>
  37.    The most important code additions are calls to the {@link HTML_Progress::setIndeterminate()}
  38.    method. When the user clicks the Start button, <important>setIndeterminate(true)</important>
  39.    is invoked ({@tutorial indeterminate.pkg#useit.ex-listing line 143}) so that the user can tell 
  40.    that the task as started, even before any meaningful information about the task's progress 
  41.    can be conveyed. Once the progress bar has some concrete status to display, a call to 
  42.    <important>setIndeterminate(false)</important> ({@tutorial indeterminate.pkg#useit.ex-listing line 76})
  43.    switches the progress bar back into its normal mode. The {@link HTML_Progress::isIndeterminate()}
  44.    method ({@tutorial indeterminate.pkg#useit.ex-listing line 75}) is used to test the 
  45.    progress bar's current state.
  46.   </para>
  47.   <para>
  48.    The other changes in the code are related to string display. A progress bar that displays 
  49.    a string is likely to be taller than one that doesn't, and, as the demo designers, 
  50.    we've arbitarily decided that this progress bar should display a string only when 
  51.    it's in the default, determinate mode. However, we want to avoid the layout ugliness 
  52.    that might result if the progress bar changed height when it changed modes. 
  53.    Thus, the code leaves in the call to {@link HTML_Progress::setStringPainted()} method
  54.    with <emphasis>true</emphasis> value ({@tutorial indeterminate.pkg#useit.ex-listing line 39}),
  55.    but adds a call to {@link HTML_Progress::setString()} method
  56.    ({@tutorial indeterminate.pkg#useit.ex-listing line 40}) with <emphasis>empty string</emphasis>
  57.    value, so that no text will be displayed. Later, when the progress bar switches from 
  58.    indeterminate to determinate mode, invoking <important>setString(null)</important> 
  59.    ({@tutorial indeterminate.pkg#useit.ex-listing line 77}) makes the progress bar display 
  60.    its default string.
  61.   </para>
  62.   <para>
  63.    One change we did not make was removing the call to {@link HTML_Progress::setValue()} from
  64.    the timer's action handler (notify method). The call doesn't do any harm because an 
  65.    indeterminate progress bar doesn't use its value property, except perharps to display it in
  66.    the status string. In fact, keeping the progress bar's data as up-to-date as possible is a
  67.    good practice, since some look and feels might not support indeterminate mode.
  68.   </para> 
  69.   <refsect2 id="{@id ex-listing}">
  70.    <title>Full example listing</title>
  71.    <programlisting role="php">
  72.    <![CDATA[
  73. <?php
  74. require_once ('HTML/Progress/monitor.php');
  75.  
  76. class Task extends HTML_Progress_Monitor
  77. {
  78.     var $_current;
  79.     
  80.     function Task()
  81.     {
  82.         $this->_current = 0;
  83.         $this->_id = md5(microtime());
  84.  
  85.         $this->_form = new HTML_QuickForm('ProgressBarDialog');
  86.  
  87.         $renderer =& $this->_form->defaultRenderer();
  88.         $renderer->setFormTemplate('
  89.             <table width="450" border="0" cellpadding="3" cellspacing="2" bgcolor="#CCCC99">
  90.             <form{attributes}>{content}
  91.             </form>
  92.             </table>
  93.             ');
  94.         $renderer->setHeaderTemplate('
  95.             <tr>
  96.         <td style="white-space:nowrap;background:#996;color:#ffc;" align="left" colspan="2"><b>{header}</b></td>
  97.         </tr>
  98.         ');
  99.         
  100.         $this->_form->addElement('header', 'windowsname', 'Progress...');
  101.         $this->_form->addElement('static', 'progress');
  102.         $this->_form->addElement('static', 'status');
  103.  
  104.         $buttons[] = &HTML_QuickForm::createElement('submit', 'start',  'Start',  'style="width:80px;"');
  105.         $buttons[] = &HTML_QuickForm::createElement('submit', 'cancel', 'Cancel', 'style="width:80px;"');
  106.         $this->_form->addGroup($buttons);
  107.  
  108.         
  109.         $this->_progress = new HTML_Progress();
  110.         $this->_progress->setIncrement(10);
  111.         $this->_progress->setStringPainted(true);     // get space for the string
  112.         $this->_progress->setString("");              // but don't paint it
  113.  
  114.         $ui = & $this->_progress->getUI();
  115.         $ui->setProgressAttributes(array(
  116.             'background-color' => '#e0e0e0'
  117.                 ));        
  118.         $ui->setStringAttributes(array(
  119.                 'color'  => '#996',
  120.             'background-color' => '#CCCC99'
  121.                 ));        
  122.         $ui->setCellAttributes(array(
  123.                 'active-color' => '#996'
  124.                 ));
  125.  
  126.         $bar =& $this->_form->getElement('progress');
  127.         $bar->setText( $this->_progress->toHtml() );
  128.  
  129.         $str =& $this->_form->getElement('status');
  130.         $str->setText('<div id="status" style="color:#000000; font-size:10px;"> </div>');
  131.  
  132.         $this->_progress->addListener($this);
  133.     }
  134.  
  135.     function notify($event)
  136.     {
  137.         if (is_array($event)) {
  138.             $log = strtolower($event['log']);
  139.             $val = $event['value'];
  140.             
  141.             switch (strtolower($log)) {
  142.              case 'incvalue':
  143.              case 'setvalue':
  144.                  $this->_current = $this->getCurrent() + 16;
  145.                  $s = $this->getMessage();
  146.                  if (!is_null($s)) {
  147.                      if ($this->_progress->isIndeterminate()) {
  148.                          $this->_progress->setIndeterminate(false);
  149.                          $this->_progress->setString(null);      // display % string
  150.                          $this->_progress->setValue(0);
  151.                      }
  152.                      if ($this->isDone()) {
  153.                          $this->_progress->removeListener($this);
  154.                          $this->_progress->setString("");       // hide % string
  155.                      }
  156.                  }
  157.                  $this->_progress->display();
  158.                  
  159.                  if ($this->_progress->getPercentComplete() == 1) {
  160.                      if ($this->_progress->isIndeterminate()) {
  161.                          $this->_progress->setValue(0);
  162.                      }
  163.                  } else {
  164.                      $this->_progress->incValue();
  165.                  }
  166.                  break;
  167.              default:
  168.             }
  169.         }
  170.     }
  171.  
  172.     function getCurrent()
  173.     {
  174.         return $this->_current;
  175.     }
  176.  
  177.     function getMessage()
  178.     {
  179.         $c = $this->getCurrent();
  180.         $s = "completed $c out of 416";
  181.  
  182.         if (function_exists('ob_get_clean')) {
  183.             $status  = ob_get_clean();      // use for PHP 4.3+
  184.         } else {
  185.             $status  = ob_get_contents();   // use for PHP 4.2+
  186.             ob_end_clean();
  187.         }
  188.         $status = '<script type="text/javascript">self.setStatus("'.$s.'"); </script>';
  189.         echo $status;
  190.         ob_start();
  191.         
  192.         if ($c >= 240 ) {
  193.             return $s;
  194.         } else {
  195.             return null;
  196.         }
  197.     }
  198.  
  199.     function isDone()
  200.     {
  201.         return ( ($this->_progress->getPercentComplete() == 1) &&
  202.                  ($this->_progress->isIndeterminate() == false) );
  203.     }
  204.  
  205.  
  206.     function isStarted()
  207.     {
  208.         $action = $this->_form->getSubmitValues();
  209.  
  210.         if (isset($action['start'])) {
  211.             return true;
  212.         }
  213.  
  214.         return false;
  215.     }
  216.  
  217.     function run()
  218.     {
  219.         if ($this->isStarted()) {
  220.             $this->_progress->setIndeterminate(true);
  221.             $this->_progress->incValue();
  222.             
  223.         } else {
  224.             $abort = $this->isCanceled();
  225.         }
  226.     }
  227.  
  228. }
  229.  
  230. $task = new Task();
  231.  
  232. ?>
  233. <!DOCTYPE html
  234.     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  235.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  236.  
  237. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  238. <head>
  239. <title>Indeterminate Mode Progress example</title>
  240. <style type="text/css">
  241. <!--
  242. <?php echo $task->getStyle(); ?>
  243.  
  244. body {
  245.     background-color: #444444;
  246.     color: #EEEEEE;
  247.     font-family: Verdana, Arial;
  248. }
  249.  
  250. a:visited, a:active, a:link {
  251.     color: yellow;
  252. }
  253. // -->
  254. </style>
  255. <script type="text/javascript">
  256. <!--
  257. <?php echo $task->getScript(); ?>
  258.  
  259. function setStatus(pString)
  260. {
  261.         if (isDom)
  262.             prog = document.getElementById('status');
  263.         if (isIE)
  264.             prog = document.all['status'];
  265.         if (isNS4)
  266.             prog = document.layers['status'];
  267.     if (prog != null) 
  268.         prog.innerHTML = pString;
  269. }
  270. //-->
  271. </script>
  272. </head>
  273. <body>
  274.  
  275. <?php 
  276. echo $task->toHtml(); 
  277.  
  278. $task->run();
  279. ?>
  280.  
  281. </body>
  282. </html>
  283.    ]]>
  284.    </programlisting>
  285.   </refsect2>
  286.  </refsect1>
  287. </refentry>
  288.