HTML_Progress
[ class tree: HTML_Progress ] [ index: HTML_Progress ] [ all elements ]
Prev Next
Observer pattern
implementation of the observer pattern

Table of Contents

Introduction

The HTML_Progress_Observer class provides an implementation of the observer pattern. In the content of the HTML_Progress package, they provide a mechanism which you can examine each important event as it is happened. This allows the implementation of special behaviour based on the value of the progress element.

Basic concept

Creating a progress observer involves implementing a subclass of the HTML_Progress_Observer class. The subclass must override the base class HTML_Progress_Observer::notify method. This method is passed a hash containing event name and progress value.

Now we will learn how to catch and manage specific events and respond to them in a specific way.

Using Progress Observers

Basic Observer

If you creates an instance HTML_Progress_Observer class without subclass, then default behaviour is to to write events (setminimum, setmaximum, setvalue) into a file progress_observer.log in current directory. This file contains for each event observed a PHP serialize (http://www.php.net/manual/en/function.serialize.php) output result.

Basic Observer

  1. <?php
  2. require_once 'HTML/Progress.php';
  3. require_once 'HTML/Progress/observer.php';
  4.  
  5.  
  6. $bar = new HTML_Progress();
  7. $bar->setAnimSpeed(100);
  8. $bar->setBorderPainted(true);
  9. $bar->setIncrement(10);
  10.  
  11. $observer = new HTML_Progress_Observer();
  12.  
  13. $ok = $bar->addListener($observer);
  14. if (!$ok) {
  15. die ("Cannot add a valid listener to progress bar !");
  16. }
  17.  
  18. $ui =& $bar->getUI();
  19. // border: 2px, solid, #000000
  20. $ui->setBorderAttributes('width = 2');
  21.  
  22. $ui->setComment('Standard Observer ProgressBar example');
  23.  
  24. ?>
  25. <!DOCTYPE html
  26. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  27. "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  28.  
  29. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  30. <head>
  31. <title>Standard Observer </title>
  32. <style type="text/css">
  33. <!--
  34. <?php echo $bar->getStyle(); ?>
  35.  
  36. body {
  37. background-color: #FFFFFF;
  38. color: #000000;
  39. font-family: Verdana, Arial;
  40. }
  41. // -->
  42. </style>
  43. <script type="text/javascript">
  44. <!--
  45. <?php echo $bar->getScript(); ?>
  46. //-->
  47. </script>
  48. </head>
  49. <body>
  50.  
  51. <?php
  52. echo $bar->toHTML();
  53. $bar->run();
  54. ?>
  55.  
  56. </body>
  57. </html>

Example above produces such results:

a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:10;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:20;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:30;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:40;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:50;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:60;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:70;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:80;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:90;} 
a:2:{s:3:"log";s:8:"incValue";s:5:"value";i:100;}


Complex Observer

In this example, we will produces a dual progress meter which will simulate a software installation. One progress bar (left side) for each file copy, and one progress bar (right side) for global progress. On each left bar loop (100%), right bar will be increased by 25%. Example below will display something like :

