/*
 *  Copyright 2018 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * This UI helper provides a dialog box to create and edit a metadata
 */
Ext.define('Ametys.plugins.contenttypeseditor.editor.EditMetadataHelper',  {
    singleton: true,
    
    /**
     * @property {Function} _cbFn The callback function invoked when a metadata is created/edited
     * @private
     */
    
    /**
     * @property {Ametys.window.DialogBox} _box The dialog box for creating/editing a metadata.
     * @private
     */
    
    /**
     * @property {String} _mode The current edition mode ('new' or 'edit')
     * @private
     */
    
    /**
     * @property {Ext.form.Panel} _generalInformationForm The general information panel
     * @private
     */
    
    /**
     * @property {Ext.form.Panel} _validatorInformationForm The panel to inquire validation information
     * @private
     */
    
    /**
     * @property {Ext.form.Panel} _enumerationInformationForm The panel to inquire enumeration information
     * @private
     */
    
    /**
     * @property {Ext.form.Panel} _repeaterInformationForm The panel to inquire repeater information
     * @private
     */
    
    /**
     * Configure and open the dialog box
     * @param {Object} config The configuration options :
     * @param {String} config.title The title of dialog box
     * @param {String} config.iconCls The CSS class for the icon of the dialog box
     * @param {String} config.metadataInfo All information about the metadata to edit
     * @param {String} config.mode The mode. Can only be 'add' or 'edit.
     * @param {String} config.focusField The field to focus.
     * @param {String[]} config.metadataNames Names of metadata used for this content type.
     * @param {Function} config.callback The callback function invoked when a new content type is created. The callback function will received the following parameters:
     * @param {Object} config.callback.contentTypeInfos All information about a content type 
     */
    open: function(config)
    {
        this._mode = config.mode || 'add';
        this._cbFn = config.callback;
        this._metadataNames = config.metadataNames;
        this._createDialogBox(config.title, config.iconCls, config.focusField);
        if (this._mode == 'edit')
        {
            this._type = config.metadataInfo.type;
            this._widgetParams = config.metadataInfo.widgetParams;
            this._enumerationValues = config.metadataInfo.defaultEnumerator;
            this._fillFields(config.metadataInfo);
        }
        else
        {
            this._type = null;
            this._widgetParams = null;
            this._enumerationValues = null;
        }
        this._box.show();
        this._updateButtons();
    },
    
     /**
     * @private
     * Fills the forms of the dialog box with values.
     * @param {Object} metadataInfo The data to fill
     */
    _fillFields: function(metadataInfo)
    {
        this._generalInformationForm.getForm().findField("metadata-type").setValue(metadataInfo.type);
        this._generalInformationForm.getForm().findField("metadata-type").disable();
        this._generalInformationForm.getForm().findField("metadata-content-type").setValue(metadataInfo.linkedContentType);
        this._generalInformationForm.getForm().findField("metadata-content-type").disable();
        this._generalInformationForm.getForm().findField("metadata-invert").setValue(metadataInfo.invertRelationPath);
        this._generalInformationForm.getForm().findField("metadata-invert").disable();
        this._generalInformationForm.getForm().findField("metadata-name").setValue(metadataInfo.name);
        this._generalInformationForm.getForm().findField("metadata-name").disable();
        this._generalInformationForm.getForm().findField("metadata-label").setValue(metadataInfo.label);
        this._generalInformationForm.getForm().findField("metadata-description").setValue(metadataInfo.description);
        this._generalInformationForm.getForm().findField("metadata-multiple").setValue(metadataInfo.multiple);
        this._generalInformationForm.getForm().findField("metadata-multiple").disable();
        
        if (metadataInfo.type === "repeater")
        {
            this._repeaterInformationForm.getForm().findField("metadata-initialize-size").setValue(metadataInfo.initializeSize);
            this._repeaterInformationForm.getForm().findField("metadata-min-size").setValue(metadataInfo.minSize);
            this._repeaterInformationForm.getForm().findField("metadata-max-size").setValue(metadataInfo.maxSize);
            this._repeaterInformationForm.getForm().findField("metadata-add-label").setValue(metadataInfo.addLabel);
            this._repeaterInformationForm.getForm().findField("metadata-delete-label").setValue(metadataInfo.deleteLabel);
            this._repeaterInformationForm.getForm().findField("metadata-header-label").setValue(metadataInfo.headerLabel);
            this._generalInformationForm.getForm().findField("metadata-enumeration").setDisabled(true);
            this._generalInformationForm.getForm().findField("metadata-widget-name").setDisabled(true);
            this._generalInformationForm.getComponent("metadata-widget-params").setDisabled(true);
        } 
        else if (metadataInfo.type === "composite")
        {
            this._generalInformationForm.getForm().findField("metadata-enumeration").setDisabled(true);
            this._generalInformationForm.getForm().findField("metadata-widget-name").setDisabled(true);
            this._generalInformationForm.getComponent("metadata-widget-params").setDisabled(true);
        }
        else
        {
            this._generalInformationForm.getForm().findField("metadata-widget-name").setValue(metadataInfo.widget);
            
	        if (metadataInfo.validator)
	        {
                if (metadataInfo.defaultValidator)
                {
                    this._displayDefaultValidator(true);
                    this._displayCustomValidator(false);
                    this._validatorInformationForm.getForm().findField("metadata-validator").setValue("default-validator");
                    this._validatorInformationForm.getForm().findField("metadata-mandatory").setValue(metadataInfo.defaultValidator.mandatory);
                    this._validatorInformationForm.getForm().findField("metadata-regexp").setValue(metadataInfo.defaultValidator.regexp);
                    this._validatorInformationForm.getForm().findField("metadata-invalid-text").setValue(metadataInfo.defaultValidator.invalidText);
                }
                else
                {
                    this._displayDefaultValidator(true);    
                    this._displayCustomValidator(false);
                    this._validatorInformationForm.getForm().findField("metadata-validator").setValue("custom-validator");
                    this._validatorInformationForm.getForm().findField("metadata-custom-validator-class").setValue(metadataInfo.customValidatorClass);
                    this._validatorInformationForm.getForm().findField("metadata-custom-validator-configuration").setValue(metadataInfo.customValidatorConfiguration);
                }
	        }
            else
            {
                this._validatorInformationForm.getForm().findField("metadata-validator").setValue("no-validation");
            }
            
            if (metadataInfo.enumerated)
            {
                if (metadataInfo.customEnumeratorClass)
                {
                    this._displayDefaultEnumerator(false);
                    this._displayCustomEnumerator(true);
                    this._generalInformationForm.getForm().findField("metadata-enumeration").setValue("custom-enumeration");
                    this._generalInformationForm.getForm().findField("metadata-custom-enumerator-class").setValue(metadataInfo.customEnumeratorClass);
                    this._generalInformationForm.getForm().findField("metadata-custom-enumerator-configuration").setValue(metadataInfo.customEnumeratorConfiguration);
                }
                else
                {
                    this._displayCustomEnumerator(false);
                    this._displayDefaultEnumerator(true);
                    this._generalInformationForm.getForm().findField("metadata-enumeration").setValue("static-enumeration");
                }
            }
            else
            {
                this._displayCustomEnumerator(false);
                this._displayDefaultEnumerator(false);
                this._generalInformationForm.getForm().findField("metadata-enumeration").setValue("not-enumerated");
            }
        } 
    },

    /**
     * @private
     * Created the dialog box
     * @param {String} title The dialog title
     * @param {String} iconCls The css class for icon
     * @param {String} focusField The field name to focus
     */
    _createDialogBox: function(title, iconCls, focusField)
    {
        this._cards = [this._createFirstCard(), this._createSecondCard(), this._createFifthCard()];
        
        this._box = Ext.create('Ametys.window.DialogBox', {
            title : title || "",
            iconCls: iconCls || 'ametysicon-add64',
            
            width: 650,
            height: 650,
            layout: {
                type: 'card'
            },
            
            defaultFocus: this._generalInformationForm.getForm().findField(focusField),
            items: this._cards,
            
            referenceHolder: true,
            defaultButton: 'next',
            
            closeAction: 'destroy',
            
            buttons: [{
                itemId: 'button-previous',
                text: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ACTIONS_PREVIOUS}}",
                disabled: true,
                handler: this._previous,
                scope: this
            }, {
                reference: 'next',
                itemId: 'button-next',
                text: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ACTIONS_NEXT}}",
                handler: this._next,
                scope: this
            }, {
                reference: 'finish',
                itemId: 'button-finish',
                text: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ACTIONS_FINISH}}",
                handler: this._finish,
                scope: this
            }, {
                itemId: 'button-cancel',
                text: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ACTIONS_CANCEL}}",
                handler: this._cancel,
                scope: this
            }]
        })
    },
    
    /**
     * General information card
     */
    _createFirstCard: function()
    {
        this._generalInformationForm = Ext.create('Ext.form.Panel', {
            scrollable: true,
            defaults: {
                cls: 'ametys',
                labelSeparator: '',
                labelAlign: 'top',
                labelStyle: 'font-weight: bold',
                labelWidth: 120,
                width: '100%',
                msgTarget: 'side'
            },
            
            items: [
                {
                    xtype: "component",
                    cls: "a-text",
                    html: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_GENERAL_INFO_HINT}}"
                },
                {
	                xtype: "textfield",
	                fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_NAME_INPUT_LABEL}}" + " *",
	                ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_NAME_INPUT_DESCRIPTION}}",
	                allowBlank: false,
                    name: "metadata-name",
                    id: "metadata-name",
                    listeners:
                    {
                        'change' : this._onChangeName,
                        scope: this
                    },
                    regex: /^[a-zA-Z]((?!__)[a-zA-Z0-9_-])*$/  
                },
                {
                    xtype: "enhancedmultilingualstring",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_LABEL_INPUT_LABEL}} *",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_LABEL_INPUT_DESCRIPTION}}",
                    allowBlank: false,
                    name: 'metadata-label',
                    id: 'metadata-label'
                },
                {
                    xtype: "enhancedmultilingualstring",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_DESCRIPTION_INPUT_LABEL}} *",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_DESCRIPTION_INPUT_DESCRIPTION}}",
                    allowBlank: false,
                    name: 'metadata-description',
                    id: 'metadata-description'
                },
                {
                    xtype: "combobox",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_INPUT_LABEL}} *",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_INPUT_DESCRIPTION}}",
                    allowBlank: false,
                    name: 'metadata-type',
                    id: 'metadata-type',
                    valueField: 'id',
                    value: 'string',
                    displayField: 'label',
                    editable: true,
                    forceSelection: true,
                    queryMode: 'local',
                    listeners: 
                    {
                        'select' : this._onTypeSelected,
                        scope: this
                    
                    },
                    store: 
                    {
                        autoLoad: true,
                        proxy: {
                            type: 'ametys',
                            role: 'org.ametys.plugins.contenttypeseditor.edition.EditContentTypeInformationHelper',
                            methodName: 'getAttributeType',
                            methodArguments: []
                        },
                        sorters: [{property: 'label', direction: 'ASC'}],
                        fields: ['id', 'label']
                    }
                },
                {
                    xtype: "edition.select-content-types",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_CONTENT_TYPE_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_CONTENT_TYPE_DESCRIPTION}}",
                    multiple: "false",
                    name: 'metadata-content-type',
                    id: 'metadata-content-type',
                    disabled: true,
                    hidden: true,
                    listeners:
                    {
                        'change' : this._onContentTypeChanged,
                        scope: this
                    }
                },
                {
                    xtype: "combobox",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_INVERT_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_INVERT_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadata-invert',
                    id: 'metadata-invert',
                    valueField: 'name',
                    displayField: 'name',
                    editable: false,
                    forceSelection: true,
                    queryMode: 'local',
                    disabled: true,
                    hidden: true,
                    store: 
                    {
                        autoLoad: false,
                        sorters: [{property: 'name', direction: 'ASC'}],
                        fields: ['name'],
                        listeners:
                        {
                            'beforeload' : this._onBeforeLoadInvert,
                            scope: this
                        }
                    }
                },
                {
                    xtype: "checkbox",
                    boxLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_MULTIPLE_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_MULTIPLE_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: "metadata-multiple",
                    id: "metadata-multiple"
                },
                {
                    xtype: "combobox",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ENUMERATION_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ENUMERATION_INPUT_DESCRIPTION}}",
                    allowBlank: false,
                    name: "metadata-enumeration",
                    id: "metadata-enumeration",
                    valueField: 'id',
                    displayField: 'label',
                    value: 'not-enumerated',
                    editable: true,
                    forceSelection: true,
                    queryMode: 'local',
                    store: {
                        autoLoad: true,
                        sorters: [{property: 'label'}],
                        fields: [
                            {name: 'id'},
                            {name: 'label'}
                        ],
                        data : [
                            {id: 'not-enumerated', label: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_NO_ENUMERATION_INPUT_LABEL}}"},
                            {id: 'static-enumeration', label: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_STATIC_ENUMERATION_INPUT_LABEL}}"},
                            {id: 'custom-enumeration', label: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_CUSTOM_ENUMERATION_INPUT_LABEL}}"}
                        ]
                    },
                    listeners:
                    {
                        'select' : this._onEnumerationSelected,
                        scope: this
                    }
                },
                {
                    xtype: "button",
                    text: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ENUMERATION_VALUES_BUTTON_LABEL}}",
                    tooltip: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ENUMERATION_VALUES_BUTTON_DESCRIPTION}}",
                    id: "metadata-enumeration-values",
                    disabled: true,
                    hidden: true,
                    listeners: {
                        click: Ext.bind(this._onEnumerationValuesClick, this)
                    }
                },
                {
                    xtype: "textfield",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_CUSTOM_ENUMERATION_CLASS_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_CUSTOM_ENUMERATION_CLASS_DESCRIPTION}}",
                    allowBlank: true,
                    name: "metadata-custom-enumerator-class",
                    id: "metadata-custom-enumerator-class",
                    disabled: true,
                    hidden: true
                },
                {
                    xtype: "textarea",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_CUSTOM_VALIDATOR_CONFIGURATION_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_CUSTOM_VALIDATOR_CONFIGURATION_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadata-custom-enumerator-configuration',
                    id: 'metadata-custom-enumerator-configuration',
                    disabled: true,
                    hidden: true
                },                
                {
                    xtype: "combobox",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_WIDGET_NAME_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_WIDGET_NAME_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: "metadata-widget-name",
                    id: "metadata-widget-name",
                    valueField: 'id',
                    displayField: 'label',
                    editable: true,
                    forceSelection: true,
                    queryMode: 'local',
                    store: 
                    {
                        autoLoad: true,
                        proxy: {
                            type: 'ametys',
                            role: 'org.ametys.plugins.contenttypeseditor.edition.EditContentTypeInformationHelper',
                            methodName: 'getWidgets',
                            methodArguments: []
                        },
                        sorters: [{property: 'label', direction: 'ASC'}],
                        fields: ['id', 'label']
                    }
                },
                {
                    xtype: "button",
                    text: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_WIDGET_PARAMS_BUTTON_LABEL}}",
                    tooltip: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_WIDGET_PARAMS_BUTTON_DESCRIPTION}}",
                    allowBlank: true,
                    id: "metadata-widget-params",
                    name: "metadata-widget-params",
                    listeners: {
                        click: this._onWidgetParamsClick,
                        scope: this
                    }
                }                
            ]
        });
        return this._generalInformationForm;
    },
    
    /**
     * @private
     * Listener when a new name is entered for a metadata
     * @param {Ext.form.field.Text} textfield The text field
     * @param {String} newValue The newly entered value
     */
    _onChangeName: function(textfield, newValue)
    {
        if (newValue && newValue.length > 0 && this._metadataNames && !this._isValidMetadataName(newValue))
        {
            textfield.markWarning("{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_NAME_ERROR_LABEL}}");
        }        
        else
        {
            textfield.clearWarning();
        }
    },
    
    /**
     * @private
     * Listener when the content type is changed
     */
    _onContentTypeChanged: function()
    {
        if (this._mode != 'edit')
        {
            this._generalInformationForm.getForm().findField("metadata-invert").getStore().load();
        }
    },
    
    /**
     * @private
     * Listener before the load of the store of the field that handle invert metadata
     * @param {Ext.data.Store} store The metadata store
     */
    _onBeforeLoadInvert: function(store)
    {
        if (this._mode != 'edit')
        {
            var contentType = this._generalInformationForm.getForm().findField("metadata-content-type").getValue();
            if (contentType)
            {
                Ametys.data.ServerComm.callMethod({
                    role: "org.ametys.plugins.contenttypeseditor.edition.EditContentTypeInformationHelper",
                    methodName: "getAttributePaths",
                    parameters: [contentType],
                    callback: {
                        handler: function(metadataPaths) {
                            store.setData(metadataPaths);
                        }
                    }
                }); 
            }
        }
    },
    
    /**
     * Validation card
     */
    _createSecondCard: function()
    {
        this._validatorInformationForm = Ext.create('Ext.form.Panel', {
            scrollable: true,
            defaults: {
                cls: 'ametys',
                labelSeparator: '',
                labelAlign: 'top',
                labelStyle: 'font-weight: bold',
                labelWidth: 120,
                width: '100%',
                msgTarget: 'side'
            },
            
            items: [
                {
                    xtype: "component",
                    cls: "a-text",
                    html: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_VALIDATION_HINT}}"
                },
                {
                    xtype: "combobox",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_VALIDATOR_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_VALIDATOR_INPUT_DESCRIPTION}}",
                    allowBlank: false,
                    name: "metadata-validator",
                    id: "metadata-validator",
                    valueField: 'id',
                    displayField: 'label',
                    value: 'no-validation',
                    editable: true,
                    forceSelection: true,
                    queryMode: 'local',
                    store: {
                        autoLoad: true,
                        sorters: [{property: 'label', direction: 'ASC'}],
                        fields: [
                            {name: 'id'},
                            {name: 'label', type: 'string'}
                        ],
                        data : [
                            {id: 'no-validation', label: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_NO_VALIDATION_INPUT_LABEL}}"},
                            {id: 'default-validator', label: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_DEFAULT_VALIDATOR_INPUT_LABEL}}"},
                            {id: 'custom-validator', label: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_CUSTOM_VALIDATOR_INPUT_LABEL}}"}
                        ]
                    },
                    listeners:
                    {
                        'select' : this._onValidationSelected,
                        scope: this
                    }
                },
                {
                    xtype: "checkbox",
                    boxLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_MANDATORY_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_MANDATORY_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: "metadata-mandatory",
                    id: "metadata-mandatory",
                    disabled: true,
                    hidden: true,
                    listeners:
                    {
                        'change' : this._onChangeMandatory,
                        scope: this
                    }
                },
                {
                    xtype: "textfield",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_REGEXP_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_REGEXP_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadata-regexp',
                    id: 'metadata-regexp',
                    disabled: true,
                    hidden: true
                },
                {
                    xtype: "enhancedmultilingualstring",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_INVALID_TEXT_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_INVALID_TEXT_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadata-invalid-text',
                    id: 'metadata-invalid-text',
                    disabled: true,
                    hidden: true
                },
                {
                    xtype: "textfield",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_CUSTOM_VALIDATOR_CLASS_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_CUSTOM_VALIDATOR_CLASS_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadata-custom-validator-class',
                    id: 'metadata-custom-validator-class',
                    disabled: true,
                    hidden: true
                },
                {
                    xtype: "textarea",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_CUSTOM_VALIDATOR_CONFIGURATION_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_TYPE_CUSTOM_VALIDATOR_CONFIGURATION_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadata-custom-validator-configuration',
                    id: 'metadata-custom-validator-configuration',
                    disabled: true,
                    hidden: true
                }
            ]
        });
        return this._validatorInformationForm;
    },
    
    /**
     * @private
     * Listener when the validator select has changed
     * @param {Ext.form.field.ComboBox} combo Fires when at least one list item is selected.
     * @param {Ext.data.Model} record The value will be a single record
     */
    _onValidationSelected: function(combo, record)
    {
        if (record.id == 'no-validation')
        {
            // Not validator
            this._displayDefaultValidator(false);
            this._displayCustomValidator(false);
        }
        else if (record.id == 'default-validator')
        {
            // Default validator
            this._displayDefaultValidator(true);
            this._displayCustomValidator(false);
        }
        else
        {
            // Custom validator
            this._displayCustomValidator(true);
            this._displayDefaultValidator(false); 
        }
    },
    
    /**
     * Listener when the mandatory checkbox has chaned
     * @param {Ext.form.field.Checkbox} checkbox The field
     * @param {Boolean} value The new value
     */
    _onChangeMandatory: function(checkbox, value)
    {
        if (value)
        {
            checkbox.markWarning("{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_METADATA_MANDATORY_INPUT_WARNING}}");
        }        
        else
        {
            checkbox.clearWarning();
        }
    },
    
    /**
     * @private
     * Display or hide the "default" validator fields
     * @param {Boolean} display true to display it
     */
    _displayDefaultValidator: function(display)
    {
        this._validatorInformationForm.getForm().findField("metadata-mandatory").setValue("");
        this._validatorInformationForm.getForm().findField("metadata-regexp").setValue("");
        this._validatorInformationForm.getForm().findField("metadata-invalid-text").setValue("");
        
        this._validatorInformationForm.getForm().findField("metadata-mandatory").setHidden(!display);
        this._validatorInformationForm.getForm().findField("metadata-regexp").setHidden(!display);
        this._validatorInformationForm.getForm().findField("metadata-invalid-text").setHidden(!display);
        
        this._validatorInformationForm.getForm().findField("metadata-mandatory").setDisabled(!display);
        this._validatorInformationForm.getForm().findField("metadata-regexp").setDisabled(!display);
        this._validatorInformationForm.getForm().findField("metadata-invalid-text").setDisabled(!display);
    },
    
    /**
     * @private
     * Display or hide the "custom" validator fields
     * @param {Boolean} display true to display it
     */
    _displayCustomValidator: function(display)
    {
        this._validatorInformationForm.getForm().findField("metadata-custom-validator-class").setValue("");
        this._validatorInformationForm.getForm().findField("metadata-custom-validator-configuration").setValue();
        
        this._validatorInformationForm.getForm().findField("metadata-custom-validator-class").setHidden(!display);
        this._validatorInformationForm.getForm().findField("metadata-custom-validator-configuration").setHidden(!display);
        
        this._validatorInformationForm.getForm().findField("metadata-custom-validator-class").setDisabled(!display);
        this._validatorInformationForm.getForm().findField("metadata-custom-validator-configuration").setDisabled(!display);
    },
   
    /**
     * @private
     * Listener when clicking on the button to edit widget params
     */
    _onWidgetParamsClick: function()
    {
        var config = 
        {
            title: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_WIDGET_PARAMS_TITLE}}",
            isMultilingual: true,
            columnLabelName: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_WIDGET_PARAMS_GRID_COL_LABEL}}",
            columnValueName: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_WIDGET_PARAMS_GRID_COL_VALUE}}",
            columnLabelRegexp: "",
            columnValueRegexp: "",
            addButtonDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_WIDGET_PARAMS_GRID_ACTION_ADD_DESCRIPTION}}",
            removeButtonDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_WIDGET_PARAMS_GRID_ACTION_DEL_DESCRIPTION}}",
            defaultLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_WIDGET_PARAMS_GRID_ACTION_ADD_DEFAULTLABEL}}",
            defaultValue: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_WIDGET_PARAMS_GRID_ACTION_ADD_DEFAULTVALUE}}",
            values: this._widgetParams,
            callback: Ext.bind(this._onWidgetParamsClickCb, this)
        };
        Ametys.plugins.contenttypeseditor.form.field.SelectValuesHelper.create(config);
    },
    
    /**
     * @private
     * Callback when the widget params are modified
     * @param {Object[]} widgetParams An array of object with 'label' and 'value'
     */
    _onWidgetParamsClickCb: function(widgetParams)
    {
        this._widgetParams = widgetParams;
    },
    
    /**
     * @private
     * Listener when the button to edit enumeration values is clicked
     */
    _onEnumerationValuesClick: function()
    {
        var config = 
        {
            title: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_ENUMERATION_VALUES_TITLE}}",
            isMultilingual: true,
            columnLabelName: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_ENUMERATION_VALUES_GRID_COL_LABEL}}",
            columnValueName: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_ENUMERATION_VALUES_GRID_COL_VALUE}}",
            columnLabelRegexp: "",
            columnValueRegexp: "",
            addButtonDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_ENUMERATION_VALUES_GRID_ACTION_ADD_DESCRIPTION}}",
            removeButtonDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_ENUMERATION_VALUES_GRID_ACTION_DEL_DESCRIPTION}}",
            defaultLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_ENUMERATION_VALUES_GRID_ACTION_ADD_DEFAULTLABEL}}",
            defaultValue: "{{i18n PLUGINS_CONTENTTYPESEDITOR_SELECT_VALUES_HELPER_ENUMERATION_VALUES_GRID_ACTION_ADD_DEFAULTVALUE}}",
            values: this._enumerationValues,
            callback: Ext.bind(this._onEnumerationValuesClickCb, this)
        };
        Ametys.plugins.contenttypeseditor.form.field.SelectValuesHelper.create(config);
    },
    
    /**
     * @private
     * Callback when the enumerations are modified
     * @param {Object[]} enumerationValues An array of object with 'label' and 'value'
     */
    _onEnumerationValuesClickCb: function(enumerationValues)
    {
        this._enumerationValues = enumerationValues;
    },
    
     /**
     * @private
     * Fires when at least one list item is selected.
     * @param {Ext.form.field.ComboBox} combo The combobox
     * @param {Ext.data.Model} record The value will be a single record
     */
    _onEnumerationSelected: function(combo, record)
    {
        if (record.id == 'not-enumerated')
        {
            // Not validator
            this._displayDefaultEnumerator(false);
            this._displayCustomEnumerator(false);   
        }
        else if (record.id == 'static-enumeration')
        {
            // Static enumeration
            this._displayDefaultEnumerator(true);
            this._displayCustomEnumerator(false); 
            
        }
        else
        {
            // Custom enumeration
            this._displayDefaultEnumerator(false);
            this._displayCustomEnumerator(true); 
        }
    },
    
    /**
     * @private
     * Display all the fields related the enumerator when it is "custom"
     * @param {Boolean} display true to display, false to hide
     */
    _displayCustomEnumerator: function(display)
    {
        this._generalInformationForm.getForm().findField("metadata-custom-enumerator-configuration").setValue("");
        this._generalInformationForm.getForm().findField("metadata-custom-enumerator-class").setValue("");
        
        this._generalInformationForm.getForm().findField("metadata-custom-enumerator-class").setHidden(!display);
        this._generalInformationForm.getForm().findField("metadata-custom-enumerator-configuration").setHidden(!display);
        
        this._generalInformationForm.getForm().findField("metadata-custom-enumerator-class").setDisabled(!display);
        this._generalInformationForm.getForm().findField("metadata-custom-enumerator-configuration").setDisabled(!display);
    },
    
    /**
     * @private
     * Display all the fields related the enumerator when it is "default"
     * @param {Boolean} display true to display, false to hide
     */
    _displayDefaultEnumerator: function(display)
    {
        this._generalInformationForm.down('#metadata-enumeration-values').setHidden(!display);
        this._generalInformationForm.down('#metadata-enumeration-values').setDisabled(!display);
    },
    
    /**
     * Repeater card
     */
    _createFifthCard: function()
    {
        this._repeaterInformationForm = Ext.create('Ext.form.Panel', {
            scrollable: true,
            defaults: {
                cls: 'ametys',
                labelSeparator: '',
                labelAlign: 'top',
                labelStyle: 'font-weight: bold',
                labelWidth: 120,
                width: '100%',
                msgTarget: 'side'
            },
            
            items: [
                {
                    xtype: "component",
                    cls: "a-text",
                    html: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_REPEATER_HINT}}"
                },
                {
                    xtype: "enhancedmultilingualstring",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ADD_LABEL_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_ADD_LABEL_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: "metadata-add-label",
                    id: "metadata-add-label"
                },
                {
                    xtype: "enhancedmultilingualstring",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_DELETE_LABEL_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_DELETE_LABEL_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: "metadata-delete-label",
                    id: "metadata-delete-label"
                },
                {
                    xtype: "textfield",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_HEADER_LABEL_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_HEADER_LABEL_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: "metadata-header-label",
                    id: "metadata-header-label"
                },
                {
                    xtype: "numberfield",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_MIN_SIZE_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_MIN_SIZE_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    minValue: 0,
                    value: 0,
                    name: "metadata-min-size",
                    id: "metadata-min-size"
                },
                {
                    xtype: "numberfield",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_MAX_SIZE_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_MAX_SIZE_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    minValue: -1,
                    value: -1,
                    name: "metadata-max-size",
                    id: "metadata-max-size"
                },
                {
                    xtype: "numberfield",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_INITIALIZE_SIZE_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATA_DIALOG_INITIALIZE_SIZE_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    minValue: 0,
                    value: 1,
                    name: "metadata-initialize-size",
                    id: "metadata-initialize-size"
                }
            ]
        });
        return this._repeaterInformationForm;
    },
    
    /**
     * Get the current active card
     * @return {Ext.container.Container} the active card
     */
    _getCurrentCard: function()
    {
        var currentCardIndex = this._getCurrentCardIndex();
        return this._getCard(currentCardIndex);
    },
    
    /**
     * Get card to the specified index
     * @param {Number} index The card index
     * @return {Ext.container.Container} the card if found.
     */
    _getCard: function(index)
    {
        return this._cards[index];
    },
    
    /**
     * Get the index of the current active card
     * @return {Number} the index
     */
    _getCurrentCardIndex: function()
    {
        return this._box.items.indexOf(this._box.getLayout().activeItem);
    },
    
    /**
     * Get the number of cards
     * @return {Number} the number of cards
     */
    _getCardsCount: function()
    {
        return this._cards.length;
    },
    
    /**
     * Return true if the card is the last one
     * @param {Number} cardIndex The index
     * @return {Boolean} True if the card is the last one
     */
    _isLastCard: function(cardIndex)
    {
        var lastIndex;
        if (this._type === "repeater")
        {
            var lastIndex = this._getCardsCount() - 1;
        } 
        else if (this._type === "composite")
        {
            lastIndex = 0;
        }
        else
        {
            var lastIndex = this._getCardsCount() - 2;
        }
        return (lastIndex == cardIndex);
    },
    
    
    /**
     * @private
     * Get the index of previous card
     * @return the index of previous card or -1 if there is no previous card
     */
    _getPreviousCardIndex: function()
    {
        var currentCardIndex = this._getCurrentCardIndex();
        if (this._type === "repeater" && currentCardIndex == 2)
        {
            return 0;
        }
        else
        {
            return currentCardIndex - 1; 
        }
        
    },
    
     /**
     * Go to the next card
     */
    _goToNextCard: function()
    {
        var nextCardIndex = this._getNextCardIndex();

        this._showCard(nextCardIndex);
    },
    
    /**
     * @private
     * Get the index of next card
     * @return the index of next card or -1 if there is no next card
     */
    _getNextCardIndex: function()
    {
        var currentCardIndex = this._getCurrentCardIndex();
        if (this._isLastCard(currentCardIndex))
        {
            return -1;
        }
        else
        {
            if (this._type === "repeater")
            {
                return 2;
            }
            else
            {
                return currentCardIndex + 1;
            }
        }
    },
    
    /**
     * @private
     * Show card to the specified index
     * @param {Number} index The index of card to activate
     */
    _showCard: function(index)
    {
        this._box.getLayout().setActiveItem(index); 
        this._updateButtons();
    },
    
    /**
     * Fires when at least one list item is selected.
     * @param {Ext.form.field.ComboBox} combo The combo box
     * @param {Ext.data.Model} record A single record
     */
    _onTypeSelected: function(combo, record)
    {
        this._type = record.id;
        
        if (this._type == "content")
        {
            this._generalInformationForm.getForm().findField("metadata-content-type").setHidden(false);
            this._generalInformationForm.getForm().findField("metadata-content-type").setDisabled(false);
            this._generalInformationForm.getForm().findField("metadata-invert").setHidden(false);
            this._generalInformationForm.getForm().findField("metadata-invert").setDisabled(false);
        }
        else
        {
            this._generalInformationForm.getForm().findField("metadata-content-type").setValue("");
            this._generalInformationForm.getForm().findField("metadata-content-type").setHidden(true);
            this._generalInformationForm.getForm().findField("metadata-content-type").setDisabled(true);
            this._generalInformationForm.getForm().findField("metadata-invert").setValue("");
            this._generalInformationForm.getForm().findField("metadata-invert").setHidden(true);
            this._generalInformationForm.getForm().findField("metadata-invert").setDisabled(true);
        }
        
        if (this._type == "repeater" || this._type == "composite")
        {
            this._generalInformationForm.getForm().findField("metadata-enumeration").setValue('not-enumerated');
            this._generalInformationForm.getForm().findField("metadata-enumeration").setDisabled(true);
            this._generalInformationForm.getForm().findField("metadata-widget-name").setValue(false);
            this._generalInformationForm.getForm().findField("metadata-widget-name").setDisabled(true);
            this._generalInformationForm.getComponent("metadata-widget-params").setDisabled(true);
            this._generalInformationForm.getForm().findField("metadata-multiple").setValue(false);
            this._generalInformationForm.getForm().findField("metadata-multiple").setDisabled(true);
        }
        else
        {
            this._generalInformationForm.getForm().findField("metadata-enumeration").setDisabled(false);
            this._generalInformationForm.getForm().findField("metadata-widget-name").setDisabled(false);
            this._generalInformationForm.getComponent("metadata-widget-params").setDisabled(false);
            this._generalInformationForm.getForm().findField("metadata-multiple").setDisabled(false);
        }
        
        this._updateButtons();
    },
    
    /**
     * @private
     * Update dialog box buttons
     */
    _updateButtons: function()
    {
        var hasAPreviousCard = this._getPreviousCardIndex() != -1;
        var hasANextCard= this._getNextCardIndex() != -1;
        
        // Buttons
        var previousButton = this._box.down("button[itemId='button-previous']");
        var nextButton = this._box.down("button[itemId='button-next']");
        
        previousButton.setDisabled(!hasAPreviousCard);
        nextButton.setDisabled(!hasANextCard);
        
        if (this._type === "composite")
        {
            nextButton.setDisabled(true);
        }
        else if (this._isLastCard(this._getCurrentCardIndex()))
        {
            nextButton.setDisabled(true);
        }
    },
    
    
    /**
     * Handler when clicking on 'previous' button
     */
    _previous: function()
    {
        var isValidForm = this._getCurrentCard().isValid();
        if (!isValidForm)
        {
            return;
        }
        
        var previousCardIndex = this._getPreviousCardIndex();
        this._showCard(previousCardIndex);
    },
    
    /**
     * Handler when clicking on 'next' button.
     */
    _next: function()
    {
        var isValidForm = this._getCurrentCard().isValid();
        if (this._getCurrentCardIndex() == 0 && this._mode == 'add')
        {
            isValidForm = this._isValidMetadataName();
        }
        if (!isValidForm)
        {
            return;
        }
        
        var currentCard = this._getCurrentCard();
        this._goToNextCard();
    },
    
    /**
     * Handler when clicking on 'finish' button.
     */
    _finish: function()
    {
        var isValidForm = this._getCurrentCard().isValid();
        if (this._mode == 'add')
        {
            isValidForm = this._isValidMetadataName();
        }
        if (!isValidForm)
        {
            return;
        }
        this._validate();
    },
    
    /**
     * @private
     * Ensure the name is valid (unused)
     * @param {String} value The new metadataname
     * @return {Boolean} true if the new name is valid
     */
    _isValidMetadataName: function(value)
    {
        if (this._metadataNames)
        {
	        var metadataName = "";
	        if (value)
	        {
	            metadataName = value;
	        }
	        else
	        {
	            metadataName = this._generalInformationForm.getForm().findField("metadata-name").getRawValue();
	        }
	        return !Ext.Array.contains(this._metadataNames, metadataName.toLowerCase()); 
        }
        else
        {
            return true;
        }
    },
    
    /**
     * Handler for 'Ok' action
     */
    _validate: function()
    {
        if (Ext.isFunction(this._cbFn))
        {
            var values = this._getFormValues();
            this._cbFn(values);
        }
        this._enumerationValues = null;
        this._widgetParams = null;
        this._box.close();
    },
    
    /**
     * @private
     * Gets the whole values of the form (from all the cards of the dialog box)
     * @return {Object} The form values
     */
    _getFormValues: function()
    {
        var values = {
            dataType: "model_item",
            id: this._generalInformationForm.getForm().findField("metadata-name").getValue(),
            name: this._generalInformationForm.getForm().findField("metadata-name").getValue(),
            label: this._generalInformationForm.getForm().findField("metadata-label").getValue(),
            description: this._generalInformationForm.getForm().findField("metadata-description").getValue(),
            type: this._generalInformationForm.getForm().findField("metadata-type").getValue(),
            multiple: this._generalInformationForm.getForm().findField("metadata-multiple").getValue(),
            leaf: false
        };
        
        var linkedContentType = this._generalInformationForm.getForm().findField("metadata-content-type").getValue();
        if (linkedContentType)
        {
            var widgetStore = this._generalInformationForm.getForm().findField("metadata-content-type").getStore();
            
            values.linkedContentType = {
                id: linkedContentType,
                label: widgetStore.getById(linkedContentType).data.label
            };
            
            values.invertRelationPath = this._generalInformationForm.getForm().findField("metadata-invert").getValue()
        }
        
        if (this._type === "repeater")
        {
            values.type = "repeater";
            values.initializeSize = this._repeaterInformationForm.getForm().findField("metadata-initialize-size").getValue(),
            values.minSize = this._repeaterInformationForm.getForm().findField("metadata-min-size").getValue(),
            values.maxSize = this._repeaterInformationForm.getForm().findField("metadata-max-size").getValue(),
            values.addLabel = this._repeaterInformationForm.getForm().findField("metadata-add-label").getValue(),
            values.deleteLabel = this._repeaterInformationForm.getForm().findField("metadata-delete-label").getValue(),
            values.headerLabel = this._repeaterInformationForm.getForm().findField("metadata-header-label").getValue()
        } 
        else if (this._type !== "composite")
        {
            values.leaf = true;
            values.children = [];
            values.widget = this._generalInformationForm.getForm().findField("metadata-widget-name").getValue();
            values.widgetParams = this._widgetParams;
            
            var validator = this._validatorInformationForm.getForm().findField("metadata-validator").getValue();
            values.mandatory = this._validatorInformationForm.getForm().findField("metadata-mandatory").getValue();
            if (validator == 'no-validation')
            {
                values.validator = false;
            }
            else if (validator == 'default-validator')
            {
                values.validator = true;
                values.defaultValidator = {};
                values.defaultValidator.mandatory = this._validatorInformationForm.getForm().findField("metadata-mandatory").getValue();
                values.defaultValidator.regexp = this._validatorInformationForm.getForm().findField("metadata-regexp").getValue();
                values.defaultValidator.invalidText = this._validatorInformationForm.getForm().findField("metadata-invalid-text").getValue();
            }
            else if (validator == 'custom-validator')
            {
                values.validator = true;
                values.customValidatorClass = this._validatorInformationForm.getForm().findField("metadata-custom-validator-class").getValue();
                values.customValidatorConfiguration = this._validatorInformationForm.getForm().findField("metadata-custom-validator-configuration").getValue();
            }
            
            var enumerated = this._generalInformationForm.getForm().findField("metadata-enumeration").getValue();
            if (enumerated == 'not-enumerated')
            {
                values.enumerated = false;
            }
            else if (enumerated == 'static-enumeration')
            {
                values.enumerated = true;
                values.defaultEnumerator = this._enumerationValues;
            }
            else if (enumerated == 'custom-enumeration')
            {
                values.enumerated = true;
                values.customEnumeratorClass = this._enumerationInformationForm.getForm().findField("metadata-custom-enumerator-class").getValue();
                values.customEnumeratorConfiguration = this._enumerationInformationForm.getForm().findField("metadata-custom-enumerator-configuration").getValue()
            }
        }
        
        return values;
    },
    
    /**
     * Handler when clicking on 'cancel' button
     */
    _cancel: function()
    {
        this._enumerationValues = null;
        this._widgetParams = null;
        this._box.close();
    }
    
});