Eclipse Papercut #6 – Modifying Mylyn Context
Monday, October 5th, 2009In this episode of Eclipse Papercuts I will demonstrate how to modify the Mylyn tasks context. Many thanks to David Green for providing sample code to access the Mylyn context.
Mylyn makes certain assumptions how the developer works. If you start a new task the Mylyn context is empty and fills up based on the selected files.
Which is not always the way I work. Frequently I know that for my task a whole package (or project) is relevant. I would like to include all classes in this package / project in my Mylyn task.
Ok, lets see how we can solve this papercut. I describe how to add Java classes from a package to an active task.
Create an command which extends the package explorer with a new popup commands as described here Extend the package explorer.
Create the command handler with the following coding:
package de.vogella.mylyn.tasksmodify.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.context.core.ContextCore;
import org.eclipse.mylyn.context.core.IInteractionContext;
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin;
import org.eclipse.mylyn.monitor.core.InteractionEvent;
import org.eclipse.mylyn.monitor.core.InteractionEvent.Kind;
import org.eclipse.ui.handlers.HandlerUtil;
public class SampleHandler extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
IStructuredSelection selection = (IStructuredSelection) HandlerUtil
.getActiveMenuSelection(event);
if (selection ==null){
return null;
}
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IPackageFragment) {
IPackageFragment mypackage = (IPackageFragment) firstElement;
try {
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE){
ICompilationUnit[] compilationUnits = mypackage.getCompilationUnits();
for (ICompilationUnit iCompilationUnit : compilationUnits) {
IInteractionContext activeContext = ContextCore.getContextManager()
.getActiveContext();
ContextCorePlugin.getContextManager().processInteractionEvent(iCompilationUnit,
Kind.PROPAGATION, InteractionEvent.ID_UNKNOWN, activeContext);
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
}
return null;
}
}
Launch your plugin. Create a Mylyn task and select a package in the package explorer. Select your commands and all files of this package will be added to the task.
Further reading: If you want to access other Java Elements, e.g. Project and add them to the Mylyn task this tutorial might help you: Eclipse JDT.
Please note that ContextCorePlugin is supposed to be internal API but I did not find another way of accessing the Mylyn tasks.








