001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.templateeditor.template;
011:
012: import org.eclipse.swt.graphics.Image;
013:
014: import org.eclipse.jface.resource.ImageDescriptor;
015: import org.eclipse.jface.resource.ImageRegistry;
016:
017: import org.eclipse.jface.text.BadLocationException;
018: import org.eclipse.jface.text.IDocument;
019: import org.eclipse.jface.text.IRegion;
020: import org.eclipse.jface.text.ITextViewer;
021: import org.eclipse.jface.text.templates.TemplateContextType;
022: import org.eclipse.jface.text.templates.Template;
023: import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
024:
025: import org.eclipse.ui.examples.templateeditor.editors.TemplateEditorUI;
026:
027: /**
028: * A completion processor for XML templates.
029: */
030: public class XMLCompletionProcessor extends TemplateCompletionProcessor {
031: private static final String DEFAULT_IMAGE = "$nl$/icons/template.gif"; //$NON-NLS-1$
032:
033: /**
034: * We watch for angular brackets since those are often part of XML
035: * templates.
036: *
037: * @param viewer the viewer
038: * @param offset the offset left of which the prefix is detected
039: * @return the detected prefix
040: */
041: protected String extractPrefix(ITextViewer viewer, int offset) {
042: IDocument document = viewer.getDocument();
043: int i = offset;
044: if (i > document.getLength())
045: return ""; //$NON-NLS-1$
046:
047: try {
048: while (i > 0) {
049: char ch = document.getChar(i - 1);
050: if (ch != '<' && !Character.isJavaIdentifierPart(ch))
051: break;
052: i--;
053: }
054: return document.get(i, offset - i);
055: } catch (BadLocationException e) {
056: return ""; //$NON-NLS-1$
057: }
058: }
059:
060: /**
061: * Cut out angular brackets for relevance sorting, since the template name
062: * does not contain the brackets.
063: *
064: * @param template the template
065: * @param prefix the prefix
066: * @return the relevance of the <code>template</code> for the given <code>prefix</code>
067: */
068: protected int getRelevance(Template template, String prefix) {
069: if (prefix.startsWith("<")) //$NON-NLS-1$
070: prefix = prefix.substring(1);
071: if (template.getName().startsWith(prefix))
072: return 90;
073: return 0;
074: }
075:
076: /**
077: * Simply return all templates.
078: *
079: * @param contextTypeId the context type, ignored in this implementation
080: * @return all templates
081: */
082: protected Template[] getTemplates(String contextTypeId) {
083: return TemplateEditorUI.getDefault().getTemplateStore()
084: .getTemplates();
085: }
086:
087: /**
088: * Return the XML context type that is supported by this plug-in.
089: *
090: * @param viewer the viewer, ignored in this implementation
091: * @param region the region, ignored in this implementation
092: * @return the supported XML context type
093: */
094: protected TemplateContextType getContextType(ITextViewer viewer,
095: IRegion region) {
096: return TemplateEditorUI.getDefault().getContextTypeRegistry()
097: .getContextType(XMLContextType.XML_CONTEXT_TYPE);
098: }
099:
100: /**
101: * Always return the default image.
102: *
103: * @param template the template, ignored in this implementation
104: * @return the default template image
105: */
106: protected Image getImage(Template template) {
107: ImageRegistry registry = TemplateEditorUI.getDefault()
108: .getImageRegistry();
109: Image image = registry.get(DEFAULT_IMAGE);
110: if (image == null) {
111: ImageDescriptor desc = TemplateEditorUI
112: .imageDescriptorFromPlugin(
113: "org.eclipse.ui.examples.javaeditor", DEFAULT_IMAGE); //$NON-NLS-1$
114: registry.put(DEFAULT_IMAGE, desc);
115: image = registry.get(DEFAULT_IMAGE);
116: }
117: return image;
118: }
119:
120: }
|