View | Details | Raw Unified | Return to bug 296441 | Differences between
and this patch

Collapse All | Expand All

(-)META-INF/MANIFEST.MF (-1 / +8 lines)
Lines 6-16 Link Here
6
Bundle-Vendor: Eclipse Mylyn
6
Bundle-Vendor: Eclipse Mylyn
7
Require-Bundle: org.eclipse.core.runtime,
7
Require-Bundle: org.eclipse.core.runtime,
8
 org.eclipse.jface,
8
 org.eclipse.jface,
9
 org.eclipse.jface.text,
9
 org.eclipse.ui.workbench,
10
 org.eclipse.ui.workbench,
11
 org.eclipse.jdt.ui,
12
 org.apache.commons.lang;bundle-version="2.3.0",
10
 org.junit,
13
 org.junit,
11
 org.eclipse.mylyn.context.core,
14
 org.eclipse.mylyn.context.core,
12
 org.eclipse.mylyn.context.tests,
15
 org.eclipse.mylyn.context.tests,
13
 org.eclipse.mylyn.team.ui,
16
 org.eclipse.mylyn.team.ui,
14
 org.eclipse.mylyn.resources.ui
17
 org.eclipse.mylyn.resources.ui,
18
 org.eclipse.mylyn.ide.ui,
19
 org.eclipse.mylyn.tasks.ui;bundle-version="3.4.0",
20
 org.eclipse.mylyn.tasks.core;bundle-version="3.4.0",
21
 org.eclipse.mylyn.tasks.tests;bundle-version="3.4.0"
