### Eclipse Workspace Patch 1.0 #P org.eclipse.mylyn.ide.tests Index: META-INF/MANIFEST.MF =================================================================== RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.ide.tests/META-INF/MANIFEST.MF,v retrieving revision 1.25 diff -u -r1.25 MANIFEST.MF --- META-INF/MANIFEST.MF 3 Feb 2010 17:17:04 -0000 1.25 +++ META-INF/MANIFEST.MF 14 Feb 2010 17:51:42 -0000 @@ -6,11 +6,18 @@ Bundle-Vendor: Eclipse Mylyn Require-Bundle: org.eclipse.core.runtime, org.eclipse.jface, + org.eclipse.jface.text, org.eclipse.ui.workbench, + org.eclipse.jdt.ui, + org.apache.commons.lang;bundle-version="2.3.0", org.junit, org.eclipse.mylyn.context.core, org.eclipse.mylyn.context.tests, org.eclipse.mylyn.team.ui, - org.eclipse.mylyn.resources.ui + org.eclipse.mylyn.resources.ui, + org.eclipse.mylyn.ide.ui, + org.eclipse.mylyn.tasks.ui;bundle-version="3.4.0", + org.eclipse.mylyn.tasks.core;bundle-version="3.4.0", + org.eclipse.mylyn.tasks.tests;bundle-version="3.4.0" Export-Package: org.eclipse.mylyn.ide.tests;x-internal:=true Bundle-RequiredExecutionEnvironment: J2SE-1.5 Index: src/org/eclipse/mylyn/ide/tests/TaskTemplateResolverTest.java =================================================================== RCS file: src/org/eclipse/mylyn/ide/tests/TaskTemplateResolverTest.java diff -N src/org/eclipse/mylyn/ide/tests/TaskTemplateResolverTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/mylyn/ide/tests/TaskTemplateResolverTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,143 @@ +/******************************************************************************* + * Copyright (c) 2010 Andreas Höhmann. + * 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: + * Andreas Höhmann - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.ide.tests; + +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.exception.ExceptionUtils; +import org.eclipse.jdt.internal.corext.template.java.JavaContextType; +import org.eclipse.jdt.internal.corext.template.java.JavaDocContextType; +import org.eclipse.jdt.internal.ui.JavaPlugin; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.templates.ContextTypeRegistry; +import org.eclipse.jface.text.templates.DocumentTemplateContext; +import org.eclipse.jface.text.templates.Template; +import org.eclipse.jface.text.templates.TemplateBuffer; +import org.eclipse.jface.text.templates.TemplateContextType; +import org.eclipse.jface.text.templates.TemplateException; +import org.eclipse.jface.text.templates.TemplateTranslator; +import org.eclipse.jface.text.templates.TemplateVariable; +import org.eclipse.jface.text.templates.TemplateVariableResolver; +import org.eclipse.mylyn.internal.tasks.core.LocalTask; +import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; +import org.eclipse.mylyn.tasks.core.ITask; +import org.eclipse.mylyn.tasks.tests.connector.MockTask; + +/** + * @author andreas hoehmann + * @since 3.4 + */ +public class TaskTemplateResolverTest extends TestCase { + + /** + * Test with no active task. The resolver should not be able to resolve the mylyn template-variable. + */ + public void testNoTaskActive() { + canHandleTemplateResolver("${activeTaskKey}", "activeTaskKey"); + canHandleTemplateResolver("${activeTaskPrefix}", "activeTaskPrefix"); + } + + /** + * Test with active task. The resolver must resolve the mylyn template-variable to the expected values. + */ + public void testActiveLocalTask() { + ITask task = new LocalTask("12345", "Test Task"); + TasksUiPlugin.getTaskActivityManager().activateTask(task); + canHandleTemplateResolver("${activeTaskKey}", "12345"); + canHandleTemplateResolver("${activeTaskPrefix}", "task"); + canHandleTemplateResolver("${activeTaskPrefix} ${activeTaskKey}", "task 12345"); + canHandleTemplateResolver("${activeTaskPrefix} #${activeTaskKey}", "task #12345"); + } + + /** + * Test with active task. Special check if task-key contains a "-". + */ + public void testActiveTaskJira() { + ITask task = new MockTask("http://foo.bar", "12345"); + task.setTaskKey("DEMO-2"); + TasksUiPlugin.getTaskActivityManager().activateTask(task); + canHandleTemplateResolver("${activeTaskKey}", "DEMO-2"); + canHandleTemplateResolver("${activeTaskPrefix} ${activeTaskKey}", "task DEMO-2"); + } + + /** + * Test with active task. Check if fallback from task-key to task-id is working. + */ + public void testActiveTaskIdOrKey() { + ITask task = new MockTask("http://foo.bar", "12345"); + TasksUiPlugin.getTaskActivityManager().activateTask(task); + canHandleTemplateResolver("${activeTaskKey}", "12345"); + canHandleTemplateResolver("${activeTaskPrefix}", "task"); + // from now the task have a key ... the resolve will prefer this key + task.setTaskKey("foobar"); + canHandleTemplateResolver("${activeTaskKey}", "foobar"); + } + + private void canHandleTemplateResolver(String templateContent, String expectedResolvedTemplate) { + // check java template context + canHandleTemplateResolver(JavaContextType.ID_ALL, templateContent, expectedResolvedTemplate); + // check javadoc template context + canHandleTemplateResolver(JavaDocContextType.ID, templateContent, expectedResolvedTemplate); + } + + private void canHandleTemplateResolver(final String contextType, final String templateContent, + final String expectedResolvedTemplate) { + final ContextTypeRegistry registry = JavaPlugin.getDefault().getTemplateContextRegistry(); + final TemplateContextType context = registry.getContextType(contextType); + final Template template = new Template("name", "description", contextType, templateContent, false); + final TemplateTranslator translator = new TemplateTranslator(); + TemplateBuffer buffer = null; + try { + buffer = translator.translate(template); + } catch (TemplateException e) { + fail(String.format("Can't translate template '%s' : %s", ToStringBuilder.reflectionToString(template), + ExceptionUtils.getMessage(e))); + } + final TemplateVariable[] variables = buffer.getVariables(); + for (final TemplateVariable variable : variables) { + assertTrue(String.format("No resolver found for variable '%s' in template '%s'", + ToStringBuilder.reflectionToString(variable), ToStringBuilder.reflectionToString(template)), + canHandleVariable(context, variable)); + } + assertEquals(expectedResolvedTemplate, getResolveTemplate(context, template)); + } + + private boolean canHandleVariable(final TemplateContextType context, final TemplateVariable variable) { + for (final Iterator iterator = context.resolvers(); iterator.hasNext();) { + final TemplateVariableResolver resolver = (TemplateVariableResolver) iterator.next(); + if (variable.getType().equals(resolver.getType())) { + return true; + } + } + return false; + } + + private String getResolveTemplate(final TemplateContextType context, final Template template) { + final DocumentTemplateContext templateContext = new DocumentTemplateContext(context, new Document(), + new Position(0)); + TemplateBuffer templateBuffer = null; + try { + templateBuffer = templateContext.evaluate(template); + } catch (BadLocationException e) { + fail(String.format("Can't evaluate template '%s' : %s", ToStringBuilder.reflectionToString(template), + ExceptionUtils.getMessage(e))); + } catch (TemplateException e) { + fail(String.format("Can't evaluate template '%s' : %s", ToStringBuilder.reflectionToString(template), + ExceptionUtils.getMessage(e))); + } + return templateBuffer.getString(); + } +} \ No newline at end of file #P org.eclipse.mylyn.ide.ui Index: META-INF/MANIFEST.MF =================================================================== RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.ide.ui/META-INF/MANIFEST.MF,v retrieving revision 1.378 diff -u -r1.378 MANIFEST.MF --- META-INF/MANIFEST.MF 3 Feb 2010 17:14:18 -0000 1.378 +++ META-INF/MANIFEST.MF 14 Feb 2010 17:51:44 -0000 @@ -15,7 +15,9 @@ org.eclipse.mylyn.context.ui;bundle-version="[3.0.0,4.0.0)", org.eclipse.mylyn.commons.core;bundle-version="[3.0.0,4.0.0)", org.eclipse.mylyn.resources.ui;bundle-version="[3.0.0,4.0.0)", - org.eclipse.mylyn.commons.ui;bundle-version="[3.0.0,4.0.0)" + org.eclipse.mylyn.commons.ui;bundle-version="[3.0.0,4.0.0)", + org.eclipse.mylyn.tasks.core;bundle-version="[3.0.0,4.0.0)", + org.eclipse.mylyn.tasks.ui;bundle-version="[3.0.0,4.0.0)" Bundle-ActivationPolicy: lazy Export-Package: org.eclipse.mylyn.ide.ui, org.eclipse.mylyn.internal.ide.ui;x-internal:=true, Index: plugin.xml =================================================================== RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.ide.ui/plugin.xml,v retrieving revision 1.68 diff -u -r1.68 plugin.xml --- plugin.xml 12 Aug 2009 00:19:17 -0000 1.68 +++ plugin.xml 14 Feb 2010 17:51:44 -0000 @@ -222,5 +222,37 @@ + + + + + + + + + + Index: plugin.properties =================================================================== RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.ide.ui/plugin.properties,v retrieving revision 1.3 diff -u -r1.3 plugin.properties --- plugin.properties 24 Jul 2009 12:06:30 -0000 1.3 +++ plugin.properties 14 Feb 2010 17:51:43 -0000 @@ -25,3 +25,6 @@ FocusTaskMarkersViewAction.tooltip = Focus on Active Task FocusBookmarkMarkersViewAction.label = Focus on Active Task FocusBookmarkMarkersViewAction.tooltip = Focus on Active Task + +MylynTemplateVariableResolver.activeTaskId.description = Return the ID of the active task +MylynTemplateVariableResolver.activeTaskPrefix.description = Return the Prefix of the active task Index: src/org/eclipse/mylyn/internal/ide/ui/TasksTemplateVariableResolver.java =================================================================== RCS file: src/org/eclipse/mylyn/internal/ide/ui/TasksTemplateVariableResolver.java diff -N src/org/eclipse/mylyn/internal/ide/ui/TasksTemplateVariableResolver.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/mylyn/internal/ide/ui/TasksTemplateVariableResolver.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,114 @@ +/******************************************************************************* + * Copyright (c) 2010 Andreas Höhmann. + * 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: + * Andreas Höhmann - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.internal.ide.ui; + +import org.eclipse.jface.text.templates.TemplateContext; +import org.eclipse.jface.text.templates.TemplateVariableResolver; +import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; +import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal; +import org.eclipse.mylyn.tasks.core.ITask; + +/** + * Resolver to resolve variables from the mylyn context. + *

