<commit_before>/*
 * Copyright (c) 2011 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */

package com.haulmont.cuba.gui.components;

import com.haulmont.chile.core.datatypes.Datatype;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.datatypes.impl.*;
import com.haulmont.chile.core.model.*;
import com.haulmont.cuba.core.entity.CategoryAttributeValue;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.core.sys.SetValueEntity;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.validators.DateValidator;
import com.haulmont.cuba.gui.components.validators.DoubleValidator;
import com.haulmont.cuba.gui.components.validators.IntegerValidator;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.RuntimePropertiesEntity;
import com.haulmont.cuba.gui.data.RuntimePropsDatasource;
import com.haulmont.cuba.gui.data.impl.DsListenerAdapter;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 *  Universal frame for editing Runtime properties
 *  of any {@link com.haulmont.cuba.core.entity.CategorizedEntity} subclass.
 *
 * <p>$Id$</p>
 *
 * @author devyatkin
 */

public class RuntimePropertiesFrame extends AbstractWindow {

    private RuntimePropsDatasource rds;
    private CollectionDatasource categoriesDs;

    public static final String NAME = "runtimePropertiesFrame";
    private static final String DEFAULT_FIELD_WIDTH = "100%";
    private String rows;
    private String cols;
    private String fieldWidth;
    private BoxLayout contentPane;
    private FieldGroup categoryFieldGroup;
    private boolean requiredControlEnabled = true;

    public RuntimePropertiesFrame(IFrame frame) {
        super(frame);
    }

    public void init(Map<String, Object> params) {
        String dsId = (String) params.get("runtimeDs");
        String categoriesDsId = (String) params.get("categoriesDs");
        rows = (String) params.get("rows");
        cols = (String) params.get("cols");
        fieldWidth = (String) params.get("fieldWidth");
        if (StringUtils.isEmpty(fieldWidth))
            fieldWidth = DEFAULT_FIELD_WIDTH;
        rds = getDsContext().get(dsId);
        categoriesDs = getDsContext().<CollectionDatasource>get(categoriesDsId);

        contentPane = getComponent("contentPane");
        initCategoryField();
        loadComponent(rds);
    }

    private void initCategoryField() {
        categoryFieldGroup = AppConfig.getFactory().createComponent(FieldGroup.NAME);
        categoryFieldGroup.setId("categoryFieldGroup");
        categoryFieldGroup.setFrame(this.<IFrame>getFrame());

        contentPane.add(categoryFieldGroup);
        registerComponent(categoryFieldGroup);

        FieldGroup.Field field = new FieldGroup.Field("category");

        field.setCustom(true);
        categoryFieldGroup.addField(field);
        categoryFieldGroup.setDatasource(rds.getMainDs());
        categoryFieldGroup.addCustomField("category", new FieldGroup.CustomFieldGenerator() {
            @Override
            public Component generateField(Datasource datasource, Object propertyId) {
                LookupField field = AppConfig.getFactory().createComponent(LookupField.NAME);
                field.setDatasource(rds.getMainDs(), "category");
                field.setOptionsDatasource(categoriesDs);
//                field.setHeight("-1px");
                field.setWidth(fieldWidth);
                field.setCaptionProperty("name");
                field.setCaption(getMessage("runtimeProperties.category"));
                return field;
            }
        });
    }

