home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ibmodf.zip / CUSTOMER.ZIP / CUSTMODL.CPP < prev    next >
Text File  |  1995-06-13  |  14KB  |  361 lines

  1.  /*******************************************************************************
  2. * FILE NAME: custmodl.cpp                                                      *
  3. *                                                                              *
  4. * DESCRIPTION:                                                                 *
  5. *   This file contains the implementation of classes/functions declared        *
  6. *   in custmodl.hpp.                                                           *
  7. *                                                                              *
  8. * COPYRIGHT:                                                                   *
  9. *   Licensed Materials - Property of IBM                                       *
  10. *   (C) Copyright IBM Corporation 1992, 1994                                   *
  11. *   All Rights Reserved                                                        *
  12. *   US Government Users Restricted Rights - Use, duplication, or               *
  13. *   disclosure                                                                 *
  14. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  15. *                                                                              *
  16. *******************************************************************************/
  17. // Priority INT_MIN (-2147483647 - 1) + 1024 + 512
  18. #pragma priority( -2147482112 )
  19.  
  20. #pragma SOMAsDefault (off)
  21.  
  22. //=============== Local and UICL includes
  23. #include <custmodl.hpp>
  24. #include <custview.hpp>
  25. #include <iframe.hpp>
  26. #include <iexcept.hpp>
  27. #include <istring.hpp>
  28. #include <ireslib.hpp>
  29. #include <irect.hpp>
  30. #include <icolor.hpp>
  31. #include <itrace.hpp>
  32.  
  33. //============= OpenDoc toolkit includes needed by code in this part which deals directly with OpenDoc
  34. #define INCL_ODDRAFT
  35. #define INCL_ODSTORAGESYSTEM
  36. #define INCL_ODSTORAGEUNIT
  37. #include <os2.hh>
  38.  
  39.  
  40. // Segment definitions 
  41. #ifdef IC_PAGETUNE
  42.   #define _CUSTMODL_CPP_
  43.   #include <ipagetun.h>
  44. #endif
  45.  
  46. #pragma SOMAsDefault (pop)
  47.  
  48. #define kKindCustomerPart "CustomerModel::custpart"
  49.  
  50. const ODType kCustomerPartData = "CustomerModel Data";
  51. const ODType kCustomerPartName = "CustomerModel Customer Name";
  52. const ODType kCustomerPartState = "CustomerModel Customer Address State";
  53. const ODType kCustomerPartBal = "CustomerModel Balance Value";
  54.  
  55. /*------------------------------------------------------------------------------
  56. | CustomerModel::CustomerModel                                                 |
  57. |                                                                              |
  58. | Constructor to create a form part - protected, default ctor                  |
  59. ------------------------------------------------------------------------------*/
  60. CustomerModel :: CustomerModel()
  61.   : fCustomer(0), fCompany(0)
  62. {
  63.    // Obtain your model data. To keep this sample simple, we just hardcode it here;
  64.    //  in real life, your data might come from a database, a file, etc.
  65.    fCustomer = new ICustomer( IString("Rob Petri") );
  66.    IAddress * address = new IAddress();
  67.    address->setStreet( IString("148 Bonnie Meadow Lane") );
  68.    address->setCity( IString("New Rochelle") );
  69.    address->setState( IString("NY") );
  70.    fCustomer->setAddress(address);
  71.  
  72.    fCompany = new ICompany( IString("Brady Productions, Inc.") );
  73.  
  74.    fBalance = new Balance(25000);
  75.  
  76. }
  77.  
  78.  
  79. /*------------------------------------------------------------------------------
  80. | CustomerModel::~CustomerModel                                                |
  81. |                                                                              |
  82. ------------------------------------------------------------------------------*/
  83. CustomerModel :: ~CustomerModel()
  84. {
  85.    delete fCompany;
  86.    delete ( fCustomer->address() );
  87.    delete fCustomer;
  88.    delete fBalance;
  89. }
  90.  
  91.  
  92. //******next three functions are only small examples of how one might interact with
  93. //****** OpenDoc toolkit to do persistance. ODFrk will simplify this in later driver...
  94.  
  95. /*------------------------------------------------------------------------------
  96. | CustomerModel::InitPart                                                     |
  97. |                                                                             |
  98. | Override of ODPart::InitPart.                                               |
  99. |       For now, you must deal directly with OD toolkit here.                 |
  100. ------------------------------------------------------------------------------*/
  101. void CustomerModel :: InitPart (ODStorageUnit *su)
  102. {
  103.    if (IsInitialized())
  104.       return;
  105.  
  106.    // Call the inherited InitPart (don't call InitPersistentObject).
  107.    Inherited::InitPart (su);
  108.  
  109.    // Identify part.
  110.    su->Focus (kODPropPart,
  111.               kODPosUndefined,
  112.               kODISOStr,
  113.               0,
  114.               kODPosUndefined);
  115.    su->SetValue (strlen(kKindCustomerPart) + 1, (ODValue) kKindCustomerPart);   
  116.  
  117.    // Create an auxiliary storage unit for the class' contribution to the part
  118.    // contents.
  119.    auxsu = su->GetDraft()->CreateStorageUnit();
  120.  
  121.    // All of this class' content will be added to the auxiliary storaage unit  
  122.    auxsu->AddProperty (kODPropContents);
  123.    auxsu->AddValue (kCustomerPartName);
  124.    auxsu->AddValue (kCustomerPartState);
  125.    auxsu->AddValue (kCustomerPartBal);
  126.                                     
  127.    su->Focus (kODPropContents,
  128.               kODPosUndefined,
  129.               kODNULL,
  130.               0,
  131.               kODPosUndefined);
  132.    su->AddValue ((ODValueType) kCustomerPartData);
  133.    ODStorageUnitRef auxsuRef = su->GetStrongStorageUnitRef (auxsu);
  134.    su->SetValue (sizeof (ODStorageUnitRef), &auxsuRef);
  135.  
  136. }
  137.  
  138. /*------------------------------------------------------------------------------
  139. | CustomerModel::InitPartFromStorage                                          |
  140. |                                                                             |
  141. | Override of ODPart::InitPartFromStorage.                                    |
  142. |       For now, you must deal directly with OD toolkit here                  |
  143. ------------------------------------------------------------------------------*/
  144. void CustomerModel :: InitPartFromStorage (ODStorageUnit* su)
  145. {
  146.    ODStorageUnitRef auxsuRef;
  147.    ODULong          valueSize;
  148.  
  149.    if (IsInitialized())
  150.       return;
  151.  
  152.    // Call inherited InitPartFromStorage (not InitPersistentObjectFromStorage).
  153.    Inherited::InitPartFromStorage (su);
  154.  
  155.    // Get this class' auxiliary storage unit.
  156.    su->Focus (kODPropContents,
  157.               kODPosUndefined,
  158.               kCustomerPartData,
  159.               0,
  160.               kODPosUndefined);
  161.  
  162.    su->GetValue (su->GetSize(), &auxsuRef);
  163.    auxsu = su->GetDraft()->GetStorageUnit (su->GetIDFromStorageUnitRef (auxsuRef));
  164.  
  165.    // Initialize from persistent data.
  166.    auxsu->Focus (kODPropContents,
  167.                  kODPosUndefined,
  168.                  kCustomerPartName,
  169.                  0,
  170.                  kODPosUndefined);
  171.    valueSize = auxsu->GetSize();
  172.    char* customerName = (char*) malloc (valueSize);
  173.    auxsu->GetValue (valueSize, customerName);
  174.    customer()->setName( IString (customerName) );
  175.    free (customerName);
  176.  
  177.    auxsu->Focus (kODNULL,
  178.                  kODPosSame,
  179.                  kCustomerPartState,
  180.                  0,
  181.                  kODPosUndefined);
  182.    valueSize = auxsu->GetSize();
  183.    char* state = (char*) malloc (valueSize);
  184.    auxsu->GetValue (valueSize, state);
  185.    customer()->address()->setState(IString (state));
  186.    free (state);
  187.  
  188.    auxsu->Focus (kODPropContents,
  189.                  kODPosUndefined,
  190.                  kCustomerPartBal,
  191.                  0,
  192.                  kODPosUndefined);
  193.    int tempBal;
  194.    auxsu->GetValue (auxsu->GetSize(), &tempBal);
  195.    balance()->setValue(tempBal);
  196.  
  197. }
  198.  
  199. /*------------------------------------------------------------------------------
  200. | CustomerModel::Externalize                                                  |
  201. |                                                                             |
  202. | Override of ODPart::Externalize.  Write out persistent data to storage.     |
  203. |       For now, you must deal directly with OD toolkit here                  |
  204. ------------------------------------------------------------------------------*/
  205. void CustomerModel :: Externalize ()
  206. {
  207.    ODStorageUnit   *su = GetStorageUnit();
  208.    ODStorageUnitRef auxsuRef;
  209.    ODULong          valueSize, dataSize;
  210.  
  211.    Inherited::Externalize();
  212.  
  213.    auxsu->Focus (kODPropContents,
  214.                  kODPosUndefined,
  215.                  kCustomerPartName,
  216.                  0,
  217.                  kODPosUndefined);
  218.    valueSize = auxsu->GetSize();
  219.    dataSize = customer()->name().length() + 1;
  220.    if (valueSize <= dataSize)
  221.    {
  222.       auxsu->SetValue ( valueSize, (ODValue)(char*)(customer()->name()) );
  223.       auxsu->InsertValue (dataSize - valueSize,
  224.                           (ODValue)(char*)(customer()->name().subString(valueSize + 1)) );
  225.    }
  226.    else
  227.    {
  228.       auxsu->SetValue ( dataSize, (ODValue)(char*)(customer()->name()) );
  229.       auxsu->DeleteValue (valueSize - dataSize);
  230.    }
  231.  
  232.    auxsu->Focus (kODNULL,
  233.                  kODPosSame,
  234.                  kCustomerPartState,
  235.                  0,
  236.                  kODPosUndefined);
  237.    valueSize = auxsu->GetSize();
  238.    IString tempState = customer()->address()->state();
  239.    dataSize = tempState.length() + 1;
  240.    if (valueSize <= dataSize)
  241.    {
  242.       auxsu->SetValue (valueSize, (ODValue)(char*)tempState);
  243.       auxsu->InsertValue (dataSize - valueSize,
  244.                           (ODValue)(char*)(tempState.subString(valueSize + 1)));
  245.    }
  246.    else
  247.    {
  248.       auxsu->SetValue (dataSize, (ODValue)(char*)tempState);
  249.       auxsu->DeleteValue (valueSize - dataSize);
  250.    }
  251.  
  252.    auxsu->Focus (kODNULL,
  253.                  kODPosSame,
  254.                  kCustomerPartBal,
  255.                  0,
  256.                  kODPosUndefined);
  257.    int tempBal = balance()->value();
  258.    auxsu->SetValue (sizeof (int), (ODValue)&tempBal);
  259.  
  260.    auxsu->Externalize();
  261.  
  262.    su->Focus (kODPropContents,
  263.               kODPosUndefined,
  264.               kCustomerPartData,
  265.               0,
  266.               kODPosUndefined);
  267.    auxsuRef = su->GetStrongStorageUnitRef (auxsu);
  268.    su->SetValue (sizeof (ODStorageUnitRef), &auxsuRef);
  269. }
  270.  
  271.  
  272. /*------------------------------------------------------------------------------
  273. | CustomerModel::openViews                                                    |
  274. |                                                                             |
  275. | Create an initial view for this model                                       |
  276. ------------------------------------------------------------------------------*/
  277. CustomerModel& CustomerModel :: openViews ()
  278. {
  279.   // This is one way to create views. It is also possible for some other piece
  280.   // of code outside this class to create the view object(s), add them to this model, etc.
  281.   openView(ID_FIRSTVIEW);
  282.   openView(ID_SECONDVIEW);
  283.  
  284.   return *this;
  285. }
  286.  
  287.  
  288. /*------------------------------------------------------------------------------
  289. | CustomerModel::openView                                                     |
  290. |                                                                             |
  291. | Restore view for this model                                                 |
  292. ------------------------------------------------------------------------------*/
  293. CustomerModel& CustomerModel :: openView (IResourceId viewId)
  294. {
  295.   MyCustomerView *firstView;
  296.   MySecondaryCustomerView *secondView;
  297.   switch (viewId) {
  298.    case ID_FIRSTVIEW:
  299.      firstView =
  300.           new MyCustomerView (ID_FIRSTVIEW,
  301.                               this,
  302.                               ISize (200, 100));  // Specified size
  303.                                                      // only used during initial
  304.                                                      // creation.
  305.                                                   // style defaults
  306.      firstView->setModelUpdateMode(ODFFormView::immediate);
  307.      firstView->makeConnections();
  308.      addView (firstView);
  309.      firstView->initializeView();
  310.      firstView->setBackgroundColor(IColor::red);
  311.      firstView->setForegroundColor(IColor::black);
  312.      break;
  313.    case ID_SECONDVIEW:
  314.      secondView =
  315.            new MySecondaryCustomerView (ID_SECONDVIEW,
  316.                                         this,
  317.                                         ISize (200,320) );  // style defaults
  318.      // let ModelUpdateMode default (to onFocusChange)
  319.      secondView->makeConnections();
  320.      addView(secondView);
  321.      secondView->initializeView();
  322.      break;
  323.   }
  324.  
  325.   return *this;
  326. }
  327.  
  328.  
  329. /*------------------------------------------------------------------------------
  330. | CustomerModel::customer                                                     |
  331. |                                                                             |
  332. | Return the "customer" model content object                                  |
  333. ------------------------------------------------------------------------------*/
  334. ICustomer* CustomerModel :: customer ()
  335. {
  336.   return fCustomer;
  337. }
  338.  
  339.  
  340. /*------------------------------------------------------------------------------
  341. | CustomerModel::company                                                      |
  342. |                                                                             |
  343. | Return the "company" model content object                                   |
  344. ------------------------------------------------------------------------------*/
  345. ICompany* CustomerModel :: company ()
  346. {
  347.   return fCompany;
  348. }
  349.  
  350.  
  351. /*------------------------------------------------------------------------------
  352. | CustomerModel::balance                                                      |
  353. |                                                                             |
  354. | Return the "balance" model content object                                   |
  355. ------------------------------------------------------------------------------*/
  356. Balance* CustomerModel :: balance ()
  357. {
  358.   return fBalance;
  359. }
  360.  
  361.