<commit_before>package org.openlca.app.tools.openepd;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.openlca.app.M;
import org.openlca.app.components.EntityCombo;
import org.openlca.app.rcp.images.Icon;
import org.openlca.app.rcp.images.Images;
import org.openlca.app.tools.openepd.model.Ec3ImpactModel;
import org.openlca.app.tools.openepd.model.Ec3ImpactResult;
import org.openlca.app.tools.openepd.model.Ec3IndicatorResult;
import org.openlca.app.tools.openepd.model.Ec3Measurement;
import org.openlca.app.tools.openepd.model.Ec3ScopeValue;
import org.openlca.app.util.Actions;
import org.openlca.app.util.Labels;
import org.openlca.app.util.Numbers;
import org.openlca.app.util.UI;
import org.openlca.app.viewers.Viewers;
import org.openlca.app.viewers.tables.Tables;
import org.openlca.app.viewers.tables.modify.ComboBoxCellModifier;
import org.openlca.app.viewers.tables.modify.DoubleCellModifier;
import org.openlca.app.viewers.tables.modify.ModifySupport;
import org.openlca.core.model.ImpactCategory;
import org.openlca.core.model.ModelType;
import org.openlca.core.model.ResultModel;
import org.openlca.util.Strings;

public class ResultSection {

	private final EpdEditor editor;
	private final ResultModel result;

	private String selectedScope = "A1A2A3";
	private Ec3ImpactModel.Method selectedMethod;
	private Section section;
	private Consumer<ResultSection> onDelete;
	private final List<MappedValue> mappedValues = new ArrayList<>();

	private ResultSection(EpdEditor editor, ResultModel result) {
		this.editor = editor;
		this.result = result;

		// select the best matching EC3 LCIA method for the LCIA method
		// of the setup
		var impactModel = editor.impactModel;
		if (result.setup != null && result.setup.impactMethod() != null) {
			var resultName = result.setup.impactMethod().name;
			int score = 0;
			for (var next : impactModel.methods()) {
				var nextScore = Ec3ImpactModel.mapScore(resultName, next.keywords());
				if (nextScore > score) {
					score = nextScore;
					selectedMethod = next;
				}
			}
		}

		if (selectedMethod != null) {
			mappedValues.addAll(MappedValue.initAll(selectedMethod, result));
		}
	}

	static ResultSection of(EpdEditor editor, ResultModel result) {
		return new ResultSection(editor, result);
	}

	ResultSection onDelete(Consumer<ResultSection> onDelete) {
		this.onDelete = onDelete;
		return this;
	}

	Ec3ImpactResult createEpdResult() {
		if (selectedMethod == null)
			return null;
		var indicatorResults = new ArrayList<Ec3IndicatorResult>();
		for (var val : mappedValues) {
			var epdIndicator = val.epdIndicator;
			var value = Ec3Measurement.of(val.amount, epdIndicator.unit());
			var scopeValue = new Ec3ScopeValue(selectedScope, value);
			var result = Ec3IndicatorResult.of((epdIndicator.id()));
			result.values().add(scopeValue);
			indicatorResults.add(result);
		}
		return new Ec3ImpactResult(selectedMethod.id(), indicatorResults);
	}

