home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / treecnr.zip / treecnr.cpv < prev    next >
Text File  |  1996-03-13  |  5KB  |  137 lines

  1.  
  2. // ===========================================================================
  3. // TreeCnr::changeToParentNamed
  4. // ---------------------------------------------------------------------------
  5. //
  6. // ===========================================================================
  7. TreeCnr &
  8.   TreeCnr::changeToParentNamed(
  9.     IContainerControl * aCnr,
  10.     IContainerObject *  aCnrObject,
  11.     IString             parentName)
  12. {
  13.    Boolean found = false;
  14.    IContainerControl::ObjectCursor cnrCursor(*aCnr);
  15.  
  16.    if (parentName.size())
  17.       {
  18.       // ----------------------------------------------------
  19.       // Find the container object representing the parent to
  20.       // move aCnrObject under.
  21.       // ----------------------------------------------------
  22.       cnrCursor.setCurrent(aCnrObject);
  23.       while (cnrCursor.isValid() && !found)
  24.          {
  25.          if (cnrCursor.current()->iconText() == parentName)
  26.             found = true;
  27.          else
  28.             cnrCursor.previous();
  29.          }
  30.       }
  31.  
  32.    // -------------------------------------------------------
  33.    // Now move aCnrObject under the found parent.
  34.    // -------------------------------------------------------
  35.    if (found)
  36.       aCnr->moveObjectTo(aCnrObject, cnrCursor.current());
  37.    else
  38.       // ----------------------------------------------------
  39.       // if not found, make it a root.
  40.       // ----------------------------------------------------
  41.       aCnr->moveObjectTo(aCnrObject);
  42.  
  43.    return *this;
  44. }
  45.  
  46. // ===========================================================================
  47. // TreeCnr::addObjectInTreeView
  48. // ---------------------------------------------------------------------------
  49. // Adds aPerson into the aCol and make's sure that the container object is
  50. // inserted in the proper place within aCnr.  The parent object is determined
  51. // by looking at the parentName attribute of aPerson.
  52. // ===========================================================================
  53. TreeCnr & TreeCnr::addObjectInTreeView(
  54.              IContainerControl * aCnr,
  55.              IVSequence<Person *> * aCol,
  56.              Person * aPerson )
  57. {
  58.   // -------------------------------------------------------------------------
  59.   // Search for the parent object in the collection to get
  60.   // a cursor to add the new object after.
  61.   // -------------------------------------------------------------------------
  62.   ICursor * colCursor = aCol->newCursor( );
  63.   Boolean found = false;
  64.   colCursor->setToFirst();
  65.   while (colCursor->isValid() && !found)
  66.     {
  67.     if (aCol->elementAt(*colCursor)->name() == aPerson->parentName())
  68.        found = true;
  69.     else
  70.        colCursor->setToNext();
  71.     }
  72.  
  73.   if (found)
  74.     {
  75.     // -----------------------------------------------------------------------
  76.     // Add the person to the collection.  Doing this addition automatically
  77.     // adds a container object to the container, but not with the correct
  78.     // parent.  Therefore, me must go into the container and fix up the parent.
  79.     // -----------------------------------------------------------------------
  80.     aCol->addAsNext(aPerson, *colCursor);
  81.     IContainerObject *cnrObject =
  82.        IContainerControl::TextCursor(*aCnr, aPerson->name(), true, false, true).first();
  83.     changeToParentNamed(aCnr, cnrObject, aPerson->parentName());
  84.     }
  85.  
  86.   delete colCursor;
  87.   return *this;
  88. }
  89.  
  90. // ===========================================================================
  91. // TreeCnr::deleteObjectInTreeView
  92. // ===========================================================================
  93. TreeCnr & TreeCnr::deleteObjectInTreeView(
  94.              IContainerControl * aCnr,
  95.              IVSequence<Person *> * aCol,
  96.              Person * aPerson )
  97. {
  98.   IContainerObject *cnrObject =
  99.     IContainerControl::TextCursor(*aCnr, aPerson->name()).first();
  100.   unsigned long numOfDescendents =
  101.     aCnr->descendentsOf(cnrObject).numberOfElements();
  102.  
  103.   // -------------------------------------------------------------------------
  104.   // Search through the collection to get a cursor to the person
  105.   // that we want to delete.
  106.   // -------------------------------------------------------------------------
  107.   ICursor * colCursor = aCol->newCursor( );
  108.   Boolean found = false;
  109.   colCursor->setToFirst();
  110.   while (colCursor->isValid() && !found)
  111.     {
  112.     if (aCol->elementAt(*colCursor) == aPerson)
  113.        found = true;
  114.     else
  115.        colCursor->setToNext();
  116.     }
  117.  
  118.   if (found)
  119.     {
  120.     // -----------------------------------------------------------------------
  121.     // Delete the element and all its descendents from the collection
  122.     // in reverse order.  Deleting the element from the collection will
  123.     // automatically remove it from the container.
  124.     // -----------------------------------------------------------------------
  125.     unsigned long cursorPos = aCol->position(*colCursor);
  126.     for (int i = numOfDescendents + cursorPos; i >= cursorPos; i--)
  127.        {
  128.        Person * t = aCol->elementAtPosition(i);
  129.        aCol->removeAtPosition(i);
  130.        delete t;
  131.        }
  132.     }
  133.   delete colCursor;
  134.  
  135.   return *this;
  136. }
  137.