/**
 * 
 */
package org.mwc.debrief.core.interfaces;

import java.io.InputStream;

import org.eclipse.core.runtime.*;
import org.eclipse.ui.IEditorInput;
import org.mwc.cmap.core.interfaces.INamedItem;
import org.mwc.debrief.core.DebriefPlugin;
import org.mwc.debrief.core.editors.PlotEditor;

/** Interface for classes which are capable of populating a plot from a file
 * 
 * @author ian.mayo
 *
 */
public interface IPlotLoader extends INamedItem
{
	/** load the supplied editor input into the plot
	 * 
	 * @param thePlot the plot destination
	 * @param inputStream the file source
	 * @param fileName TODO
	 */
	public void loadFile(final PlotEditor thePlot, final InputStream inputStream, final String fileName);

	
	/** test whether this loader can load the suppled input source
	 * 
	 * @param fileName the input file to check
	 * @return yes/no
	 */
	public boolean canLoad(String fileName);
	
	/** utility method to initialise this loader - we need to do
	 * this since when we use these objects as plugins Eclipse has to 
	 * call the zero-argument constructor (and we supply the data with these methods)
	 * 
	 * @param name
	 * @param icon
	 * @param fileTypes
	 */
	public void init(String name, String icon, String fileTypes);
	
	abstract public static class BaseLoader implements IPlotLoader
	{
		String _myName;
		String _icon;
		String _fileTypes;
		
		public void init(final String name,  final String icon, final String fileTypes)
		{
			_myName = name;
			_icon = icon;
			_fileTypes = fileTypes;
		}
		
		public final String getName()
		{
			return _myName;
		}
		
		public String getFileName(IEditorInput input)
		{
			String res = null;

			res = "c:\\boat1.rep";
			
			return res;
		}

		
		/** test whether this loader can load the suppled input source
		 * 
		 * @param fileName the input file to check
		 * @return yes/no
		 */
		public boolean canLoad(String fileName)
		{
			return (fileName.toUpperCase().indexOf(_fileTypes.toUpperCase()) > 0);
		}
		
	
		
	}
	
	public static class DeferredPlotLoader extends BaseLoader
	{
		final IConfigurationElement _config;
		
		BaseLoader _myLoader = null;
		
		/** constructor - stores the information necessary to load the data
		 * 
		 * @param configElement
		 * @param name
		 * @param icon
		 * @param fileTypes
		 */
		public DeferredPlotLoader(IConfigurationElement configElement,
				String name,  String icon, String fileTypes)
		{
			_config = configElement;
			init(name, icon, fileTypes);
		}
		

		public void loadFile(PlotEditor thePlot, InputStream inputStream, String fileName)
		{
			if(_myLoader == null)
			{
				try
				{
					System.out.println("About to load new loader for:" + getName());

					// and create the loader
					_myLoader = (BaseLoader) _config.createExecutableExtension("class");

					// hey, stick the data in
					_myLoader.init(_myName, _icon, _fileTypes);					
						
				}
				catch (CoreException e)
				{			
					DebriefPlugin.logError(Status.ERROR, "Failed to create instance of loader:"
							+ _config, e);
					
				}
			}

			if(_myLoader != null)
			{
				// we either had it already, or we're trying to load it now. go for it
				_myLoader.loadFile(thePlot, inputStream, fileName);
			}
			else
			{
				DebriefPlugin.logError(Status.ERROR, "Unable to load file. Loader unavailable for:"
						+ _config, null);
				
			}
			
		}

		
	}
}

@@ -74,16 +74,50 @@
 tStream;
+%0Aimport java.util.StringTokenizer;
 %0A%0Aimport
@@ -1933,23 +1933,212 @@
 %0A%09%09%7B%0A%09%09%09
-return 
+boolean res = false;%0A%09%09%09// now pass through our list%0A%09%09%09String%5B%5D mySuffixes = _fileTypes.split(%22;%22);%0A%09%09%09for (int i = 0; i %3C mySuffixes.length; i++)%0A%09%09%09%7B%0A%09%09%09%09String mySuffix = mySuffixes%5Bi%5D;%0A%09%09%09%09if
 (fileNam
@@ -2157,26 +2157,25 @@
 e().
-indexOf(_fileTypes
+endsWith(mySuffix
 .toU
@@ -2189,13 +2189,57 @@
 e())
- %3E 0)
+)%0A%09%09%09%09%7B%0A%09%09%09%09%09res = true;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09return res
 ;%0A%09%09

Handle wider range of file suffixes