home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / acdf5 / acdfmdl5.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  6.9 KB  |  241 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //                                              SAMPLE CODE
  3. //
  4. // FileName: ACDFMdl5.cpp
  5. //
  6. // ClassName: ACompDocFwkModel
  7. //
  8. // Description: Compound Document Framework Model
  9. //              This sample demonstrates the use of streaming collection class.
  10. //              A collection of the TPerson is created depending on the screen
  11. //              selection in the view.
  12. //              This sample show how to use both the ISequence collection class
  13. //              and the IKeySet collection class
  14. //              There are also 2 different types of notification messages sent 
  15. //              to the view depending on what is changed.
  16. ///////////////////////////////////////////////////////////////////////////////
  17. #include "ACDFMdl5.hpp"
  18. #include "ACDFVw5.hpp"
  19. #include "ACDFSt5.hpp"
  20. #include <imsgbox.hpp>
  21.  
  22. #include <fstream.h>
  23. #include <inotifev.hpp>
  24. #include <iexcept.hpp>
  25. #include <icconst.h>
  26. #include <ibasstrm.hpp>
  27. #include <assert.h>
  28. #include <iframe.hpp>
  29.  
  30. TypeExtensionMacro(ACompDocFwkModel)
  31.  
  32. const INotificationId ACompDocFwkModel::kListChanged = "List changed";
  33. const INotificationId ACompDocFwkModel::kSelectionChanged = "Selection changed";
  34.  
  35. ACompDocFwkModel::ACompDocFwkModel()  
  36. // Default Constructor
  37. {       IFUNCTRACE_DEVELOP();
  38.     fType = kSeq; // Default setting
  39. }
  40.  
  41. ACompDocFwkModel::~ACompDocFwkModel()
  42. // Default Destructor
  43. {       IFUNCTRACE_DEVELOP();}
  44.  
  45. ACompDocFwkModel::ACompDocFwkModel( const ACompDocFwkModel& other ) :
  46.         IModel( other ) // force assert
  47. // Copy Constructor
  48. {       IFUNCTRACE_DEVELOP(); }
  49.  
  50.  
  51. IBaseStream& ACompDocFwkModel::operator>>=(IBaseStream& toWhere) const
  52. // Stream member data out
  53. {       IFUNCTRACE_DEVELOP();
  54.     writeVersion(toWhere, kOriginalVersion);
  55.     IModel::operator>>=( toWhere );
  56.     fPersonList >>= toWhere;
  57.     fPersonKeyList >>= toWhere;
  58.     return toWhere;
  59. }
  60.  
  61.  
  62. IBaseStream& ACompDocFwkModel::operator<<=(IBaseStream& fromWhere)
  63. // Stream member data in
  64. {       IFUNCTRACE_DEVELOP();
  65.     switch (readVersion(fromWhere))
  66.     {
  67.         case kOriginalVersion:
  68.             IModel::operator<<=( fromWhere );
  69.             fPersonList <<= fromWhere;
  70.             fPersonKeyList <<= fromWhere;
  71.             break;
  72.         default:
  73.         ITHROWLIBRARYERROR(IC_STREAM_VERSION_UNSUPPORTED, 
  74.               IBaseErrorInfo::invalidRequest,
  75.               IException::recoverable);
  76.     }
  77.     return fromWhere;
  78. }
  79.  
  80.  
  81. void ACompDocFwkModel::addToList(TPerson& myPerson)
  82. // Add the person to the list
  83. {       IFUNCTRACE_DEVELOP();
  84.  
  85.     if (fType == kSeq)
  86.     {
  87.         IElemPointer<TPerson> elem = new TPerson(myPerson);
  88.         fPersonList.addAsLast(elem);
  89.     }
  90.     if (fType == kKey)
  91.     {
  92.         fPersonKeyList.add(myPerson);
  93.     }
  94.     
  95.     notifyOfChange(INotificationEvent(kListChanged, notifier()));
  96. }       
  97. void ACompDocFwkModel::deleteFromList(TPerson& myPerson)
  98. // Add the person to the list
  99. {       IFUNCTRACE_DEVELOP();
  100.     
  101.     switch (fType)
  102.     {
  103.         case kSeq:
  104.         {
  105.             unsigned long myNumberPersons = fPersonList.numberOfElements();
  106.             for (int i=1;i <= myNumberPersons; i++)
  107.             {
  108.                 TPerson *thisPerson = fPersonList.elementAtPosition(i);
  109.                 if (myPerson == *thisPerson)
  110.                 {
  111.                     fPersonList.removeAtPosition(i);
  112.                     delete thisPerson;
  113.                     break;
  114.                 }
  115.             }
  116.             break;
  117.         }
  118.         case kKey:
  119.         {
  120.             IString myKey = fPersonKeyList.key(myPerson);
  121.             ICursor& myCursor = *fPersonKeyList.newCursor();
  122.             if (fPersonKeyList.locateElementWithKey(myKey, myCursor))
  123.             {
  124.                 fPersonKeyList.removeAt(myCursor);
  125.             }
  126.             break;
  127.         }
  128.     }
  129.     notifyOfChange(INotificationEvent(kListChanged, notifier()));
  130. }       
  131.  
  132. TPerson ACompDocFwkModel::getPersonFirstList()
  133. // Get person from list
  134. {       IFUNCTRACE_DEVELOP();
  135.     
  136.     TPerson myPerson;
  137.  
  138.     if (fType == kSeq)
  139.     {
  140.         fCursor = fPersonList.newCursor();
  141.         if (fPersonList.setToFirst(*fCursor))
  142.         {
  143.             TPerson *thePerson = fPersonList.elementAt(*fCursor);
  144.             myPerson = *thePerson;
  145.         }
  146.     }
  147.     if (fType == kKey)
  148.     {
  149.         fCursor = fPersonKeyList.newCursor();
  150.         if (fPersonKeyList.setToFirst(*fCursor))
  151.             myPerson = fPersonKeyList.elementAt(*fCursor);
  152.     }
  153.     return myPerson;
  154. }
  155.  
  156. TPerson ACompDocFwkModel::getPersonNextList()
  157. // Get person from list
  158. {       IFUNCTRACE_DEVELOP();
  159.  
  160.     TPerson myPerson;
  161.  
  162.     if (fType == kSeq)
  163.     {
  164.         if (fPersonList.setToNext(*fCursor))
  165.         {
  166.             TPerson *thePerson = fPersonList.elementAt(*fCursor);
  167.             myPerson = *thePerson;
  168.         }
  169.     }
  170.     if (fType == kKey)
  171.     {
  172.         if (fPersonKeyList.setToNext(*fCursor))
  173.             myPerson = fPersonKeyList.elementAt(*fCursor);
  174.     }
  175.     return myPerson; // if cursor has not been set
  176. }
  177.  
  178. TPerson ACompDocFwkModel::getPersonList(long pos, IString name)
  179. // 
  180. {       IFUNCTRACE_DEVELOP();
  181.     if (fType == kSeq)
  182.     {
  183.          TPerson *thePerson = fPersonList.elementAtPosition(pos);
  184.          fSelectedPerson = *thePerson;
  185.     }
  186.     if (fType == kKey)
  187.     {
  188.         fSelectedPerson = fPersonKeyList.elementWithKey(name);
  189.         
  190.     }
  191.  
  192.     notifyOfChange(INotificationEvent(kSelectionChanged, notifier()));
  193.     return fSelectedPerson;         
  194. }
  195.  
  196. void ACompDocFwkModel::setCollectionType(CollectionType myType)
  197. // Set List type
  198. {       IFUNCTRACE_DEVELOP();
  199.     fType = myType;
  200.     notifyOfChange(INotificationEvent(kListChanged, notifier()));
  201. }
  202.  
  203. TPerson ACompDocFwkModel::getSelectedPerson()
  204. // Return the person that was selected
  205. {       IFUNCTRACE_DEVELOP();
  206.  
  207.     return fSelectedPerson;
  208. }
  209.  
  210. // IComponent Stationery class is created using the model, view 
  211. ACompDocFwkMyStationery CompDocFwkStationery;
  212.  
  213.  
  214. int main( int argc, char* argv[] )
  215. {
  216.     int ireturn;
  217.     try
  218.     {
  219.         // Stationery run will eventually call the IApplication::current().run()
  220.         ireturn =  CompDocFwkStationery.run( argc, argv );
  221.     }
  222.  
  223.     catch (IException& exc)
  224.     {
  225.         ofstream  errorFile("errormsg.log",ios::app);
  226.         const IExceptionLocation *excLocate = exc.locationAtIndex(0);
  227.  
  228.         errorFile << "Exception on exit " << exc.text() << endl;
  229.         errorFile << "File : " << excLocate->fileName() << endl;
  230.         errorFile << "Function : " << excLocate->functionName() << endl;
  231.         errorFile << "Line : " << excLocate->lineNumber() << endl;
  232.  
  233.         cout << "Exception on exit " << exc.text() << endl;
  234.         cout << "File : " << excLocate->fileName() << endl;
  235.         cout << "Function : " << excLocate->functionName() << endl;
  236.         cout << "Line : " << excLocate->lineNumber() << endl;
  237.     }
  238.     return ireturn;
  239. }
  240.     
  241.