//----------------------------------------------------------------------------------  
CBaseEditor *OgitorsClipboardManager::paste(CBaseEditor *parent, int index)
{
    ObjectCopyData *data = 0;

    assert(index < (int)mBuffer.size());

    if(index == -1)
        data = mBuffer[mBuffer.size() - 1];
    else
        data = mBuffer[index];

    CBaseEditor* object = OgitorsRoot::getSingletonPtr()->FindObject(data->mObjectName);
    
    OgitorsPropertyValueMap properties = data->mProperties;
    if(object)
    {
        Ogre::String newname = data->mObjectName + "Copy";
        newname += OgitorsRoot::getSingletonPtr()->CreateUniqueID(newname, "", 0);
        properties["name"].val = Ogre::Any(newname);
    }

    object = OgitorsRoot::getSingletonPtr()->CreateEditorObject(parent, data->mObjectTypeName, properties, true, false);
    if(!object)
        return 0;

    object->getCustomProperties()->initFromSet(data->mCustomProperties);
    object->load();
    OgitorsRoot::getSingletonPtr()->GetSelection()->setSelection(object);

    return object;
}
//----------------------------------------------------------------------------------
CBaseEditor *OgitorsClipboardManager::instantiateTemplate(const Ogre::String& templatename)
{
    CBaseEditor *retObject = OgitorsRoot::getSingletonPtr()->GetSelection();

    CBaseEditor *item = 0;
    ObjectTemplateMap::const_iterator it;

    it = mGeneralTemplates.find(templatename);
    if(it == mGeneralTemplates.end())
    {
        it = mProjectTemplates.find(templatename);
        if(it == mProjectTemplates.end())
            return 0;
    }

    ObjectTemplate objTemplate = it->second;
    OgitorsPropertyValueMap::iterator pit;
    NameObjectPairList list;
    NameObjectPairList objlist;
    std::vector<CBaseEditor*> objlist2;
    NameObjectPairList::iterator nit;

    Ogre::String parentname;

    int numParentObjects = 0;

    for(unsigned int i = 0;i < objTemplate.size();i++)
    {
        OgitorsPropertyValueMap objMap = objTemplate[i].mObjectProperties;
        if((pit = objMap.find("parentnode")) != objMap.end())
        {
            parentname = Ogre::any_cast<Ogre::String>(pit->second.val);
            if((nit = list.find(parentname)) != list.end())
            {
                pit->second.val = Ogre::Any(nit->second->getName());
            }
            else
            {
                objMap.erase(pit);
                ++numParentObjects;
            }
        }
        else
            ++numParentObjects;

        parentname = Ogre::any_cast<Ogre::String>(objMap["name"].val);
        objMap["name"].val = Ogre::Any(parentname + OgitorsRoot::getSingletonPtr()->CreateUniqueID(parentname,"",0));

        item = OgitorsRoot::getSingletonPtr()->CreateEditorObject(0, objTemplate[i].mTypeName, objMap, true, false);

        if(item)
        {
            item->load();
            item->getCustomProperties()->initFromSet(*(objTemplate[i].mCustomProperties));
            list.insert(NameObjectPairList::value_type(parentname, item));
            objlist.insert(NameObjectPairList::value_type(item->getName(), item));
            objlist2.push_back(item);
        }
    }

    Ogre::Vector3 pos(999999, 999999, 999999);

    if(numParentObjects == 0)
        return 0;
    else if(numParentObjects == 1)
    {
        retObject = objlist2[0];
        OgitorsRoot::getSingletonPtr()->GetSelection()->setSelection(retObject);
    }
    else
        static_cast<CMultiSelEditor*>(retObject)->setSelection(objlist);


    if(retObject->getProperties()->hasProperty("position"))
        retObject->getProperties()->setValue("position", pos);

    return retObject;
}