void PropertyGridContainerWidget::BuildPropertiesGrid(BaseMetadata* metaData,
        const METADATAPARAMSVECT& params)
{
    CleanupPropertiesGrid();

    // Metadata is state-aware.
    const Vector<UIControl::eControlState> &activeStates = PropertiesGridController::Instance()->GetActiveUIControlStates();
    metaData->SetUIControlStates(activeStates);

    this->activeWidgetsList = widgetsFactory.GetWidgets(metaData);
    for (PropertyGridWidgetsFactory::PROPERTYGRIDWIDGETSITER iter = activeWidgetsList.begin();
            iter != activeWidgetsList.end(); iter ++)
    {
        BasePropertyGridWidget* widget = (*iter);

        if (!dynamic_cast<StatePropertyGridWidget*>(*iter))
        {
            ui->scrollAreaLayout->addWidget(widget);
        }
        else
        {
            ui->topArea->show();
            ui->topAreaLayout->addWidget(widget);
        }
        widget->show();

        // Eaach widget should initialize itself by using Metadata (for property names) and
        // UI Control (for actual values).
        metaData->SetupParams(params);
        widget->Initialize(metaData);
    }

    // The Active Metatata is known.
    this->activeMetadata = metaData;
}
void PropertyGridContainerWidget::OnPropertiesGridUpdated()
{
    if (PropertiesGridController::Instance()->GetActiveTreeNodesList().empty() == false)
    {
        // List of UI Controls is selected.
        BuildPropertiesGridList();
        return;
    }
    
    // Build the Properties Grid based on the active control.
    const HierarchyTreeNode* activeTreeNode = PropertiesGridController::Instance()->GetActiveTreeNode();
    if (activeTreeNode == NULL)
    {
        // Nothing is selected - cleanup the properties grid.
        CleanupPropertiesGrid();
        return;
    }

    UIControl* activeControl = NULL;
    const HierarchyTreeControlNode* activeTreeControlNode = dynamic_cast<const HierarchyTreeControlNode*>(activeTreeNode);
    if (activeTreeControlNode)
    {
        activeControl = activeTreeControlNode->GetUIObject();
    }

    BaseMetadata* metaData = GetActiveMetadata(activeTreeNode);    
    BuildPropertiesGrid(activeControl, metaData, activeTreeNode->GetId());
}
void PropertyGridContainerWidget::OnUIControlsDeselected()
{
    // No Tree Node selected, try to build custom properties grid.
    CleanupPropertiesGrid();

    HierarchyTreeScreenNode* activeScreenNode = HierarchyTreeController::Instance()->GetActiveScreen();
    BuildCustomPropertiesGrid(activeScreenNode);
}