Back to index

lightning-sunbird  0.9+nobinonly
Classes | Public Member Functions | Protected Attributes | Friends
nsXFormsControlListItem Class Reference

A class for storing pointers to XForms controls added to an XForms model. More...

#include <nsXFormsModelElement.h>

Collaboration diagram for nsXFormsControlListItem:
Collaboration graph
[legend]

List of all members.

Classes

class  iterator
 An iterator implementation for the class. More...

Public Member Functions

 nsXFormsControlListItem (nsIXFormsControl *aControl, nsRefPtrHashtable< nsISupportsHashKey, nsXFormsControlListItem > *aHash)
 The constructor takes a hashtable pointer, which needs to point to the model's hashtable.
 nsXFormsControlListItem ()
 ~nsXFormsControlListItem ()
 nsXFormsControlListItem (const nsXFormsControlListItem &aCopy)
nsrefcnt AddRef ()
nsrefcnt Release ()
void Clear ()
 Clear contents of current node, all siblings, and all children.
nsresult RemoveControl (nsIXFormsControl *aControl, PRBool &aRemoved)
 Remove a control from the current (sub-) tree.
nsresult AddControl (nsIXFormsControl *aControl, nsIXFormsControl *aParent)
 Add a control to the (sub-) tree as a child to the given |aParent|.
nsXFormsControlListItemFindControl (nsIXFormsControl *aControl)
 Find a control in the (sub-) tree.
already_AddRefed
< nsIXFormsControl
Control ()
 Return the nsIXFormsControl that this node contains.
nsXFormsControlListItemFirstChild ()
 Return the first child of the node.
nsXFormsControlListItemNextSibling ()
 Return the next sibling of the node.
nsXFormsControlListItembegin ()
 The begining position for the node (itself)
nsXFormsControlListItemend ()
 The end position for the node (nsnull)

Protected Attributes

nsAutoRefCnt mRefCnt
nsCOMPtr< nsIXFormsControlmNode
 The XForms control itself.
nsRefPtr< nsXFormsControlListItemmNextSibling
 The next sibling of the node.
nsRefPtr< nsXFormsControlListItemmFirstChild
 The first child of the node.
nsRefPtrHashtable
< nsISupportsHashKey,
nsXFormsControlListItem > * 
mControlListHash

Friends

class nsXFormsControlListItem::iterator

Detailed Description

A class for storing pointers to XForms controls added to an XForms model.

Organized as a tree, with pointers to first child and next sibling.

Notes: 1) The root node is special; only has children and a nsnull mNode. 2) All functions operate on the node they are called on, and its subtree. Functions will never go up (as in: nearer the root) in the tree.

Definition at line 130 of file nsXFormsModelElement.h.


Constructor & Destructor Documentation

The constructor takes a hashtable pointer, which needs to point to the model's hashtable.

This is so that each item in the control list has access to the same hashtable and can add/remove items from it and find items in it.

Definition at line 250 of file nsXFormsModelElement.cpp.

  : mNode(aControl),
    mNextSibling(nsnull),
    mFirstChild(nsnull),
    mControlListHash(aHashtable)
{

}

Here is the caller graph for this function:

Definition at line 261 of file nsXFormsModelElement.cpp.

{
  Clear();
}

Here is the call graph for this function:

Definition at line 266 of file nsXFormsModelElement.cpp.

  : mNode(aCopy.mNode)
{
  if (aCopy.mNextSibling) {
    mNextSibling = new nsXFormsControlListItem(*aCopy.mNextSibling);
    NS_WARN_IF_FALSE(mNextSibling, "could not new?!");
  } else {
    mNextSibling = nsnull;
  }

  if (aCopy.mFirstChild) {
    mFirstChild = new nsXFormsControlListItem(*aCopy.mFirstChild);
    NS_WARN_IF_FALSE(mFirstChild, "could not new?!");
  } else {
    mFirstChild = nsnull;
  }
}

Here is the call graph for this function:


Member Function Documentation

Add a control to the (sub-) tree as a child to the given |aParent|.

If there is no |aParent|, it will insert it as a sibling to |this|. If |aParent| is not found, it will bail.

Parameters:
aControlThe control to insert
aParentThe (eventual) parent to insert it under

Definition at line 311 of file nsXFormsModelElement.cpp.

