/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylar.internal.ide;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.mylar.internal.core.util.MylarStatusHandler;
import org.eclipse.mylar.provisional.core.InteractionEvent;
import org.eclipse.mylar.provisional.core.MylarPlugin;

/**
 * @author Mik Kersten
 */
public class ResourceChangeMonitor implements IResourceChangeListener {

	private boolean enabled = true;

	public void resourceChanged(IResourceChangeEvent event) {
		if (!enabled || !MylarPlugin.getContextManager().isContextActive()
				|| MylarPlugin.getContextManager().isContextCapturePaused()) {
			return;
		}
		if (event.getType() != IResourceChangeEvent.POST_CHANGE) {
			return;
		}
		final List<IResource> addedResources = new ArrayList<IResource>();
		final List<IResource> changedResources = new ArrayList<IResource>();
		IResourceDelta rootDelta = event.getDelta();
		IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {
			public boolean visit(IResourceDelta delta) {
				IResourceDelta[] added = delta.getAffectedChildren(IResourceDelta.ADDED);
				for (int i = 0; i < added.length; i++) {
					IResource resource = added[i].getResource();
					addedResources.add(resource);
				}
				IResourceDelta[] changed = delta.getAffectedChildren(IResourceDelta.CHANGED);
				for (int i = 0; i < changed.length; i++) {
					IResource resource = changed[i].getResource();
					changedResources.add(resource);
				}
				IResourceDelta[] removed = delta.getAffectedChildren(IResourceDelta.REMOVED);
				for (int i = 0; i < removed.length; i++) {
					IResource resource = removed[i].getResource();
					changedResources.add(resource);
				}
				return true;
			}
		};
		try {
			rootDelta.accept(visitor);
			MylarIdePlugin.getDefault().getInterestUpdater().addResourceToContext(addedResources, InteractionEvent.Kind.SELECTION);
			MylarIdePlugin.getDefault().getInterestUpdater().addResourceToContext(changedResources, InteractionEvent.Kind.PREDICTION);
		} catch (CoreException e) {
			MylarStatusHandler.log(e, "could not accept marker visitor");
		}
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	// private void processMarkerDelata(IMarkerDelta[] markers) {
	// for(IMarkerDelta markerDelta: markers){
	// try{
	// final IMarker marker = markerDelta.getMarker();
	// if(marker == null || !marker.exists()){
	// final IMylarStructureBridge bridge =
	// MylarPlugin.getDefault().getStructureBridge(marker.getResource());
	// if(bridge != null){
	// if(!PlatformUI.getWorkbench().getDisplay().isDisposed()) {
	// PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
	// public void run() {
	// MylarPlugin.getContextManager().removeErrorPredictedInterest(bridge.getHandleIdentifier(marker.getResource()),
	// bridge.getContentType(), true);
	// }});
	// }
	// }
	// }
	//					
	// if(markerDelta.getMarker().isSubtypeOf(IMarker.PROBLEM)){
	// final IMylarStructureBridge bridge =
	// MylarPlugin.getDefault().getStructureBridge(marker.getResource());
	// if(bridge != null){
	// if(!PlatformUI.getWorkbench().getDisplay().isDisposed()) {
	// PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
	// public void run() {
	// MylarPlugin.getContextManager().addErrorPredictedInterest(bridge.getHandleIdentifier(marker.getResource()),
	// bridge.getContentType(), true);
	// }});
	// }
	// }
	// } else
	// {//if(!markerDelta.getMarker().getType().equals("org.eclipse.jdt.core.problem")){
	// final IMylarStructureBridge bridge =
	// MylarPlugin.getDefault().getStructureBridge(marker.getResource());
	// if(bridge != null){
	// if(!PlatformUI.getWorkbench().getDisplay().isDisposed()) {
	// PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
	// public void run() {
	// MylarPlugin.getContextManager().removeErrorPredictedInterest(bridge.getHandleIdentifier(marker.getResource()),
	// bridge.getContentType(), true);
	// }});
	// }
	// }
	// }
	// }catch (Exception e){
	// MylarPlugin.log(e, " could not update marker");
	// }
	// }
	// }
}

@@ -2503,40 +2503,312 @@
 %09%09%09%09
-changedResources.add(resource);%0A
+System.err.println(%22%3E%3E %22 + resource);%0A%09%09%09%09%09changedResources.add(resource);%0A%09%09%09%09%7D%0A//%09%09%09%09IResourceDelta%5B%5D moved = delta.getAffectedChildren(IResourceDelta.MOVED_TO);%0A//%09%09%09%09for (int i = 0; i %3C moved.length; i++) %7B%0A//%09%09%09%09%09IResource resource = moved%5Bi%5D.getResource();%0A//%09%09%09%09%09changedResources.add(resource);%0A//
 %09%09%09%09

Add to change set fails??