+ * This resolver can handle the following mylyn variable: + *

+ *

+ *

You want more variable? That's easy :D.

+ *

+ * First add another template-resolver (for detail information see here) for the org.eclipse.mylyn.internal.ide.ui.editors.templates. + * + *

+ * <extension point="org.eclipse.ui.editors.templates"
+ *            id="org.eclipse.mylyn.internal.ide.ui.editors.templates">
+ *   <resolver
+ *      class="org.eclipse.mylyn.internal.ide.ui.TasksTemplateVariableResolver"
+ *      contextTypeId="java"
+ *      description="%MylynTemplateVariableResolver.activeTaskId.description"
+ *      name="Active Task ID"
+ *      type="activeTaskKey">
+ *   </resolver>
+ * </extension>
+ * 
+ * + * You have to define/change the: + * + *

+ *

+ * The you must put your resolver code here. + *

    + *
  1. add a new constant for the resolver-type ("activeTaskKey")
  2. + *
  3. check your type in {@link #resolve(TemplateContext)} and resolve the type, return null if nothing + * can resolved, always trim your result
  4. + *
+ *

+ *

+ * Each returned variable should be trimmed to avoid avoid unnecessary spaces between the resolved variables, i.e. + * ("${activeTaskPrefix}${activeTaskKey}" should become "task2" and not "task 2"). + *

+ * + * @author Andreas Höhmann (bug #296441) + * @since 3.3.2 + */ +public class TasksTemplateVariableResolver extends TemplateVariableResolver { + + /** + * Would be used as resolver type to provide the ID of the active task. + */ + private static final String TYPE_ACTIVE_TASK_ID = "activeTaskKey"; //$NON-NLS-1$ + + /** + * Would be used as resolver type to provide the prefix (i.e. "bug") of the active task. + */ + private static final String TYPE_ACTIVE_TASK_PREFIX = "activeTaskPrefix"; //$NON-NLS-1$ + + /** + * {@inheritDoc} + */ + @Override + protected String resolve(final TemplateContext context) { + final String type = getType(); + if (TYPE_ACTIVE_TASK_ID.equalsIgnoreCase(type)) { + final ITask activeTask = TasksUiPlugin.getTaskActivityManager().getActiveTask(); + if (activeTask != null) { + String taskKey = activeTask.getTaskKey(); + if (taskKey == null) { + // use the task-id, i.e. for a local task, such a task doesn't have a task-key + taskKey = activeTask.getTaskId(); + } + if (taskKey != null) { + return taskKey.trim(); + } + } + } else if (TYPE_ACTIVE_TASK_PREFIX.equalsIgnoreCase(type)) { + final ITask activeTask = TasksUiPlugin.getTaskActivityManager().getActiveTask(); + if (activeTask != null) { + String taskPrefix = TasksUiInternal.getTaskPrefix(activeTask.getConnectorKind()); + if (taskPrefix != null) { + return taskPrefix.trim(); + } + } + } + // ... here later maybe more supported types ... + return null; + } +}