use of org.eclipse.jface.text.templates.TemplateContextType in project che by eclipse.
the class JavaContext method evaluateTemplate.
/**
* Evaluates a 'java' template in the context of a compilation unit
*
* @param template the template to be evaluated
* @param compilationUnit the compilation unit in which to evaluate the template
* @param position the position inside the compilation unit for which to evaluate the template
* @return the evaluated template
* @throws CoreException in case the template is of an unknown context type
* @throws BadLocationException in case the position is invalid in the compilation unit
* @throws TemplateException in case the evaluation fails
*/
public static String evaluateTemplate(Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException {
TemplateContextType contextType = JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId());
if (!(contextType instanceof CompilationUnitContextType))
throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null));
IDocument document = new Document();
if (compilationUnit != null && compilationUnit.exists())
document.set(compilationUnit.getSource());
CompilationUnitContext context = ((CompilationUnitContextType) contextType).createContext(document, position, 0, compilationUnit);
context.setForceEvaluation(true);
TemplateBuffer buffer = context.evaluate(template);
if (buffer == null)
return null;
return buffer.getString();
}
use of org.eclipse.jface.text.templates.TemplateContextType in project che by eclipse.
the class JavaPlugin method registerJavaContext.
/**
* Registers the given Java template context.
*
* @param registry the template context type registry
* @param id the context type id
* @param parent the parent context type
* @since 3.4
*/
private static void registerJavaContext(ContributionContextTypeRegistry registry, String id, TemplateContextType parent) {
TemplateContextType contextType = registry.getContextType(id);
Iterator<TemplateVariableResolver> iter = parent.resolvers();
while (iter.hasNext()) contextType.addResolver(iter.next());
}
use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.platform.text by eclipse.
the class AbstractTemplatesPage method initializeDND.
/**
* Initializes drag and drop the template items
*/
private void initializeDND() {
DragSourceAdapter dragListener = new DragSourceAdapter() {
@Override
public void dragStart(DragSourceEvent event) {
if (getSelectedTemplates().length == 0) {
event.doit = false;
}
}
@Override
public void dragSetData(DragSourceEvent event) {
if (TemplatesTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = getSelectedTemplates();
}
}
};
fTreeViewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { TemplatesTransfer.getInstance() }, dragListener);
DropTargetAdapter dropListener = new DropTargetAdapter() {
Transfer textTransfer = TextTransfer.getInstance();
Transfer templateTransfer = TemplatesTransfer.getInstance();
@Override
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
@Override
public void dragOperationChanged(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
@Override
public void dragOver(DropTargetEvent event) {
event.feedback |= DND.FEEDBACK_SCROLL;
if (event.item == null) {
event.detail = DND.DROP_NONE;
return;
}
int index = 0;
boolean isTemplateTransfer = false;
while (index < event.dataTypes.length) {
if (textTransfer.isSupportedType(event.dataTypes[index])) {
break;
}
if (templateTransfer.isSupportedType(event.dataTypes[index])) {
isTemplateTransfer = true;
break;
}
index++;
}
if (index < event.dataTypes.length) {
event.currentDataType = event.dataTypes[index];
if (event.detail == DND.DROP_DEFAULT || !isTemplateTransfer)
event.detail = DND.DROP_COPY;
return;
}
}
@Override
public void drop(DropTargetEvent event) {
if (event.item == null)
return;
Object object = ((TreeItem) event.item).getData();
final String contextId;
if (object instanceof TemplateContextType)
contextId = ((TemplateContextType) object).getId();
else
contextId = ((TemplatePersistenceData) object).getTemplate().getContextTypeId();
if (textTransfer.isSupportedType(event.currentDataType)) {
// $NON-NLS-1$ //$NON-NLS-2$
String text = ((String) event.data).replaceAll("\\$", "\\$\\$");
final Template template = new Template(createTemplateName(), TemplatesMessages.TemplatesPage_paste_description, contextId, text, true);
getShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
addTemplate(template);
}
});
return;
}
if (templateTransfer.isSupportedType(event.currentDataType)) {
final TemplatePersistenceData[] templates = (TemplatePersistenceData[]) event.data;
final int dropType = event.detail;
getShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
if (dropType == DND.DROP_COPY)
copyTemplates(templates, contextId);
else
moveTemplates(templates, contextId);
}
});
}
}
};
Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(), TemplatesTransfer.getInstance() };
fTreeViewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers, dropListener);
}
use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.platform.text by eclipse.
the class AbstractTemplatesPage method getContextTypeId.
/**
* Returns the context type id of the selected template.
*
* @return the context type id of the selected template or the first from the
* registry if no templates are selected
*/
private String getContextTypeId() {
IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection();
Object item;
if (selection.size() == 0)
return getContextTypeRegistry().contextTypes().next().getId();
if (selection.size() == 1) {
item = selection.getFirstElement();
if (item instanceof TemplatePersistenceData)
return ((TemplatePersistenceData) item).getTemplate().getContextTypeId();
return ((TemplateContextType) item).getId();
}
Iterator<?> it = selection.iterator();
String contextId = null;
while (it.hasNext()) {
item = it.next();
if (contextId == null)
contextId = getContextId(item);
else if (!contextId.equals(getContextId(item)))
return getContextTypeRegistry().contextTypes().next().getId();
}
return contextId;
}
use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.platform.text by eclipse.
the class TemplatePreferencePage method computeMinimumContextColumnWidth.
/*
* @since 3.4
*/
private int computeMinimumContextColumnWidth(GC gc) {
int width = gc.stringExtent(TemplatesMessages.TemplatePreferencePage_column_context).x;
Iterator<TemplateContextType> iter = getContextTypeRegistry().contextTypes();
while (iter.hasNext()) {
TemplateContextType contextType = iter.next();
width = Math.max(width, gc.stringExtent(contextType.getName()).x);
}
return width;
}
Aggregations