{
  // Four insertion posibilities:

  // 1) Delegate to first child from root node
  if (!mNode && mFirstChild) {
    return mFirstChild->AddControl(aControl, aParent);
  }

  // 2) control with no parent
  if (!aParent) {
    nsRefPtr<nsXFormsControlListItem> newNode =
      new nsXFormsControlListItem(aControl, mControlListHash);
    NS_ENSURE_TRUE(newNode, NS_ERROR_OUT_OF_MEMORY);

    // Empty tree (we have already checked mFirstChild)
    if (!mNode) {
      mFirstChild = newNode;
      nsCOMPtr<nsIDOMElement> ele;
      aControl->GetElement(getter_AddRefs(ele));
      mControlListHash->Put(ele, newNode);
      return NS_OK;
    }

    if (mNextSibling) {
      newNode->mNextSibling = mNextSibling;
    }
    mNextSibling = newNode;
    nsCOMPtr<nsIDOMElement> ele;
    aControl->GetElement(getter_AddRefs(ele));
    mControlListHash->Put(ele, newNode);
#ifdef DEBUG
    nsXFormsControlListItem* next = newNode->mNextSibling;
    while (next) {
      NS_ASSERTION(aControl != next->mNode,
                   "Node already in tree!!");
      next = next->mNextSibling;
    }
#endif

    return NS_OK;
  }

  // Locate parent
  nsXFormsControlListItem* parentControl = FindControl(aParent);
  NS_ASSERTION(parentControl, "Parent not found?!");

  // 3) parentControl has a first child, insert as sibling to that
  if (parentControl->mFirstChild) {
    return parentControl->mFirstChild->AddControl(aControl, nsnull);
  }

  // 4) first child for parentControl
  nsRefPtr<nsXFormsControlListItem> newNode =
    new nsXFormsControlListItem(aControl, mControlListHash);
  NS_ENSURE_TRUE(newNode, NS_ERROR_OUT_OF_MEMORY);

  parentControl->mFirstChild = newNode;
  nsCOMPtr<nsIDOMElement> ele;
  aControl->GetElement(getter_AddRefs(ele));
  mControlListHash->Put(ele, newNode);

  return NS_OK;
}

Here is the call graph for this function:

Definition at line 161 of file nsXFormsModelElement.h.

  {
    ++mRefCnt;
    NS_LOG_ADDREF(this, mRefCnt, "nsXFormsControlListItem",
                  sizeof(nsXFormsControlListItem));
    return mRefCnt;
  }

The begining position for the node (itself)

Definition at line 488 of file nsXFormsModelElement.cpp.

{
  // handle root
  if (!mNode)
    return mFirstChild;

  return this;
}

Clear contents of current node, all siblings, and all children.

Definition at line 285 of file nsXFormsModelElement.cpp.

{
  if (mFirstChild) {
    mFirstChild->Clear();
    NS_ASSERTION(!(mFirstChild->mFirstChild || mFirstChild->mNextSibling),
                 "child did not clear members!!");
    mFirstChild = nsnull;
  }
  if (mNextSibling) {
    mNextSibling->Clear();
    NS_ASSERTION(!(mNextSibling->mFirstChild || mNextSibling->mNextSibling),
                 "sibling did not clear members!!");
    mNextSibling = nsnull;
  }
  if (mNode) {
    /* we won't bother removing each item one by one from the hashtable.  This
     * approach assumes that we are clearing the whole model's list of controls
     * due to the model going away.  After the model clears this list, it will
     * clear the hashtable all at once.
     */
    mControlListHash = nsnull;
    mNode = nsnull;
  }
}

Here is the caller graph for this function:

Return the nsIXFormsControl that this node contains.

Definition at line 478 of file nsXFormsModelElement.cpp.

{
  nsIXFormsControl* res = nsnull;
  if (mNode)
    NS_ADDREF(res = mNode);
  NS_WARN_IF_FALSE(res, "Returning nsnull for a control. Bad sign.");
  return res;
}

Here is the call graph for this function:

The end position for the node (nsnull)

Definition at line 498 of file nsXFormsModelElement.cpp.

{
  return nsnull;
}

Find a control in the (sub-) tree.

Parameters:
aControlThe control to find

Definition at line 465 of file nsXFormsModelElement.cpp.

