package com.eclipserunner.model.adapters;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.internal.WorkingSet;
import org.eclipse.ui.navigator.resources.ProjectExplorer;

import com.eclipserunner.model.IFilteredRunnerModel;
import com.eclipserunner.model.INodeFilter;
import com.eclipserunner.model.filters.ProjectFilter;
import com.eclipserunner.views.impl.RunnerView;

/**
 * Adapter listening for JDT selection events.
 * 
 * @author bary
 */
@SuppressWarnings("restriction")
public class RunnerModelJdtSelectionListenerAdapter implements ISelectionListener {

	@SuppressWarnings("unused")
	private IFilteredRunnerModel model;
	private RunnerView view;
	private ProjectFilter projectFilter;

	public RunnerModelJdtSelectionListenerAdapter(IFilteredRunnerModel model, RunnerView view) {
		this.model = model;
		this.view = view;
		// Find ProjectFilter filter ;)
		for (INodeFilter filter : model.getFilters()) {
			if (filter instanceof ProjectFilter) {
				projectFilter = (ProjectFilter) filter;
				break;
			}
		}
	}

	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (part instanceof ProjectExplorer) {
			handleProjectExplorerSelection(selection);
		}
		else if (part instanceof PackageExplorerPart) {
			handlePackageExplorerSelection(selection);
		}
		// TODO: Implement or remove
		else {
			System.out.println("DEBUG: Unhandled JDT selection change part:" + part.getClass().getName() + ", selection: " + selection.getClass().getSimpleName());
		}
		
		// Refresh View 
		view.refresh();
	}

	private void handleProjectExplorerSelection(ISelection selection) {
		Object selectedElement = getSelectedElement(selection);
		if (selectedElement instanceof IResource) {
			projectFilter.setProjectName(getProjectName((IResource)selectedElement));
		}
		// TODO: Implement or remove
		else {
			System.out.println("DEBUG: Unhandled ProjectExplorer selection: " + selection.getClass().getSimpleName());
		}
	}
	
	private String getProjectName(IResource resource) {
		while (resource != null) {
			if (IResource.PROJECT == resource.getType()) {
				IProject project = (IProject) resource;
				return project.getName();
			}
			resource = resource.getParent();
		}		
		return null;
	}
	
	private void handlePackageExplorerSelection(ISelection selection) {
		Object selectedElement = getSelectedElement(selection);
		if (selectedElement instanceof WorkingSet) {
			projectFilter.setProjectName(null);   // clear project filter
		}
		else if (selectedElement instanceof JavaProject) {
			projectFilter.setProjectName(getProjectName((JavaProject) selectedElement));			
		}
		// TODO: Implement or remove
		else {
			System.out.println("DEBUG: Unhandled PackageExplorer selection: " + selection.getClass().getSimpleName());
		}		
	}
	
	private String getProjectName(JavaProject javaProject) {
		IProject project = javaProject.getProject();
		return project.getName();	
	}

	private Object getSelectedElement(ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			return structuredSelection.getFirstElement();
		}
		return null;
	}
	
}

@@ -125,16 +125,60 @@
 source;%0A
+import org.eclipse.core.runtime.IAdaptable;%0A
 import o
@@ -2865,62 +2865,124 @@
 %0A%09%09%7D
-%0A%09%09else if (selectedElement instanceof JavaProject
+ else %7B%0A%09%09%09IResource resource = extractResource(selection);%0A%09%09%09if(resource!=null && resource.getProject()!=null
 ) %7B%0A
+%09
 %09%09%09p
@@ -3001,32 +3001,41 @@
 .setProjectName(
+resource.
 getProjectName((
@@ -3032,53 +3032,34 @@
 ject
-Name((JavaProject) selectedElement
+().getName(
 ));
+%0A
 %09%09%09
+%7D
 %0A%09%09%7D%0A
+//
 %09%09//
@@ -3077,32 +3077,34 @@
 ement or remove%0A
+//
 %09%09else %7B%0A%09%09%09Syst
@@ -3088,32 +3088,34 @@
 move%0A//%09%09else %7B%0A
+//
 %09%09%09System.out.pr
@@ -3208,16 +3208,18 @@
 ame());%0A
+//
 %09%09%7D%09%09%0A%09%7D
@@ -3223,148 +3223,493 @@
 %0A%09%7D%0A
-%09
 %0A%09
-private String getProjectName(JavaProject javaProject) %7B%0A%09%09IProject project = javaProject.getProject();%0A%09%09return project.getName();%09
+IResource extractResource(ISelection selection) %7B%0A%09%09if (!(selection instanceof IStructuredSelection))%0A%09%09%09return null;%0A%09%09IStructuredSelection ss = (IStructuredSelection) selection;%0A%09%09Object element = ss.getFirstElement();%0A%09%09if (element instanceof IResource)%0A%09%09%09return (IResource) element;%0A%09%09if (!(element instanceof IAdaptable))%0A%09%09%09return null;%0A%09%09IAdaptable adaptable = (IAdaptable) element;%0A%09%09Object adapter = adaptable.getAdapter(IResource.class);%0A%09%09return (IResource) adapter;%0A
 %0A%09%7D%0A
-%0A
 %09pri

fix project filter