/*
 *    Debrief - the Open Source Maritime Analysis Application
 *    http://debrief.info
 *
 *    (C) 2000-2014, PlanetMayo Ltd
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the Eclipse Public License v1.0
 *    (http://www.eclipse.org/legal/epl-v10.html)
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 */
package org.mwc.cmap.core.wizards;

import java.text.ParseException;

import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Text;
import org.mwc.cmap.core.CorePlugin;
import org.osgi.service.prefs.Preferences;

import MWC.GenericData.HiResDate;
import MWC.Utilities.TextFormatting.FullFormatDateTime;

public class EnterDTGPage extends EnterStringPage implements ModifyListener
{
	private static final String DATE = "DATE";

	public EnterDTGPage(final ISelection selection, final HiResDate startDate,
			final String pageTitle, final String pageExplanation, final String fieldExplanation, final String imagePath, final String helpContext)
	{
		super(selection,
				FullFormatDateTime.toString(startDate.getDate().getTime()), pageTitle,
				pageExplanation, fieldExplanation, imagePath, helpContext, true, null);

		// tell the editor we're listening for modifications
		super.addModifiedListener(this);

		setDefaults();
	}

	private void setDefaults()
	{
		final Preferences prefs = getPrefs();

		if (prefs != null)
		{
			_startName = prefs.get(DATE, _startName);
		}
	}

	/**
	 * sort out our answer
	 * 
	 * @return
	 */
	public HiResDate getDate()
	{
		HiResDate res = null;
		try
		{
			final long time = FullFormatDateTime.fromString(super.getString());
			res = new HiResDate(time);
		}
		catch (final ParseException e)
		{
			CorePlugin.logError(Status.ERROR, "Parsing this text:"
					+ super.getString(), e);
		}
		return res;
	}
	

	@Override
	public void dispose()
	{
		// try to store some defaults
		final Preferences prefs = getPrefs();
		prefs.put(DATE, _myWrapper.getName());

		super.dispose();
	}

	public void modifyText(final ModifyEvent e)
	{
		// ok, check we can parse it
		final Text text = (Text) e.widget;
		final String val = text.getText();

		try
		{
			// do a trial conversion, just to check it's valid
			FullFormatDateTime.fromString(val);
		}
		catch (final ParseException e1)
		{
			// invalid - try to stop it!!!
			super.setErrorMessage("Date not formatted correctly");

		}
	}

}

@@ -602,16 +602,40 @@
 eption;%0D
+%0Aimport java.util.Date;%0D
 %0A%0D%0Aimpor
@@ -1426,21 +1426,12 @@
 ing(
-startDate.get
+new 
 Date
@@ -1632,121 +1632,74 @@
 );%0D%0A
-%0D%0A%09%09setDefaults();%0D%0A%09%7D%0D%0A%0D%0A%09private void setDefaults()%0D%0A%09%7B%0D%0A%09%09final Preferences prefs = getPrefs();%0D%0A%0D%0A%09%09if (prefs
+%09%09%0D%0A%09%09// if we have a date, overwrite today's date%0D%0A%09%09if(startDate
  != 
@@ -1712,17 +1712,18 @@
 %0A%09%09%7B%0D%0A%09%09
-%09
+  
 _startNa
@@ -1731,34 +1731,65 @@
 e = 
-prefs.get(DATE, _startName
+FullFormatDateTime.toString(startDate.getDate().getTime()
 );%0D%0A
@@ -1799,16 +1799,18 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%0D%0A
 %09/**%0D%0A%09 
@@ -2783,18 +2783,16 @@
 tly%22);%0D%0A
-%0D%0A
 %09%09%7D%0D%0A%09%7D%0D
@@ -2792,13 +2792,11 @@
 %09%7D%0D%0A%09%7D%0D%0A
-%0D%0A
 %7D%0D%0A

allow for null date, but overwrite default if one is present