Java Code Examples for org.eclipse.jface.text.templates.Template#getContextTypeId()

The following examples show how to use org.eclipse.jface.text.templates.Template#getContextTypeId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnimplementedCodeCleanUp.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private String getOverridingMethodComment() {
	String templateName= CodeTemplateContextType.OVERRIDECOMMENT_ID;

	Template template= getCodeTemplate(templateName);
	if (template == null)
		return ""; //$NON-NLS-1$

	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), null, "\n"); //$NON-NLS-1$

	context.setVariable(CodeTemplateContextType.FILENAME, "Face.java"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.PACKAGENAME, "test"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.PROJECTNAME, "TestProject"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, "Face"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, "method"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.RETURN_TYPE, "void"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, "test.IFace#foo()"); //$NON-NLS-1$

	return evaluateTemplate(template, context);
}
 
Example 2
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static String getMethodBodyContent(boolean isConstructor, IJavaProject project, String destTypeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
	String templateName= isConstructor ? CodeTemplateContextType.CONSTRUCTORSTUB_ID : CodeTemplateContextType.METHODSTUB_ID;
	Template template= getCodeTemplate(templateName, project);
	if (template == null) {
		return bodyStatement;
	}
	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
	context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
	context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
	context.setVariable(CodeTemplateContextType.BODY_STATEMENT, bodyStatement);
	String str= evaluateTemplate(context, template, new String[] { CodeTemplateContextType.BODY_STATEMENT });
	if (str == null && !Strings.containsOnlyWhitespaces(bodyStatement)) {
		return bodyStatement;
	}
	return str;
}
 
Example 3
Source File: JavaTemplatesPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void updatePatternViewer(Template template) {
	if (template == null) {
		getPatternViewer().getDocument().set(""); //$NON-NLS-1$
		return ;
	}
	String contextId= template.getContextTypeId();
	TemplateContextType type= getContextTypeRegistry().getContextType(contextId);
	fTemplateProcessor.setContextType(type);

	IDocument doc= getPatternViewer().getDocument();

	String start= null;
	if ("javadoc".equals(contextId)) { //$NON-NLS-1$
		start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
	} else
		start= ""; //$NON-NLS-1$

	doc.set(start + template.getPattern());
	int startLen= start.length();
	getPatternViewer().setDocument(doc, startLen, doc.getLength() - startLen);
}
 
Example 4
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static String getCompilationUnitContent(ICompilationUnit cu, String packDecl, String fileComment, String typeComment, String typeContent, String lineDelimiter) throws CoreException {
	Template template= getCodeTemplate(CodeTemplateContextType.NEWTYPE_ID, cu.getJavaProject());
	if (template == null) {
		return null;
	}

	IJavaProject project= cu.getJavaProject();
	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
	context.setCompilationUnitVariables(cu);
	context.setVariable(CodeTemplateContextType.PACKAGE_DECLARATION, packDecl);
	context.setVariable(CodeTemplateContextType.TYPE_COMMENT, typeComment != null ? typeComment : ""); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.TYPE_DECLARATION, typeContent);
	context.setVariable(CodeTemplateContextType.TYPENAME, JavaCore.removeJavaLikeExtension(cu.getElementName()));

	String[] fullLine= { CodeTemplateContextType.PACKAGE_DECLARATION, CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT };
	return evaluateTemplate(context, template, fullLine);
}
 
