/***************************************************************************\
 *                           Instance methods                              *
\***************************************************************************/
void BoolFieldEditor::internalFieldChanged (void)
{

    GetFieldHandlePtr TheFieldHandle = getEditingFC()->getField(getEditingFieldId());
    bool Value;

    if(TheFieldHandle->getCardinality() == FieldType::SingleField)
    {
        Value = static_cast<const SFBool*>(TheFieldHandle->getField())->getValue();
    }
    else
    {
        Value = static_cast<const MFBool*>(TheFieldHandle->getField())->operator[](getEditingFieldIndex());
    }

    if(_EditingCheckbox->getSelected() != Value)
    {
        _ButtonSelectedConnection.disconnect();
        _ButtonDeselectedConnection.disconnect();

        _EditingCheckbox->setSelected(Value);
    }

    _ButtonSelectedConnection = _EditingCheckbox->connectButtonSelected(boost::bind(&BoolFieldEditor::handleButtonSelected, this, _1));
    _ButtonDeselectedConnection = _EditingCheckbox->connectButtonDeselected(boost::bind(&BoolFieldEditor::handleButtonDeselected, this, _1));

}
void BoolFieldEditor::runCommand  (bool value)
{
    bool fieldValue;
    GetFieldHandlePtr TheFieldHandle = getEditingFC()->getField(getEditingFieldId());

    if(TheFieldHandle->getCardinality() == FieldType::SingleField)
    {
        fieldValue = static_cast<const SFBool*>(TheFieldHandle->getField())->getValue();
    }
    else
    {
        fieldValue = static_cast<const MFBool*>(TheFieldHandle->getField())->operator[](getEditingFieldIndex());
    }

    if(value != fieldValue)
    {
        //Call the command to set the Field
        SetFieldValueCommandPtr SetCommand = SetFieldValueCommand::create(getEditingFC(), 
                                                                          getEditingFieldId(), 
                                                                          (value ? "TRUE" : "FALSE"), 
                                                                          getEditingFieldIndex());

        getCommandManager()->executeCommand(SetCommand);
    }
}
OSG_BEGIN_NAMESPACE

/*---------------------------------------------------------------------*/
/*! \name Connection handling                                          */
/*! \{                                                                 */

/*! \ingroup GrpBaseFieldContainerConnector
    \relatesalso AttachmentContainer
 */

bool addConnection(      OSG::AttachmentContainer *pSrcContainer,
                         const OSG::Char8               *szSrcName,
                         OSG::FieldContainer      *pDstContainer,
                         const OSG::Char8               *szDstName    )
{
    if(pSrcContainer == NULL || szSrcName == NULL ||
            pDstContainer == NULL || szDstName == NULL  )
    {
        return false;
    }

    const FieldDescriptionBase *pSrcDesc = NULL;
    const FieldDescriptionBase *pDstDesc = NULL;

    GetFieldHandlePtr pSrcHnd = pSrcContainer->getField(szSrcName);
    GetFieldHandlePtr pDstHnd = pDstContainer->getField(szDstName);

    if(pSrcHnd != NULL && pSrcHnd->isValid() == true)
    {
        pSrcDesc = pSrcHnd->getDescription();
    }

    if(pDstHnd != NULL && pDstHnd->isValid() == true)
    {
        pDstDesc = pDstHnd->getDescription();
    }

    // check core for node
    if(pSrcDesc == NULL)
    {
        Node *pNode = dynamic_cast<Node *>(pSrcContainer);

        if(pNode != NULL && pNode->getCore() != NULL)
        {
            pSrcHnd = pNode->getCore()->getField(szSrcName);

            if(pSrcHnd != NULL && pSrcHnd->isValid() == true)
            {
                pSrcDesc = pSrcHnd->getDescription();
            }
        }
    }

    // same here
    if(pDstDesc == NULL)
    {
        Node *pNode = dynamic_cast<Node *>(pDstContainer);

        if(pNode != NULL && pNode->getCore() != NULL)
        {
            pDstHnd = pNode->getCore()->getField(szDstName);

            if(pDstHnd != NULL && pDstHnd->isValid() == true)
            {
                pDstDesc = pDstHnd->getDescription();
            }
        }
    }

    if(pSrcDesc == NULL || pDstDesc == NULL)
    {
        FWARNING(("addConnection: Failed to obtain field descriptions for "
                  "source container [%p] field [%s] desc [%p] - "
                  "destination container [%p] field [%s] desc [%p]\n",
                  static_cast<void *>(pSrcContainer),
                  szSrcName,
                  static_cast<const void *>(pSrcDesc),
                  static_cast<void *>(pDstContainer),
                  szDstName,
                  static_cast<const void *>(pDstDesc)                      ));

        return false;
    }

    const Field *pSrcField = pSrcHnd->getField();
    Field *pDstField = const_cast<Field *>(pDstHnd->getField());

    pSrcContainer =
        dynamic_cast<AttachmentContainer *>(pSrcHnd->getContainer());

    pDstContainer =
        dynamic_cast<FieldContainer      *>(pDstHnd->getContainer());

    if(pSrcContainer == NULL || pDstContainer == NULL)
    {
        FWARNING(("addConnection: Failed to obtain field handles for "
                  "source container [%p] - destination container [%p]\n",
                  static_cast<void *>(pSrcContainer),
                  static_cast<void *>(pDstContainer)));

        return false;
    }

    BasicFieldConnector *pConn = pSrcDesc->createConnector(pSrcField,
                                 pDstDesc,
                                 pDstField);

    if(pConn != NULL)
    {
        pConn->setTargetContainer(pDstContainer);

        addConnector(pSrcContainer, pConn);
    }

    return true;
}