<commit_before>package org.mwc.debrief.satc_interface.actions;

import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.mwc.cmap.core.CorePlugin;
import org.mwc.cmap.core.operations.CMAPOperation;
import org.mwc.cmap.core.property_support.RightClickSupport.RightClickContextItemGenerator;
import org.mwc.debrief.satc_interface.SATC_Interface_Activator;
import org.mwc.debrief.satc_interface.data.SATC_Solution;
import org.mwc.debrief.satc_interface.data.wrappers.ContributionWrapper;
import org.mwc.debrief.satc_interface.utilities.conversions;
import org.mwc.debrief.satc_interface.wizards.NewStraightLegWizard;

import Debrief.Wrappers.SensorContactWrapper;
import Debrief.Wrappers.SensorWrapper;
import MWC.GUI.Editable;
import MWC.GUI.Layer;
import MWC.GUI.Layers;
import MWC.GenericData.HiResDate;
import MWC.GenericData.TimePeriod;
import MWC.GenericData.TimePeriod.BaseTimePeriod;
import MWC.GenericData.WorldDistance;
import MWC.GenericData.WorldLocation;
import MWC.Utilities.TextFormatting.FormatRNDateTime;

import com.planetmayo.debrief.satc.model.GeoPoint;
import com.planetmayo.debrief.satc.model.contributions.BaseContribution;
import com.planetmayo.debrief.satc.model.contributions.BearingMeasurementContribution;
import com.planetmayo.debrief.satc.model.contributions.BearingMeasurementContribution.BMeasurement;
import com.planetmayo.debrief.satc.model.contributions.CourseAnalysisContribution;
import com.planetmayo.debrief.satc.model.contributions.CourseForecastContribution;
import com.planetmayo.debrief.satc.model.contributions.LocationAnalysisContribution;
import com.planetmayo.debrief.satc.model.contributions.RangeForecastContribution;
import com.planetmayo.debrief.satc.model.contributions.RangeForecastContribution.ROrigin;
import com.planetmayo.debrief.satc.model.contributions.SpeedAnalysisContribution;
import com.planetmayo.debrief.satc.model.contributions.SpeedForecastContribution;
import com.planetmayo.debrief.satc.model.contributions.StraightLegForecastContribution;
import com.planetmayo.debrief.satc.model.generator.IContributions;
import com.planetmayo.debrief.satc.model.generator.ISolver;
import com.planetmayo.debrief.satc.model.manager.ISolversManager;
import com.planetmayo.debrief.satc_rcp.SATC_Activator;

