/*----------------------------- class specific ----------------------------*/
void GenericFieldContainerEditor::onCreate(const GenericFieldContainerEditor *Id)
{
	Inherited::onCreate(Id);
    if(Id != NULL)
    {
        _BoldFont = UIFont::create();
        UIFontUnrecPtr ProtoFont(dynamic_cast<Label*>(Label::getClassType().getPrototype())->getFont());
        _BoldFont->setFamily(ProtoFont->getFamily());
        _BoldFont->setGlyphPixelSize(ProtoFont->getGlyphPixelSize());
        _BoldFont->setSize(ProtoFont->getSize());
        _BoldFont->setStyle(TextFace::STYLE_BOLD);

        BorderLayoutConstraintsRefPtr WestConstraint = BorderLayoutConstraints::create();
        WestConstraint->setRegion(BorderLayoutConstraints::BORDER_WEST);

        BorderLayoutConstraintsRefPtr NorthConstraint = BorderLayoutConstraints::create();
        NorthConstraint->setRegion(BorderLayoutConstraints::BORDER_NORTH);

        BorderLayoutConstraintsRefPtr CenterConstraint = BorderLayoutConstraints::create();
        CenterConstraint->setRegion(BorderLayoutConstraints::BORDER_CENTER);

        BorderLayoutConstraintsRefPtr SouthConstraint = BorderLayoutConstraints::create();
        SouthConstraint->setRegion(BorderLayoutConstraints::BORDER_SOUTH);

        //Backgournds
        ColorLayerRefPtr HeaderBgLayer = ColorLayer::create();
        HeaderBgLayer->setColor(Color4f(0.7f,0.7f,0.7f,1.0f));

        //Type Panel
        _ContainerTypeLabel = Label::create();
        _ContainerTypeLabel->setAlignment(Vec2f(0.5f,0.5f));
        _ContainerTypeLabel->setBackgrounds(HeaderBgLayer);
        _ContainerTypeLabel->setConstraints(WestConstraint);
        _ContainerTypeLabel->setPreferredSize(Vec2f(160.0f,22.0f));

        _ContainerIdLabel = Label::create();
        _ContainerIdLabel->setAlignment(Vec2f(0.5f,0.5f));
        _ContainerIdLabel->setBackgrounds(HeaderBgLayer);
        _ContainerIdLabel->setConstraints(CenterConstraint);


        BorderLayoutRefPtr TheBorderLayout = BorderLayout::create();
        PanelRefPtr TypePanel = Panel::createEmpty();
        TypePanel->setPreferredSize(Vec2f(160.0f,33.0f));
        TypePanel->setInset(Vec4f(1.0f,1.0f,1.0f,10.0f));
        TypePanel->pushToChildren(_ContainerIdLabel);
        TypePanel->pushToChildren(_ContainerTypeLabel);
        TypePanel->setLayout(TheBorderLayout);
        TypePanel->setConstraints(NorthConstraint);
        pushToChildren(TypePanel);

        //Fields Panel
        GridBagLayoutRecPtr AllFieldsPanelLayout = GridBagLayout::create();
        AllFieldsPanelLayout->setColumns(1);

        _FieldsContainer = Panel::createEmpty();
        _FieldsContainer->setConstraints(CenterConstraint);
        _FieldsContainer->setLayout(AllFieldsPanelLayout);
        if(getShowFields())
        {
            pushToChildren(_FieldsContainer);
        }

        //Events Panel
        GridLayoutRecPtr AllProducedEventsPanelLayout = GridLayout::create();
        AllProducedEventsPanelLayout->setColumns(1);
        AllProducedEventsPanelLayout->setHorizontalGap(0.0f);
        AllProducedEventsPanelLayout->setVerticalGap(0.0f);

        _ProducedEventsContainer = Panel::createEmpty();
        _ProducedEventsContainer->setConstraints(SouthConstraint);
        _ProducedEventsContainer->setLayout(AllProducedEventsPanelLayout);
        _ProducedEventsContainer->setInset(Vec4f(0.0f,0.0f,15.0f,0.0f));
        if(getShowEvents())
        {
            pushToChildren(_ProducedEventsContainer);
        }

        //Main Layout
        BorderLayoutRefPtr MainLayout = BorderLayout::create();
        setLayout(MainLayout);

        _GenericNameAttachmentEditor = GenericNameAttachmentEditor::create();
    }
}
/*-------------------------------------------------------------------------*\
 -  private                                                                 -
\*-------------------------------------------------------------------------*/
void GenericFieldContainerEditor::updateFieldsPanel(FieldContainer* fc)
{
    _FieldsContainer->clearChildren();

    UInt32 NumFields(fc->getType().getNumFieldDescs());
    FieldDescriptionBase* Desc;
    FieldEditorComponentUnrecPtr TheEditor;
    LabelUnrecPtr TheLabel;
    ComponentRecPtr TheToolTip;
    GridBagLayoutConstraintsRefPtr LayoutConstraints;
    PanelRefPtr FieldPanel;
    UInt32 NumRows(0),NumRowsForField(1);

    BorderLayoutRefPtr TheBorderLayout = BorderLayout::create();
    BorderLayoutConstraintsRefPtr WestConstraint = BorderLayoutConstraints::create();
    WestConstraint->setRegion(BorderLayoutConstraints::BORDER_WEST);
    BorderLayoutConstraintsRefPtr CenterConstraint = BorderLayoutConstraints::create();
    CenterConstraint->setRegion(BorderLayoutConstraints::BORDER_CENTER);

    //Backgrounds
    ColorLayerRefPtr HeaderBgLayer = ColorLayer::create();
    HeaderBgLayer->setColor(Color4f(0.7f,0.7f,0.7f,1.0f));

    ColorLayerRefPtr LightBgLayer = ColorLayer::create();
    LightBgLayer->setColor(Color4f(0.9f,0.9f,0.9f,1.0f));
    ColorLayerRefPtr DarkBgLayer = ColorLayer::create();
    DarkBgLayer->setColor(Color4f(0.8f,0.8f,0.8f,1.0f));

    LayoutConstraints = GridBagLayoutConstraints::create();
    LayoutConstraints->setGridX(0);
    LayoutConstraints->setGridY(NumRows);
    LayoutConstraints->setGridHeight(1);
    LayoutConstraints->setGridWidth(2);
    LayoutConstraints->setFill(GridBagLayoutConstraints::FILL_BOTH);

    LabelRecPtr FieldsLabel = Label::create();
    FieldsLabel->setAlignment(Vec2f(0.5f,0.5f));
    FieldsLabel->setText("Fields");
    FieldsLabel->setBackgrounds(HeaderBgLayer);
    FieldsLabel->setConstraints(LayoutConstraints);
    FieldsLabel->setFont(_BoldFont);

    _FieldsContainer->pushToChildren(FieldsLabel);
    ++NumRows;

    if(_GenericNameAttachmentEditor->isTypeEditable(fc->getType()))
    {
        //Create the Label
        TheLabel = Label::create();
        TheLabel->setText("Name");
        TheLabel->setBackgrounds(NULL);
        TheLabel->setConstraints(WestConstraint);
        TheLabel->setPreferredSize(Vec2f(160.0f,22.0f));

        //Attach the Generic Name Editor
        _GenericNameAttachmentEditor->setCommandManager(_CmdManager);
        _GenericNameAttachmentEditor->attachContainer(fc);
        _GenericNameAttachmentEditor->setConstraints(CenterConstraint);

        //Create the Panel
        LayoutConstraints = GridBagLayoutConstraints::create();
        LayoutConstraints->setGridX(0);
        LayoutConstraints->setGridY(NumRows);
        LayoutConstraints->setGridHeight(1);
        LayoutConstraints->setGridWidth(1);
        LayoutConstraints->setFill(GridBagLayoutConstraints::FILL_BOTH);

        FieldPanel = Panel::createEmpty();
        FieldPanel->setInset(Vec4f(1.0f,1.0f,1.0f,1.0f));
        FieldPanel->pushToChildren(TheLabel);
        FieldPanel->pushToChildren(_GenericNameAttachmentEditor);
        FieldPanel->setLayout(TheBorderLayout);
        FieldPanel->setConstraints(LayoutConstraints);
        FieldPanel->setBackgrounds(LightBgLayer);

        _FieldsContainer->pushToChildren(FieldPanel);
        ++NumRows;
    }

    UInt32 UsedFieldCount(0);
    for(UInt32 i(1) ; i<=NumFields ; ++i)
    {
        Desc = fc->getFieldDescription(i);
        if(Desc != NULL &&
           !Desc->isInternal() &&
           Desc->getFieldType().getClass() != FieldType::ParentPtrField &&
           //HACK: Stop the pixel field from being editable on Images
           !(fc->getType().isDerivedFrom(Image::getClassType()) &&
             Desc->getFieldId() == Image::PixelFieldId))
        {
            //Create the Editor
            TheEditor = FieldEditorFactory::the()->createDefaultEditor(fc, Desc->getFieldId(), _CmdManager);
            if(TheEditor != NULL)
            {
                NumRowsForField = TheEditor->getNumRequestedRows();
                pushToEditors(TheEditor);
                TheEditor->setConstraints(CenterConstraint);

                //Create the Label
                TheLabel = Label::create();
                TheLabel->setText(Desc->getCName());
                TheLabel->setBackgrounds(NULL);
                TheLabel->setConstraints(WestConstraint);
                TheLabel->setPreferredSize(Vec2f(160.0f,22.0f));
                TheToolTip = createFieldToolTip(Desc);
                TheLabel->setToolTip(TheToolTip);

                //Create the Panel
                LayoutConstraints = GridBagLayoutConstraints::create();
                LayoutConstraints->setGridX(0);
                LayoutConstraints->setGridY(NumRows);
                LayoutConstraints->setGridHeight(NumRowsForField);
                LayoutConstraints->setGridWidth(1);
                LayoutConstraints->setFill(GridBagLayoutConstraints::FILL_BOTH);


                FieldPanel = Panel::createEmpty();
                FieldPanel->setInset(Vec4f(1.0f,1.0f,1.0f,1.0f));
                FieldPanel->pushToChildren(TheLabel);
                FieldPanel->pushToChildren(TheEditor);
                FieldPanel->setLayout(TheBorderLayout);
                FieldPanel->setConstraints(LayoutConstraints);
                if((UsedFieldCount%2) == 0)
                {
                    FieldPanel->setBackgrounds(DarkBgLayer);
                }
                else
                {
                    FieldPanel->setBackgrounds(LightBgLayer);
                }

                _FieldsContainer->pushToChildren(FieldPanel);
                NumRows += NumRowsForField;
                TheEditor->setPreferredSize(Vec2f(50.0f,22.0f * NumRowsForField));
                ++UsedFieldCount;
            }
        }
    }

    //Set the number of rows for the grid layout
    dynamic_cast<GridBagLayout*>(_FieldsContainer->getLayout())->setRows(NumRows);
    _FieldsContainer->setPreferredSize(Vec2f(400.0f, NumRows*24.0f));
}