diff options
author | teicher | 2004-03-19 10:49:08 +0000 |
---|---|---|
committer | teicher | 2004-03-19 10:49:08 +0000 |
commit | fa2f7a70c657eb42555ba5521c90ec930b9543de (patch) | |
tree | 3d58baee80cdaba94b1a074f7579b48bdfd886d7 /org.eclipse.ui.examples.javaeditor/Template Editor Example | |
parent | b85ebfebd841a945be7ffa33a718394da2f517b2 (diff) | |
download | eclipse.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')
19 files changed, 939 insertions, 0 deletions
diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/ColorManager.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/ColorManager.java new file mode 100644 index 00000000000..54b46c15899 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/ColorManager.java @@ -0,0 +1,28 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; + +public class ColorManager { + + protected Map fColorTable = new HashMap(10); + + public void dispose() { + Iterator e = fColorTable.values().iterator(); + while (e.hasNext()) + ((Color) e.next()).dispose(); + } + public Color getColor(RGB rgb) { + Color color = (Color) fColorTable.get(rgb); + if (color == null) { + color = new Color(Display.getCurrent(), rgb); + fColorTable.put(rgb, color); + } + return color; + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/IXMLColorConstants.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/IXMLColorConstants.java new file mode 100644 index 00000000000..dfb35ee3f74 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/IXMLColorConstants.java @@ -0,0 +1,11 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.swt.graphics.RGB; + +public interface IXMLColorConstants { + RGB XML_COMMENT = new RGB(128, 0, 0); + RGB PROC_INSTR = new RGB(128, 128, 128); + RGB STRING = new RGB(0, 128, 0); + RGB DEFAULT = new RGB(0, 0, 0); + RGB TAG = new RGB(0, 0, 128); +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/NonRuleBasedDamagerRepairer.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/NonRuleBasedDamagerRepairer.java new file mode 100644 index 00000000000..b0d4d13f8a2 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/NonRuleBasedDamagerRepairer.java @@ -0,0 +1,138 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.DocumentEvent; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.TextPresentation; +import org.eclipse.jface.text.presentation.IPresentationDamager; +import org.eclipse.jface.text.presentation.IPresentationRepairer; +import org.eclipse.jface.util.Assert; +import org.eclipse.swt.custom.StyleRange; + +public class NonRuleBasedDamagerRepairer + implements IPresentationDamager, IPresentationRepairer { + + /** The document this object works on */ + protected IDocument fDocument; + /** The default text attribute if non is returned as data by the current token */ + protected TextAttribute fDefaultTextAttribute; + + /** + * Constructor for NonRuleBasedDamagerRepairer. + */ + public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) { + Assert.isNotNull(defaultTextAttribute); + + fDefaultTextAttribute = defaultTextAttribute; + } + + /** + * @see IPresentationRepairer#setDocument(IDocument) + */ + public void setDocument(IDocument document) { + fDocument = document; + } + + /** + * Returns the end offset of the line that contains the specified offset or + * if the offset is inside a line delimiter, the end offset of the next line. + * + * @param offset the offset whose line end offset must be computed + * @return the line end offset for the given offset + * @exception BadLocationException if offset is invalid in the current document + */ + protected int endOfLineOf(int offset) throws BadLocationException { + + IRegion info = fDocument.getLineInformationOfOffset(offset); + if (offset <= info.getOffset() + info.getLength()) + return info.getOffset() + info.getLength(); + + int line = fDocument.getLineOfOffset(offset); + try { + info = fDocument.getLineInformation(line + 1); + return info.getOffset() + info.getLength(); + } catch (BadLocationException x) { + return fDocument.getLength(); + } + } + + /** + * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean) + */ + public IRegion getDamageRegion( + ITypedRegion partition, + DocumentEvent event, + boolean documentPartitioningChanged) { + if (!documentPartitioningChanged) { + try { + + IRegion info = + fDocument.getLineInformationOfOffset(event.getOffset()); + int start = Math.max(partition.getOffset(), info.getOffset()); + + int end = + event.getOffset() + + (event.getText() == null + ? event.getLength() + : event.getText().length()); + + if (info.getOffset() <= end + && end <= info.getOffset() + info.getLength()) { + // optimize the case of the same line + end = info.getOffset() + info.getLength(); + } else + end = endOfLineOf(end); + + end = + Math.min( + partition.getOffset() + partition.getLength(), + end); + return new Region(start, end - start); + + } catch (BadLocationException x) { + } + } + + return partition; + } + + /** + * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion) + */ + public void createPresentation( + TextPresentation presentation, + ITypedRegion region) { + addRange( + presentation, + region.getOffset(), + region.getLength(), + fDefaultTextAttribute); + } + + /** + * Adds style information to the given text presentation. + * + * @param presentation the text presentation to be extended + * @param offset the offset of the range to be styled + * @param length the length of the range to be styled + * @param attr the attribute describing the style of the range to be styled + */ + protected void addRange( + TextPresentation presentation, + int offset, + int length, + TextAttribute attr) { + if (attr != null) + presentation.addStyleRange( + new StyleRange( + offset, + length, + attr.getForeground(), + attr.getBackground(), + attr.getStyle())); + } +}
\ No newline at end of file diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TagRule.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TagRule.java new file mode 100644 index 00000000000..bb6ebda4e40 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TagRule.java @@ -0,0 +1,31 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.text.rules.*; + +public class TagRule extends MultiLineRule { + + public TagRule(IToken token) { + super("<", ">", token); + } + protected boolean sequenceDetected( + ICharacterScanner scanner, + char[] sequence, + boolean eofAllowed) { + int c = scanner.read(); + if (sequence[0] == '<') { + if (c == '?') { + // processing instruction - abort + scanner.unread(); + return false; + } + if (c == '!') { + scanner.unread(); + // comment - abort + return false; + } + } else if (sequence[0] == '>') { + scanner.unread(); + } + return super.sequenceDetected(scanner, sequence, eofAllowed); + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateEditor.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateEditor.java new file mode 100644 index 00000000000..91b6ae6372a --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateEditor.java @@ -0,0 +1,47 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.action.IAction; + +import org.eclipse.jface.text.source.ISourceViewer; + +import org.eclipse.ui.texteditor.ExtendedTextEditor; +import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; +import org.eclipse.ui.texteditor.TextOperationAction; + +/** + * A simple xml editor with template capabilities. + */ +public class TemplateEditor extends ExtendedTextEditor { + + private static final String TEMPLATE_PROPOSALS= "template_proposals_action"; //$NON-NLS-1$ + private ColorManager colorManager; + + /** + * Creates a new template editor. + */ + public TemplateEditor() { + super(); + colorManager = new ColorManager(); + setSourceViewerConfiguration(new XMLConfiguration(colorManager)); + setDocumentProvider(new XMLDocumentProvider()); + } + + public void dispose() { + colorManager.dispose(); + super.dispose(); + } + + protected void createActions() { + super.createActions(); + + IAction action= new TextOperationAction( + TemplateMessages.getResourceBundle(), + "Editor." + TEMPLATE_PROPOSALS + ".", //$NON-NLS-1$ //$NON-NLS-2$ + this, + ISourceViewer.CONTENTASSIST_PROPOSALS); + action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); + setAction(TEMPLATE_PROPOSALS, action); + markAsStateDependentAction(TEMPLATE_PROPOSALS, true); + } + +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateEditorUI.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateEditorUI.java new file mode 100644 index 00000000000..2bb49790a1f --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateEditorUI.java @@ -0,0 +1,96 @@ +package org.eclipse.ui.examples.templateeditor.editors; + + +import java.io.IOException; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; + +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.resource.ImageRegistry; + +import org.eclipse.jface.text.templates.ContextTypeRegistry; +import org.eclipse.jface.text.templates.persistence.TemplateStore; + +import org.eclipse.ui.examples.javaeditor.JavaEditorExamplePlugin; +import org.eclipse.ui.examples.templateeditor.template.XMLContextType; + +/** + * The main plugin class to be used in the desktop. + */ +public class TemplateEditorUI { + /** Key to store custom templates. */ + private static final String CUSTOM_TEMPLATES_KEY= "org.eclipse.ui.examples.templateeditor.customtemplates"; //$NON-NLS-1$ + + /** The shared instance. */ + private static TemplateEditorUI fInstance; + + /** The template store. */ + private TemplateStore fStore; + /** The context type registry. */ + private ContextTypeRegistry fRegistry; + + private TemplateEditorUI() { + } + + /** + * Returns the shared instance. + * + * @return the shared instance + */ + public static TemplateEditorUI getDefault() { + if (fInstance == null) + fInstance= new TemplateEditorUI(); + return fInstance; + } + + /** + * Returns this plug-in's template store. + * + * @return the template store of this plug-in instance + */ + public TemplateStore getTemplateStore() { + if (fStore == null) { + fStore= new TemplateStore(JavaEditorExamplePlugin.getDefault().getPreferenceStore(), CUSTOM_TEMPLATES_KEY); + try { + fStore.load(); + } catch (IOException e) { + JavaEditorExamplePlugin.getDefault().getLog().log(new Status(IStatus.ERROR, "org.eclipse.ui.examples.javaeditor", IStatus.OK, "", e)); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + return fStore; + } + + /** + * Returns this plug-in's context type registry. + * + * @return the context type registry for this plug-in instance + */ + public ContextTypeRegistry getContextTypeRegistry() { + if (fRegistry == null) { + // create an configure the contexts available in the template editor + fRegistry= new ContextTypeRegistry(); + fRegistry.addContextType(XMLContextType.XML_CONTEXT_TYPE); + } + return fRegistry; + } + + /* Forward plug-in methods to javaeditor example plugin default instance */ + public ImageRegistry getImageRegistry() { + return JavaEditorExamplePlugin.getDefault().getImageRegistry(); + } + + public static ImageDescriptor imageDescriptorFromPlugin(String string, String default_image) { + return JavaEditorExamplePlugin.imageDescriptorFromPlugin(string, default_image); + } + + public IPreferenceStore getPreferenceStore() { + return JavaEditorExamplePlugin.getDefault().getPreferenceStore(); + } + + public void savePluginPreferences() { + JavaEditorExamplePlugin.getDefault().savePluginPreferences(); + } + +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateMessages.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateMessages.java new file mode 100644 index 00000000000..2497adc0637 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateMessages.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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.editors; + +import java.text.MessageFormat; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +class TemplateMessages { + + private static final String RESOURCE_BUNDLE= TemplateMessages.class.getName(); + private static ResourceBundle fgResourceBundle= ResourceBundle.getBundle(RESOURCE_BUNDLE); + + private TemplateMessages() { + } + + /** + * @param key + * @return + */ + public static String getString(String key) { + try { + return fgResourceBundle.getString(key); + } catch (MissingResourceException e) { + return '!' + key + '!'; + } + } + + /** + * Gets a string from the resource bundle and formats it with the argument + * + * @param key the string used to get the bundle value, must not be null + * @param arg + * @return + */ + public static String getFormattedString(String key, Object arg) { + return MessageFormat.format(getString(key), new Object[] { arg }); + } + + + /** + * Gets a string from the resource bundle and formats it with arguments + * @param key + * @param args + * @return + */ + public static String getFormattedString(String key, Object[] args) { + return MessageFormat.format(getString(key), args); + } + + /** + * @return + */ + public static ResourceBundle getResourceBundle() { + return fgResourceBundle; + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateMessages.properties b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateMessages.properties new file mode 100644 index 00000000000..244f3f20fad --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/TemplateMessages.properties @@ -0,0 +1,3 @@ +Editor.template_proposals_action.label= Template Assist +Editor.template_proposals_action.description= Shows the available templates +Editor.template_proposals_action.tooltip= Show Template Proposals diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLConfiguration.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLConfiguration.java new file mode 100644 index 00000000000..d1ebe992696 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLConfiguration.java @@ -0,0 +1,91 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextDoubleClickStrategy; +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.contentassist.ContentAssistant; +import org.eclipse.jface.text.contentassist.IContentAssistProcessor; +import org.eclipse.jface.text.contentassist.IContentAssistant; +import org.eclipse.jface.text.presentation.IPresentationReconciler; +import org.eclipse.jface.text.presentation.PresentationReconciler; +import org.eclipse.jface.text.rules.DefaultDamagerRepairer; +import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.source.ISourceViewer; + +import org.eclipse.ui.editors.text.TextSourceViewerConfiguration; +import org.eclipse.ui.examples.templateeditor.template.XMLCompletionProcessor; + +public class XMLConfiguration extends TextSourceViewerConfiguration { + + private XMLDoubleClickStrategy doubleClickStrategy; + private XMLTagScanner tagScanner; + private XMLScanner scanner; + private ColorManager colorManager; + + public XMLConfiguration(ColorManager colorManager) { + this.colorManager= colorManager; + } + + public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { + return new String[]{IDocument.DEFAULT_CONTENT_TYPE, XMLPartitionScanner.XML_COMMENT, + XMLPartitionScanner.XML_TAG}; + } + + public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) { + if (doubleClickStrategy == null) + doubleClickStrategy= new XMLDoubleClickStrategy(); + return doubleClickStrategy; + } + + protected XMLScanner getXMLScanner() { + if (scanner == null) { + scanner= new XMLScanner(colorManager); + scanner.setDefaultReturnToken(new Token( + new TextAttribute(colorManager.getColor(IXMLColorConstants.DEFAULT)))); + } + return scanner; + } + + protected XMLTagScanner getXMLTagScanner() { + if (tagScanner == null) { + tagScanner= new XMLTagScanner(colorManager); + tagScanner + .setDefaultReturnToken(new Token(new TextAttribute(colorManager.getColor(IXMLColorConstants.TAG)))); + } + return tagScanner; + } + + public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { + PresentationReconciler reconciler= new PresentationReconciler(); + + DefaultDamagerRepairer dr= new DefaultDamagerRepairer(getXMLTagScanner()); + reconciler.setDamager(dr, XMLPartitionScanner.XML_TAG); + reconciler.setRepairer(dr, XMLPartitionScanner.XML_TAG); + + dr= new DefaultDamagerRepairer(getXMLScanner()); + reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); + reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); + + NonRuleBasedDamagerRepairer ndr= new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager + .getColor(IXMLColorConstants.XML_COMMENT))); + reconciler.setDamager(ndr, XMLPartitionScanner.XML_COMMENT); + reconciler.setRepairer(ndr, XMLPartitionScanner.XML_COMMENT); + + return reconciler; + } + + public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { + ContentAssistant assistant= new ContentAssistant(); + assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer)); + + IContentAssistProcessor processor= new XMLCompletionProcessor(); + assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE); + assistant.setContentAssistProcessor(processor, XMLPartitionScanner.XML_TAG); + + assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE); + assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer)); + + return assistant; + } + +}
\ No newline at end of file diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLDocumentProvider.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLDocumentProvider.java new file mode 100644 index 00000000000..cca6b535b6b --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLDocumentProvider.java @@ -0,0 +1,25 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentPartitioner; +import org.eclipse.jface.text.rules.DefaultPartitioner; +import org.eclipse.ui.editors.text.FileDocumentProvider; + +public class XMLDocumentProvider extends FileDocumentProvider { + + protected IDocument createDocument(Object element) throws CoreException { + IDocument document = super.createDocument(element); + if (document != null) { + IDocumentPartitioner partitioner = + new DefaultPartitioner( + new XMLPartitionScanner(), + new String[] { + XMLPartitionScanner.XML_TAG, + XMLPartitionScanner.XML_COMMENT }); + partitioner.connect(document); + document.setDocumentPartitioner(partitioner); + } + return document; + } +}
\ No newline at end of file diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLDoubleClickStrategy.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLDoubleClickStrategy.java new file mode 100644 index 00000000000..474cad30386 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLDoubleClickStrategy.java @@ -0,0 +1,112 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.text.*; + +public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy { + protected ITextViewer fText; + + public void doubleClicked(ITextViewer part) { + int pos = part.getSelectedRange().x; + + if (pos < 0) + return; + + fText = part; + + if (!selectComment(pos)) { + selectWord(pos); + } + } + protected boolean selectComment(int caretPos) { + IDocument doc = fText.getDocument(); + int startPos, endPos; + + try { + int pos = caretPos; + char c = ' '; + + while (pos >= 0) { + c = doc.getChar(pos); + if (c == '\\') { + pos -= 2; + continue; + } + if (c == Character.LINE_SEPARATOR || c == '\"') + break; + --pos; + } + + if (c != '\"') + return false; + + startPos = pos; + + pos = caretPos; + int length = doc.getLength(); + c = ' '; + + while (pos < length) { + c = doc.getChar(pos); + if (c == Character.LINE_SEPARATOR || c == '\"') + break; + ++pos; + } + if (c != '\"') + return false; + + endPos = pos; + + int offset = startPos + 1; + int len = endPos - offset; + fText.setSelectedRange(offset, len); + return true; + } catch (BadLocationException x) { + } + + return false; + } + protected boolean selectWord(int caretPos) { + + IDocument doc = fText.getDocument(); + int startPos, endPos; + + try { + + int pos = caretPos; + char c; + + while (pos >= 0) { + c = doc.getChar(pos); + if (!Character.isJavaIdentifierPart(c)) + break; + --pos; + } + + startPos = pos; + + pos = caretPos; + int length = doc.getLength(); + + while (pos < length) { + c = doc.getChar(pos); + if (!Character.isJavaIdentifierPart(c)) + break; + ++pos; + } + + endPos = pos; + selectRange(startPos, endPos); + return true; + + } catch (BadLocationException x) { + } + + return false; + } + + private void selectRange(int startPos, int stopPos) { + int offset = startPos + 1; + int length = stopPos - offset; + fText.setSelectedRange(offset, length); + } +}
\ No newline at end of file diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLPartitionScanner.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLPartitionScanner.java new file mode 100644 index 00000000000..bba47486060 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLPartitionScanner.java @@ -0,0 +1,22 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.text.rules.*; + +public class XMLPartitionScanner extends RuleBasedPartitionScanner { + public final static String XML_DEFAULT = "__xml_default"; + public final static String XML_COMMENT = "__xml_comment"; + public final static String XML_TAG = "__xml_tag"; + + public XMLPartitionScanner() { + + IToken xmlComment = new Token(XML_COMMENT); + IToken tag = new Token(XML_TAG); + + IPredicateRule[] rules = new IPredicateRule[2]; + + rules[0] = new MultiLineRule("<!--", "-->", xmlComment); + rules[1] = new TagRule(tag); + + setPredicateRules(rules); + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLScanner.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLScanner.java new file mode 100644 index 00000000000..947552d0150 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLScanner.java @@ -0,0 +1,22 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.text.rules.*; +import org.eclipse.jface.text.*; + +public class XMLScanner extends RuleBasedScanner { + + public XMLScanner(ColorManager manager) { + IToken procInstr = + new Token( + new TextAttribute( + manager.getColor(IXMLColorConstants.PROC_INSTR))); + + IRule[] rules = new IRule[2]; + //Add rule for processing instructions + rules[0] = new SingleLineRule("<?", "?>", procInstr); + // Add generic whitespace rule. + rules[1] = new WhitespaceRule(new XMLWhitespaceDetector()); + + setRules(rules); + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLTagScanner.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLTagScanner.java new file mode 100644 index 00000000000..13ab267f14d --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLTagScanner.java @@ -0,0 +1,24 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.text.*; +import org.eclipse.jface.text.rules.*; + +public class XMLTagScanner extends RuleBasedScanner { + + public XMLTagScanner(ColorManager manager) { + IToken string = + new Token( + new TextAttribute(manager.getColor(IXMLColorConstants.STRING))); + + IRule[] rules = new IRule[3]; + + // Add rule for double quotes + rules[0] = new SingleLineRule("\"", "\"", string, '\\'); + // Add a rule for single quotes + rules[1] = new SingleLineRule("'", "'", string, '\\'); + // Add generic whitespace rule. + rules[2] = new WhitespaceRule(new XMLWhitespaceDetector()); + + setRules(rules); + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLWhitespaceDetector.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLWhitespaceDetector.java new file mode 100644 index 00000000000..d8c5be4629e --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/editors/XMLWhitespaceDetector.java @@ -0,0 +1,10 @@ +package org.eclipse.ui.examples.templateeditor.editors; + +import org.eclipse.jface.text.rules.IWhitespaceDetector; + +public class XMLWhitespaceDetector implements IWhitespaceDetector { + + public boolean isWhitespace(char c) { + return (c == ' ' || c == '\t' || c == '\n' || c == '\r'); + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/preferences/TemplatesPreferencePage.java b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/preferences/TemplatesPreferencePage.java new file mode 100644 index 00000000000..7c30caa718d --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Template Editor Example/org/eclipse/ui/examples/templateeditor/preferences/TemplatesPreferencePage.java @@ -0,0 +1,32 @@ +package org.eclipse.ui.examples.templateeditor.preferences; + +import org.eclipse.jface.preference.PreferencePage; + +import org.eclipse.ui.IWorkbenchPreferencePage; +import org.eclipse.ui.examples.templateeditor.editors.TemplateEditorUI; +import org.eclipse.ui.texteditor.templates.TemplatePreferencePage; + +/** + * @see PreferencePage + */ +public class TemplatesPreferencePage extends TemplatePreferencePage implements IWorkbenchPreferencePage { + + public TemplatesPreferencePage() { + setPreferenceStore(TemplateEditorUI.getDefault().getPreferenceStore()); + setTemplateStore(TemplateEditorUI.getDefault().getTemplateStore()); + setContextTypeRegistry(TemplateEditorUI.getDefault().getContextTypeRegistry()); + } + + protected boolean isShowFormatterSetting() { + return false; + } + + + public boolean performOk() { + boolean ok= super.performOk(); + + TemplateEditorUI.getDefault().savePluginPreferences(); + + return ok; + } +} 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 00000000000..a244aa3ac59 --- /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 00000000000..b691e581ed8 --- /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 00000000000..01a6484a776 --- /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()); + } + +} |