Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » js editor templates - custom variable doesn't resolve(how to add dependency between two template variables?)
js editor templates - custom variable doesn't resolve [message #870969] Thu, 10 May 2012 11:58 Go to next message
macbeth Missing name is currently offline macbeth Missing nameFriend
Messages: 2
Registered: May 2012
Junior Member
Hi,

I am trying to create a custom variable which could be used in javascript editor templates. I have a class extending TemplateVariableResolver and overriding
public void resolve(TemplateVariable variable, TemplateContext context) method.

My template looks like this:
foo${variableOne:abcd}bar${variableTwo:capitalize(variableOne)}

the custom variable "capitalize" should uppercase the first letter of anything that is contained in variableOne.

the output is:
fooabcdbarAbcd

this is fine, but if I edit the text abcd, the variableTwo isn't re-resolved

I tried to add dependency between the two variables in the TemplateContext but it didn't work. How can this be done?

thanks

My code (experimental, with reflection and discouraged access):
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateVariable;
import org.eclipse.jface.text.templates.TemplateVariableResolver;
import org.eclipse.wst.jsdt.internal.corext.template.java.JavaContext;
import org.eclipse.wst.jsdt.internal.ui.text.template.contentassist.MultiVariable;

public class CapitalizingVariableResolver extends TemplateVariableResolver {
	
	
	@Override
	public void resolve(TemplateVariable variable, TemplateContext context) {
		@SuppressWarnings("unchecked")
		final List<String> params = variable.getVariableType().getParams();

		if (params.isEmpty())
			return;

		try {
			MultiVariable v = dirtyReflectionHack(context, params.get(0));
			final String currentValue = (String) v.getCurrentChoice();
			if (currentValue == null || currentValue.length() == 0)
				return;
			
			JavaContext jc = (JavaContext) context;
			jc.addDependency(v, (MultiVariable) variable);
			variable.setValue(currentValue.substring(0, 1).toUpperCase() + currentValue.substring(1));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("rawtypes")
	private MultiVariable dirtyReflectionHack(TemplateContext context, String paramVar) throws Exception {
			Field f = context.getClass().getDeclaredField("fVariables");
			f.setAccessible(true);
			return (MultiVariable)((Map) f.get(context)).get(paramVar);
	}
}
Re: js editor templates - custom variable doesn't resolve [message #874355 is a reply to message #870969] Sun, 20 May 2012 16:40 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 2332
Registered: July 2009
Senior Member
Are you certain that it can be done?

---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: js editor templates - custom variable doesn't resolve [message #876235 is a reply to message #874355] Thu, 24 May 2012 07:52 Go to previous message
macbeth Missing name is currently offline macbeth Missing nameFriend
Messages: 2
Registered: May 2012
Junior Member
No, not at all Smile
Previous Topic:lowercase HTML preference ignored
Next Topic:Open Web Page Designer(org.eclipse.jst.pagedesigner.PageDesignerEditor) in RCP app
Goto Forum:
  


Current Time: Sun Nov 23 01:21:18 GMT 2014

Powered by FUDForum. Page generated in 0.01844 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software