15
Export-Package: org.eclipse.mylyn.ide.tests;x-internal:=true
22
Export-Package: org.eclipse.mylyn.ide.tests;x-internal:=true
16
Bundle-RequiredExecutionEnvironment: J2SE-1.5
23
Bundle-RequiredExecutionEnvironment: J2SE-1.5
(-)src/org/eclipse/mylyn/ide/tests/TaskTemplateResolverTest.java (+143 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 Andreas Höhmann.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Andreas Höhmann - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.mylyn.ide.tests;
12
13
import java.util.Iterator;
14
15
import junit.framework.TestCase;
16
17
import org.apache.commons.lang.builder.ToStringBuilder;
18
import org.apache.commons.lang.exception.ExceptionUtils;
19
import org.eclipse.jdt.internal.corext.template.java.JavaContextType;
20
import org.eclipse.jdt.internal.corext.template.java.JavaDocContextType;
21
import org.eclipse.jdt.internal.ui.JavaPlugin;
22
import org.eclipse.jface.text.BadLocationException;
23
import org.eclipse.jface.text.Document;
24
import org.eclipse.jface.text.Position;
25
import org.eclipse.jface.text.templates.ContextTypeRegistry;
26
import org.eclipse.jface.text.templates.DocumentTemplateContext;
27
import org.eclipse.jface.text.templates.Template;
28
import org.eclipse.jface.text.templates.TemplateBuffer;
29
import org.eclipse.jface.text.templates.TemplateContextType;
30
import org.eclipse.jface.text.templates.TemplateException;
31
import org.eclipse.jface.text.templates.TemplateTranslator;
32
import org.eclipse.jface.text.templates.TemplateVariable;
33
import org.eclipse.jface.text.templates.TemplateVariableResolver;
34
import org.eclipse.mylyn.internal.tasks.core.LocalTask;
35
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
36
import org.eclipse.mylyn.tasks.core.ITask;
37
import org.eclipse.mylyn.tasks.tests.connector.MockTask;
38
39
/**
40
 * @author andreas hoehmann
41
 * @since 3.4
42
 */
43
public class TaskTemplateResolverTest extends TestCase {
44
45
	/**
46
	 * Test with no active task. The resolver should not be able to resolve the mylyn template-variable.
47
	 */
48
	public void testNoTaskActive() {
49
		canHandleTemplateResolver("${activeTaskKey}", "activeTaskKey");
50
		canHandleTemplateResolver("${activeTaskPrefix}", "activeTaskPrefix");
51
	}
52
53
	/**
54
	 * Test with active task. The resolver must resolve the mylyn template-variable to the expected values.
55
	 */
56
	public void testActiveLocalTask() {
57
		ITask task = new LocalTask("12345", "Test Task");
58
		TasksUiPlugin.getTaskActivityManager().activateTask(task);
59
		canHandleTemplateResolver("${activeTaskKey}", "12345");
60
		canHandleTemplateResolver("${activeTaskPrefix}", "task");
61
		canHandleTemplateResolver("${activeTaskPrefix} ${activeTaskKey}", "task 12345");
62
		canHandleTemplateResolver("${activeTaskPrefix} #${activeTaskKey}", "task #12345");
63
	}
64
65
	/**
66
	 * Test with active task. Special check if task-key contains a "-".
67
	 */
68
	public void testActiveTaskJira() {
69
		ITask task = new MockTask("http://foo.bar", "12345");
70
		task.setTaskKey("DEMO-2");
71
		TasksUiPlugin.getTaskActivityManager().activateTask(task);
72
		canHandleTemplateResolver("${activeTaskKey}", "DEMO-2");
73
		canHandleTemplateResolver("${activeTaskPrefix} ${activeTaskKey}", "task DEMO-2");
74
	}
75
76
	/**
77
	 * Test with active task. Check if fallback from task-key to task-id is working.
78
	 */
79
	public void testActiveTaskIdOrKey() {
80
		ITask task = new MockTask("http://foo.bar", "12345");
81
		TasksUiPlugin.getTaskActivityManager().activateTask(task);
82
		canHandleTemplateResolver("${activeTaskKey}", "12345");
83
		canHandleTemplateResolver("${activeTaskPrefix}", "task");
84
		// from now the task have a key ... the resolve will prefer this key
85
		task.setTaskKey("foobar");
86
		canHandleTemplateResolver("${activeTaskKey}", "foobar");
87
	}
88
89
	private void canHandleTemplateResolver(String templateContent, String expectedResolvedTemplate) {
90
		// check java template context
91
		canHandleTemplateResolver(JavaContextType.ID_ALL, templateContent, expectedResolvedTemplate);
92
		// check javadoc template context
93
		canHandleTemplateResolver(JavaDocContextType.ID, templateContent, expectedResolvedTemplate);
94
	}
95
96
	private void canHandleTemplateResolver(final String contextType, final String templateContent,
97
			final String expectedResolvedTemplate) {
98
		final ContextTypeRegistry registry = JavaPlugin.getDefault().getTemplateContextRegistry();
99
		final TemplateContextType context = registry.getContextType(contextType);
100
		final Template template = new Template("name", "description", contextType, templateContent, false);
101
		final TemplateTranslator translator = new TemplateTranslator();
102
		TemplateBuffer buffer = null;
103
		try {
104
			buffer = translator.translate(template);
105
		} catch (TemplateException e) {
106
			fail(String.format("Can't translate template '%s' : %s", ToStringBuilder.reflectionToString(template),
107
					ExceptionUtils.getMessage(e)));
108
		}
109
		final TemplateVariable[] variables = buffer.getVariables();
110
		for (final TemplateVariable variable : variables) {
111
			assertTrue(String.format("No resolver found for variable '%s' in template '%s'",
112
					ToStringBuilder.reflectionToString(variable), ToStringBuilder.reflectionToString(template)),
113
					canHandleVariable(context, variable));
114
		}
115
		assertEquals(expectedResolvedTemplate, getResolveTemplate(context, template));
116
	}
117
118
	private boolean canHandleVariable(final TemplateContextType context, final TemplateVariable variable) {
119
		for (final Iterator iterator = context.resolvers(); iterator.hasNext();) {
120
			final TemplateVariableResolver resolver = (TemplateVariableResolver) iterator.next();
121
			if (variable.getType().equals(resolver.getType())) {
122
				return true;
123
			}
124
		}
125
		return false;
126
	}
127
128
	private String getResolveTemplate(final TemplateContextType context, final Template template) {
129
		final DocumentTemplateContext templateContext = new DocumentTemplateContext(context, new Document(),
130
				new Position(0));
131
		TemplateBuffer templateBuffer = null;
132
		try {
133
			templateBuffer = templateContext.evaluate(template);
134
		} catch (BadLocationException e) {
135
			fail(String.format("Can't evaluate template '%s' : %s", ToStringBuilder.reflectionToString(template),
136
					ExceptionUtils.getMessage(e)));
137
		} catch (TemplateException e) {
138
			fail(String.format("Can't evaluate template '%s' : %s", ToStringBuilder.reflectionToString(template),
139
					ExceptionUtils.getMessage(e)));
140
		}
141
		return templateBuffer.getString();
142
	}
143
}
(-)META-INF/MANIFEST.MF (-1 / +3 lines)
Lines 15-21 Link Here
15
 org.eclipse.mylyn.context.ui;bundle-version="[3.0.0,4.0.0)",
15
 org.eclipse.mylyn.context.ui;bundle-version="[3.0.0,4.0.0)",
16
 org.eclipse.mylyn.commons.core;bundle-version="[3.0.0,4.0.0)",
16
 org.eclipse.mylyn.commons.core;bundle-version="[3.0.0,4.0.0)",
17
 org.eclipse.mylyn.resources.ui;bundle-version="[3.0.0,4.0.0)",
17
 org.eclipse.mylyn.resources.ui;bundle-version="[3.0.0,4.0.0)",
18
 org.eclipse.mylyn.commons.ui;bundle-version="[3.0.0,4.0.0)"
18
 org.eclipse.mylyn.commons.ui;bundle-version="[3.0.0,4.0.0)",
19
 org.eclipse.mylyn.tasks.core;bundle-version="[3.0.0,4.0.0)",
20
 org.eclipse.mylyn.tasks.ui;bundle-version="[3.0.0,4.0.0)"
19
Bundle-ActivationPolicy: lazy
21
Bundle-ActivationPolicy: lazy
20
Export-Package: org.eclipse.mylyn.ide.ui,
22
Export-Package: org.eclipse.mylyn.ide.ui,
21
 org.eclipse.mylyn.internal.ide.ui;x-internal:=true,
23
 org.eclipse.mylyn.internal.ide.ui;x-internal:=true,
(-)plugin.xml (+32 lines)
Lines 222-226 Link Here
222
          </newWizardShortcut>
222
          </newWizardShortcut>
223
       </perspectiveExtension>
223
       </perspectiveExtension>
224
    </extension>
224
    </extension>
225
    <extension
226
          id="org.eclipse.mylyn.internal.ide.ui.editors.templates"
227
          point="org.eclipse.ui.editors.templates">
228
       <resolver
229
             class="org.eclipse.mylyn.internal.ide.ui.TasksTemplateVariableResolver"
230
             contextTypeId="java"
231
             description="%MylynTemplateVariableResolver.activeTaskId.description"
232
             name="Active Task ID"
233
             type="activeTaskKey">
234
       </resolver>
235
       <resolver
236
             class="org.eclipse.mylyn.internal.ide.ui.TasksTemplateVariableResolver"
237
             contextTypeId="javadoc"
238
             description="%MylynTemplateVariableResolver.activeTaskId.description"
239
             name="Active Task ID"
240
             type="activeTaskKey">
241
       </resolver>
242
       <resolver
243
             class="org.eclipse.mylyn.internal.ide.ui.TasksTemplateVariableResolver"
244
             contextTypeId="java"
245
             description="%MylynTemplateVariableResolver.activeTaskPrefix.description"
246
             name="Active Task Prefix"
247
             type="activeTaskPrefix">
248
       </resolver>
249
       <resolver
250
             class="org.eclipse.mylyn.internal.ide.ui.TasksTemplateVariableResolver"
251
             contextTypeId="javadoc"
252
             description="%MylynTemplateVariableResolver.activeTaskPrefix.description"
253
             name="Active Task Prefix"
254
             type="activeTaskPrefix">
255
       </resolver>
256
    </extension>
225
257
226
</plugin>
258
</plugin>
(-)plugin.properties (+3 lines)
Lines 25-27 Link Here
25
FocusTaskMarkersViewAction.tooltip = Focus on Active Task
25
FocusTaskMarkersViewAction.tooltip = Focus on Active Task
26
FocusBookmarkMarkersViewAction.label = Focus on Active Task
26
FocusBookmarkMarkersViewAction.label = Focus on Active Task
27
FocusBookmarkMarkersViewAction.tooltip = Focus on Active Task
27
FocusBookmarkMarkersViewAction.tooltip = Focus on Active Task
28
29
MylynTemplateVariableResolver.activeTaskId.description = Return the ID of the active task
30
MylynTemplateVariableResolver.activeTaskPrefix.description = Return the Prefix of the active task
(-)src/org/eclipse/mylyn/internal/ide/ui/TasksTemplateVariableResolver.java (+114 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 Andreas Höhmann.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Andreas Höhmann - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.mylyn.internal.ide.ui;
12
13
import org.eclipse.jface.text.templates.TemplateContext;
14
import org.eclipse.jface.text.templates.TemplateVariableResolver;
15
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
16
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
17
import org.eclipse.mylyn.tasks.core.ITask;
18
19
/**
20
 * Resolver to resolve variables from the <b>mylyn</b> context.
21
 * <p>
22
 * This resolver can handle the following mylyn variable:
23
 * <ul>
24
 * <li>activeTaskPrefix - the prefix for the active task (if any) or null</li>
25
 * <li>activeTaskKey - the key (or id) for the active task (if any) or null</li>
26
 * </ul>
27
 * </p>
28
 * <h4>You want more variable? That's easy :D.</h4>
29
 * <p>
30
 * First add another template-resolver (for detail information see <a href="http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/extension-points/org_eclipse_ui_editors_templates.html"
31
 * >here</a>) for the <code>org.eclipse.mylyn.internal.ide.ui.editors.templates</code>.
32
 * 
33
 * <pre>
34
 * &lt;extension point="org.eclipse.ui.editors.templates"
35
 *            id="org.eclipse.mylyn.internal.ide.ui.editors.templates"&gt;
36
 *   &lt;resolver
37
 *      class="org.eclipse.mylyn.internal.ide.ui.TasksTemplateVariableResolver"
38
 *      contextTypeId="<b>java</b>"
39
 *      description="%MylynTemplateVariableResolver.<b>activeTaskId.description</b>"
40
 *      name="<b>Active Task ID</b>"
41
 *      type="<b>activeTaskKey</b>"&gt;
42
 *   &lt;/resolver&gt;
43
 * &lt;/extension&gt;
44
 * </pre>
45
 * 
46
 * You have to define/change the:
47
 * <ul>
48
 * <li><b>contextTypeId</b> - you should define an resolver for the "java" and the "javadoc" context and maybe more
49
 * contexts.</li>
50
 * <li><b>type</b> - this is the property which can be used by users in there templates</li>
51
 * <li><b>description</b> - this is the localized description for the resolver, have a look into
52
 * <code>plugin.properties</code></li>
53
 * <li><b>name</b> - the display name of the resolver (for internal usage only)</li>
54
 * </ul>
55
 * </p>
56
 * <p>
57
 * The you must put your resolver code here.
58
 * <ol>
59
 * <li>add a new constant for the resolver-<b>type</b> ("activeTaskKey")</li>
60
 * <li>check your type in {@link #resolve(TemplateContext)} and resolve the type, return <code>null</code> if nothing
61
 * can resolved, always <b>trim</b> your result</li>
62
 * </ol>
63
 * </p>
64
 * <p>
65
 * <i>Each returned variable should be trimmed to avoid avoid unnecessary spaces between the resolved variables, i.e.
66
 * ("${activeTaskPrefix}${activeTaskKey}" should become "task2" and not "task 2").</i>
67
 * </p>
68
 * 
69
 * @author Andreas Höhmann (bug #296441)
70
 * @since 3.3.2
71
 */
72
public class TasksTemplateVariableResolver extends TemplateVariableResolver {
73
74
	/**
75
	 * Would be used as resolver <code>type</code> to provide the ID of the active task.
76
	 */
77
	private static final String TYPE_ACTIVE_TASK_ID = "activeTaskKey"; //$NON-NLS-1$
78
79
	/**
80
	 * Would be used as resolver <code>type</code> to provide the prefix (i.e. "bug") of the active task.
81
	 */
82
	private static final String TYPE_ACTIVE_TASK_PREFIX = "activeTaskPrefix"; //$NON-NLS-1$
83
84
	/**
85
	 * {@inheritDoc}
86
	 */
87
	@Override
88
	protected String resolve(final TemplateContext context) {
89
		final String type = getType();
90
		if (TYPE_ACTIVE_TASK_ID.equalsIgnoreCase(type)) {
91
			final ITask activeTask = TasksUiPlugin.getTaskActivityManager().getActiveTask();
92
			if (activeTask != null) {
93
				String taskKey = activeTask.getTaskKey();
94
				if (taskKey == null) {
95
					// use the task-id, i.e. for a local task, such a task doesn't have a task-key
96
					taskKey = activeTask.getTaskId();
97
				}
98
				if (taskKey != null) {
99
					return taskKey.trim();
100
				}
101
			}
102
		} else if (TYPE_ACTIVE_TASK_PREFIX.equalsIgnoreCase(type)) {
103
			final ITask activeTask = TasksUiPlugin.getTaskActivityManager().getActiveTask();
104
			if (activeTask != null) {
105
				String taskPrefix = TasksUiInternal.getTaskPrefix(activeTask.getConnectorKind());
106
				if (taskPrefix != null) {
107
					return taskPrefix.trim();
108
				}
109
			}
110
		}
111
		// ... here later maybe more supported types ...
112
		return null;
113
	}
114
}

Return to bug 296441