/***************************************************************************\
 *                           Instance methods                              *
\***************************************************************************/
bool FieldEditorComponent::attachField(FieldContainer* fc, UInt32 fieldId, UInt32 index)
{
    //Cancel current editing
    cancelEditing();

    //Check that this is a valid FieldContainer
    if(fc == NULL)
    {
        SWARNING << "Cannot attach to a Null FieldContainer." << std::endl;
        return false;
    }

    //Check that this is a valid FieldId
    const FieldDescriptionBase* Desc(fc->getFieldDescription(fieldId));
    if(Desc == NULL)
    {
        SWARNING << "Cannot attach to field with id " << fieldId
                 << ", on a FieldContainer with type " << fc->getType().getCName()
                 << " because that is an invalid field id." << std::endl;
        return false;
    }
    //Check that this is a type to edit for this editor
    if(!isTypeEditable(Desc->getFieldType().getContentType()))
    {
        SWARNING << " Cannot attach to field " << Desc->getCName() 
                 << ", with content type " << Desc->getFieldType().getContentType().getCName()
                 << " because that content type is not supported by this editor type." << std::endl;
        return false;
    }

    //Check that this is a valid index
    GetFieldHandlePtr TheFieldHandle = fc->getField(fieldId);
    if(!TheFieldHandle.get())
    {
        SWARNING << "Cannot attach to field " << Desc->getCName() 
                 << ", on a FieldContainer with type " << fc->getType().getCName()
                 << " because no GetFieldHandle is defined for that field type." << std::endl;
        return false;
    }

    if(!internalAttachField(fc, fieldId, index))
    {
        return false;
    }

    //Attach to the field
    setEditingFC(fc);
    setEditingFieldId(fieldId);

    //Tell the Editor that the field has changed
    fieldChanged(fc, Desc->getFieldMask());

    //Attach to the Changed function callback for the container
    attachFieldCallback();

    return true;
}
void FCPtrFieldEditor::internalFieldChanged (void)
{
    GetFieldHandlePtr TheFieldHandle = getEditingFC()->getField(getEditingFieldId());

    assert(TheFieldHandle->getType().getClass() == FieldType::PtrField ||
           TheFieldHandle->getType().getClass() == FieldType::ChildPtrField);

    //Get the Editing FCPtr
    FieldContainer* EditingFC(NULL);
    if(TheFieldHandle->getCardinality() == FieldType::SingleField)
    {
        GetSFieldHandle<FieldContainerPtrSFieldBase>* ThePtrFieldHandle(dynamic_cast<GetSFieldHandle<FieldContainerPtrSFieldBase>*>(TheFieldHandle.get()));
        EditingFC = ThePtrFieldHandle->get();
    }
    else
    {
        GetMFieldHandle<FieldContainerPtrMFieldBase>* ThePtrFieldHandle(dynamic_cast<GetMFieldHandle<FieldContainerPtrMFieldBase>*>(TheFieldHandle.get()));
        EditingFC = ThePtrFieldHandle->get(getEditingFieldIndex());
    }

    //Update the Editing Text
    std::string EditingText("NULL");
    std::string Name("");
    std::string TypeName("");

    if(EditingFC != NULL)
    {
        //Get the Id of the FieldContainer
        EditingText = boost::lexical_cast<std::string>(EditingFC->getId());

        //If the FieldContainer has a name attachment then get the name
        if(EditingFC->getType().isDerivedFrom(AttachmentContainer::getClassType()) && 
           getName(dynamic_cast<AttachmentContainer*>(EditingFC)))
        {
            Name = getName(dynamic_cast<AttachmentContainer*>(EditingFC));
        }

        //Get the name of the type of the FieldContainer
        TypeName = EditingFC->getType().getName();
    }


    //Update the Labels and TextFields
    _EditingTextField->setText(EditingText);
    _NameTypeLabel->setText(Name + " [" + TypeName + "]");
}
void SetFieldValueCommand::execute(void)
{
    //Check for a valid Field Container
    if(_FC == NULL)
    {
        SWARNING << "FieldContainer is NULL." << std::endl;
        return;
    }

    //Check for valid Field
    GetFieldHandlePtr TheFieldHandle = _FC->getField(_FieldId);
    if(!TheFieldHandle->isValid())
    {
        SWARNING << "No Field with Id: " << _FieldId << " in FieldContainers of type " << _FC->getType().getName() << std::endl;
        return;
    }

    //Check for valid indexing
    if(TheFieldHandle->getCardinality() == FieldType::SingleField && _Index != 0)
    {
        SWARNING << "Cannot reference index " << _Index << ", on field " << TheFieldHandle->getDescription()->getName() 
                 << ", on FieldContianer of type " << _FC->getType().getName()
                 << " because that field is a SingleField." << std::endl;
        return;
    }
    else if(TheFieldHandle->getCardinality() == FieldType::MultiField && _Index >= TheFieldHandle->size())
    {
        SWARNING << "Cannot set the value of index " << _Index << ", on field " << TheFieldHandle->getDescription()->getName() 
                 << ", on FieldContianer of type " << _FC->getType().getName()
                 << " because that field has size " << TheFieldHandle->size() << std::endl;
        return;
    }


    //Get the previous value
    if(_PrevValue.empty())
    {
        std::ostringstream StrStream;
        OutStream TheOutStream(StrStream);
        if(TheFieldHandle->getCardinality() == FieldType::SingleField)
        {
            if(TheFieldHandle->isPointerField())
            {
                _PrevPtrValue = dynamic_cast<GetSFieldHandle<FieldContainerPtrSFieldBase>*>(TheFieldHandle.get())->get();
                if(dynamic_cast<GetSFieldHandle<FieldContainerPtrSFieldBase>*>(TheFieldHandle.get())->get())
                {
                    _PrevValue = boost::lexical_cast<std::string>(dynamic_cast<GetSFieldHandle<FieldContainerPtrSFieldBase>*>(TheFieldHandle.get())->get()->getId());
                }
                else
                {
                    _PrevValue = "0";
                }
            }
            else
            {
                TheFieldHandle->pushValueToStream(TheOutStream);
                _PrevValue = StrStream.str();
            }
        }
        else
        {
            if(TheFieldHandle->isPointerField())
            {
                _PrevPtrValue = dynamic_cast<GetMFieldHandle<FieldContainerPtrMFieldBase>*>(TheFieldHandle.get())->get(_Index);
                if(_PrevPtrValue)
                {
                    _PrevValue = boost::lexical_cast<std::string>(dynamic_cast<GetMFieldHandle<FieldContainerPtrMFieldBase>*>(TheFieldHandle.get())->get(_Index)->getId());
                }
                else
                {
                    _PrevValue = "0";
                }
            }
            else
            {
                TheFieldHandle->pushIndexedValueToStream(TheOutStream, _Index);
                _PrevValue = StrStream.str();
            }
        }

        //Remove quotes from strings
        if(TheFieldHandle->getType().getContentType() == FieldTraits<std::string>::getType())
        {
            _PrevValue = _PrevValue.substr(1,StrStream.str().size()-2);
        }
    }

    //Set the value
    if(TheFieldHandle->getCardinality() == FieldType::SingleField)
    {
        if(TheFieldHandle->isPointerField())
        {
            _PtrValue = FieldContainerFactory::the()->getContainer(boost::lexical_cast<UInt32>(_Value));
            
            //Check the pointer types match
            if(_PtrValue != NULL &&
                !isFieldContentDerivedFrom(TheFieldHandle->getType(),&_PtrValue->getType()))
            {
                SWARNING << "Cannot set the value of field " << TheFieldHandle->getDescription()->getName() 
                         << ", on FieldContianer of type " << _FC->getType().getName()
                         << " because the value attemting to be set is not derived from the type the field stores." << std::endl;
                return;
            }
            if(_PtrValue != _PrevPtrValue)
            {
                dynamic_cast<EditSFieldHandle<FieldContainerPtrSFieldBase>*>(_FC->editField(_FieldId).get())->set(_PtrValue);
            }
        }
        else
        {
            _FC->editField(_FieldId)->pushValueFromCString(_Value.c_str());
        }
    }
    else
    {
        if(TheFieldHandle->isPointerField())
        {
            _PtrValue = FieldContainerFactory::the()->getContainer(boost::lexical_cast<UInt32>(_Value));
            
            //Check the pointer types match
            if(_PtrValue != NULL && !isFieldContentDerivedFrom(TheFieldHandle->getType(),&_PtrValue->getType()))
            {
                SWARNING << "Cannot set the value of field " << TheFieldHandle->getDescription()->getName() 
                         << ", on FieldContianer of type " << _FC->getType().getName()
                         << " because the value attemting to be set is not derived from the type the field stores." << std::endl;
                return;
            }
            if(_PtrValue != _PrevPtrValue)
            {
                dynamic_cast<EditMFieldHandle<FieldContainerPtrMFieldBase>*>(_FC->editField(_FieldId).get())->replace(_Index, _PtrValue);
            }
        }
        else
        {
            _FC->editField(_FieldId)->pushIndexedValueFromCString(_Value.c_str(), _Index);
        }
    }

    Inherited::execute();
	_HasBeenDone = true;
}
/***************************************************************************\
 *                           Instance methods                              *
\***************************************************************************/
void GenericFieldEditor::internalFieldChanged (void)
{
    GetFieldHandlePtr TheFieldHandle = getEditingFC()->getField(getEditingFieldId());

    if(TheFieldHandle->isPointerField())
    {
        if(TheFieldHandle->getCardinality() == FieldType::SingleField)
        {
            GetSFieldHandle<FieldContainerPtrSFieldBase>* ThePtrFieldHandle(dynamic_cast<GetSFieldHandle<FieldContainerPtrSFieldBase>*>(TheFieldHandle.get()));
            if(ThePtrFieldHandle->get() != NULL)
            {
                _EditingTextField->setText(boost::lexical_cast<std::string>(ThePtrFieldHandle->get()->getId()));
            }
            else
            {
                _EditingTextField->setText("0");
            }
        }
        else
        {
            GetMFieldHandle<FieldContainerPtrMFieldBase>* ThePtrFieldHandle(dynamic_cast<GetMFieldHandle<FieldContainerPtrMFieldBase>*>(TheFieldHandle.get()));
            if(ThePtrFieldHandle->size() > getEditingFieldIndex() &&
               ThePtrFieldHandle->get(getEditingFieldIndex()) != NULL)
            {
                _EditingTextField->setText(boost::lexical_cast<std::string>(ThePtrFieldHandle->get(getEditingFieldIndex())->getId()));
            }
            else
            {
                _EditingTextField->setText("0");
            }
        }
    }
    else
    {
        std::ostringstream StrStream;
        OutStream TheOutStream(StrStream);
        if(TheFieldHandle->getCardinality() == FieldType::SingleField)
        {
            TheFieldHandle->pushValueToStream(TheOutStream);
        }
        else
        {
            TheFieldHandle->pushIndexedValueToStream(TheOutStream, getEditingFieldIndex());
        }

        //Remove quotes from strings
        if(TheFieldHandle->getType().getContentType() == FieldTraits<std::string>::getType())
        {
            _EditingTextField->setText(StrStream.str().substr(1,StrStream.str().size()-2));
        }
        else
        {
            _EditingTextField->setText(StrStream.str());
        }
    }

}