Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions | Friends
InsertElementTxn Class Reference

A transaction that inserts a single element. More...

#include <InsertElementTxn.h>

Inheritance diagram for InsertElementTxn:
Inheritance graph
[legend]
Collaboration diagram for InsertElementTxn:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_IMETHOD Init (nsIDOMNode *aNode, nsIDOMNode *aParent, PRInt32 aOffset, nsIEditor *aEditor)
 initialize the transaction.
virtual ~InsertElementTxn ()
NS_IMETHOD DoTransaction (void)
NS_IMETHOD UndoTransaction (void)
NS_IMETHOD Merge (nsITransaction *aTransaction, PRBool *aDidMerge)
NS_IMETHOD GetTxnDescription (nsAString &aTxnDescription)
NS_IMETHOD RedoTransaction (void)
NS_IMETHOD GetIsTransient (PRBool *aIsTransient)
void doTransaction ()
 Executes the transaction.
void undoTransaction ()
 Restores the state to what it was before the transaction was executed.
void redoTransaction ()
 Executes the transaction again.
boolean merge (in nsITransaction aTransaction)
 Attempts to merge a transaction into "this" transaction.

Static Public Member Functions

static const nsIIDGetCID ()

Public Attributes

readonly attribute boolean isTransient
 The transaction's transient state.
readonly attribute DOMString txnDescription

Protected Attributes

nsCOMPtr< nsIDOMNodemNode
 the element to insert
nsCOMPtr< nsIDOMNodemParent
 the node into which the new node will be inserted
nsIEditormEditor
 the editor for this transaction
PRInt32 mOffset
 the index in mParent for the new node

Private Member Functions

 InsertElementTxn ()

Friends

class TransactionFactory

Detailed Description

A transaction that inserts a single element.

Definition at line 54 of file InsertElementTxn.h.


Constructor & Destructor Documentation

Definition at line 49 of file InsertElementTxn.cpp.

  : EditTxn()
{
}

Definition at line 73 of file InsertElementTxn.cpp.

{
}

Member Function Documentation

Executes the transaction.

Reimplemented from EditTxn.

Definition at line 77 of file InsertElementTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy) 
  { 
    nsCOMPtr<nsIContent>nodeAsContent = do_QueryInterface(mNode);
    nsCOMPtr<nsIContent>parentAsContent = do_QueryInterface(mParent);
    nsString namestr;
    mNode->GetNodeName(namestr);
    char* nodename = ToNewCString(namestr);
    printf("%p Do Insert Element of %p <%s> into parent %p at offset %d\n", 
           this, nodeAsContent.get(), nodename,
           parentAsContent.get(), mOffset); 
    nsMemory::Free(nodename);
  }
#endif

  if (!mNode || !mParent) return NS_ERROR_NOT_INITIALIZED;

  nsCOMPtr<nsIDOMNodeList> childNodes;
  nsresult result = mParent->GetChildNodes(getter_AddRefs(childNodes));
  if (NS_FAILED(result)) return result;
  nsCOMPtr<nsIDOMNode>refNode;
  if (childNodes)
  {
    PRUint32 count;
    childNodes->GetLength(&count);
    if (mOffset>(PRInt32)count) mOffset = count;
    // -1 is sentinel value meaning "append at end"
    if (mOffset == -1) mOffset = count;
    result = childNodes->Item(mOffset, getter_AddRefs(refNode));
    if (NS_FAILED(result)) return result; 
    // note, it's ok for mRefNode to be null.  that means append
  }

  mEditor->MarkNodeDirty(mNode);

  nsCOMPtr<nsIDOMNode> resultNode;
  result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode));
  if (NS_FAILED(result)) return result;
  if (!resultNode) return NS_ERROR_NULL_POINTER;

  // only set selection to insertion point if editor gives permission
  PRBool bAdjustSelection;
  mEditor->ShouldTxnSetSelection(&bAdjustSelection);
  if (bAdjustSelection)
  {
    nsCOMPtr<nsISelection> selection;
    result = mEditor->GetSelection(getter_AddRefs(selection));
    if (NS_FAILED(result)) return result;
    if (!selection) return NS_ERROR_NULL_POINTER;
    // place the selection just after the inserted element
    selection->Collapse(mParent, mOffset+1);
  }
  else
  {
    // do nothing - dom range gravity will adjust selection
  }
  return result;
}

Here is the call graph for this function:

static const nsIID& InsertElementTxn::GetCID ( ) [inline, static]

