<commit_before>/*******************************************************************************
 * Copyright (c) 2004 - 2005 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.java;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IResource;
import org.eclipse.mylar.core.IMylarContext;
import org.eclipse.mylar.core.IMylarContextListener;
import org.eclipse.mylar.core.IMylarElement;
import org.eclipse.mylar.core.IMylarStructureBridge;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.ide.MylarIdePlugin;
import org.eclipse.mylar.tasklist.ITask;
import org.eclipse.mylar.tasklist.MylarTasklistPlugin;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.core.subscribers.SubscriberChangeSetCollector;

/**
 * @author <NAME>
 */
public class MylarChangeSetManager implements IMylarContextListener {

	private SubscriberChangeSetCollector collector;
	private Map<ITask, TaskContextChangeSet> changeSets = new HashMap<ITask, TaskContextChangeSet>();
	
	public MylarChangeSetManager() {
		this.collector = CVSUIPlugin.getPlugin().getChangeSetManager();
	}

	public IResource[] getResources(ITask task) {
		TaskContextChangeSet changeSet = changeSets.get(task);
		if (changeSet != null) {
			return changeSet.getResources();
		} else {
			return null;
		}
	}
	
	public void contextActivated(IMylarContext context) {
		try {
			ITask task = getTask(context); 
			if (task == null) {
				MylarPlugin.log("could not resolve task for context", this);
			} else if (!changeSets.containsKey(task)) { 
				TaskContextChangeSet changeSet = new TaskContextChangeSet(task, collector);
				changeSet.add(changeSet.getResources());
				changeSets.put(task, changeSet);
				if (!collector.contains(changeSet)) collector.add(changeSet);
			}
		} catch (Exception e) {
			MylarPlugin.fail(e, "could not update change set", false);
		}
	}

	public void contextDeactivated(IMylarContext context) {
		// TODO: support multiple tasks
		for (ITask task : changeSets.keySet()) {
			collector.remove(changeSets.get(task));			
		}
		changeSets.clear();
	}

	public List<TaskContextChangeSet> getChangeSets() {
		return new ArrayList<TaskContextChangeSet>(changeSets.values());
	}
	
	private ITask getTask(IMylarContext context) {
		List<ITask> activeTasks = MylarTasklistPlugin.getTaskListManager().getTaskList().getActiveTasks();
		
		// TODO: support multiple tasks
		if (activeTasks.size() > 0) {
			return activeTasks.get(0);
		} else {
			return null;
		}
	}
	
	public void interestChanged(IMylarElement element) {
		IMylarStructureBridge bridge = MylarPlugin.getDefault().getStructureBridge(element.getContentType());
		if (bridge.isDocument(element.getHandleIdentifier())) {
			IResource resource = MylarIdePlugin.getDefault().getResourceForElement(element);
			if (resource != null) {
				for (TaskContextChangeSet changeSet: getChangeSets()) {
					if (!changeSet.contains(resource)) {
						try {
							if (element.getInterest().isInteresting()) {
								changeSet.add(new IResource[] { resource });
							} else {
								changeSet.remove(resource);
							}
						} catch (TeamException e) {
							MylarPlugin.fail(e, "could not add resource to change set", false);
						}
					} 
				}
			}
		}
	}
	
	public void interestChanged(List<IMylarElement> elements) {
		for (IMylarElement element : elements) {
			interestChanged(element);
		}
	}

	public void nodeDeleted(IMylarElement node) {
		// ignore
	}

