QDomNode QDomNamedNodeMapProto:: removeNamedItem(const QString& name)
{
  QDomNamedNodeMap *item = qscriptvalue_cast<QDomNamedNodeMap*>(thisObject());
  if (item)
    return item->removeNamedItem(name);
  return QDomNode();
}
void DialogEditNodeTable::ApplyButtonPressed()
{
    XmlEditCommandAggregator* commandAggregator = new XmlEditCommandAggregator(m_pXmlDocument);

    //Get all the attributes
    QDomNamedNodeMap attributes = m_node.attributes();

    //First remove the attributes the isn't present now
    for(int i=attributes.size()-1; i >= 0; i--)
    {
        bool attributePresent = false;
        QString attributeName = attributes.item(i).nodeName();

        //For each present attribute in the table
        for(int k=0; k < m_pTable->rowCount() && !attributePresent; ++k)
        {
            QString tableAttributeName = m_pTable->item(k, 0)->text();

            if(tableAttributeName != "" && attributeName == tableAttributeName)
            {
                attributePresent = true;
            }
        }

        //If the attribute is no longer present
        if(!attributePresent)
        {
            //Remove from the list
            attributes.removeNamedItem(attributeName);
            //Add an action to remove it
            commandAggregator->AddCommand(new XmlEditCommandRemoveAttribute(m_pXmlDocument, m_identifierNumber, attributeName));
        }
    }

    //Now the attributesList contains all the attribute that is present in the node
    //In the table it can be present more attribute (newly added ones)

    //Add the attributes that isn't present yet (new attribute) and modify the attributes already present
    for(int k=0; k < m_pTable->rowCount(); ++k)
    {
        bool attributeAlreadyPresent = false;
        QString tableAttributeName = m_pTable->item(k, 0)->text();
        QString tableAttributeValue = m_pTable->item(k, 1)->text();

        QString previousAttributeValue;

        //Not add the empty attribute name
        if(tableAttributeName != "")
        {
            //For each attribute already present
            for(int i=0;i < attributes.size() && !attributeAlreadyPresent; ++i)
            {
                if(attributes.item(i).nodeName() == tableAttributeName)
                {
                    attributeAlreadyPresent = true;
                    previousAttributeValue = attributes.item(i).nodeValue();
                }
            }

            //If the attribute is not present yet
            if(!attributeAlreadyPresent)
            {
                //Add an action to add it
                commandAggregator->AddCommand(new XmlEditCommandAddAttribute(m_pXmlDocument, m_identifierNumber, tableAttributeName,
                                                                             tableAttributeValue));
            }
            else //The attribute is yet present so maybe its value is changed
            {
                //If the value of the already present attribute is changed
                if(previousAttributeValue != tableAttributeValue)
                {
                    //Add an action to change its value
                    commandAggregator->AddCommand(new XmlEditCommandEditAttribute(m_pXmlDocument, m_identifierNumber, tableAttributeName,
                                                                                 tableAttributeValue));
                }
            }
        }
    }

    const QString& currentValue = m_pValueLine->text();

    //If the value is changed set the new
    if(m_node.toElement().text() != currentValue)
    {
        commandAggregator->AddCommand(new XmlEditCommandSetNodeValue(m_pXmlDocument, m_identifierNumber, currentValue));
    }

    if(commandAggregator->Size() > 0)
    {
        m_pXmlEditCommandInvoker->ExecuteACommand( commandAggregator );
    }
    QDialog::accept();
}