Reimplemented from EditTxn.

Definition at line 58 of file InsertElementTxn.h.

{ static const nsIID iid = INSERT_ELEMENT_TXN_CID; return iid; }

Here is the caller graph for this function:

NS_IMETHODIMP EditTxn::GetIsTransient ( PRBool aIsTransient) [inherited]

Reimplemented in JoinElementTxn, SetDocTitleTxn, and EditAggregateTxn.

Definition at line 66 of file EditTxn.cpp.

{
  if (aIsTransient)
    *aIsTransient = PR_FALSE;
  return NS_OK;
}
NS_IMETHODIMP InsertElementTxn::GetTxnDescription ( nsAString &  aTxnDescription)

Reimplemented from EditTxn.

Definition at line 158 of file InsertElementTxn.cpp.

{
  aString.AssignLiteral("InsertElementTxn");
  return NS_OK;
}
NS_IMETHODIMP InsertElementTxn::Init ( nsIDOMNode aNode,
nsIDOMNode aParent,
PRInt32  aOffset,
nsIEditor aEditor 
)

initialize the transaction.

Parameters:
aNodethe node to insert
aParentthe node to insert into
aOffsetthe offset in aParent to insert aNode

Definition at line 54 of file InsertElementTxn.cpp.

{
  NS_ASSERTION(aNode && aParent && aEditor, "bad arg");
  if (!aNode || !aParent || !aEditor)
    return NS_ERROR_NULL_POINTER;

  mNode = do_QueryInterface(aNode);
  mParent = do_QueryInterface(aParent);
  mOffset = aOffset;
  mEditor = aEditor;
  if (!mNode || !mParent || !mEditor)
    return NS_ERROR_INVALID_ARG;
  return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP InsertElementTxn::Merge ( nsITransaction aTransaction,
PRBool aDidMerge 
)

Reimplemented from EditTxn.

Definition at line 151 of file InsertElementTxn.cpp.

{
  if (aDidMerge)
    *aDidMerge = PR_FALSE;
  return NS_OK;
}
boolean nsITransaction::merge ( in nsITransaction  aTransaction) [inherited]

Attempts to merge a transaction into "this" transaction.

Both transactions must be in their undo state, doTransaction() methods already called. The transaction manager calls this method to coalesce a new transaction with the transaction on the top of the undo stack. This method returns a boolean value that indicates the merge result. A true value indicates that the transactions were merged successfully, a false value if the merge was not possible or failed. If true, the transaction manager will Release() the new transacton instead of pushing it on the undo stack.

Parameters:
aTransactionthe previously executed transaction to merge.

Executes the transaction again.

Can only be called on a transaction that was previously undone.

In most cases, the redoTransaction() method will actually call the doTransaction() method to execute the transaction again.

Restores the state to what it was before the transaction was executed.

Reimplemented from EditTxn.

Definition at line 138 of file InsertElementTxn.cpp.

{
#ifdef NS_DEBUG
  if (gNoisy) { printf("%p Undo Insert Element of %p into parent %p at offset %d\n", 
                       this, mNode.get(), mParent.get(), mOffset); }
#endif

  if (!mNode || !mParent) return NS_ERROR_NOT_INITIALIZED;

  nsCOMPtr<nsIDOMNode> resultNode;
  return mParent->RemoveChild(mNode, getter_AddRefs(resultNode));
}

Here is the call graph for this function:


Friends And Related Function Documentation

friend class TransactionFactory [friend]

Definition at line 99 of file InsertElementTxn.h.


Member Data Documentation

The transaction's transient state.

This attribute is checked by the transaction manager after the transaction's Execute() method is called. If the transient state is false, a reference to the transaction is held by the transaction manager so that the transactions' undoTransaction() and redoTransaction() methods can be called. If the transient state is true, the transaction manager returns immediately after the transaction's doTransaction() method is called, no references to the transaction are maintained. Transient transactions cannot be undone or redone by the transaction manager.

Definition at line 79 of file nsITransaction.idl.

the editor for this transaction

Definition at line 94 of file InsertElementTxn.h.

the element to insert

Definition at line 88 of file InsertElementTxn.h.

the index in mParent for the new node

Definition at line 97 of file InsertElementTxn.h.

the node into which the new node will be inserted

Definition at line 91 of file InsertElementTxn.h.

readonly attribute DOMString nsPIEditorTransaction::txnDescription [inherited]

Definition at line 50 of file nsPIEditorTransaction.idl.


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