<commit_before>package org.openlca.app.results.regionalized;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.openlca.app.App;
import org.openlca.app.components.ResultTypeSelection;
import org.openlca.app.db.Cache;
import org.openlca.app.rcp.html.HtmlPage;
import org.openlca.app.rcp.html.HtmlView;
import org.openlca.app.util.UI;
import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.geo.RegionalizedResultProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

class KmlResultView extends FormPage implements HtmlPage {

	private Logger log = LoggerFactory.getLogger(getClass());

	private RegionalizedResultProvider result;
	private Browser browser;
	private ResultTypeSelection flowImpactSelection;
	private boolean incompleteData = false;

	public KmlResultView(FormEditor editor, RegionalizedResultProvider result) {
		super(editor, "KmlResultView", "Result map");
		this.result = result;
	}

	@Override
	public String getUrl() {
		return HtmlView.KML_RESULT_VIEW.getUrl();
	}

	@Override
	public void onLoaded() {
		Set<FlowDescriptor> flowDescriptors = result.result.getFlowDescriptors();
		if (flowDescriptors.isEmpty())
			return;
		FlowDescriptor flow = flowDescriptors.iterator().next();
		flowImpactSelection.selectWithEvent(flow);
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		ScrolledForm form = UI.formHeader(managedForm, "Result map");
		FormToolkit toolkit = managedForm.getToolkit();
		Composite body = UI.formBody(form, toolkit);
		Composite composite = toolkit.createComposite(body);
		UI.gridLayout(composite, 2);
		flowImpactSelection = ResultTypeSelection
				.on(result.result, Cache.getEntityCache())
				.withEventHandler(new KmlSelectionHandler(result))
				.create(composite, toolkit);
		browser = UI.createBrowser(body, this);
		UI.gridData(browser, true, true);
		form.reflow(true);
	}

	@Override
	public void setActive(boolean active) {
		super.setActive(active);
		if (incompleteData)
			// reset browser data
			try {
				browser.evaluate("initData()");
			} catch (Exception e) {
				log.error("failed to evaluate initData()", e);
			}
	}

	private class KmlSelectionHandler extends SelectionHandler {

		private List<Job> delayedJobs = new ArrayList<>();

		private KmlSelectionHandler(RegionalizedResultProvider result) {
			super(result);
		}

		@Override
		protected void processResultData(List<LocationResult> results) {
			delayedJobs.clear();
			incompleteData = false;
			double maximum = getMaximum(results);
			evaluate("initData(" + maximum + ")");
			for (LocationResult result : results)
				sendToView(result);
		}

		private double getMaximum(List<LocationResult> results) {
			Double maximum = null;
			for (LocationResult result : results)
				if (maximum == null)
					maximum = result.amount;
				else
					maximum = Math.max(maximum, result.amount);
			if (maximum == null)
				return 0;
			return maximum;
		}

		private void sendToView(LocationResult result) {
			if (result == null)
				return;
			if (result.amount == 0d)<commit_msg>Fixed issue when data is set before html is loaded<commit_after>package org.openlca.app.results.regionalized;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.openlca.app.App;
import org.openlca.app.components.ResultTypeSelection;
import org.openlca.app.db.Cache;
import org.openlca.app.rcp.html.HtmlPage;
import org.openlca.app.rcp.html.HtmlView;
import org.openlca.app.util.UI;
import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.geo.RegionalizedResultProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

class KmlResultView extends FormPage implements HtmlPage {

	private Logger log = LoggerFactory.getLogger(getClass());

	private RegionalizedResultProvider result;
	private Browser browser;
	private ResultTypeSelection flowImpactSelection;
	private boolean incompleteData = false;
	private boolean loaded;

	public KmlResultView(FormEditor editor, RegionalizedResultProvider result) {
		super(editor, "KmlResultView", "Result map");
		this.result = result;
	}

	@Override
	public String getUrl() {
		return HtmlView.KML_RESULT_VIEW.getUrl();
	}

	@Override
	public void onLoaded() {
		loaded = true;
		Set<FlowDescriptor> flowDescriptors = result.result.getFlowDescriptors();
		if (flowDescriptors.isEmpty())
			return;
		FlowDescriptor flow = flowDescriptors.iterator().next();
		flowImpactSelection.selectWithEvent(flow);
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		ScrolledForm form = UI.formHeader(managedForm, "Result map");
		FormToolkit toolkit = managedForm.getToolkit();
		Composite body = UI.formBody(form, toolkit);
		Composite composite = toolkit.createComposite(body);
		UI.gridLayout(composite, 2);
		flowImpactSelection = ResultTypeSelection
				.on(result.result, Cache.getEntityCache())
				.withEventHandler(new KmlSelectionHandler(result))
				.create(composite, toolkit);
		browser = UI.createBrowser(body, this);
		UI.gridData(browser, true, true);
		form.reflow(true);
	}

	@Override
	public void setActive(boolean active) {
		super.setActive(active);
		if (incompleteData)
			// reset browser data
			try {
				browser.evaluate("initData()");
			} catch (Exception e) {
				log.error("failed to evaluate initData()", e);
			}
	}

	private class KmlSelectionHandler extends SelectionHandler {

		private List<Job> delayedJobs = new ArrayList<>();

		private KmlSelectionHandler(RegionalizedResultProvider result) {
			super(result);
		}

		@Override
		protected void processResultData(List<LocationResult> results) {
			if (!loaded)
				return;
			delayedJobs.clear();
			incompleteData = false;
			double maximum = getMaximum(results);
			evaluate("initData(" + maximum + ")");
			for (LocationResult result : results)
				sendToView(result);
		}

		private double getMaximum(List<LocationResult> results) {
			Double maximum = null;
			for (LocationResult result : results)
				if (maximum == null)
					maximum = result.amount;
				else
					maximum = Math.max(maximum, result.amount);
			if (maximum == null)
				return 0;
			return maximum;
		}

		private void sendToView(LocationResult result) {
			if (result == null)
				return;
			if (result.amount == 0d)
{'fraction_non_alphanumeric': 0.10943817091872299, 'fraction_numerical': 0.0013941168269901018, 'mean_word_length': 4.865903515944399, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3957078', 'n_tokens_mistral': 2460, 'n_tokens_neox': 2212, 'n_words': 480}