<commit_before>/*
 * Copyright (c) 2008-2016 Haulmont.
 *
 * 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.
 *
 */

package com.haulmont.cuba.gui.app.core.scheduled;

import com.haulmont.cuba.core.app.SchedulingService;
import com.haulmont.cuba.core.app.scheduled.MethodInfo;
import com.haulmont.cuba.core.app.scheduled.MethodParameterInfo;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.entity.ScheduledTask;
import com.haulmont.cuba.core.entity.ScheduledTaskDefinedBy;
import com.haulmont.cuba.core.entity.SchedulingType;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.theme.ThemeConstants;
import com.haulmont.cuba.gui.xml.layout.ComponentsFactory;
import org.apache.commons.lang.StringUtils;

import javax.inject.Inject;
import java.util.*;

public class ScheduledTaskEditor extends AbstractEditor<ScheduledTask> {

    @Inject
    protected LookupField beanNameField;

    @Inject
    protected LookupField methodNameField;

    @Inject
    protected LookupField userNameField;

    @Inject
    protected OptionsGroup definedByField;

    @Inject
    protected TextField classNameField;

    @Inject
    protected TextField scriptNameField;

    @Inject
    protected Label beanNameLabel;

    @Inject
    protected Label methodNameLabel;

    @Inject
    protected Label classNameLabel;

    @Inject
    protected Label scriptNameLabel;

    @Inject
    protected Container methodParamsBox;

    @Inject
    protected Datasource<ScheduledTask> taskDs;

    @Inject
    protected SchedulingService service;

    @Inject
    protected ComponentsFactory componentsFactory;

    @Inject
    protected OptionsGroup schedulingTypeField;

    @Inject
    protected TextField cronField;

    @Inject
    protected TextField periodField;

    @Inject
    protected DateField startDateField;

    @Inject
    protected Label cronLabel;

    @Inject
    protected Label periodLabel;

    @Inject
    protected Label startDateLabel;

    @Inject
    protected ThemeConstants themeConstants;

    @Inject
    protected LinkButton cronHelpButton;

    @Inject
    protected BoxLayout cronHbox;

    @Inject
    protected BoxLayout methodNameHbox;

    @Inject
    private CheckBox logStartField;
    @Inject
    private CheckBox logFinishField;

    //List holds an information about methods of selected bean
    protected List<MethodInfo> availableMethods = new ArrayList<>();

    protected void show(Component... components) {
        for (Component component : components) {
            component.setVisible(true);
        }
    }

    protected void hide(Component... components) {
        for (Component component : components) {
            component.setVisible(false);
        }
    }

    protected void hideAll() {
        hide(classNameField, classNameLabel, scriptNameField, scriptNameLabel, beanNameField, beanNameLabel,
                methodNameField, methodNameLabel, methodNameHbox, methodParamsBox);
    }

    protected void clear(Field... fields) {
        for (Field component : fields) {
            component.setValue(null);
        }
    }

