home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DClap / DTaskMaster.cpp < prev    next >
Encoding:
Text File  |  1996-07-05  |  4.5 KB  |  202 lines  |  [TEXT/R*ch]

  1. // DTaskMaster.cp
  2. // d.g.gilbert
  3.  
  4.  
  5. #include "Dvibrant.h"
  6. #include "DList.h"
  7. #include "DTask.h"
  8. #include "DTaskMaster.h"
  9. #include "DTaskCentral.h"
  10. #include "DCommand.h"
  11.  
  12.  
  13.  
  14. //class DTaskMaster : public DObject
  15.  
  16. DTaskMaster::DTaskMaster(long id, DTaskMaster* superior, DList* subordinates) :
  17.     fId(id),
  18.     fSuperior(superior),                 
  19.     fSubordinates(subordinates) 
  20. {
  21.     if (superior) superior->AddSubordinate(this); 
  22. }
  23.  
  24. DTaskMaster::~DTaskMaster() 
  25. {
  26.     if (fSubordinates) fSubordinates->suicide(); //delete fSubordinates;
  27. #if 1
  28.     gTaskCentral->FinishTasksByOwner(this); 
  29. #else
  30.     if (gLastCommand && gLastCommand->fSource == this) {
  31.         delete gLastCommand; 
  32.         gLastCommand= NULL;
  33.         }
  34. #endif
  35.     fSubordinates= NULL; //?
  36. }
  37.     
  38.     
  39. void DTaskMaster::AddSubordinate(DTaskMaster* subordinate)
  40. {
  41.     if (!fSubordinates) fSubordinates= new DList();
  42.     fSubordinates->InsertLast(subordinate);
  43. }
  44.  
  45. Boolean DTaskMaster::IsSuperior(DTaskMaster* item) 
  46. {
  47.     // recursion ALERT
  48.     if (this == item) 
  49.         return true;  // do we want case of starter == superior??
  50.     if (fSuperior) {
  51.         if (fSuperior == item)
  52.             return true;
  53.         else if (fSuperior->IsSuperior(item))
  54.             return true;
  55.         }
  56.     return false;
  57. }
  58.  
  59. DTaskMaster* DTaskMaster::FindSuperior(long superid) 
  60. {
  61.     // recursion ALERT
  62.     if (fId == superid) 
  63.         return this;  // do we want case of starter == superior??
  64.     if (fSuperior) {
  65.         DTaskMaster* super= fSuperior;
  66.         if (super->fId == superid)
  67.             return super;
  68.         else if ((super= super->FindSuperior(superid)) != NULL)
  69.             return super;
  70.         }
  71.     return NULL;
  72. }
  73.  
  74.  
  75.  
  76. Boolean DTaskMaster::IsSubordinate(DTaskMaster* item) 
  77. {
  78.     // recursion ALERT
  79.     if (this == item) 
  80.         return true;   
  81.     if (fSubordinates) {
  82.         long i, n= fSubordinates->GetSize();
  83.         for (i= 0; i<n; i++) {
  84.             DTaskMaster* subord= (DTaskMaster*)fSubordinates->At(i);
  85.             if (subord == item) 
  86.                 return true;
  87.             else if (subord->IsSubordinate(item))
  88.                 return true;
  89.             }        
  90.         }
  91.     return false;
  92. }
  93.  
  94.  
  95. DTaskMaster* DTaskMaster::FindSubordinate(long subid) 
  96. {
  97.     // recursion ALERT
  98.     if (fId == subid) 
  99.         return this;   
  100.     else if (fSubordinates) {
  101.         long i, n= fSubordinates->GetSize();
  102.         for (i= 0; i<n; i++) {
  103.             DTaskMaster* subord= (DTaskMaster*)fSubordinates->At(i);
  104.             if (subord->fId == subid) 
  105.                 return subord;
  106.             else if ((subord= subord->FindSubordinate(subid)) !=NULL)
  107.                 return subord;
  108.             }        
  109.         }
  110.     return NULL;
  111. }
  112.  
  113.  
  114. Boolean DTaskMaster::IsMyAction(DTaskMaster* action) 
  115. {
  116.  
  117. #if 0
  118.     //general scheme that subclasses can use for this method
  119.     switch(action->fId) {
  120.     case kMyAction1: handleaction1(); return true;
  121.     case kMyAction2: handleaction2(); return true;
  122.     default: return Inherited::IsMyAction(action);
  123.     }
  124. #endif
  125.  
  126.         // this is base class method, we must look to (a) subordinates, then (b) superior
  127.         // to see if they know how to handle this action
  128. #if 0 
  129.         // !! THIS IS BAD when a subordinate passes this message to me and
  130.         // i try to go back to subordinates !!!  ?? Don't ask subords if they
  131.         // can handle? -- assume focused view first got message and passed
  132.         // buck up line of superiors...
  133.     if (fSubordinates) {
  134.         long i, n= fSubordinates->GetSize();
  135.         for (i= 0; i<n; i++) {
  136.             DTaskMaster* subord= (DTaskMaster*)fSubordinates->At(i);
  137.             if (subord->IsMyAction(action)) return true;
  138.             }        
  139.         }
  140. #endif
  141.  
  142.     if (fSuperior) 
  143.         return fSuperior->IsMyAction(action);
  144.     return false;
  145. }
  146.  
  147.     
  148. DTask* DTaskMaster::newTask(long tasknum, short kind, long extra) 
  149. {
  150.     return new DTask(tasknum, kind, this, extra);
  151. }
  152.  
  153. void DTaskMaster::PostTask(DTask* theTask) 
  154. {
  155.     if (theTask) gTaskCentral->AddTask(theTask);
  156. }
  157.  
  158. void DTaskMaster::ProcessTask(DTask* theTask) 
  159. {
  160.     if (theTask) {
  161.         if (theTask->fKind == DTask::kCommander 
  162.          && theTask != gLastCommand
  163.          && ((DCommand*)theTask)->fCausesChange ) // new 18jun95
  164.          {
  165.             if (gLastCommand) delete gLastCommand; 
  166.             gLastCommand= (DCommand*) theTask;
  167.             gLastCommand->newOwner(); // avoid the TaskCentral Reaper  !!                
  168.             }
  169.         theTask->DoIt();
  170.         }
  171. }
  172.  
  173. Boolean DTaskMaster::IsMyTask(DTask* theTask) 
  174. {
  175.         // assume that subclasses will override DoTask/DoMenuTask/DoMouseTask methods
  176.         // to handle their own tasks.  This handler needs to do some overall processing,
  177.         // then pass on to next handler if task isn't taken care of...
  178.     if (!theTask) 
  179.         return true; //??
  180.     else if (theTask->fKind == DTask::kMenu) 
  181.         return this->DoMenuTask(theTask->fNumber, theTask);
  182. #if 1
  183.     else if (theTask->fKind == DTask::kCommander) {
  184.         this->ProcessTask(theTask);
  185.         return true;
  186.         }
  187. #endif
  188. #if 0
  189.     else if (theTask->fKind == DTask::kTracker) {
  190.         //this->ProcessTask(theTask);
  191.         return true;
  192.         }
  193. #endif
  194.     else 
  195.         return false;
  196. }
  197.         
  198. Boolean DTaskMaster::DoMenuTask(long tasknum, DTask* theTask) 
  199. {
  200.     return false;
  201. }
  202.