/** * Creates a concrete template context for the given region in the document. This involves finding out which * context type is valid at the given location, and then creating a context of this type. The default implementation * returns a <code>DocumentTemplateContext</code> for the context type at the given location. * * @param viewer the viewer for which the context is created * @param region the region into <code>document</code> for which the context is created * @return a template context that can handle template insertion at the given location, or <code>null</code> */ protected TemplateContext createContext(ITextViewer viewer, IRegion region) { TemplateContextType contextType= getContextType(viewer, region); if (contextType != null) { IDocument document= viewer.getDocument(); return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength()); } return null; }
/** * Creates a concrete template context for the given region in the document. This involves finding out which * context type is valid at the given location, and then creating a context of this type. The default implementation * returns a <code>DocumentTemplateContext</code> for the context type at the given location. * * @param viewer the viewer for which the context is created * @param region the region into <code>document</code> for which the context is created * @return a template context that can handle template insertion at the given location, or <code>null</code> */ protected TemplateContext createContext(ITextViewer viewer, IRegion region) { TemplateContextType contextType= getContextType(viewer, region); if (contextType != null) { IDocument document= viewer.getDocument(); return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength()); } return null; }
/** * Returns template string to insert. * * @return String to insert or null if none is to be inserted */ String getTemplateString() { String templateString = null; Template template = getSelectedTemplate(); if (template != null) { TemplateContextType contextType = XMLUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsXML.NEW); IDocument document = new Document(); TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0); try { TemplateBuffer buffer = context.evaluate(template); templateString = buffer.getString(); } catch (Exception e) { Logger.log(Logger.WARNING_DEBUG, "Could not create template for new xml", e); //$NON-NLS-1$ } } return templateString; }