/*
* @see ITemplateEditor#edit(TemplateBuffer)
*/
public void edit(TemplateBuffer templateBuffer, TemplateContext context) throws CoreException {
TextBuffer textBuffer = TextBuffer.create(templateBuffer.getString());
TemplatePosition[] variables = templateBuffer.getVariables();
MultiTextEdit positions = variablesToPositions(variables);
MultiTextEdit multiEdit = new MultiTextEdit();
// iterate over all variables and try to resolve them
for (int i = 0; i != variables.length; i++) {
TemplatePosition variable = variables[i];
if (variable.isResolved())
continue;
String name = variable.getName();
int[] offsets = variable.getOffsets();
int length = variable.getLength();
TemplateVariable evaluator = (TemplateVariable) fVariables.get(name);
String value = (evaluator == null) ? null : evaluator.evaluate(context);
if (value == null)
continue;
variable.setLength(value.length());
variable.setResolved(evaluator.isResolved(context));
for (int k = 0; k != offsets.length; k++)
multiEdit.add(SimpleTextEdit.createReplace(offsets[k], length, value));
}
TextBufferEditor editor = new TextBufferEditor(textBuffer);
editor.add(positions);
editor.add(multiEdit);
editor.performEdits(null);
positionsToVariables(positions, variables);
templateBuffer.setContent(textBuffer.getContent(), variables);
}