Eclipse Papercut #6 – Modifying Mylyn Context

In 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.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse, Papercut and tagged , , , , . Bookmark the permalink.

4 Responses to Eclipse Papercut #6 – Modifying Mylyn Context

  1. Cool! Seems the Mylyn virus spread a lot recently. =) It’s really nice to see how to interact with Mylyn programatically. Nevertheless I doubt if it is really the way to go. Actually when you add a single class to the context my navigating to it, all its parent nodes get added to the context, too. So you implicitly add the package to the context. Revealing package contents is just a matter of ALT-clicking it and you can drill down into the classes siblings. But maybe this is just a matter of favoured working habits.

    Regards,
    Ollie

  2. Lars Vogel says:

    @Oliver: During coding I sometimes realize that the task on hand is complicated and that I will not be able to solve it within the next minutes. In this case I create a new task but I want to pre-populate the context with the the files I already looked at. Via the API this is possible, e.g. to reading all open editors and putting them into the context. Doing this manually is annoying.

    In addtion I use Mylyn also to work on none coding tasks, e.g. my articles for http://www.vogella.de. Having the option to add all files of a subfolder is very useful in this context.

    I think Mylyn is wonderful but no tool fits always to all tasks. Having an API to influence it behavior is important to extends is usability.

  3. David Green says:

    Lars, nice article! Empty Mylyn contexts are somewhat of an achilles heel. Being able to populate a context with things that you know are interesting is definitely useful. As you know, we do this at MAKE Technologies with our MDD tooling. Don’t forget that Mylyn also provides a way to copy a context from one task to another. Right-click the task in the task list (or the header section in the task editor) and select Context -> Copy To…, then from the dialog select the target task.

  4. Wim Jongman says:

    @Lars you can copy contexts and clone tasks. However, knowing how to fool around with the context is fun and enough reason for the papercut. Thanks.

Comments are closed.