{
  if (!aControl)
    return nsnull;

  nsRefPtr<nsXFormsControlListItem> listItem;
  nsCOMPtr<nsIDOMElement> element;
  aControl->GetElement(getter_AddRefs(element));
  mControlListHash->Get(element, getter_AddRefs(listItem));
  return listItem;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return the first child of the node.

Definition at line 217 of file nsXFormsModelElement.h.

{ return mFirstChild; };

Return the next sibling of the node.

Definition at line 220 of file nsXFormsModelElement.h.

{ return mNextSibling; };

Definition at line 169 of file nsXFormsModelElement.h.

  {
    --mRefCnt;
    NS_LOG_RELEASE(this, mRefCnt, "nsXFormsControlListItem");
    if (mRefCnt == 0) {
      mRefCnt = 1;
      delete this;
      return 0;
    }
    return mRefCnt;
  }

Remove a control from the current (sub-) tree.

Will search from that node and down in the tree for the node.

Parameters:
aControlThe control to remove
aRemovedWas the control found and removed?

Definition at line 378 of file nsXFormsModelElement.cpp.

{
  nsXFormsControlListItem* deleteMe = nsnull;
  aRemoved = PR_FALSE;

  // Try children
  if (mFirstChild) {
    // The control to remove is our first child
    if (mFirstChild->mNode == aControl) {
      deleteMe = mFirstChild;

      // Fix siblings
      if (deleteMe->mNextSibling) {
        mFirstChild = deleteMe->mNextSibling;
        deleteMe->mNextSibling = nsnull;
      } else {
        mFirstChild = nsnull;
      }

      // Fix children
      if (deleteMe->mFirstChild) {
        if (!mFirstChild) {
          mFirstChild = deleteMe->mFirstChild;
        } else {
          nsXFormsControlListItem *insertPos = mFirstChild;
          while (insertPos->mNextSibling) {
            insertPos = insertPos->mNextSibling;
          }
          insertPos->mNextSibling = deleteMe->mFirstChild;
        }
        deleteMe->mFirstChild = nsnull;
      }
    } else {
      // Run through children
      nsresult rv = mFirstChild->RemoveControl(aControl, aRemoved);
      NS_ENSURE_SUCCESS(rv, rv);
      if (aRemoved)
        return rv;
    }
  }

  // Try siblings
  if (!deleteMe && mNextSibling) {
    if (mNextSibling->mNode == aControl) {
      deleteMe = mNextSibling;
      // Fix siblings
      if (deleteMe->mNextSibling) {
        mNextSibling = deleteMe->mNextSibling;
        deleteMe->mNextSibling = nsnull;
      } else {
        mNextSibling = nsnull;
      }
      // Fix children
      if (deleteMe->mFirstChild) {
        if (!mNextSibling) {
          mNextSibling = deleteMe->mFirstChild;
        } else {
          nsXFormsControlListItem *insertPos = mNextSibling;
          while (insertPos->mNextSibling) {
            insertPos = insertPos->mNextSibling;
          }
          insertPos->mNextSibling = deleteMe->mFirstChild;
        }
        deleteMe->mFirstChild = nsnull;
      }
    } else {
      // run through siblings
      return mNextSibling->RemoveControl(aControl, aRemoved);
    }
  }

  if (deleteMe) {
    NS_ASSERTION(!(deleteMe->mNextSibling),
                 "Deleted control should not have siblings!");
    NS_ASSERTION(!(deleteMe->mFirstChild),
                 "Deleted control should not have children!");
    nsCOMPtr<nsIDOMElement> element;
    deleteMe->mNode->GetElement(getter_AddRefs(element));
    mControlListHash->Remove(element);
    aRemoved = PR_TRUE;
  }

  return NS_OK;
}

Here is the call graph for this function:


Friends And Related Function Documentation

friend class nsXFormsControlListItem::iterator [friend]

Definition at line 242 of file nsXFormsModelElement.h.


Member Data Documentation

Definition at line 145 of file nsXFormsModelElement.h.

The first child of the node.

Definition at line 143 of file nsXFormsModelElement.h.

The next sibling of the node.

Definition at line 140 of file nsXFormsModelElement.h.

The XForms control itself.

Definition at line 137 of file nsXFormsModelElement.h.

Definition at line 134 of file nsXFormsModelElement.h.


The documentation for this class was generated from the following files: