home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
treecnr.zip
/
treecnr.cpv
< prev
next >
Wrap
Text File
|
1996-03-13
|
5KB
|
137 lines
// ===========================================================================
// TreeCnr::changeToParentNamed
// ---------------------------------------------------------------------------
//
// ===========================================================================
TreeCnr &
TreeCnr::changeToParentNamed(
IContainerControl * aCnr,
IContainerObject * aCnrObject,
IString parentName)
{
Boolean found = false;
IContainerControl::ObjectCursor cnrCursor(*aCnr);
if (parentName.size())
{
// ----------------------------------------------------
// Find the container object representing the parent to
// move aCnrObject under.
// ----------------------------------------------------
cnrCursor.setCurrent(aCnrObject);
while (cnrCursor.isValid() && !found)
{
if (cnrCursor.current()->iconText() == parentName)
found = true;
else
cnrCursor.previous();
}
}
// -------------------------------------------------------
// Now move aCnrObject under the found parent.
// -------------------------------------------------------
if (found)
aCnr->moveObjectTo(aCnrObject, cnrCursor.current());
else
// ----------------------------------------------------
// if not found, make it a root.
// ----------------------------------------------------
aCnr->moveObjectTo(aCnrObject);
return *this;
}
// ===========================================================================
// TreeCnr::addObjectInTreeView
// ---------------------------------------------------------------------------
// Adds aPerson into the aCol and make's sure that the container object is
// inserted in the proper place within aCnr. The parent object is determined
// by looking at the parentName attribute of aPerson.
// ===========================================================================
TreeCnr & TreeCnr::addObjectInTreeView(
IContainerControl * aCnr,
IVSequence<Person *> * aCol,
Person * aPerson )
{
// -------------------------------------------------------------------------
// Search for the parent object in the collection to get
// a cursor to add the new object after.
// -------------------------------------------------------------------------
ICursor * colCursor = aCol->newCursor( );
Boolean found = false;
colCursor->setToFirst();
while (colCursor->isValid() && !found)
{
if (aCol->elementAt(*colCursor)->name() == aPerson->parentName())
found = true;
else
colCursor->setToNext();
}
if (found)
{
// -----------------------------------------------------------------------
// Add the person to the collection. Doing this addition automatically
// adds a container object to the container, but not with the correct
// parent. Therefore, me must go into the container and fix up the parent.
// -----------------------------------------------------------------------
aCol->addAsNext(aPerson, *colCursor);
IContainerObject *cnrObject =
IContainerControl::TextCursor(*aCnr, aPerson->name(), true, false, true).first();
changeToParentNamed(aCnr, cnrObject, aPerson->parentName());
}
delete colCursor;
return *this;
}
// ===========================================================================
// TreeCnr::deleteObjectInTreeView
// ===========================================================================
TreeCnr & TreeCnr::deleteObjectInTreeView(
IContainerControl * aCnr,
IVSequence<Person *> * aCol,
Person * aPerson )
{
IContainerObject *cnrObject =
IContainerControl::TextCursor(*aCnr, aPerson->name()).first();
unsigned long numOfDescendents =
aCnr->descendentsOf(cnrObject).numberOfElements();
// -------------------------------------------------------------------------
// Search through the collection to get a cursor to the person
// that we want to delete.
// -------------------------------------------------------------------------
ICursor * colCursor = aCol->newCursor( );
Boolean found = false;
colCursor->setToFirst();
while (colCursor->isValid() && !found)
{
if (aCol->elementAt(*colCursor) == aPerson)
found = true;
else
colCursor->setToNext();
}
if (found)
{
// -----------------------------------------------------------------------
// Delete the element and all its descendents from the collection
// in reverse order. Deleting the element from the collection will
// automatically remove it from the container.
// -----------------------------------------------------------------------
unsigned long cursorPos = aCol->position(*colCursor);
for (int i = numOfDescendents + cursorPos; i >= cursorPos; i--)
{
Person * t = aCol->elementAtPosition(i);
aCol->removeAtPosition(i);
delete t;
}
}
delete colCursor;
return *this;
}