Search in sources :

Example 96 with Template

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

the class StubUtility method getMethodBodyContent.

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

Example 97 with Template

use of org.eclipse.jface.text.templates.Template in project dbeaver by serge-rider.

the class SQLCompletionProcessor method makeTemplateProposals.

@NotNull
private ICompletionProposal[] makeTemplateProposals(ITextViewer viewer, SQLCompletionAnalyzer.CompletionRequest request) {
    String wordPart = request.wordPart.toLowerCase();
    final List<SQLTemplateCompletionProposal> templateProposals = new ArrayList<>();
    // Templates
    for (Template template : editor.getTemplatesPage().getTemplateStore().getTemplates()) {
        if (template.getName().toLowerCase().startsWith(wordPart)) {
            templateProposals.add(new SQLTemplateCompletionProposal(template, new SQLContext(SQLTemplatesRegistry.getInstance().getTemplateContextRegistry().getContextType(template.getContextTypeId()), viewer.getDocument(), new Position(request.wordDetector.getStartOffset(), request.wordDetector.getLength()), editor), new Region(request.documentOffset, 0), null));
        }
    }
    Collections.sort(templateProposals, new Comparator<SQLTemplateCompletionProposal>() {

        @Override
        public int compare(SQLTemplateCompletionProposal o1, SQLTemplateCompletionProposal o2) {
            return o1.getDisplayString().compareTo(o2.getDisplayString());
        }
    });
    return templateProposals.toArray(new ICompletionProposal[templateProposals.size()]);
}
Also used : Position(org.eclipse.jface.text.Position) SQLTemplateCompletionProposal(org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplateCompletionProposal) Region(org.eclipse.jface.text.Region) SQLContext(org.jkiss.dbeaver.ui.editors.sql.templates.SQLContext) Template(org.eclipse.jface.text.templates.Template) NotNull(org.jkiss.code.NotNull)

Example 98 with Template

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

the class StubUtility method getCompilationUnitContent.

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);
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.TYPE_COMMENT, typeComment != null ? typeComment : "");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : "");
    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);
}
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 99 with Template

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

the class SQLCompletionProcessor method makeTemplateProposals.

@NotNull
private ICompletionProposal[] makeTemplateProposals(ITextViewer viewer, SQLCompletionAnalyzer.CompletionRequest request) {
    String wordPart = request.wordPart.toLowerCase();
    final List<SQLTemplateCompletionProposal> templateProposals = new ArrayList<>();
    // Templates
    for (Template template : editor.getTemplatesPage().getTemplateStore().getTemplates()) {
        if (template.getName().toLowerCase().startsWith(wordPart)) {
            templateProposals.add(new SQLTemplateCompletionProposal(template, new SQLContext(SQLTemplatesRegistry.getInstance().getTemplateContextRegistry().getContextType(template.getContextTypeId()), viewer.getDocument(), new Position(request.wordDetector.getStartOffset(), request.wordDetector.getLength()), editor), new Region(request.documentOffset, 0), null));
        }
    }
    Collections.sort(templateProposals, new Comparator<SQLTemplateCompletionProposal>() {

        @Override
        public int compare(SQLTemplateCompletionProposal o1, SQLTemplateCompletionProposal o2) {
            return o1.getDisplayString().compareTo(o2.getDisplayString());
        }
    });
    return templateProposals.toArray(new ICompletionProposal[templateProposals.size()]);
}
Also used : SQLTemplateCompletionProposal(org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplateCompletionProposal) SQLContext(org.jkiss.dbeaver.ui.editors.sql.templates.SQLContext) Template(org.eclipse.jface.text.templates.Template) NotNull(org.jkiss.code.NotNull)

Example 100 with Template

use of org.eclipse.jface.text.templates.Template 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)

Aggregations

Template (org.eclipse.jface.text.templates.Template)158 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)27 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)22 TemplateException (org.eclipse.jface.text.templates.TemplateException)21 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)19 IDocument (org.eclipse.jface.text.IDocument)18 Document (org.eclipse.jface.text.Document)17 ArrayList (java.util.ArrayList)15 BadLocationException (org.eclipse.jface.text.BadLocationException)15 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)15 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)14 TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)13 TemplateStore (org.eclipse.jface.text.templates.persistence.TemplateStore)12 CoreException (org.eclipse.core.runtime.CoreException)11 Region (org.eclipse.jface.text.Region)11 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)11 ISubReference (org.eclipse.titan.designer.AST.ISubReference)11 IRegion (org.eclipse.jface.text.IRegion)10 ITextSelection (org.eclipse.jface.text.ITextSelection)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10