Example 5
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Don't use this method directly, use CodeGeneration.
 * 
 * @param templateID the template id of the type body to get. Valid id's are
 *            {@link CodeTemplateContextType#CLASSBODY_ID},
 *            {@link CodeTemplateContextType#INTERFACEBODY_ID},
 *            {@link CodeTemplateContextType#ENUMBODY_ID},
 *            {@link CodeTemplateContextType#ANNOTATIONBODY_ID},
 * @param cu the compilation unit to which the template is added
 * @param typeName the type name
 * @param lineDelim the line delimiter to use
 * @return return the type body template or <code>null</code>
 * @throws CoreException thrown if the template could not be evaluated
 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeBody(String, ICompilationUnit, String, String)
 */
public static String getTypeBody(String templateID, ICompilationUnit cu, String typeName, String lineDelim) throws CoreException {
	if (!VALID_TYPE_BODY_TEMPLATES.contains(templateID)) {
		throw new IllegalArgumentException("Invalid code template ID: " + templateID); //$NON-NLS-1$
	}

	Template template= getCodeTemplate(templateID, cu.getJavaProject());
	if (template == null) {
		return null;
	}
	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
	context.setCompilationUnitVariables(cu);
	context.setVariable(CodeTemplateContextType.TYPENAME, typeName);

	return evaluateTemplate(context, template);
}
 
Example 6
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static String getGetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String bareFieldName, String lineDelimiter) throws CoreException {
	String templateName= CodeTemplateContextType.GETTERCOMMENT_ID;
	Template template= getCodeTemplate(templateName, cu.getJavaProject());
	if (template == null) {
		return null;
	}
	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
	context.setCompilationUnitVariables(cu);
	context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
	context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
	context.setVariable(CodeTemplateContextType.FIELD, fieldName);
	context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldType);
	context.setVariable(CodeTemplateContextType.BARE_FIELD_NAME, bareFieldName);

	return evaluateTemplate(context, template);
}
 
Example 7
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static String getSetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String paramName, String bareFieldName, String lineDelimiter)
		throws CoreException {
	String templateName= CodeTemplateContextType.SETTERCOMMENT_ID;
	Template template= getCodeTemplate(templateName, cu.getJavaProject());
	if (template == null) {
		return null;
	}

	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
	context.setCompilationUnitVariables(cu);
	context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
	context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
	context.setVariable(CodeTemplateContextType.FIELD, fieldName);
	context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldType);
	context.setVariable(CodeTemplateContextType.BARE_FIELD_NAME, bareFieldName);
	context.setVariable(CodeTemplateContextType.PARAM, paramName);

	return evaluateTemplate(context, template);
}
 
Example 8
Source File: TypeScriptTemplatePreferencePage.java    From typescript.java with MIT License 5 votes vote down vote up
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	SourceViewer viewer= getViewer();
	
	if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String contextId= template.getContextTypeId();
		TemplateContextType type= JSDTTypeScriptUIPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
		fTemplateProcessor.setContextType(type);
		
		IDocument doc= viewer.getDocument();
		
		String start= null;
		if ("javadoc".equals(contextId)) { //$NON-NLS-1$
			start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
		} else
			start= ""; //$NON-NLS-1$
		
		doc.set(start + template.getPattern());
		int startLen= start.length();
		viewer.setDocument(doc, startLen, doc.getLength() - startLen);

	} else {
		viewer.getDocument().set(""); //$NON-NLS-1$
	}		
}
 
Example 9
Source File: UnimplementedCodeCleanUp.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getMethodBody() {
	String templateName= CodeTemplateContextType.METHODSTUB_ID;
	Template template= getCodeTemplate(templateName);
	if (template == null)
		return ""; //$NON-NLS-1$

	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), null, "\n"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, "method"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, "Face"); //$NON-NLS-1$
	context.setVariable(CodeTemplateContextType.BODY_STATEMENT, ""); //$NON-NLS-1$
	return evaluateTemplate(template, context);
}
 
Example 10
Source File: JavaTemplatePreferencePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void updateViewerInput() {
	IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
	SourceViewer viewer= getViewer();

	if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
		TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
		Template template= data.getTemplate();
		String contextId= template.getContextTypeId();
		TemplateContextType type= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
		fTemplateProcessor.setContextType(type);

		IDocument doc= viewer.getDocument();

		String start= null;
		if ("javadoc".equals(contextId)) { //$NON-NLS-1$
			start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
		} else
			start= ""; //$NON-NLS-1$

		doc.set(start + template.getPattern());
		int startLen= start.length();
		viewer.setDocument(doc, startLen, doc.getLength() - startLen);

	} else {
		viewer.getDocument().set(""); //$NON-NLS-1$
	}
}
 
Example 11
Source File: JavaTemplatesPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected Image getImage(Template template) {
	String contextId= template.getContextTypeId();
	if (SWTContextType.ID_ALL.equals(contextId) || SWTContextType.ID_STATEMENTS.equals(contextId) || SWTContextType.ID_MEMBERS.equals(contextId))
		return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_SWT_TEMPLATE);
	return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE);
}
 
Example 12
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Only to be used by tests
 * 
 * @param templateId the template id
 * @param pattern the new pattern
 * @param project not used
 */