	public void landmarkAdded(IMylarElement node) {
		// ignore<commit_msg>Progress on: Bug 116450: making a resource less interesting fails to remove it from the commit set Issue URL: https://bugs.eclipse.org/bugs/show_bug.cgi?id=116450<commit_after>/*******************************************************************************
 * Copyright (c) 2004 - 2005 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.java;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.mylar.core.IMylarContext;
import org.eclipse.mylar.core.IMylarContextListener;
import org.eclipse.mylar.core.IMylarElement;
import org.eclipse.mylar.core.IMylarStructureBridge;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.ide.MylarIdePlugin;
import org.eclipse.mylar.tasklist.ITask;
import org.eclipse.mylar.tasklist.MylarTasklistPlugin;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.core.subscribers.SubscriberChangeSetCollector;

/**
 * @author <NAME>
 */
public class MylarChangeSetManager implements IMylarContextListener {

	private SubscriberChangeSetCollector collector;
	private Map<ITask, TaskContextChangeSet> changeSets = new HashMap<ITask, TaskContextChangeSet>();
	
	public MylarChangeSetManager() {
		this.collector = CVSUIPlugin.getPlugin().getChangeSetManager();
	}

	public IResource[] getResources(ITask task) {
		TaskContextChangeSet changeSet = changeSets.get(task);
		if (changeSet != null) {
			return changeSet.getResources();
		} else {
			return null;
		}
	}
	
	public void contextActivated(IMylarContext context) {
		try {
			ITask task = getTask(context); 
			if (task == null) {
				MylarPlugin.log("could not resolve task for context", this);
			} else if (!changeSets.containsKey(task)) { 
				TaskContextChangeSet changeSet = new TaskContextChangeSet(task, collector);
				changeSet.add(changeSet.getResources());
				changeSets.put(task, changeSet);
				if (!collector.contains(changeSet)) collector.add(changeSet);
			}
		} catch (Exception e) {
			MylarPlugin.fail(e, "could not update change set", false);
		}
	}

	public void contextDeactivated(IMylarContext context) {
		// TODO: support multiple tasks
		for (ITask task : changeSets.keySet()) {
			collector.remove(changeSets.get(task));			
		}
		changeSets.clear();
	}

	public List<TaskContextChangeSet> getChangeSets() {
		return new ArrayList<TaskContextChangeSet>(changeSets.values());
	}
	
	private ITask getTask(IMylarContext context) {
		List<ITask> activeTasks = MylarTasklistPlugin.getTaskListManager().getTaskList().getActiveTasks();
		
		// TODO: support multiple tasks
		if (activeTasks.size() > 0) {
			return activeTasks.get(0);
		} else {
			return null;
		}
	}
	
	public void interestChanged(IMylarElement element) {
		IMylarStructureBridge bridge = MylarPlugin.getDefault().getStructureBridge(element.getContentType());
		
		if (bridge.isDocument(element.getHandleIdentifier())) {
			IResource resource = MylarIdePlugin.getDefault().getResourceForElement(element);
			if (resource != null) {
				for (TaskContextChangeSet changeSet: getChangeSets()) {
					try {
						if (!changeSet.contains(resource)) {
							if (element.getInterest().isInteresting()) {
								changeSet.add(new IResource[] { resource });
							} 
						} else if (!element.getInterest().isInteresting()){
							changeSet.remove(resource);
							
							// HACK: touching ensures file is added outside of set
							if (resource instanceof IFile) {
								((IFile)resource).touch(new NullProgressMonitor());
							}
							if (!collector.contains(changeSet)) {
								collector.add(changeSet);
							}
						}
					} catch (Exception e) {
						MylarPlugin.fail(e, "could not add resource to change set", false);
					}
				}
			}
		}
	}
	
	public void interestChanged(List<IMylarElement> elements) {
		for (IMylarElement element : elements) {
			interestChanged(element);
		}
	}

	public void nodeDeleted(IMylarElement node) {
		// ignore
		System.err.println(">>> deleted: "+ node);
	}

	public void landmarkAdded(IMylarElement node) {
		// ignore
{'fraction_non_alphanumeric': 0.142775295061304, 'fraction_numerical': 0.004583476566976052, 'mean_word_length': 4.482412060301508, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5530733', 'n_tokens_mistral': 2949, 'n_tokens_neox': 2551, 'n_words': 565}