int start= fCursorSelectionEvaluator.getStart(); if (start == -1) { fSelectionStart= pattern.length(); fSelectionEnd= fSelectionStart; } else { fSelectionStart= start; fSelectionEnd= fCursorSelectionEvaluator.getEnd();
fInterpolator.interpolate(pattern, fModelEvaluator); TemplateModel model= fModelEvaluator.getModel();  if (model.getEditableCount() == 0) { pattern= fInterpolator.interpolate(pattern, fCursorSelectionEvaluator);   int cursorStart= fCursorSelectionEvaluator.getStart(); if (cursorStart == -1) { fSelectionStart= pattern.length(); fSelectionEnd= fSelectionStart; } else { fSelectionStart= cursorStart; fSelectionEnd= fCursorSelectionEvaluator.getEnd(); } } else {  fContext.setDocument(document); TemplateEditorPopup popup= new TemplateEditorPopup(fContext, model);  if (!popup.open()) {  fSelectionStart= fContext.getEnd() - fContext.getStart(); fSelectionEnd= fSelectionStart; return; }  pattern= popup.getText();  int[] selection= popup.getSelection(); if (selection[0] == -1) { fSelectionStart= pattern.length(); fSelectionEnd= fSelectionStart; } else { fSelectionStart= selection[0]; fSelectionEnd= selection[1]; }
	public void apply(IDocument document) {		int indentationLevel= guessIndentationLevel(document, fStart);				String pattern= fTemplate.getPattern();		// interpolate		pattern= fInterpolator.interpolate(pattern, fArgumentEvaluator);				pattern= fInterpolator.interpolate(pattern, fLocalVariableEvaluator);		fCursorSelectionEvaluator.reset();		pattern= fInterpolator.interpolate(pattern, fCursorSelectionEvaluator);		// side effect: stores selection		int start= fCursorSelectionEvaluator.getStart();		if (start == -1) {			fSelectionStart= pattern.length();			fSelectionEnd= fSelectionStart;		} else {					fSelectionStart= start;			fSelectionEnd= fCursorSelectionEvaluator.getEnd();					}		boolean format= TemplatePreferencePage.useCodeFormatter();		if (format) {			CodeFormatter formatter= new CodeFormatter(JavaCore.getOptions());			formatter.setPositionsToMap(new int[] {fSelectionStart, fSelectionEnd});			formatter.setInitialIndentationLevel(indentationLevel);			pattern= formatter.formatSourceString(pattern);						int[] positions= formatter.getMappedPositions();			fSelectionStart= positions[0];			fSelectionEnd= positions[1];		} else {			CodeIndentator indentator= new CodeIndentator();			indentator.setPositionsToMap(new int[] {fSelectionStart, fSelectionEnd});			indentator.setIndentationLevel(indentationLevel);						pattern= indentator.indentate(pattern);			int[] positions= indentator.getMappedPositions();			fSelectionStart= positions[0];			fSelectionEnd= positions[1];			}		// strip indentation on first line		String finalString= trimBegin(pattern);		int charactersRemoved= pattern.length() - finalString.length();		fSelectionStart -= charactersRemoved;		fSelectionEnd -= charactersRemoved;				try {			document.replace(fStart, fEnd - fStart, finalString);		} catch (BadLocationException x) {} // ignore	}