<commit_before>package org.workcraft.plugins.circuit;

import org.workcraft.dom.Model;
import org.workcraft.dom.math.MathModel;
import org.workcraft.dom.references.FileReference;
import org.workcraft.dom.references.Identifier;
import org.workcraft.exceptions.DeserialisationException;
import org.workcraft.formula.jj.ParseException;
import org.workcraft.formula.visitors.StringGenerator;
import org.workcraft.gui.actions.Action;
import org.workcraft.gui.properties.PropertyDeclaration;
import org.workcraft.gui.properties.PropertyDescriptor;
import org.workcraft.gui.properties.PropertyHelper;
import org.workcraft.gui.properties.TextAction;
import org.workcraft.observation.PropertyChangedEvent;
import org.workcraft.plugins.builtin.settings.AnalysisDecorationSettings;
import org.workcraft.plugins.circuit.refinement.ComponentInterface;
import org.workcraft.plugins.circuit.utils.CircuitUtils;
import org.workcraft.plugins.circuit.utils.RefinementUtils;
import org.workcraft.plugins.stg.Stg;
import org.workcraft.utils.*;
import org.workcraft.workspace.ModelEntry;

import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public class CircuitPropertyHelper {

    private static final String PROPERTY_ENVIRONMENT = "Environment";
    private static final String REFINEMENT_MODEL_TITLE = "Refinement model";

    public static Collection<PropertyDescriptor> getComponentProperties(VisualCircuit circuit) {
        List<VisualFunctionComponent> components = new ArrayList<>(Hierarchy.getChildrenOfType(
                circuit.getCurrentLevel(), VisualFunctionComponent.class));

        components.sort((component1, component2) -> SortUtils.compareNatural(
                circuit.getMathName(component1), circuit.getMathName(component2)));

        Collection<PropertyDescriptor> result = new ArrayList<>();
        for (VisualFunctionComponent component : components) {
            result.add(getComponentProperty(circuit, component));
        }
        return result;
    }

    private static PropertyDescriptor getComponentProperty(VisualCircuit circuit, VisualFunctionComponent component) {
        String name = Identifier.truncateNamespaceSeparator(circuit.getMathName(component));

        Action rightAction = new Action(PropertyHelper.SEARCH_SYMBOL,
                () -> circuit.select(component),
                "Select component '" + name + "'");

        Color backgroundColor = getComponentPropertyBackgroundColor(component);

        return new PropertyDeclaration<>(TextAction.class, null,
                value -> {
                    String newName = value.getText();
                    if (!name.equals(newName)) {
                        circuit.setMathName(component, newName);
                        component.sendNotification(new PropertyChangedEvent(component, Model.PROPERTY_NAME));
                    }
                },
                () -> new TextAction(name).setRightAction(rightAction).setBackground(backgroundColor)
            ).setSpan();
    }

    private static Color getComponentPropertyBackgroundColor(VisualFunctionComponent component) {
        File refinementFile = component == null ? null : component.getReferencedComponent().getRefinementFile();
        Color color = refinementFile == null ? null : refinementFile.exists()
                ? AnalysisDecorationSettings.getClearColor()
                : AnalysisDecorationSettings.getProblemColor();

        return ColorUtils.colorise(GuiUtils.getTableCellBackgroundColor(), color);
    }

    public static PropertyDescriptor getEnvironmentProperty(VisualCircuit circuit) {
        return new PropertyDeclaration<>(FileReference.class, PROPERTY_ENVIRONMENT,
                circuit.getMathModel()::setEnvironment, circuit.getMathModel()::getEnvironment);
    }

    public static PropertyDescriptor getRefinementProperty(VisualCircuit circuit, VisualFunctionComponent component) {
        return new PropertyDeclaration<>(FileReference.class, CircuitComponent.PROPERTY_REFINEMENT,
                value -> setRefinementIfCompatible(circuit, component, value),
                () -> component.getReferencedComponent().getRefinement());
    }

    private static void setRefinementIfCompatible(VisualCircuit circuit, VisualFunctionComponent component, FileReference value) {
        if (value == null) {<commit_msg>Highlight Circuit refinements in orange and STG in green<commit_after>package org.workcraft.plugins.circuit;

import org.workcraft.dom.Model;
import org.workcraft.dom.math.MathModel;
import org.workcraft.dom.references.FileReference;
import org.workcraft.dom.references.Identifier;
import org.workcraft.exceptions.DeserialisationException;
import org.workcraft.formula.jj.ParseException;
import org.workcraft.formula.visitors.StringGenerator;
import org.workcraft.gui.actions.Action;
import org.workcraft.gui.properties.PropertyDeclaration;
import org.workcraft.gui.properties.PropertyDescriptor;
import org.workcraft.gui.properties.PropertyHelper;
import org.workcraft.gui.properties.TextAction;
import org.workcraft.observation.PropertyChangedEvent;
import org.workcraft.plugins.builtin.settings.AnalysisDecorationSettings;
import org.workcraft.plugins.circuit.refinement.ComponentInterface;
import org.workcraft.plugins.circuit.utils.CircuitUtils;
import org.workcraft.plugins.circuit.utils.RefinementUtils;
import org.workcraft.plugins.stg.Stg;
import org.workcraft.utils.*;
import org.workcraft.workspace.ModelEntry;

import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public class CircuitPropertyHelper {

    private static final String PROPERTY_ENVIRONMENT = "Environment";
    private static final String REFINEMENT_MODEL_TITLE = "Refinement model";

    public static Collection<PropertyDescriptor> getComponentProperties(VisualCircuit circuit) {
        List<VisualFunctionComponent> components = new ArrayList<>(Hierarchy.getChildrenOfType(
                circuit.getCurrentLevel(), VisualFunctionComponent.class));

        components.sort((component1, component2) -> SortUtils.compareNatural(
                circuit.getMathName(component1), circuit.getMathName(component2)));

        Collection<PropertyDescriptor> result = new ArrayList<>();
        for (VisualFunctionComponent component : components) {
            result.add(getComponentProperty(circuit, component));
        }
        return result;
    }

    private static PropertyDescriptor getComponentProperty(VisualCircuit circuit, VisualFunctionComponent component) {
        String name = Identifier.truncateNamespaceSeparator(circuit.getMathName(component));

        Action rightAction = new Action(PropertyHelper.SEARCH_SYMBOL,
                () -> circuit.select(component),
                "Select component '" + name + "'");

        Color backgroundColor = getComponentPropertyBackgroundColor(component);

        return new PropertyDeclaration<>(TextAction.class, null,
                value -> {
                    String newName = value.getText();
                    if (!name.equals(newName)) {
                        circuit.setMathName(component, newName);
                        component.sendNotification(new PropertyChangedEvent(component, Model.PROPERTY_NAME));
                    }
                },
                () -> new TextAction(name).setRightAction(rightAction).setBackground(backgroundColor)
            ).setSpan();
    }

    private static Color getComponentPropertyBackgroundColor(VisualFunctionComponent component) {
        Color color = null;
        File refinementFile = component == null ? null : component.getReferencedComponent().getRefinementFile();
        if (refinementFile != null) {
            ModelEntry me = null;
            if (FileUtils.isAvailableFile(refinementFile)) {
                try {
                    me = WorkUtils.loadModel(refinementFile);
                } catch (DeserialisationException ignored) {
                }
            }
            if (WorkspaceUtils.isApplicable(me, Circuit.class)) {
                color = AnalysisDecorationSettings.getFixerColor();
            } else if (WorkspaceUtils.isApplicable(me, Stg.class)) {
                color = AnalysisDecorationSettings.getClearColor();
            } else {
                color = AnalysisDecorationSettings.getProblemColor();
            }
        }
        return ColorUtils.colorise(GuiUtils.getTableCellBackgroundColor(), color);
    }

    public static PropertyDescriptor getEnvironmentProperty(VisualCircuit circuit) {
        return new PropertyDeclaration<>(FileReference.class, PROPERTY_ENVIRONMENT,
                circuit.getMathModel()::setEnvironment, circuit.getMathModel()::getEnvironment);
    }

    public static PropertyDescriptor getRefinementProperty(VisualCircuit circuit, VisualFunctionComponent component) {
        return new PropertyDeclaration<>(FileReference.class, CircuitComponent.PROPERTY_REFINEMENT,
                value -> setRefinementIfCompatible(circuit, component, value),
                () -> component.getReferencedComponent().getRefinement());
    }

    private static void setRefinementIfCompatible(VisualCircuit circuit, VisualFunctionComponent component, FileReference value) {
        if (value == null) {
{'fraction_non_alphanumeric': 0.08917333333333333, 'fraction_numerical': 0.0008533333333333333, 'mean_word_length': 3.5870841487279845, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, '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': '6820283', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2213, 'n_words': 427}