Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorteicher2004-03-19 10:49:08 +0000
committerteicher2004-03-19 10:49:08 +0000
commitfa2f7a70c657eb42555ba5521c90ec930b9543de (patch)
tree3d58baee80cdaba94b1a074f7579b48bdfd886d7 /org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template
parentb85ebfebd841a945be7ffa33a718394da2f517b2 (diff)
downloadeclipse.platform.text-fa2f7a70c657eb42555ba5521c90ec930b9543de.tar.gz
eclipse.platform.text-fa2f7a70c657eb42555ba5521c90ec930b9543de.tar.xz
eclipse.platform.text-fa2f7a70c657eb42555ba5521c90ec930b9543de.zip
added template editor example to javaeditor example plug-in
Diffstat (limited to 'org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template')
-rw-r--r--org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/AntVariableResolver.java40
-rw-r--r--org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/XMLCompletionProcessor.java99
-rw-r--r--org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/XMLContextType.java43
3 files changed, 182 insertions, 0 deletions
diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/AntVariableResolver.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/AntVariableResolver.java
new file mode 100644
index 000000000..a244aa3ac
--- /dev/null
+++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/AntVariableResolver.java
@@ -0,0 +1,40 @@
+package org.eclipse.ui.examples.templateeditor.template;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+import org.eclipse.jface.text.templates.TemplateContext;
+import org.eclipse.jface.text.templates.TemplateVariableResolver;
+
+/**
+ * Looks up existing ant variables and proposes them. The proposals are sorted by
+ * their prefix-likeness with the variable type.
+ */
+public class AntVariableResolver extends TemplateVariableResolver {
+ /*
+ * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolveAll(org.eclipse.jface.text.templates.TemplateContext)
+ */
+ protected String[] resolveAll(TemplateContext context) {
+ String[] proposals= new String[] { "${srcDir}", "${dstDir}" }; //$NON-NLS-1$ //$NON-NLS-2$
+
+ Arrays.sort(proposals, new Comparator() {
+
+ public int compare(Object o1, Object o2) {
+ return getCommonPrefixLength(getType(), (String) o2) - getCommonPrefixLength(getType(), (String) o1);
+ }
+
+ private int getCommonPrefixLength(String type, String var) {
+ int i= 0;
+ CharSequence vSeq= var.subSequence(2, var.length() - 1); // strip away ${}
+ while (i < type.length() && i < vSeq.length())
+ if (Character.toLowerCase(type.charAt(i)) == Character.toLowerCase(vSeq.charAt(i)))
+ i++;
+ else
+ break;
+ return i;
+ }
+ });
+
+ return proposals;
+ }
+}
diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/XMLCompletionProcessor.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/XMLCompletionProcessor.java
new file mode 100644
index 000000000..b691e581e
--- /dev/null
+++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/XMLCompletionProcessor.java
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ui.examples.templateeditor.template;
+
+import org.eclipse.swt.graphics.Image;
+
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.ImageRegistry;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.templates.ContextType;
+import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
+
+import org.eclipse.ui.examples.templateeditor.editors.TemplateEditorUI;
+
+
+/**
+ * A completion processor for XML templates.
+ */
+public class XMLCompletionProcessor extends TemplateCompletionProcessor {
+ private static final String DEFAULT_IMAGE= "icons/template.gif"; //$NON-NLS-1$
+
+ /**
+ * We watch for angular brackets since those are often part of XML
+ * templates.
+ */
+ protected String extractPrefix(ITextViewer viewer, int offset) {
+ IDocument document= viewer.getDocument();
+ int i= offset;
+ if (i > document.getLength())
+ return ""; //$NON-NLS-1$
+
+ try {
+ while (i > 0) {
+ char ch= document.getChar(i - 1);
+ if (ch != '<' && !Character.isJavaIdentifierPart(ch))
+ break;
+ i--;
+ }
+
+ return document.get(i, offset - i);
+ } catch (BadLocationException e) {
+ return ""; //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Cut out angular brackets for relevance sorting, since the template name
+ * does not contain the brackets.
+ */
+ protected int getRelevance(Template template, String prefix) {
+ if (prefix.startsWith("<")) //$NON-NLS-1$
+ prefix= prefix.substring(1);
+ if (template.getName().startsWith(prefix))
+ return 90;
+ return 0;
+ }
+
+ /**
+ * Simply return all templates.
+ */
+ protected Template[] getTemplates(String contextTypeId) {
+ return TemplateEditorUI.getDefault().getTemplateStore().getTemplates();
+ }
+
+ /**
+ * Return the XML context type that is supported by this plugin.
+ */
+ protected ContextType getContextType(ITextViewer viewer, IRegion region) {
+ return TemplateEditorUI.getDefault().getContextTypeRegistry().getContextType(XMLContextType.XML_CONTEXT_TYPE);
+ }
+
+ /**
+ * Always return the default image.
+ */
+ protected Image getImage(Template template) {
+ ImageRegistry registry= TemplateEditorUI.getDefault().getImageRegistry();
+ Image image= registry.get(DEFAULT_IMAGE);
+ if (image == null) {
+ ImageDescriptor desc= TemplateEditorUI.imageDescriptorFromPlugin("org.eclipse.ui.examples.javaeditor", DEFAULT_IMAGE); //$NON-NLS-1$
+ registry.put(DEFAULT_IMAGE, desc);
+ image= registry.get(DEFAULT_IMAGE);
+ }
+ return image;
+ }
+
+}
diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/XMLContextType.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/XMLContextType.java
new file mode 100644
index 000000000..01a6484a7
--- /dev/null
+++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/template/XMLContextType.java
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ui.examples.templateeditor.template;
+
+import org.eclipse.jface.text.templates.ContextType;
+import org.eclipse.jface.text.templates.GlobalVariables;
+
+
+/**
+ * A very simple context type.
+ */
+public class XMLContextType extends ContextType {
+
+ /** This context's id */
+ public static final String XML_CONTEXT_TYPE= "org.eclipse.ui.examples.templateeditor.xml"; //$NON-NLS-1$
+
+ /**
+ * Creates a new XML context type.
+ */
+ public XMLContextType() {
+ addGlobalResolvers();
+ }
+
+ private void addGlobalResolvers() {
+ addResolver(new GlobalVariables.Cursor());
+ addResolver(new GlobalVariables.WordSelection());
+ addResolver(new GlobalVariables.LineSelection());
+ addResolver(new GlobalVariables.Dollar());
+ addResolver(new GlobalVariables.Date());
+ addResolver(new GlobalVariables.Year());
+ addResolver(new GlobalVariables.Time());
+ addResolver(new GlobalVariables.User());
+ }
+
+}

Back to the top