/***************************************************************************\
 *                           Instance methods                              *
\***************************************************************************/
bool FCPtrFieldEditor::internalAttachField (FieldContainer* fc, UInt32 fieldId, UInt32 index)
{
    Inherited::internalAttachField(fc, fieldId, index);
    if(!_FindFCStore)
    {
        const FieldContainerType* ThePtrType(getFieldContainerTypeFromPtrType(fc->getFieldDescription(fieldId)->getFieldType().getContentType()));
        _FindFCStore = _DefaultFindFCStorePrototype->clone();
    }
    return true;
}
void GenericMultiFieldEditor::insertAtIndex(FieldContainer* const fc,
                                            UInt32 fieldID,
                                            UInt32 index,
                                            const FieldContainerType* type,
                                            CommandManager* cmdMgr)
{
    const FieldContainerType* ThePtrType(getFieldContainerTypeFromPtrType(fc->getFieldDescription(fieldID)->getFieldType().getContentType()));

    //Create the Container
    CreateFieldContainerCommandPtr CreateCommand;
    
    //If the type is a node, then create a NodeCore instead
    //and then insert it as the core of a newly created node
    if(*ThePtrType == Node::getClassType())
    {
        CreateFieldContainerCommandPtr CreateCoreCommand = CreateFieldContainerCommand::create(type);
        cmdMgr->executeCommand(CreateCoreCommand);
        
        CreateCommand = CreateFieldContainerCommand::create(&Node::getClassType());
        cmdMgr->executeCommand(CreateCommand);

        dynamic_cast<Node*>(CreateCommand->getContainer())->setCore(dynamic_cast<NodeCore*>(CreateCoreCommand->getContainer()));
    }
    else
    {
        CreateCommand = CreateFieldContainerCommand::create(type);
        cmdMgr->executeCommand(CreateCommand);
    }
        
    EditFieldHandlePtr TheFieldHandle = fc->editField(fieldID);
    EditMFieldHandle<FieldContainerPtrMFieldBase>* TheHandle(dynamic_cast<EditMFieldHandle<FieldContainerPtrMFieldBase>*>(TheFieldHandle.get()));

    if(index >= fc->getField(fieldID)->size() ||
       !TheHandle->supportsInsert())
    {
        //Set the value of the field
        AddFieldElementCommandPtr AddCommand = AddFieldElementCommand::create(fc,
                                                                            fieldID,
                                                                            boost::lexical_cast<std::string>(CreateCommand->getContainer()->getId()));

        cmdMgr->executeCommand(AddCommand);
    }
    else
    {
        //Set the value of the field
        InsertFieldElementCommandPtr InsertIndexCommand = InsertFieldElementCommand::create(fc,
                                                                                            fieldID,
                                                                                            boost::lexical_cast<std::string>(CreateCommand->getContainer()->getId()),
                                                                                            index);

        cmdMgr->executeCommand(InsertIndexCommand);
    }
}
void FCPtrFieldEditor::openCreateHandler(void)
{
    const FieldContainerType* ThePtrType(getFieldContainerTypeFromPtrType(getEditingFC()->getFieldDescription(getEditingFieldId())->getFieldType().getContentType()));
    if(ThePtrType == NULL)
    {
        return;
    }

    //If the type is a node, then create a NodeCore instead
    //and then insert it as the core of a newly created node
    if(*ThePtrType == Node::getClassType())
    {
        ThePtrType = &NodeCore::getClassType();
    }

    std::vector<std::string> inputValues;
    UInt32 NumFieldContainersFound(0);
    const FieldContainerType* FoundType(NULL);
    for(UInt32 j(0) ; NumFieldContainersFound<FieldContainerFactory::the()->getNumTypes(); ++j)
    {
        FoundType = FieldContainerFactory::the()->findType(j);
        if(FoundType != NULL)
        {
            if(FoundType->isDerivedFrom(*ThePtrType)  && !FoundType->isAbstract())
            {
                inputValues.push_back(FoundType->getName());
            }
            ++NumFieldContainersFound;
        }
    }

    DialogWindowRefPtr TheDialog = DialogWindow::createInputDialog("Create Field Container",
                                                                   "Choose the type of object to create",
                                                                   DialogWindow::INPUT_COMBO,
                                                                   true,
                                                                   inputValues);
    _CreateContainerDialogClosedConnection = TheDialog->connectDialogWindowClosed(boost::bind(&FCPtrFieldEditor::handleCreateContainerDialogClosed, this, _1));

    Pnt2f CenteredPosition = calculateAlignment(Pnt2f(0.0f,0.0f), getParentWindow()->getParentDrawingSurface()->getSize(), TheDialog->getPreferredSize(), 0.5f, 0.5f);
    TheDialog->setPosition(CenteredPosition);
    TheDialog->setAllwaysOnTop(true);
    TheDialog->setModal(true);
    TheDialog->setResizable(true);

    getParentWindow()->getParentDrawingSurface()->openWindow(TheDialog);
}
void FCPtrFieldEditor::handleCreateContainerDialogClosed(DialogWindowEventDetails* const details)
{
    if(details->getOption() != DialogWindowEventDetails::DIALOG_OPTION_CANCEL)
    {
        const FieldContainerType* ThePtrType(getFieldContainerTypeFromPtrType(getEditingFC()->getFieldDescription(getEditingFieldId())->getFieldType().getContentType()));

        const FieldContainerType* TheType(FieldContainerFactory::the()->findType(details->getInput().c_str()));

        if(TheType != NULL)
        {
            //Create the Container
            CreateFieldContainerCommandPtr CreateCommand;
            
            //If the type is a node, then create a NodeCore instead
            //and then insert it as the core of a newly created node
            if(*ThePtrType == Node::getClassType())
            {
                CreateFieldContainerCommandPtr CreateCoreCommand = CreateFieldContainerCommand::create(TheType);
                getCommandManager()->executeCommand(CreateCoreCommand);
                
                CreateCommand = CreateFieldContainerCommand::create(&Node::getClassType());
                getCommandManager()->executeCommand(CreateCommand);

                dynamic_cast<Node*>(CreateCommand->getContainer())->setCore(dynamic_cast<NodeCore*>(CreateCoreCommand->getContainer()));
            }
            else
            {
                CreateCommand = CreateFieldContainerCommand::create(TheType);
                getCommandManager()->executeCommand(CreateCommand);
            }

            //Set the value of the field
            SetFieldValueCommandPtr SetCommand =
                SetFieldValueCommand::create(getEditingFC(),
                                             getEditingFieldId(),
                                             boost::lexical_cast<std::string>(CreateCommand->getContainer()->getId()),
                                             getEditingFieldIndex());

            getCommandManager()->executeCommand(SetCommand);
        }
    }
    _CreateContainerDialogClosedConnection.disconnect();
}
void FCPtrFieldEditor::openFindContainerHandler(void)
{
    const FieldContainerType* ThePtrType(getFieldContainerTypeFromPtrType(getEditingFC()->getFieldDescription(getEditingFieldId())->getFieldType().getContentType()));
    if(ThePtrType == NULL)
    {
        return;
    }

    std::vector<std::string> inputValues;
    _FindFCStore->setTypeToStore(ThePtrType);
    FCPtrEditorStore::FieldContianerVector fcStore(_FindFCStore->getList());

    std::string value;
    for(UInt32 i(0) ; i<fcStore.size(); ++i)
    {
        value.clear();
        if(fcStore[i]->getType().isDerivedFrom(AttachmentContainer::getClassType()) &&
           getName(dynamic_pointer_cast<AttachmentContainer>(fcStore[i])))
        {
            value += std::string(getName(dynamic_pointer_cast<AttachmentContainer>(fcStore[i]))) + " ";
        }
        value += "[" + fcStore[i]->getType().getName() + "] " + boost::lexical_cast<std::string>(fcStore[i]->getId());
        inputValues.push_back(value);
    }

    DialogWindowRefPtr TheDialog = DialogWindow::createInputDialog("Find Field Container",
                                                                   "Choose the container to use",
                                                                   DialogWindow::INPUT_LIST,
                                                                   true,
                                                                   inputValues);
    _FindContainerDialogClosedConnection = TheDialog->connectDialogWindowClosed(boost::bind(&FCPtrFieldEditor::handleFindContainerDialogClosed, this, _1));

    Pnt2f CenteredPosition = calculateAlignment(Pnt2f(0.0f,0.0f), getParentWindow()->getParentDrawingSurface()->getSize(), TheDialog->getPreferredSize(), 0.5f, 0.5f);
    TheDialog->setPosition(CenteredPosition);
    TheDialog->setAllwaysOnTop(true);
    TheDialog->setResizable(true);

    getParentWindow()->getParentDrawingSurface()->openWindow(TheDialog);
}
void GenericMultiFieldEditor::insertAtIndex(FieldContainer* const fc,
                                            UInt32 fieldID,
                                            UInt32 index,
                                            UIDrawingSurface* const drawingSurface,
                                            CommandManager* cmdMgr)
{
    //Is this a pointer field
    GetFieldHandlePtr TheFieldHandle(fc->getField(fieldID));
    if(TheFieldHandle->isPointerField())
    {
        //Create
        const FieldContainerType* ThePtrType(getFieldContainerTypeFromPtrType(fc->getFieldDescription(fieldID)->getFieldType().getContentType()));
        if(ThePtrType == NULL)
        {
            return;
        }

        //If the type is a node, then create a NodeCore instead
        //and then insert it as the core of a newly created node
        if(*ThePtrType == Node::getClassType())
        {
            ThePtrType = &NodeCore::getClassType();
        }

        std::vector<std::string> inputValues;
        UInt32 NumFieldContainersFound(0);
        const FieldContainerType* FoundType(NULL);
        for(UInt32 j(0) ; NumFieldContainersFound<FieldContainerFactory::the()->getNumTypes(); ++j)
        {
            FoundType = FieldContainerFactory::the()->findType(j);
            if(FoundType != NULL)
            {
                if(FoundType->isDerivedFrom(*ThePtrType)  && !FoundType->isAbstract())
                {
                    inputValues.push_back(FoundType->getName());
                }
                ++NumFieldContainersFound;
            }
        }

        if(inputValues.size() == 1)
        {
            insertAtIndex(fc,fieldID,index,FieldContainerFactory::the()->findType(inputValues[0].c_str()),cmdMgr);
        }
        else
        {
            DialogWindowRefPtr TheDialog = DialogWindow::createInputDialog("Create " + fc->getFieldDescription(fieldID)->getFieldType().getContentType().getName(),
                                                                           "Choose the type of object to create",
                                                                           DialogWindow::INPUT_COMBO,
                                                                           true,
                                                                           inputValues);
            TheDialog->connectDialogWindowClosed(boost::bind(&GenericMultiFieldEditor::handleInsertFCPtrDialogClosed,
                                                             _1,
                                                             fc,
                                                             fieldID,
                                                             index,
                                                             cmdMgr));

            Pnt2f CenteredPosition = calculateAlignment(Pnt2f(0.0f,0.0f), drawingSurface->getSize(), TheDialog->getPreferredSize(), 0.5f, 0.5f);
            TheDialog->setPosition(CenteredPosition);
            TheDialog->setAllwaysOnTop(true);
            TheDialog->setModal(true);
            TheDialog->setResizable(true);

            drawingSurface->openWindow(TheDialog);
        }
    }
    else
    {
        //Insert the default
        InsertFieldElementCommandPtr InsertIndexCommand = InsertFieldElementCommand::create(fc, fieldID, "", index);

        cmdMgr->executeCommand(InsertIndexCommand);
    }
}