    private void loadComponent(Datasource ds) {

        ds.addListener(new DsListenerAdapter() {
            @Override
            public void stateChanged(Datasource ds, Datasource.State prevState, Datasource.State state) {
                if (!Datasource.State.VALID.equals(state)) {
                    return;
                }
                Component runtime = getComponent("runtime");
                final FieldGroup newRuntime = AppConfig.getFactory().createComponent(FieldGroup.NAME);
                newRuntime.setBorderVisible(false);
                newRuntime.setId("runtime");
                if (runtime != null) {
                    contentPane.remove(runtime);
                    Component cfg = contentPane.getComponent("categoryFieldGroup");
                    if (cfg != null) {
                        contentPane.remove(cfg);
                        contentPane.add(cfg);
                    }
                }
                newRuntime.setFrame(getFrame());
                contentPane.add(newRuntime);
                registerComponent(newRuntime);

                final java.util.List<FieldGroup.Field> fields = newRuntime.getFields();
                for (FieldGroup.Field field : fields)
                    newRuntime.removeField(field);

                int rowsPerColumn;
                int propertiesCount = rds.getMetaClass().getProperties().size();
                if (StringUtils.isNotBlank(cols)) {
                    int propertiesSize = propertiesCount;
                    if (propertiesSize % Integer.valueOf(cols) == 0)
                        rowsPerColumn = propertiesSize / Integer.valueOf(cols);
                    else
                        rowsPerColumn = propertiesSize / Integer.valueOf(cols) + 1;
                } else if (StringUtils.isNotBlank(rows)) {
                    rowsPerColumn = Integer.valueOf(rows);
                } else {
                    rowsPerColumn = propertiesCount;
                }

                int columnNo = 0;
                int fieldsCount = 0;
                final java.util.List<FieldGroup.Field> rootFields = loadFields(newRuntime, ds);
                for (final FieldGroup.Field field : rootFields) {
                    fieldsCount++;
                    newRuntime.addField(field, columnNo);
                    if (fieldsCount % rowsPerColumn == 0) {
                        newRuntime.setColumns(columnNo + 1);
                        columnNo++;
                    }
                }
                if (!rootFields.isEmpty())
                    newRuntime.setDatasource(ds);

                addCustomFields(newRuntime, rootFields, ds);

                for (final FieldGroup.Field field : newRuntime.getFields()) {
                    loadValidators(newRuntime, field);
                    loadRequired(newRuntime, field);
                    //loadEditable(component, field);
                    //loadEnabled(component, field);
                }<commit_msg>fix npe in runtime props frame<commit_after>/*
 * Copyright (c) 2011 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */

package com.haulmont.cuba.gui.components;

import com.haulmont.chile.core.datatypes.Datatype;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.datatypes.impl.*;
import com.haulmont.chile.core.model.*;
import com.haulmont.cuba.core.entity.CategoryAttributeValue;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.core.sys.SetValueEntity;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.validators.DateValidator;
import com.haulmont.cuba.gui.components.validators.DoubleValidator;
import com.haulmont.cuba.gui.components.validators.IntegerValidator;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.RuntimePropertiesEntity;
import com.haulmont.cuba.gui.data.RuntimePropsDatasource;
import com.haulmont.cuba.gui.data.impl.DsListenerAdapter;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 *  Universal frame for editing Runtime properties
 *  of any {@link com.haulmont.cuba.core.entity.CategorizedEntity} subclass.
 *
 * <p>$Id$</p>
 *
 * @author devyatkin
 */

public class RuntimePropertiesFrame extends AbstractWindow {

    private RuntimePropsDatasource rds;
    private CollectionDatasource categoriesDs;

    public static final String NAME = "runtimePropertiesFrame";
    private static final String DEFAULT_FIELD_WIDTH = "100%";
    private String rows;
    private String cols;
    private String fieldWidth;
    private BoxLayout contentPane;
    private FieldGroup categoryFieldGroup;
    private boolean requiredControlEnabled = true;

    public RuntimePropertiesFrame(IFrame frame) {
        super(frame);
    }

    public void init(Map<String, Object> params) {
        String dsId = (String) params.get("runtimeDs");
        String categoriesDsId = (String) params.get("categoriesDs");
        rows = (String) params.get("rows");
        cols = (String) params.get("cols");
        fieldWidth = (String) params.get("fieldWidth");
        if (StringUtils.isEmpty(fieldWidth))
            fieldWidth = DEFAULT_FIELD_WIDTH;
        rds = getDsContext().get(dsId);
        categoriesDs = getDsContext().<CollectionDatasource>get(categoriesDsId);

        contentPane = getComponent("contentPane");
        initCategoryField();
        loadComponent(rds);
    }

    private void initCategoryField() {
        categoryFieldGroup = AppConfig.getFactory().createComponent(FieldGroup.NAME);
        categoryFieldGroup.setId("categoryFieldGroup");
        categoryFieldGroup.setFrame(this.<IFrame>getFrame());

        contentPane.add(categoryFieldGroup);
        registerComponent(categoryFieldGroup);

        FieldGroup.Field field = new FieldGroup.Field("category");

        field.setCustom(true);
        categoryFieldGroup.addField(field);
        categoryFieldGroup.setDatasource(rds.getMainDs());
        categoryFieldGroup.addCustomField("category", new FieldGroup.CustomFieldGenerator() {
            @Override
            public Component generateField(Datasource datasource, Object propertyId) {
                LookupField field = AppConfig.getFactory().createComponent(LookupField.NAME);
                field.setDatasource(rds.getMainDs(), "category");
                field.setOptionsDatasource(categoriesDs);
//                field.setHeight("-1px");
                field.setWidth(fieldWidth);
                field.setCaptionProperty("name");
                field.setCaption(getMessage("runtimeProperties.category"));
                return field;
            }
        });
    }

    private void loadComponent(Datasource ds) {

        ds.addListener(new DsListenerAdapter() {
            @Override
            public void stateChanged(Datasource ds, Datasource.State prevState, Datasource.State state) {
                if (!Datasource.State.VALID.equals(state)) {
                    return;
                }
                Component runtime = getComponent("runtime");
                final FieldGroup newRuntime = AppConfig.getFactory().createComponent(FieldGroup.NAME);
                newRuntime.setBorderVisible(false);
                newRuntime.setId("runtime");
                if (runtime != null) {
                    contentPane.remove(runtime);
                    Component cfg = contentPane.getComponent("categoryFieldGroup");
                    if (cfg != null) {
                        contentPane.remove(cfg);
                        contentPane.add(cfg);
                    }
                }
                newRuntime.setFrame(getFrame());
                contentPane.add(newRuntime);
                registerComponent(newRuntime);

                final java.util.List<FieldGroup.Field> fields = newRuntime.getFields();
                for (FieldGroup.Field field : fields)
                    newRuntime.removeField(field);

                int rowsPerColumn;
                int propertiesCount = rds.getMetaClass().getProperties().size();
                if (StringUtils.isNotBlank(cols)) {
                    int propertiesSize = propertiesCount;
                    if (propertiesSize % Integer.valueOf(cols) == 0)
                        rowsPerColumn = propertiesSize / Integer.valueOf(cols);
                    else
                        rowsPerColumn = propertiesSize / Integer.valueOf(cols) + 1;
                } else if (StringUtils.isNotBlank(rows)) {
                    rowsPerColumn = Integer.valueOf(rows);
                } else {
                    rowsPerColumn = propertiesCount;
                }

                int columnNo = 0;
                int fieldsCount = 0;
                final java.util.List<FieldGroup.Field> rootFields = loadFields(newRuntime, ds);
                for (final FieldGroup.Field field : rootFields) {
                    fieldsCount++;
                    newRuntime.addField(field, columnNo);
                    if (fieldsCount % rowsPerColumn == 0) {
                        columnNo++;
                        newRuntime.setColumns(columnNo + 1);
                    }
                }
                if (!rootFields.isEmpty())
                    newRuntime.setDatasource(ds);

                addCustomFields(newRuntime, rootFields, ds);

                for (final FieldGroup.Field field : newRuntime.getFields()) {
                    loadValidators(newRuntime, field);
                    loadRequired(newRuntime, field);
                    //loadEditable(component, field);
                    //loadEnabled(component, field);
                }
{'fraction_non_alphanumeric': 0.09735231211741278, 'fraction_numerical': 0.002065049044914817, 'mean_word_length': 2.253358925143954, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6818844', 'n_tokens_mistral': 3589, 'n_tokens_neox': 3347, 'n_words': 639}