public class CreateSolutionFromSensorData implements
		RightClickContextItemGenerator
{

	@Override
	public void generate(IMenuManager parent, final Layers theLayers,
			Layer[] parentLayers, Editable[] subjects)
	{
		ArrayList<SensorContactWrapper> validCuts = null;

		IMenuManager thisMenu = new MenuManager("SemiAuto TMA");
		parent.add(thisMenu);

		for (int i = 0; i < subjects.length; i++)
		{
			Editable thisItem = subjects[i];
			if (thisItem instanceof SensorContactWrapper)
			{
				if (validCuts == null)
					validCuts = new ArrayList<SensorContactWrapper>();

				validCuts.add((SensorContactWrapper) thisItem);
			}
			else if (thisItem instanceof SensorWrapper)
			{
				if (validCuts == null)
					validCuts = new ArrayList<SensorContactWrapper>();

				SensorWrapper sw = (SensorWrapper) thisItem;
				Enumeration<Editable> cuts = sw.elements();
				while (cuts.hasMoreElements())
				{
					validCuts.add((SensorContactWrapper) cuts.nextElement());
				}
			}
			else if ((thisItem instanceof ContributionWrapper)
					|| (thisItem instanceof SATC_Solution))
			{
				if (subjects.length == 1)
				{
					BaseTimePeriod period = null;

					if (thisItem instanceof ContributionWrapper)
					{
						ContributionWrapper cw = (ContributionWrapper) thisItem;
						BaseContribution theCont = cw.getContribution();
						period = new TimePeriod.BaseTimePeriod(new HiResDate(
								theCont.getStartDate()), new HiResDate(theCont.getFinishDate()));

					}
					else
					{
						SATC_Solution solution = (SATC_Solution) thisItem;

						if (solution.getSolver().getProblemSpace().getStartDate() != null)
							period = new TimePeriod.BaseTimePeriod(solution.getStartDTG(),
									solution.getEndDTG());
					}

					// did it work?
					if (period != null)
					{
						parent.add(new Separator());
						
						SATC_Solution solution = (SATC_Solution) parentLayers[0];
						String actionTitle = "Add new contribution";

						parent.add(new DoIt("Add Speed Forecast for period covered by ["
								+ thisItem.getName() + "]",
								new SpeedForecastContributionFromCuts(solution, actionTitle,
										theLayers, period)));
						parent.add(new DoIt("Add Course Forecast for period covered by ["
								+ thisItem.getName() + "]",
								new CourseForecastContributionFromCuts(solution, actionTitle,
										theLayers, period)));
						parent.add(new DoIt("Add Straight Leg for period covered by ["
								+ thisItem.getName() + "]",
								new StraightLegForecastContributionFromCuts(solution,
										actionTitle, theLayers, period)));
					}
				}
			}
		}

		// ok, is it worth going for?
		if (validCuts != null)
		{
			final String title;
			if (validCuts.size() > 1)
				title = "sensor cuts";
			else
				title = "sensor cut";

			// right,stick in a separator
			thisMenu.add(new Separator());

			// see if there's an existing solution in there.
			SATC_Solution[] existingSolutions = findExistingSolutionsIn(theLayers);

			if ((existingSolutions != null) && (existingSolutions.length > 0))
			{
				for (int i = 0; i < existingSolutions.length; i++)
				{
					final SATC_Solution layer = existingSolutions[i];

					// create a top level menu item
					MenuManager thisD = new MenuManager("Add to " + layer.getName());
					thisMenu.add(thisD);

					// add the child items
					addItemsTo(layer, thisD, validCuts, title, null, "");
				}
			}

			DoIt wizardItem = new DoIt("Create new scenario from these cuts",
					new BearingMeasurementContributionFromCuts(null, title, theLayers,
							validCuts));
			wizardItem.setImageDescriptor(SATC_Interface_Activator
					.getImageDescriptor("icons/document_new.png"));
			thisMenu.add(wizardItem);
		}
	}

	protected void addItemsTo(final SATC_Solution solution,
			final MenuManager parent,
			final ArrayList<SensorContactWrapper> validItems, final String title,
			Layers layers, String verb1)
	{

		String actionTitle = "Add new contribution";

		DoIt wizardItem = new DoIt("Open Straight Leg Wizard for period [" + title
				+ "]", new StraightLegWizardFromCuts(solution, actionTitle, layers,
				validItems));
		wizardItem.setImageDescriptor(SATC_Interface_Activator
				.getImageDescriptor("icons/wizard.png"));
		parent.add(wizardItem);
		parent.add(new Separator());
		parent.add(new DoIt(verb1 + "Bearing Measurement from " + title,
				new BearingMeasurementContributionFromCuts(solution, actionTitle,
						layers, validItems)));
		parent.add(new DoIt(verb1 + "Speed Forecast for period [" + title + "]",
				new SpeedForecastContributionFromCuts(solution, actionTitle, layers,
						validItems)));
		parent.add(new DoIt(verb1 + "Range Forecast for period [" + title + "]",
				new RangeForecastContributionFromCuts(solution, actionTitle, layers,
						validItems)));
		parent.add(new DoIt(verb1 + "Course Forecast for period [" + title + "]",
				new CourseForecastContributionFromCuts(solution, actionTitle, layers,
						validItems)));
		parent.add(new DoIt(verb1 + "Straight Leg for period [" + title + "]",
				new StraightLegForecastContributionFromCuts(solution, actionTitle,
						layers, validItems)));

	}

	protected static class DoIt extends Action
	{
		private final IUndoableOperation _myOperation;

		DoIt(String title, IUndoableOperation operation)
		{
			super(title);
			_myOperation = operation;
		}

		@Override
		public void run()
		{
			CorePlugin.run(_myOperation);
		}

	}<commit_msg>also use icons for popup menus<commit_after>package org.mwc.debrief.satc_interface.actions;

import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.mwc.cmap.core.CorePlugin;
import org.mwc.cmap.core.operations.CMAPOperation;
import org.mwc.cmap.core.property_support.RightClickSupport.RightClickContextItemGenerator;
import org.mwc.debrief.satc_interface.SATC_Interface_Activator;
import org.mwc.debrief.satc_interface.data.SATC_Solution;
import org.mwc.debrief.satc_interface.data.wrappers.ContributionWrapper;
import org.mwc.debrief.satc_interface.utilities.conversions;
import org.mwc.debrief.satc_interface.wizards.NewStraightLegWizard;

import Debrief.Wrappers.SensorContactWrapper;
import Debrief.Wrappers.SensorWrapper;
import MWC.GUI.Editable;
import MWC.GUI.Layer;
import MWC.GUI.Layers;
import MWC.GenericData.HiResDate;
import MWC.GenericData.TimePeriod;
import MWC.GenericData.TimePeriod.BaseTimePeriod;
import MWC.GenericData.WorldDistance;
import MWC.GenericData.WorldLocation;
import MWC.Utilities.TextFormatting.FormatRNDateTime;

import com.planetmayo.debrief.satc.model.GeoPoint;
import com.planetmayo.debrief.satc.model.contributions.BaseContribution;
import com.planetmayo.debrief.satc.model.contributions.BearingMeasurementContribution;
import com.planetmayo.debrief.satc.model.contributions.BearingMeasurementContribution.BMeasurement;
import com.planetmayo.debrief.satc.model.contributions.CourseAnalysisContribution;
import com.planetmayo.debrief.satc.model.contributions.CourseForecastContribution;
import com.planetmayo.debrief.satc.model.contributions.LocationAnalysisContribution;
import com.planetmayo.debrief.satc.model.contributions.RangeForecastContribution;
import com.planetmayo.debrief.satc.model.contributions.RangeForecastContribution.ROrigin;
import com.planetmayo.debrief.satc.model.contributions.SpeedAnalysisContribution;
import com.planetmayo.debrief.satc.model.contributions.SpeedForecastContribution;
import com.planetmayo.debrief.satc.model.contributions.StraightLegForecastContribution;
import com.planetmayo.debrief.satc.model.generator.IContributions;
import com.planetmayo.debrief.satc.model.generator.ISolver;
import com.planetmayo.debrief.satc.model.manager.ISolversManager;
import com.planetmayo.debrief.satc_rcp.SATC_Activator;

public class CreateSolutionFromSensorData implements
		RightClickContextItemGenerator
{

	@Override
	public void generate(IMenuManager parent, final Layers theLayers,
			Layer[] parentLayers, Editable[] subjects)
	{
		ArrayList<SensorContactWrapper> validCuts = null;

		IMenuManager thisMenu = new MenuManager("SemiAuto TMA");
		parent.add(thisMenu);

		for (int i = 0; i < subjects.length; i++)
		{
			Editable thisItem = subjects[i];
			if (thisItem instanceof SensorContactWrapper)
			{
				if (validCuts == null)
					validCuts = new ArrayList<SensorContactWrapper>();

				validCuts.add((SensorContactWrapper) thisItem);
			}
			else if (thisItem instanceof SensorWrapper)
			{
				if (validCuts == null)
					validCuts = new ArrayList<SensorContactWrapper>();

				SensorWrapper sw = (SensorWrapper) thisItem;
				Enumeration<Editable> cuts = sw.elements();
				while (cuts.hasMoreElements())
				{
					validCuts.add((SensorContactWrapper) cuts.nextElement());
				}
			}
			else if ((thisItem instanceof ContributionWrapper)
					|| (thisItem instanceof SATC_Solution))
			{
				if (subjects.length == 1)
				{
					BaseTimePeriod period = null;

					if (thisItem instanceof ContributionWrapper)
					{
						ContributionWrapper cw = (ContributionWrapper) thisItem;
						BaseContribution theCont = cw.getContribution();
						period = new TimePeriod.BaseTimePeriod(new HiResDate(
								theCont.getStartDate()), new HiResDate(theCont.getFinishDate()));

					}
					else
					{
						SATC_Solution solution = (SATC_Solution) thisItem;

						if (solution.getSolver().getProblemSpace().getStartDate() != null)
							period = new TimePeriod.BaseTimePeriod(solution.getStartDTG(),
									solution.getEndDTG());
					}

					// did it work?
					if (period != null)
					{
						parent.add(new Separator());
						
						SATC_Solution solution = (SATC_Solution) parentLayers[0];
						String actionTitle = "Add new contribution";

						parent.add(new DoIt("Add Speed Forecast for period covered by ["
								+ thisItem.getName() + "]",
								new SpeedForecastContributionFromCuts(solution, actionTitle,
										theLayers, period),"icons/speed.png"));
						parent.add(new DoIt("Add Course Forecast for period covered by ["
								+ thisItem.getName() + "]",
								new CourseForecastContributionFromCuts(solution, actionTitle,
										theLayers, period),"icons/direction.png"));
						parent.add(new DoIt("Add Straight Leg for period covered by ["
								+ thisItem.getName() + "]",
								new StraightLegForecastContributionFromCuts(solution,
										actionTitle, theLayers, period),"icons/leg.png"));
					}
				}
			}
		}

		// ok, is it worth going for?
		if (validCuts != null)
		{
			final String title;
			if (validCuts.size() > 1)
				title = "sensor cuts";
			else
				title = "sensor cut";

			// right,stick in a separator
			thisMenu.add(new Separator());

			// see if there's an existing solution in there.
			SATC_Solution[] existingSolutions = findExistingSolutionsIn(theLayers);

			if ((existingSolutions != null) && (existingSolutions.length > 0))
			{
				for (int i = 0; i < existingSolutions.length; i++)
				{
					final SATC_Solution layer = existingSolutions[i];

					// create a top level menu item
					MenuManager thisD = new MenuManager("Add to " + layer.getName());
					thisMenu.add(thisD);

					// add the child items
					addItemsTo(layer, thisD, validCuts, title, null, "");
				}
			}

			DoIt wizardItem = new DoIt("Create new scenario from these cuts",
					new BearingMeasurementContributionFromCuts(null, title, theLayers,
							validCuts));
			wizardItem.setImageDescriptor(SATC_Interface_Activator
					.getImageDescriptor("icons/document_new.png"));
			thisMenu.add(wizardItem);
		}
	}

	protected void addItemsTo(final SATC_Solution solution,
			final MenuManager parent,
			final ArrayList<SensorContactWrapper> validItems, final String title,
			Layers layers, String verb1)
	{

		String actionTitle = "Add new contribution";

		DoIt wizardItem = new DoIt("Open Straight Leg Wizard for period covered by [" + title
				+ "]", new StraightLegWizardFromCuts(solution, actionTitle, layers,
				validItems));
		wizardItem.setImageDescriptor(SATC_Interface_Activator
				.getImageDescriptor("icons/wizard.png"));
		parent.add(wizardItem);
		parent.add(new Separator());
		parent.add(new DoIt(verb1 + "Bearing Measurement from " + title,
				new BearingMeasurementContributionFromCuts(solution, actionTitle,
						layers, validItems),"icons/bearings.gif"));
		parent.add(new DoIt(verb1 + "Speed Forecast for period covered by [" + title + "]",
				new SpeedForecastContributionFromCuts(solution, actionTitle, layers,
						validItems),"icons/speed.png"));
		parent.add(new DoIt(verb1 + "Range Forecast for period covered by [" + title + "]",
				new RangeForecastContributionFromCuts(solution, actionTitle, layers,
						validItems),"icons/range.png"));
		parent.add(new DoIt(verb1 + "Course Forecast for period covered by [" + title + "]",
				new CourseForecastContributionFromCuts(solution, actionTitle, layers,
						validItems),"icons/direction.png"));
		parent.add(new DoIt(verb1 + "Straight Leg for period covered by [" + title + "]",
				new StraightLegForecastContributionFromCuts(solution, actionTitle,
						layers, validItems),"icons/leg.png"));

	}

	protected static class DoIt extends Action
	{
		private final IUndoableOperation _myOperation;

		DoIt(String title, IUndoableOperation operation)
		{
			super(title);
			_myOperation = operation;
		}

		DoIt(String title, IUndoableOperation operation, String path)
		{
			this(title, operation);
			this.setImageDescriptor(SATC_Interface_Activator.getImageDescriptor(path));
		}

		@Override
		public void run()
		{
			CorePlugin.run(_myOperation);
		}

	}
{'fraction_non_alphanumeric': 0.10411919368974584, 'fraction_numerical': 0.0014022787028921998, 'mean_word_length': 4.535575679172057, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '3026163', 'n_tokens_mistral': 6347, 'n_tokens_neox': 5264, 'n_words': 1020}