<commit_before>package org.mwc.cmap.grideditor.chart.location;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdapterFactory;
import org.mwc.cmap.grideditor.GridEditorUndoSupport;
import org.mwc.cmap.grideditor.chart.BackedChartItem;
import org.mwc.cmap.grideditor.chart.ChartDataManager;
import org.mwc.cmap.grideditor.chart.DataPointsDragTracker;
import org.mwc.cmap.grideditor.chart.GriddableItemChartComponent;
import org.mwc.cmap.grideditor.chart.JFreeChartComposite;
import org.mwc.cmap.grideditor.chart.Value2ValueManager;
import org.mwc.cmap.grideditor.command.BeanUtil;
import org.mwc.cmap.grideditor.command.OperationEnvironment;
import org.mwc.cmap.grideditor.command.SetDescriptorValueOperation;
import org.mwc.cmap.gridharness.data.GriddableItemDescriptor;
import org.mwc.cmap.gridharness.data.GriddableItemDescriptorExtension;

import MWC.GUI.TimeStampedDataItem;
import MWC.GenericData.WorldLocation;


public class LocationChartAccess implements IAdapterFactory {

	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (false == adaptableObject instanceof GriddableItemDescriptorExtension) {
			return null;
		}
		if (!ChartDataManager.class.isAssignableFrom(adapterType)) {
			return null;
		}
		GriddableItemDescriptorExtension descriptor = (GriddableItemDescriptorExtension) adaptableObject;
		if (WorldLocation.class.isAssignableFrom(descriptor.getType())) {
			return createLocationChart(descriptor);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Class[] getAdapterList() {
		return new Class[] { ChartDataManager.class };
	}

	private ChartDataManager createLocationChart(final GriddableItemDescriptor descriptor) {
		return new Longitude2LatitudeChartManager(descriptor);
	}

	private static class Longitude2LatitudeChartManager extends Value2ValueManager {

		private DataPointsDragTracker myDragTracker;

		public Longitude2LatitudeChartManager(GriddableItemDescriptor descriptor) {
			super(descriptor, createLatitudeAccessor(descriptor), createLongitudeAccessor(descriptor));
		}

		@Override
		public void attach(JFreeChartComposite chartPanel) {
			super.attach(chartPanel);
			final GridEditorUndoSupport undoSupport = chartPanel.getActionContext().getUndoSupport();
			if (undoSupport != null) {
				myDragTracker = new DataPointsDragTracker(chartPanel, false) {

					@Override
					protected void dragCompleted(BackedChartItem item, double finalX, double finalY) {
						OperationEnvironment environment = new OperationEnvironment(undoSupport.getUndoContext(), getInput(), item.getDomainItem(), getDescriptor());
						WorldLocation location = new WorldLocation(finalY, finalX,0);
						SetDescriptorValueOperation setLocation = new SetDescriptorValueOperation(environment, location);
						try {
							undoSupport.getOperationHistory().execute(setLocation, null, null);
						} catch (ExecutionException e) {
							throw new RuntimeException("Can't set the location of :" + location + //<commit_msg>Dragging points working nicely<commit_after>package org.mwc.cmap.grideditor.chart.location;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdapterFactory;
import org.mwc.cmap.grideditor.GridEditorUndoSupport;
import org.mwc.cmap.grideditor.chart.BackedChartItem;
import org.mwc.cmap.grideditor.chart.ChartDataManager;
import org.mwc.cmap.grideditor.chart.DataPointsDragTracker;
import org.mwc.cmap.grideditor.chart.GriddableItemChartComponent;
import org.mwc.cmap.grideditor.chart.JFreeChartComposite;
import org.mwc.cmap.grideditor.chart.Value2ValueManager;
import org.mwc.cmap.grideditor.command.BeanUtil;
import org.mwc.cmap.grideditor.command.OperationEnvironment;
import org.mwc.cmap.grideditor.command.SetDescriptorValueOperation;
import org.mwc.cmap.gridharness.data.GriddableItemDescriptor;
import org.mwc.cmap.gridharness.data.GriddableItemDescriptorExtension;

import MWC.GUI.TimeStampedDataItem;
import MWC.GenericData.WorldLocation;


public class LocationChartAccess implements IAdapterFactory {

	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (false == adaptableObject instanceof GriddableItemDescriptorExtension) {
			return null;
		}
		if (!ChartDataManager.class.isAssignableFrom(adapterType)) {
			return null;
		}
		GriddableItemDescriptorExtension descriptor = (GriddableItemDescriptorExtension) adaptableObject;
		if (WorldLocation.class.isAssignableFrom(descriptor.getType())) {
			return createLocationChart(descriptor);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Class[] getAdapterList() {
		return new Class[] { ChartDataManager.class };
	}

	private ChartDataManager createLocationChart(final GriddableItemDescriptor descriptor) {
		return new Longitude2LatitudeChartManager(descriptor);
	}

	private static class Longitude2LatitudeChartManager extends Value2ValueManager {

		private DataPointsDragTracker myDragTracker;

		public Longitude2LatitudeChartManager(GriddableItemDescriptor descriptor) {
			super(descriptor, createLatitudeAccessor(descriptor), createLongitudeAccessor(descriptor));
		}

		@Override
		public void attach(JFreeChartComposite chartPanel) {
			super.attach(chartPanel);
			final GridEditorUndoSupport undoSupport = chartPanel.getActionContext().getUndoSupport();
			if (undoSupport != null) {
				myDragTracker = new DataPointsDragTracker(chartPanel, false) {

					@Override
					protected void dragCompleted(BackedChartItem item, double finalX, double finalY) {
						OperationEnvironment environment = new OperationEnvironment(undoSupport.getUndoContext(), getInput(), item.getDomainItem(), getDescriptor());
						WorldLocation location = new WorldLocation(finalX, finalY,0);
						SetDescriptorValueOperation setLocation = new SetDescriptorValueOperation(environment, location);
						try {
							undoSupport.getOperationHistory().execute(setLocation, null, null);
						} catch (ExecutionException e) {
							throw new RuntimeException("Can't set the location of :" + location + //
{'fraction_non_alphanumeric': 0.08678693320331546, 'fraction_numerical': 0.0019502681618722574, 'mean_word_length': 6.343675417661098, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2149423', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1724, 'n_words': 319}