    @Override
    public void init(Map<String, Object> params) {
        schedulingTypeField.setOptionsList(Arrays.asList(SchedulingType.values()));
        schedulingTypeField.addValueChangeListener(e -> setSchedulingTypeField((SchedulingType) e.getValue()));

        definedByField.setOptionsList(Arrays.asList(ScheduledTaskDefinedBy.values()));
        definedByField.addValueChangeListener(e -> {
            if (ScheduledTaskDefinedBy.BEAN == e.getValue()) {
                clear(classNameField, scriptNameField);
                hideAll();
                show(beanNameField, beanNameLabel, methodNameField, methodNameLabel, methodNameHbox, methodParamsBox);
            } else if (ScheduledTaskDefinedBy.CLASS == e.getValue()) {
                clear(beanNameField, methodNameField, scriptNameField);
                hideAll();
                show(classNameField, classNameLabel);
            } else if (ScheduledTaskDefinedBy.SCRIPT == e.getValue()) {
                clear(beanNameField, methodNameField, classNameField);
                hideAll();
                show(scriptNameField, scriptNameLabel);
            } else {
                clear(beanNameField, methodNameField, classNameField, scriptNameField);
                hideAll();
            }
        });

        Map<String, List<MethodInfo>> availableBeans = service.getAvailableBeans();
        beanNameField.setOptionsList(new ArrayList<>(availableBeans.keySet()));
        beanNameField.addValueChangeListener(e -> {
            methodNameField.setValue(null);
            if (e.getValue() == null) {
                methodNameField.setOptionsList(Collections.emptyList());
            } else {
                availableMethods = availableBeans.get(e.getValue());

                if (availableMethods != null) {
                    HashMap<String, Object> optionsMap = new HashMap<>();
                    for (MethodInfo availableMethod : availableMethods) {
                        optionsMap.put(availableMethod.getMethodSignature(), availableMethod);
                    }
                    methodNameField.setOptionsMap(optionsMap);
                }
            }
        });

        methodNameField.addValueChangeListener(e -> {
            clearMethodParamsGrid();
            if (e.getValue() != null) {
                createMethodParamsGrid((MethodInfo) e.getValue());
            }<commit_msg>PL-9912 Hide Method Parameters groupBox in Scheduled Task editor if there aren't any<commit_after>/*
 * Copyright (c) 2008-2016 Haulmont.
 *
 * 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.
 *
 */

package com.haulmont.cuba.gui.app.core.scheduled;

import com.haulmont.cuba.core.app.SchedulingService;
import com.haulmont.cuba.core.app.scheduled.MethodInfo;
import com.haulmont.cuba.core.app.scheduled.MethodParameterInfo;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.entity.ScheduledTask;
import com.haulmont.cuba.core.entity.ScheduledTaskDefinedBy;
import com.haulmont.cuba.core.entity.SchedulingType;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.theme.ThemeConstants;
import com.haulmont.cuba.gui.xml.layout.ComponentsFactory;
import org.apache.commons.lang.StringUtils;

import javax.inject.Inject;
import java.util.*;

public class ScheduledTaskEditor extends AbstractEditor<ScheduledTask> {

    @Inject
    protected LookupField beanNameField;

    @Inject
    protected LookupField methodNameField;

    @Inject
    protected LookupField userNameField;

    @Inject
    protected OptionsGroup definedByField;

    @Inject
    protected TextField classNameField;

    @Inject
    protected TextField scriptNameField;

    @Inject
    protected Label beanNameLabel;

    @Inject
    protected Label methodNameLabel;

    @Inject
    protected Label classNameLabel;

    @Inject
    protected Label scriptNameLabel;

    @Inject
    protected Container methodParamsBox;

    @Inject
    protected Datasource<ScheduledTask> taskDs;

    @Inject
    protected SchedulingService service;

    @Inject
    protected ComponentsFactory componentsFactory;

    @Inject
    protected OptionsGroup schedulingTypeField;

    @Inject
    protected TextField cronField;

    @Inject
    protected TextField periodField;

    @Inject
    protected DateField startDateField;

    @Inject
    protected Label cronLabel;

    @Inject
    protected Label periodLabel;

    @Inject
    protected Label startDateLabel;

    @Inject
    protected ThemeConstants themeConstants;

    @Inject
    protected LinkButton cronHelpButton;

    @Inject
    protected BoxLayout cronHbox;

    @Inject
    protected BoxLayout methodNameHbox;

    @Inject
    private CheckBox logStartField;
    @Inject
    private CheckBox logFinishField;

    //List holds an information about methods of selected bean
    protected List<MethodInfo> availableMethods = new ArrayList<>();

    protected void show(Component... components) {
        for (Component component : components) {
            component.setVisible(true);
        }
    }

    protected void hide(Component... components) {
        for (Component component : components) {
            component.setVisible(false);
        }
    }

    protected void hideAll() {
        hide(classNameField, classNameLabel, scriptNameField, scriptNameLabel, beanNameField, beanNameLabel,
                methodNameField, methodNameLabel, methodNameHbox, methodParamsBox);
    }

    protected void clear(Field... fields) {
        for (Field component : fields) {
            component.setValue(null);
        }
    }

    @Override
    public void init(Map<String, Object> params) {
        schedulingTypeField.setOptionsList(Arrays.asList(SchedulingType.values()));
        schedulingTypeField.addValueChangeListener(e -> setSchedulingTypeField((SchedulingType) e.getValue()));

        definedByField.setOptionsList(Arrays.asList(ScheduledTaskDefinedBy.values()));
        definedByField.addValueChangeListener(e -> {
            if (ScheduledTaskDefinedBy.BEAN == e.getValue()) {
                clear(classNameField, scriptNameField);
                hideAll();
                show(beanNameField, beanNameLabel, methodNameField, methodNameLabel, methodNameHbox);
            } else if (ScheduledTaskDefinedBy.CLASS == e.getValue()) {
                clear(beanNameField, methodNameField, scriptNameField);
                hideAll();
                show(classNameField, classNameLabel);
            } else if (ScheduledTaskDefinedBy.SCRIPT == e.getValue()) {
                clear(beanNameField, methodNameField, classNameField);
                hideAll();
                show(scriptNameField, scriptNameLabel);
            } else {
                clear(beanNameField, methodNameField, classNameField, scriptNameField);
                hideAll();
            }
        });

        Map<String, List<MethodInfo>> availableBeans = service.getAvailableBeans();
        beanNameField.setOptionsList(new ArrayList<>(availableBeans.keySet()));
        beanNameField.addValueChangeListener(e -> {
            methodNameField.setValue(null);
            hide(methodParamsBox);
            if (e.getValue() == null) {
                methodNameField.setOptionsList(Collections.emptyList());
            } else {
                availableMethods = availableBeans.get(e.getValue());

                if (availableMethods != null) {
                    HashMap<String, Object> optionsMap = new HashMap<>();
                    for (MethodInfo availableMethod : availableMethods) {
                        optionsMap.put(availableMethod.getMethodSignature(), availableMethod);
                    }
                    methodNameField.setOptionsMap(optionsMap);
                }
            }
        });

        methodNameField.addValueChangeListener(e -> {
            clearMethodParamsGrid();
            if (e.getValue() != null) {
                createMethodParamsGrid((MethodInfo) e.getValue());
                if (methodParamsBox.getComponents().size() > 1) {
                    show(methodParamsBox);
                } else {
                    hide(methodParamsBox);
                }
            }
{'fraction_non_alphanumeric': 0.0850428040704248, 'fraction_numerical': 0.00234210951381037, 'mean_word_length': 2.7444814030843663, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '5110379', 'n_tokens_mistral': 3274, 'n_tokens_neox': 3146, 'n_words': 752}