public static void setCodeTemplate(String templateId, String pattern, IJavaProject project) {
	TemplateStore codeTemplateStore= JavaPlugin.getDefault().getCodeTemplateStore();
	TemplatePersistenceData data= codeTemplateStore.getTemplateData(templateId);
	Template orig= data.getTemplate();
	Template copy= new Template(orig.getName(), orig.getDescription(), orig.getContextTypeId(), pattern, true);
	data.setTemplate(copy);
}
 
Example 13
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String getFieldComment(ICompilationUnit cu, String typeName, String fieldName, String lineDelimiter) throws CoreException {
	Template template= getCodeTemplate(CodeTemplateContextType.FIELDCOMMENT_ID, cu.getJavaProject());
	if (template == null) {
		return null;
	}
	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
	context.setCompilationUnitVariables(cu);
	context.setVariable(CodeTemplateContextType.FIELD_TYPE, typeName);
	context.setVariable(CodeTemplateContextType.FIELD, fieldName);

	return evaluateTemplate(context, template);
}
 
Example 14
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String getFileComment(ICompilationUnit cu, String lineDelimiter) throws CoreException {
	Template template= getCodeTemplate(CodeTemplateContextType.FILECOMMENT_ID, cu.getJavaProject());
	if (template == null) {
		return null;
	}

	IJavaProject project= cu.getJavaProject();
	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
	context.setCompilationUnitVariables(cu);
	context.setVariable(CodeTemplateContextType.TYPENAME, JavaCore.removeJavaLikeExtension(cu.getElementName()));
	return evaluateTemplate(context, template);
}
 
Example 15
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String getCatchBodyContent(ICompilationUnit cu, String exceptionType, String variableName, String enclosingType, String enclosingMethod, String lineDelimiter) throws CoreException {
	Template template= getCodeTemplate(CodeTemplateContextType.CATCHBLOCK_ID, cu.getJavaProject());
	if (template == null) {
		return null;
	}

	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
	context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, enclosingType);
	context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, enclosingMethod);
	context.setVariable(CodeTemplateContextType.EXCEPTION_TYPE, exceptionType);
	context.setVariable(CodeTemplateContextType.EXCEPTION_VAR, variableName);
	return evaluateTemplate(context, template);
}
 
Example 16
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String getSetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String paramName, String lineDelimiter) throws CoreException {
	String templateName= CodeTemplateContextType.SETTERSTUB_ID;
	Template template= getCodeTemplate(templateName, project);
	if (template == null) {
		return null;
	}
	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
	context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
	context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
	context.setVariable(CodeTemplateContextType.FIELD, fieldName);
	context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldName);
	context.setVariable(CodeTemplateContextType.PARAM, paramName);

	return evaluateTemplate(context, template);
}
 
Example 17
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String getGetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String lineDelimiter) throws CoreException {
	String templateName= CodeTemplateContextType.GETTERSTUB_ID;
	Template template= getCodeTemplate(templateName, project);
	if (template == null) {
		return null;
	}
	CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
	context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
	context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
	context.setVariable(CodeTemplateContextType.FIELD, fieldName);

	return evaluateTemplate(context, template);
}
 
Example 18
Source File: TemplateSet.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected String validateTemplate(Template template) {
	TemplateContextType type= fRegistry.getContextType(template.getContextTypeId());
	if (type == null) {
		return "Unknown context type: " + template.getContextTypeId(); //$NON-NLS-1$
	}
	try {
		type.validate(template.getPattern());
		return null;
	} catch (TemplateException e) {
		return e.getMessage();
	}
}
 
Example 19
Source File: DotTemplateProposalProvider.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private Template replaceOpVariable(EObject currentModel,
		Template edgeTemplate) {
	DotGraph dotGraph = EcoreUtil2.getContainerOfType(currentModel,
			DotGraph.class);
	boolean isDirected = dotGraph.getType() == GraphType.DIGRAPH;
	String edgeOp = isDirected ? EdgeOp.DIRECTED.toString()
			: EdgeOp.UNDIRECTED.toString();

	return new Template(edgeTemplate.getName(),
			edgeTemplate.getDescription(), edgeTemplate.getContextTypeId(),
			edgeTemplate.getPattern().replaceAll(Pattern.quote("${op}"), //$NON-NLS-1$
					edgeOp),
			edgeTemplate.isAutoInsertable());
}
 
Example 20
Source File: AbstractDocumentTemplateContextWithIndent.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
private Template createNewTemplate(Template template, String newString) {
    return new Template(template.getName(), template.getDescription(), template.getContextTypeId(), newString,
            template.isAutoInsertable());
}