/**
 * 
 */
package org.mwc.debrief.core.creators.chartFeatures;

import org.eclipse.core.runtime.Status;
import org.eclipse.ui.*;
import org.mwc.cmap.core.CorePlugin;
import org.mwc.cmap.core.operations.DebriefActionWrapper;
import org.mwc.cmap.plotViewer.actions.CoreEditorAction;
import org.mwc.cmap.plotViewer.editors.CorePlotEditor;
import org.mwc.debrief.core.preferences.PrefsPage;
import org.mwc.debrief.core.preferences.PrefsPage.PreferenceConstants;

import MWC.GUI.*;
import MWC.GUI.Tools.Action;
import MWC.GUI.Tools.Palette.PlainCreate;
import MWC.GenericData.WorldArea;

/**
 * @author ian.mayo
 */
abstract public class CoreInsertChartFeature extends CoreEditorAction
{

	public static ToolParent _theParent = null;

	/**
	 * ok, store who the parent is for the operation
	 * 
	 * @param theParent
	 */
	public static void init(ToolParent theParent)
	{
		_theParent = theParent;
	}

	/**
	 * and execute..
	 */
	protected void execute()
	{
		final PlainChart theChart = getChart();

		Action res = createAction(theChart);

		// ok, now wrap the action
		DebriefActionWrapper daw = new DebriefActionWrapper(res);

		// and add it to our buffer (which will execute it anyway)
		CorePlugin.run(daw);
	}

	protected final Action createAction(PlainChart theChart)
	{
		Action res = null;
		WorldArea wa = theChart.getDataArea();

		// see if we have an area defined
		if (wa != null)
		{
			// ok - get the object we're going to insert
			Plottable thePlottable = getPlottable(theChart);

			// lastly, get the data
			Layers theData = theChart.getLayers();

			// ok, get our layer name
			final String myLayer = getLayerName();

			// aah, and the misc layer, in which we will store the shape
			Layer theLayer = theData.findLayer(myLayer);

			// did we find it?
			if (theLayer == null)
			{
				// nope, better create it.
				theLayer = new BaseLayer();
				theLayer.setName(myLayer);
				theData.addThisLayer(theLayer);
			}

			// and put it into an action (so we can undo it)
			res = new PlainCreate.CreateLabelAction(null, theLayer, theChart.getLayers(),
					thePlottable)
			{

				public void execute()
				{
					// generate the object
					super.execute();

					// right, does the user want me to auto-select the newly created item?
					String autoSelectStr = CorePlugin.getToolParent().getProperty(
							PrefsPage.PreferenceConstants.AUTO_SELECT);
					boolean autoSelect = Boolean.getBoolean(autoSelectStr);
					if (autoSelect)
					{

						// ok, now open the properties window
						try
						{
							PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
									.showView(IPageLayout.ID_PROP_SHEET);
						}
						catch (PartInitException e)
						{
							CorePlugin.logError(Status.WARNING, "Failed to open properties view", e);
						}

						// find the editor
						CorePlotEditor editor = getEditor();

						// highlight the editor
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
								.activate(editor);

						// select the shape
						editor.selectPlottable(_theShape, _theLayer);
					}
				}
			};
		}
		else
		{
			// we haven't got an area, inform the user
			CorePlugin
					.showMessage("Create Feature",
							"Sorry, we can't create a shape until the area is defined.  Try adding a coastline first");
		}

		return res;
	}

	/**
	 * ok, create whatever we're after
	 * 
	 * @param theChart
	 * @return
	 */
	abstract protected Plottable getPlottable(PlainChart theChart);

	/**
	 * @return
	 */
	protected String getLayerName()
	{
		return Layers.CHART_FEATURES;
	}
}
@@ -385,79 +385,8 @@
 age;
-%0Aimport org.mwc.debrief.core.preferences.PrefsPage.PreferenceConstants;
 %0A%0Aim
@@ -2334,19 +2334,21 @@
 Boolean.
-get
+parse
 Boolean(

Introduce & use preferences