Search in sources :

Example 31 with TemplateContextType

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();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) IDocument(org.eclipse.jface.text.IDocument)

Example 32 with TemplateContextType

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

Example 33 with TemplateContextType

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);
}
Also used : DragSourceAdapter(org.eclipse.swt.dnd.DragSourceAdapter) TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) TreeItem(org.eclipse.swt.widgets.TreeItem) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) Point(org.eclipse.swt.graphics.Point) Template(org.eclipse.jface.text.templates.Template) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) Transfer(org.eclipse.swt.dnd.Transfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType)

Example 34 with TemplateContextType

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;
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType)

Example 35 with TemplateContextType

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;
}
Also used : TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType)

Aggregations

TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)54 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)17 Template (org.eclipse.jface.text.templates.Template)14 Document (org.eclipse.jface.text.Document)11 IDocument (org.eclipse.jface.text.IDocument)11 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)9 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)9 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)8 CoreException (org.eclipse.core.runtime.CoreException)7 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)7 IStatus (org.eclipse.core.runtime.IStatus)4 Status (org.eclipse.core.runtime.Status)4 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)4 TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)4 TemplateStore (org.eclipse.jface.text.templates.persistence.TemplateStore)4 ContextTypeIdHelper (org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper)4 IFile (org.eclipse.core.resources.IFile)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 StyledString (org.eclipse.jface.viewers.StyledString)3