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

  1. <refentry id="{@id}">
  2.  <refnamediv>
  3.   <refname>Using Progress Observers</refname>
  4.   <refpurpose>introduction to HTML_Progress observer mechanism</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.    The {@link HTML_Progress_Observer} class provides an implementation of the observer pattern.
  21.    In the content of the HTML_Progress package, they provide a mechanism which you can
  22.    examine each important event as it is happened. This allows the implementation of
  23.    special behavior based on the value of the progress element.
  24.   </para>
  25.   <para>
  26.    Creating a progress observer involves implementing a subclass of the 
  27.    {@link HTML_Progress_Observer} class. The subclass must override the base class's 
  28.    {@link HTML_Progress_Observer::notify()} method. This method is passed a hash containing
  29.    event name and value.
  30.   </para>
  31.   <para>
  32.    At this time, no concrete observer implementations are distributed with the HTML_Progress
  33.    package. But you could find some good examples named {@tutorial observer.pkg#examples.standard},
  34.    {@tutorial observer.pkg#examples.simple}, {@tutorial observer.pkg#examples.complex} available
  35.    in examples subdirectory of bundle distribution.
  36.   </para>
  37.   <para>
  38.    Now we will learn how to catch and manage specific events and respond to them in a specific way.
  39.   </para>
  40.  </refsect1>
  41.  <refsect1 id="{@id using}">
  42.   <title>Using Progress Observers </title>
  43.   <para>
  44.    If you <important>do not override the base class</important>, 
  45.    as in {@tutorial observer.pkg#examples.standard} example, the default behavior is to write 
  46.    events (setminimum, setmaximum, setvalue) 
  47.    into a file <emphasis>progress_observer.log</emphasis> in current directory.
  48.   </para>
  49.   <para>
  50.    If you decide to <important>override the base class with <emphasis>MyObserver</emphasis> 
  51.    object</important>, as in {@tutorial observer.pkg#examples.simple} example, 
  52.    the new behavior could be to write events (setminimum, setmaximum, setvalue) 
  53.    into a file <emphasis>observer_complex.log</emphasis> 
  54.    ({@tutorial observer.pkg#examples.simple line 40}),
  55.    or just send it to browser screen ({@tutorial observer.pkg#examples.simple line 39}).
  56.   </para>
  57.   <para>
  58.    In the last example, {@tutorial observer.pkg#examples.complex}, <important>the base class 
  59.    was overridden by <emphasis>Bar1Observer</emphasis> object</important>
  60.    ({@tutorial observer.pkg#examples.complex line 6}), to allow a double vertical bar synchronized.
  61.    <para><graphic fileref="../media/screenshots/observer_complex.png"></graphic></para>
  62.   </para>
  63.   <para>
  64.    But all these 3 examples won't work if we do not attach the observer object to the progress bar,
  65.    as a listener. To do so, use {@link HTML_Progress::addListener()} method. If you want to detach
  66.    the observer object, use {@link HTML_Progress::removeListener()} method. And if you want to
  67.    check what listeners are attached to progress bar, then use {@link HTML_Progress::getListeners()}
  68.    method.
  69.   </para>
  70.  </refsect1>
  71.  <refsect1 id="{@id examples}">
  72.   <title>Examples </title>
  73.   <refsect2 id="{@id standard}">
  74.    <title>observer_standard.php</title>
  75.    <para>
  76.     <programlisting role="php">
  77.     <![CDATA[
  78. <?php 
  79. require_once ('HTML/Progress.php');
  80. require_once ('HTML/Progress/observer.php');
  81.  
  82. // 1. Creates ProgressBar
  83. $bar = new HTML_Progress();
  84. $bar->setBorderPainted(true);
  85. $bar->setIncrement(10);
  86.  
  87. // 2. Creates and attach a listener 
  88. $observer = new HTML_Progress_Observer();
  89.  
  90. $ok = $bar->addListener($observer);
  91. if (!$ok) {
  92.     die ("Cannot add a valid listener to progress bar !");
  93. }
  94.  
  95. // 3. Changes look-and-feel of ProgressBar
  96. $ui =& $bar->getUI();
  97. $ui->setBorderAttributes('width = 2');   // border: 2px, solid, #000000
  98. $ui->setComment('Standard Observer ProgressBar example');
  99.  
  100. ?>
  101. <!DOCTYPE html
  102.     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  103.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  104.  
  105. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  106. <head>
  107. <title>Standard Observer ProgressBar example</title>
  108. <style type="text/css">
  109. <!--
  110. <?php echo $bar->getStyle(); ?>
  111.  
  112. body {
  113.     background-color: #FFFFFF;
  114.     color: #000000;
  115.     font-family: Verdana, Arial;
  116. }
  117.  
  118. a:visited, a:active, a:link {
  119.     color: navy;
  120. }
  121. // -->
  122. </style>
  123. <script type="text/javascript">
  124. <!--
  125. <?php echo $ui->getScript(); ?>
  126. //-->
  127. </script>
  128. </head>
  129. <body>
  130.  
  131. <?php 
  132. echo $bar->toHTML(); 
  133.  
  134. do {
  135.     $bar->display();
  136.     if ($bar->getPercentComplete() == 1) {
  137.         break;   // the progress bar has reached 100%
  138.     }
  139.     $bar->incValue();
  140. } while(1);
  141. ?>
  142.  
  143. </body>
  144. </html>
  145.     ]]>
  146.     </programlisting>
  147.    </para>
  148.   </refsect2>
  149.   <refsect2 id="{@id simple}">
  150.    <title>observer_simple.php</title>
  151.    <para>
  152.     <programlisting role="php">
  153.     <![CDATA[
  154. <?php 
  155. require_once ('HTML/Progress.php');
  156. require_once ('HTML/Progress/observer.php');
  157.  
  158. // 1. Defines ProgressBar observer
  159. class MyObserver extends HTML_Progress_Observer
  160. {
  161.     var $_console;
  162.     var $_out;
  163.     
  164.     function MyObserver($out)
  165.     {
  166.         $this->_console = '.' . DIRECTORY_SEPARATOR . 'observer_complex.log';
  167.         $this->HTML_Progress_Observer();
  168.         $this->_out = strtolower($out);
  169.     }
  170.  
  171.     function notify($event)
  172.     {
  173.         if (is_array($event)) {
  174.             $log = isset($event['log']) ? $event['log'] : "undefined event id.";
  175.             $val = isset($event['value']) ? $event['value'] : "unknown value";
  176.             $msg = "$log = $val";
  177.         } else {
  178.             $msg = $event;
  179.         }
  180.         if ($this->_out == 'file') {
  181.             error_log("$msg \n", 3, $this->_console);
  182.         } else {
  183.             print ("$msg <br />\n");
  184.     }
  185.     }
  186. }
  187.  
  188. // 2. Creates ProgressBar
  189. $bar = new HTML_Progress();
  190. $bar->setIncrement(5);
  191.  
  192. // 3. Creates and attach a listener 
  193. $observer = new MyObserver('screen');
  194. //$observer = new MyObserver('file');
  195.  
  196. $ok = $bar->addListener($observer);
  197. if (!$ok) {
  198.     die ("Cannot add a valid listener to progress bar !");
  199. }
  200.  
  201. // 4. Changes look-and-feel of ProgressBar
  202. $ui = $bar->getUI();
  203. $ui->setStringAttributes('color = red');
  204. $ui->setComment('Simple Observer ProgressBar example');
  205.  
  206. ?>
  207. <!DOCTYPE html
  208.     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  209.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  210.  
  211. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  212. <head>
  213. <title>Simple Observer ProgressBar example</title>
  214. <style type="text/css">
  215. <!--
  216. <?php echo $bar->getStyle(); ?>
  217. // -->
  218. </style>
  219. <script type="text/javascript">
  220. <!--
  221. <?php echo $ui->getScript(); ?>
  222. //-->
  223. </script>
  224. </head>
  225. <body>
  226.  
  227. <?php 
  228. echo $bar->toHTML(); 
  229.  
  230. do {
  231.     $bar->display();
  232.     if ($bar->getPercentComplete() == 1) {
  233.         break;   // the progress bar has reached 100%
  234.     }
  235.     $bar->incValue();
  236. } while(1);
  237. ?>
  238.  
  239. </body>
  240. </html>
  241.     ]]>
  242.     </programlisting>
  243.    </para>
  244.   </refsect2>
  245.   <refsect2 id="{@id complex}">
  246.    <title>observer_complex.php</title>
  247.    <para>
  248.     <programlisting role="php">
  249.     <![CDATA[
  250. <?php
  251. require_once ('HTML/Progress.php');
  252. require_once ('HTML/Progress/observer.php');
  253.  
  254. // 1. Defines ProgressBar observer
  255. class Bar1Observer extends HTML_Progress_Observer
  256. {
  257.     function Bar1Observer()
  258.     {
  259.         $this->HTML_Progress_Observer();
  260.     }
  261.  
  262.     function notify($event)
  263.     {
  264.         global $bar2;
  265.         
  266.         if (is_array($event)) {
  267.             $log = isset($event['log']) ? $event['log'] : "undefined event id.";
  268.             $val = isset($event['value']) ? $event['value'] : "unknown value";
  269.  
  270.             switch (strtolower($log)) {
  271.              case 'incvalue':
  272.                  // if you want to do special on each step of progress bar1; it's here !!!
  273.                  break;
  274.              case 'setvalue':
  275.                  if ($val == 0) {
  276.                      // updates $bar2 because $bar1 has completed a full loop
  277.                      $bar2->incValue();
  278.                      $bar2->display();
  279.                  }
  280.              default:
  281.             }
  282.         }
  283.     }
  284. }
  285.  
  286. // 2. Creates ProgressBar
  287. $bar1 = new HTML_Progress(HTML_PROGRESS_BAR_VERTICAL);
  288. $bar1->setIncrement(10);
  289. $bar1->setIdent('PB1');
  290.  
  291. $bar2 = new HTML_Progress(HTML_PROGRESS_BAR_VERTICAL);
  292. $bar2->setIncrement(25);
  293. $bar2->setIdent('PB2');
  294. $bar2->setBorderPainted(true);
  295.  
  296. // 3. Creates and attach a listener
  297. $observer = new Bar1Observer();
  298.  
  299. $ok = $bar1->addListener($observer);
  300. if (!$ok) {
  301.     die ("Cannot add a valid listener to progress bar !");
  302. }
  303.  
  304. // 4. Changes look-and-feel of ProgressBar
  305. $ui1 =& $bar1->getUI();
  306. $ui1->setComment('Complex Observer ProgressBar example');
  307. $ui1->setTabOffset(1);
  308. $ui1->setProgressAttributes(array(
  309.     'background-color' => '#e0e0e0'
  310.         ));        
  311. $ui1->setStringAttributes(array(
  312.     'valign' => 'left',
  313.     'color'  => 'red',
  314.     'background-color' => 'lightblue'
  315.     ));
  316.  
  317. $ui2 =& $bar2->getUI();
  318. $ui2->setTabOffset(1);
  319. $ui2->setBorderAttributes(array(
  320.         'width' => 1,
  321.         'style' => 'solid',
  322.         'color' => 'navy'
  323.         ));
  324. $ui2->setCellAttributes(array(
  325.         'active-color' => '#3874B4',
  326.         'inactive-color' => '#EEEECC'
  327.         ));        
  328. $ui2->setStringAttributes(array(
  329.         'width'  => '100',
  330.     'align'  => 'center',
  331.     'valign' => 'right',
  332.     'color'  => 'yellow',
  333.     'background-color' => 'lightblue'
  334.     ));
  335. ?>
  336. <!DOCTYPE html
  337.     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  338.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  339.  
  340. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  341. <head>
  342. <title>Complex Observer ProgressBar example</title>
  343. <style type="text/css">
  344. <!--
  345. <?php
  346. echo $bar1->getStyle();
  347. echo $bar2->getStyle();
  348. ?>
  349. table.container {
  350.     background-color: lightblue;
  351.     border: 2;
  352.     border-color: navy;
  353.     border-style: dashed;
  354.     cell-spacing: 4;
  355.     cell-padding: 8;
  356.     width: 50%;
  357. }
  358. // -->
  359. </style>
  360. <script type="text/javascript">
  361. <!--
  362. <?php echo $ui1->getScript(); ?>
  363. //-->
  364. </script>
  365. </head>
  366. <body>
  367.  
  368. <table class="container">
  369. <tr>
  370.     <td width="25%" align="center">
  371. <?php echo $bar1->toHTML(); ?>
  372.     </td>
  373.     <td width="25%" align="center">
  374. <?php echo $bar2->toHTML(); ?>
  375.     </td>
  376. </tr>
  377. </table>
  378.  
  379. <?php
  380. do {
  381.     $bar1->display();
  382.     if ($bar1->getPercentComplete() == 1) {
  383.         $bar1->setValue(0);  // the 1st progress bar has reached 100%, do a new loop
  384.     } else {
  385.         $bar1->incValue();   // updates 1st progress bar
  386.     }
  387. } while($bar2->getPercentComplete() < 1);
  388. ?>
  389.  
  390. </body>
  391. </html>
  392.     ]]>
  393.     </programlisting>
  394.    </para>
  395.   </refsect2>
  396.  </refsect1>
  397. </refentry>
  398.  
  399.