/******************************************************************************
 *                                                                            *
 *  (c) Copyright 2006 The Open University UK  								  *
 *                                                                            *
 *          This program code may not be used or distributed except in        *
 *                accordance with the license published at                    *
 *            http://www.ecosensus.info/software/license.htm    		      *
 *                                                                            *
 ******************************************************************************/

package info.ecosensus.udig.actions;

import java.io.File;
import java.lang.String;
import java.net.MalformedURLException;
import java.net.URL;

import net.refractions.udig.ui.IDropAction;
import net.refractions.udig.project.ILayer;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import info.ecosensus.udig.ui.UIUtilities;

public class IconDropAction extends IDropAction {

	public IconDropAction() {
		System.out.println("DROP CONSTRUCTOR");		
	}
		
	public void perform(org.eclipse.core.runtime.IProgressMonitor monitor){}
	public boolean accept() {
		System.out.println("IN first accept");
		return false; 
	}
	
	public boolean accept(java.lang.Object source,
            java.lang.Object destination) {
		
		System.out.println("source = "+source.getClass().getName());
		System.out.println("destination = "+destination.getClass().getName());
		
		if (source instanceof String && destination instanceof ILayer) {
			String temp = (String)source;
			if (temp.startsWith("ECOSENSUS:")) {
				return true;
			}
		}
		
		return false;
	}
	
	public void perform(java.lang.Object source,
            java.lang.Object destination,
            org.eclipse.core.runtime.IProgressMonitor monitor) {

		System.out.println("source in perform = "+source.getClass().getName());
		System.out.println("destination in perform = "+destination.getClass().getName());

		if (source instanceof String && destination instanceof ILayer) {
			String imagepath = (String)source;
			
			System.out.println("path = "+imagepath);
			
			if (imagepath.startsWith("ECOSENSUS:")) {
				int ind = imagepath.indexOf(":");
				imagepath = imagepath.substring(ind+1);
				String test = imagepath.toLowerCase();
				if (UIUtilities.isImage(test)) {					
					URL url = null;
					try {
						File file = new File(imagepath);
						String sFilePath = file.getAbsolutePath();
						sFilePath = UIUtilities.unixPath(sFilePath);
						url = new URL("file:///"+sFilePath);
						ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
						Image image = imageDescriptor.createImage();
						System.out.println("Icon dropped = "+imagepath);
						System.out.flush();
						
						//ILayer layer = (ILayer)destination;
						//FeatureResource resource = layer.getResource( FeatureResource, monitor);
					} catch (MalformedURLException e) {
						e.printStackTrace();
					}					
				}
			}
		}
	}
}