Complex Observer

  1. <?php
  2. require_once 'HTML/Progress.php';
  3. require_once 'HTML/Progress/observer.php';
  4.  
  5. class Bar1Observer extends HTML_Progress_Observer
  6. {
  7. function Bar1Observer()
  8. {
  9. $this->HTML_Progress_Observer();
  10. }
  11.  
  12. function notify($event)
  13. {
  14. global $bar1, $bar2;
  15. if (is_array($event)) {
  16. $log = $event['log'];
  17. $val = $event['value'];
  18.  
  19. switch (strtolower($log)) {
  20. case 'incvalue':
  21. $bar1->sleep(); // process to do on PB1
  22. break;
  23. case 'setvalue':
  24. if ($val == 0) {
  25. $bar2->incValue();
  26. $bar2->display();
  27. }
  28. default:
  29. }
  30. }
  31. }
  32. }
  33.  
  34. $bar1 = new HTML_Progress(HTML_PROGRESS_BAR_VERTICAL);
  35. $bar1->setAnimSpeed(100);
  36. $bar1->setIncrement(10);
  37. $bar1->setIdent('PB1');
  38.  
  39. $bar2 = new HTML_Progress(HTML_PROGRESS_BAR_VERTICAL);
  40. $bar2->setAnimSpeed(100);
  41. $bar2->setIncrement(25);
  42. $bar2->setIdent('PB2');
  43. $bar2->setBorderPainted(true);
  44.  
  45. $observer = new Bar1Observer();
  46.  
  47. $ok = $bar1->addListener($observer);
  48. if (!$ok) {
  49. die ("Cannot add a valid listener to progress bar !");
  50. }
  51.  
  52. $ui1 =& $bar1->getUI();
  53. $ui1->setComment('Complex Observer example');
  54. $ui1->setTabOffset(1);
  55. $ui1->setProgressAttributes(array(
  56. 'background-color' => '#e0e0e0'
  57. ));
  58. $ui1->setStringAttributes(array(
  59. 'valign' => 'left',
  60. 'color' => 'red',
  61. 'background-color' => 'lightblue'
  62. ));
  63.  
  64. $ui2 =& $bar2->getUI();
  65. $ui2->setTabOffset(1);
  66. $ui2->setBorderAttributes(array(
  67. 'width' => 1,
  68. 'style' => 'solid',
  69. 'color' => 'navy'
  70. ));
  71. $ui2->setCellAttributes(array(
  72. 'active-color' => '#3874B4',
  73. 'inactive-color' => '#EEEECC'
  74. ));
  75. $ui2->setStringAttributes(array(
  76. 'width' => '100',
  77. 'align' => 'center',
  78. 'valign' => 'right',
  79. 'color' => 'yellow',
  80. 'background-color' => 'lightblue'
  81. ));
  82. ?>
  83. <!DOCTYPE html
  84. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  85. "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  86.  
  87. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  88. <head>
  89. <title>Complex Observer </title>
  90. <style type="text/css">
  91. <!--
  92. <?php
  93. echo $bar1->getStyle();
  94. echo $bar2->getStyle();
  95. ?>
  96. table.container {
  97. background-color: lightblue;
  98. border: 2;
  99. border-color: navy;
  100. border-style: dashed;
  101. cell-spacing: 4;
  102. cell-padding: 8;
  103. width: 50%;
  104. }
  105. // -->
  106. </style>
  107. <script type="text/javascript">
  108. <!--
  109. <?php echo $bar1->getScript(); ?>
  110. //-->
  111. </script>
  112. </head>
  113. <body>
  114.  
  115. <table class="container">
  116. <tr>
  117. <td width="25%" align="center">
  118. <?php echo $bar1->toHTML(); ?>
  119. </td>
  120. <td width="25%" align="center">
  121. <?php echo $bar2->toHTML(); ?>
  122. </td>
  123. </tr>
  124. </table>
  125.  
  126. <?php
  127. do {
  128. $bar1->display();
  129. if ($bar1->getPercentComplete() == 1) {
  130. $bar1->setValue(0);
  131. } else {
  132. $bar1->incValue();
  133. }
  134. } while($bar2->getPercentComplete() < 1);
  135. ?>
  136.  
  137. </body>
  138. </html>

Lets considers the most important lines: The do-while loop (lines 128 to 135) manage the reinitialization of left bar PB1 when this one reachs 100%.
Changes on right bar PB2 are made by notify method of Bar1Observer class when event catched is "setValue" and value is equal zero (lines 24 to 27), raised by HTML_Progress::setValue method on line 131.


Prev Up Next
Using Indeterminate Mode Getting Started Monitoring functions v2

Documentation generated on Sun, 12 Sep 2004 20:22:51 +0200 by phpDocumentor 1.3.0RC3