	ResultSection render(Composite body, FormToolkit tk) {
		section = UI.section(body, tk, M.GeneralInformation);
		UI.fillHorizontal(section);
		updateSectionTitle();
		var comp = UI.sectionClient(section, tk, 1);

		var top = tk.createComposite(comp);
		UI.fillHorizontal(top);
		UI.gridLayout(top, 2, 10, 0);

		// create the method combo
		var methodCombo = new EntityCombo<>(
			UI.formCombo(top, tk, "OpenEPD LCIA Method"),
			editor.impactModel.methods(),
			m -> Strings.orEmpty(m.id()))
			.select(selectedMethod);

		// create the scope combo
		var scopes = List.of(
			"A1A2A3", "A1", "A2", "A3", "A4", "A5",
			"B1", "B2", "B3", "B4", "B5", "B6", "B7",
			"C1", "C2", "C3", "C4");
		new EntityCombo<>(
			UI.formCombo(top, tk, "Scope"), scopes, Objects::toString)<commit_msg>add a link to the underlying section result<commit_after>package org.openlca.app.tools.openepd;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.openlca.app.App;
import org.openlca.app.M;
import org.openlca.app.components.EntityCombo;
import org.openlca.app.rcp.images.Icon;
import org.openlca.app.rcp.images.Images;
import org.openlca.app.tools.openepd.model.Ec3ImpactModel;
import org.openlca.app.tools.openepd.model.Ec3ImpactResult;
import org.openlca.app.tools.openepd.model.Ec3IndicatorResult;
import org.openlca.app.tools.openepd.model.Ec3Measurement;
import org.openlca.app.tools.openepd.model.Ec3ScopeValue;
import org.openlca.app.util.Actions;
import org.openlca.app.util.Controls;
import org.openlca.app.util.Labels;
import org.openlca.app.util.Numbers;
import org.openlca.app.util.UI;
import org.openlca.app.viewers.Viewers;
import org.openlca.app.viewers.tables.Tables;
import org.openlca.app.viewers.tables.modify.ComboBoxCellModifier;
import org.openlca.app.viewers.tables.modify.DoubleCellModifier;
import org.openlca.app.viewers.tables.modify.ModifySupport;
import org.openlca.core.model.ImpactCategory;
import org.openlca.core.model.ModelType;
import org.openlca.core.model.ResultModel;
import org.openlca.util.Strings;

public class ResultSection {

	private final EpdEditor editor;
	private final ResultModel result;

	private String selectedScope = "A1A2A3";
	private Ec3ImpactModel.Method selectedMethod;
	private Section section;
	private Consumer<ResultSection> onDelete;
	private final List<MappedValue> mappedValues = new ArrayList<>();

	private ResultSection(EpdEditor editor, ResultModel result) {
		this.editor = editor;
		this.result = result;

		// select the best matching EC3 LCIA method for the LCIA method
		// of the setup
		var impactModel = editor.impactModel;
		if (result.setup != null && result.setup.impactMethod() != null) {
			var resultName = result.setup.impactMethod().name;
			int score = 0;
			for (var next : impactModel.methods()) {
				var nextScore = Ec3ImpactModel.mapScore(resultName, next.keywords());
				if (nextScore > score) {
					score = nextScore;
					selectedMethod = next;
				}
			}
		}

		if (selectedMethod != null) {
			mappedValues.addAll(MappedValue.initAll(selectedMethod, result));
		}
	}

	static ResultSection of(EpdEditor editor, ResultModel result) {
		return new ResultSection(editor, result);
	}

	ResultSection onDelete(Consumer<ResultSection> onDelete) {
		this.onDelete = onDelete;
		return this;
	}

	Ec3ImpactResult createEpdResult() {
		if (selectedMethod == null)
			return null;
		var indicatorResults = new ArrayList<Ec3IndicatorResult>();
		for (var val : mappedValues) {
			var epdIndicator = val.epdIndicator;
			var value = Ec3Measurement.of(val.amount, epdIndicator.unit());
			var scopeValue = new Ec3ScopeValue(selectedScope, value);
			var result = Ec3IndicatorResult.of((epdIndicator.id()));
			result.values().add(scopeValue);
			indicatorResults.add(result);
		}
		return new Ec3ImpactResult(selectedMethod.id(), indicatorResults);
	}

	ResultSection render(Composite body, FormToolkit tk) {
		section = UI.section(body, tk, M.GeneralInformation);
		UI.fillHorizontal(section);
		updateSectionTitle();
		var comp = UI.sectionClient(section, tk, 1);

		var top = tk.createComposite(comp);
		UI.fillHorizontal(top);
		UI.gridLayout(top, 2, 10, 0);

		// the result link
		UI.formLabel(top, tk, M.Result);
		var link = tk.createImageHyperlink(top, SWT.NONE);
		link.setImage(Images.get(ModelType.RESULT));
		link.setText(Labels.name(result));
		Controls.onClick(link, $ -> App.open(result));

		// create the method combo
		var methodCombo = new EntityCombo<>(
			UI.formCombo(top, tk, "OpenEPD LCIA Method"),
			editor.impactModel.methods(),
			m -> Strings.orEmpty(m.id()))
			.select(selectedMethod);

		// create the scope combo
		var scopes = List.of(
			"A1A2A3", "A1", "A2", "A3", "A4", "A5",
			"B1", "B2", "B3", "B4", "B5", "B6", "B7",
			"C1", "C2", "C3", "C4");
		new EntityCombo<>(
			UI.formCombo(top, tk, "Scope"), scopes, Objects::toString)
{'fraction_non_alphanumeric': 0.1271629311345953, 'fraction_numerical': 0.009754964580188132, 'mean_word_length': 4.83073798239675, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 22, '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': '6986001', 'n_tokens_mistral': 3161, 'n_tokens_neox': 2933, 'n_words': 590}