<commit_before>/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */
package com.haulmont.cuba.gui.xml.layout.loaders;

import com.haulmont.cuba.gui.GuiDevelopmentException;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.IFrame;
import com.haulmont.cuba.gui.components.ValuePathHelper;
import com.haulmont.cuba.gui.xml.layout.ComponentLoader;
import org.apache.commons.lang.ArrayUtils;

import java.util.Arrays;
import java.util.Collections;

/**
 * @author abramov
 * @version $Id$
 */
public class AssignActionPostInitTask implements ComponentLoader.PostInitTask {

    protected Component.ActionOwner component;
    protected String actionName;
    private IFrame frame;

    public AssignActionPostInitTask(Component.ActionOwner component, String actionName, IFrame frame) {
        this.component = component;
        this.actionName = actionName;
        this.frame = frame;
    }

    @Override
    public void execute(ComponentLoader.Context context, IFrame window) {
        final String[] elements = ValuePathHelper.parse(actionName);
        if (elements.length > 1) {
            final String id = elements[elements.length - 1];
            String[] subPath = (String[]) ArrayUtils.subarray(elements, 0, elements.length - 1);

            // using this.frame to look up the component inside the actual frame
            final Component holder = this.frame.getComponent(ValuePathHelper.format(subPath));
            if (holder != null) {
                if (holder instanceof Component.ActionsHolder) {
                    final Action action = ((Component.ActionsHolder) holder).getAction(id);
                    if (action != null) {
                        this.component.setAction(action);
                    } else {
                        throw new GuiDevelopmentException(String.format(
                                "Can't find action '%s' in '%s'", id, holder.getId()), context.getFullFrameId(),
                                "Holder ID", holder.getId());
                    }
                } else {
                    throw new GuiDevelopmentException(String.format(
                            "Component '%s' can't contain actions", holder.getId()), context.getFullFrameId(),
                            "Holder ID", holder.getId());
                }
            } else {
                throw new GuiDevelopmentException("Can't find component: " + Arrays.toString(subPath),
                        context.getFullFrameId(), "Component ID", Arrays.toString(subPath));
            }
        } else if (elements.length == 1) {
            final String id = elements[0];
            final Action action = window.getAction(id);

            if (action != null) {
                this.component.setAction(action);
            } else {
                throw new GuiDevelopmentException(String.format("Can't find action '%s' in window", id), context.getFullFrameId());
            }
        } else {
            throw new GuiDevelopmentException("Empty action name", context.getFullFrameId());
        }
    }
}<commit_msg>Convert Groovy screen controllers for ReportGroup to Java. Fix ReportEditor layout #PL-2449<commit_after>/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */
package com.haulmont.cuba.gui.xml.layout.loaders;

import com.haulmont.cuba.gui.GuiDevelopmentException;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.IFrame;
import com.haulmont.cuba.gui.components.ValuePathHelper;
import com.haulmont.cuba.gui.xml.layout.ComponentLoader;
import org.apache.commons.lang.ArrayUtils;

import java.util.Arrays;
import java.util.Collections;

/**
 * @author abramov
 * @version $Id$
 */
public class AssignActionPostInitTask implements ComponentLoader.PostInitTask {

    protected Component.ActionOwner component;
    protected String actionName;
    private IFrame frame;

    public AssignActionPostInitTask(Component.ActionOwner component, String actionName, IFrame frame) {
        this.component = component;
        this.actionName = actionName;
        this.frame = frame;
    }

    @Override
    public void execute(ComponentLoader.Context context, IFrame window) {
        final String[] elements = ValuePathHelper.parse(actionName);
        if (elements.length > 1) {
            final String id = elements[elements.length - 1];
            String[] subPath = (String[]) ArrayUtils.subarray(elements, 0, elements.length - 1);

            // using this.frame to look up the component inside the actual frame
            final Component holder = this.frame.getComponent(ValuePathHelper.format(subPath));
            if (holder != null) {
                if (holder instanceof Component.ActionsHolder) {
                    final Action action = ((Component.ActionsHolder) holder).getAction(id);
                    if (action != null) {
                        this.component.setAction(action);
                    } else {
                        throw new GuiDevelopmentException(String.format(
                                "Can't find action '%s' in '%s'", id, holder.getId()), context.getFullFrameId(),
                                "Holder ID", holder.getId());
                    }
                } else {
                    throw new GuiDevelopmentException(String.format(
                            "Component '%s' can't contain actions", holder.getId()), context.getFullFrameId(),
                            "Holder ID", holder.getId());
                }
            } else {
                throw new GuiDevelopmentException(
                        "Can't find component: " + Arrays.toString(subPath) + " for action: " + actionName,
                        context.getFullFrameId(), "Component ID", Arrays.toString(subPath));
            }
        } else if (elements.length == 1) {
            final String id = elements[0];
            final Action action = window.getAction(id);

            if (action != null) {
                this.component.setAction(action);
            } else {
                throw new GuiDevelopmentException(String.format("Can't find action '%s' in window", id), context.getFullFrameId());
            }
        } else {
            throw new GuiDevelopmentException("Empty action name", context.getFullFrameId());
        }
    }
}
{'fraction_non_alphanumeric': 0.10120481927710843, 'fraction_numerical': 0.0036144578313253013, 'mean_word_length': 2.2175387596899223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6004045', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1635, 'n_words': 423}