Search in sources :

Example 6 with Template

use of org.eclipse.jface.text.templates.Template in project che by eclipse.

the class StubUtility method getCatchBodyContent.

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);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 7 with Template

use of org.eclipse.jface.text.templates.Template in project che by eclipse.

the class StubUtility method getGetterComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getGetterComment(ICompilationUnit, String, String, String, String, String, String)
	 */
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);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 8 with Template

use of org.eclipse.jface.text.templates.Template in project che by eclipse.

the class StubUtility method getFileComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getFileComment(ICompilationUnit, String)
	 */
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);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) IJavaProject(org.eclipse.jdt.core.IJavaProject) Template(org.eclipse.jface.text.templates.Template)

Example 9 with Template

use of org.eclipse.jface.text.templates.Template in project che by eclipse.

the class UnimplementedCodeCleanUp method getMethodBody.

private String getMethodBody() {
    String templateName = CodeTemplateContextType.METHODSTUB_ID;
    Template template = getCodeTemplate(templateName);
    if (template == null)
        //$NON-NLS-1$
        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, "");
    return evaluateTemplate(template, context);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 10 with Template

use of org.eclipse.jface.text.templates.Template in project che by eclipse.

the class TemplateReaderWriter method read.

/**
	 * Reads templates from an <code>InputSource</code> and adds them to the templates.
	 *
	 * @param source the input source
	 * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur
	 * @param singleId the template id to extract, or <code>null</code> to read in all templates
	 * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code>
	 * @throws IOException if reading from the stream fails
	 */
private TemplatePersistenceData[] read(InputSource source, ResourceBundle bundle, String singleId) throws IOException {
    try {
        Collection templates = new ArrayList();
        Set ids = new HashSet();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = factory.newDocumentBuilder();
        parser.setErrorHandler(new DefaultHandler());
        Document document = parser.parse(source);
        NodeList elements = document.getElementsByTagName(TEMPLATE_ELEMENT);
        int count = elements.getLength();
        for (int i = 0; i != count; i++) {
            Node node = elements.item(i);
            NamedNodeMap attributes = node.getAttributes();
            if (attributes == null)
                continue;
            String id = getStringValue(attributes, ID_ATTRIBUTE, null);
            if (id != null && ids.contains(id))
                //$NON-NLS-1$
                throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.duplicate.id"));
            if (singleId != null && !singleId.equals(id))
                continue;
            boolean deleted = getBooleanValue(attributes, DELETED_ATTRIBUTE, false);
            String name = getStringValue(attributes, NAME_ATTRIBUTE);
            name = translateString(name, bundle);
            //$NON-NLS-1$
            String description = getStringValue(attributes, DESCRIPTION_ATTRIBUTE, "");
            description = translateString(description, bundle);
            String context = getStringValue(attributes, CONTEXT_ATTRIBUTE);
            if (name == null || context == null)
                //$NON-NLS-1$
                throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.error.missing_attribute"));
            boolean enabled = getBooleanValue(attributes, ENABLED_ATTRIBUTE, true);
            boolean autoInsertable = getBooleanValue(attributes, AUTO_INSERTABLE_ATTRIBUTE, true);
            StringBuffer buffer = new StringBuffer();
            NodeList children = node.getChildNodes();
            for (int j = 0; j != children.getLength(); j++) {
                String value = children.item(j).getNodeValue();
                if (value != null)
                    buffer.append(value);
            }
            String pattern = buffer.toString();
            pattern = translateString(pattern, bundle);
            Template template = new Template(name, description, context, pattern, autoInsertable);
            TemplatePersistenceData data = new TemplatePersistenceData(template, enabled, id);
            data.setDeleted(deleted);
            templates.add(data);
            if (singleId != null && singleId.equals(id))
                break;
        }
        return (TemplatePersistenceData[]) templates.toArray(new TemplatePersistenceData[templates.size()]);
    } catch (ParserConfigurationException e) {
        Assert.isTrue(false);
    } catch (SAXException e) {
        //$NON-NLS-1$
        throw (IOException) new IOException("Could not read template file").initCause(e);
    }
    // dummy
    return null;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Template(org.eclipse.jface.text.templates.Template) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Collection(java.util.Collection) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashSet(java.util.HashSet)

Aggregations

Template (org.eclipse.jface.text.templates.Template)37 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)28 BadLocationException (org.eclipse.jface.text.BadLocationException)7 IDocument (org.eclipse.jface.text.IDocument)7 CoreException (org.eclipse.core.runtime.CoreException)6 Document (org.eclipse.jface.text.Document)6 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)6 TemplateException (org.eclipse.jface.text.templates.TemplateException)6 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IOException (java.io.IOException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TemplatePersistenceData (org.eclipse.che.jface.text.templates.persistence.TemplatePersistenceData)2 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)2 Position (org.eclipse.jface.text.Position)2 Region (org.eclipse.jface.text.Region)2