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.javaeditor.java;
011:
012: import java.text.MessageFormat;
013:
014: import org.eclipse.jface.text.ITextViewer;
015: import org.eclipse.jface.text.TextPresentation;
016: import org.eclipse.jface.text.contentassist.*;
017:
018: /**
019: * Example Java completion processor.
020: */
021: public class JavaCompletionProcessor implements IContentAssistProcessor {
022:
023: /**
024: * Simple content assist tip closer. The tip is valid in a range
025: * of 5 characters around its popup location.
026: */
027: protected static class Validator implements
028: IContextInformationValidator, IContextInformationPresenter {
029:
030: protected int fInstallOffset;
031:
032: /*
033: * @see IContextInformationValidator#isContextInformationValid(int)
034: */
035: public boolean isContextInformationValid(int offset) {
036: return Math.abs(fInstallOffset - offset) < 5;
037: }
038:
039: /*
040: * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
041: */
042: public void install(IContextInformation info,
043: ITextViewer viewer, int offset) {
044: fInstallOffset = offset;
045: }
046:
047: /*
048: * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
049: */
050: public boolean updatePresentation(int documentPosition,
051: TextPresentation presentation) {
052: return false;
053: }
054: }
055:
056: protected final static String[] fgProposals = {
057: "abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "continue", "default", "do", "double", "else", "extends", "false", "final", "finally", "float", "for", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while" }; //$NON-NLS-48$ //$NON-NLS-47$ //$NON-NLS-46$ //$NON-NLS-45$ //$NON-NLS-44$ //$NON-NLS-43$ //$NON-NLS-42$ //$NON-NLS-41$ //$NON-NLS-40$ //$NON-NLS-39$ //$NON-NLS-38$ //$NON-NLS-37$ //$NON-NLS-36$ //$NON-NLS-35$ //$NON-NLS-34$ //$NON-NLS-33$ //$NON-NLS-32$ //$NON-NLS-31$ //$NON-NLS-30$ //$NON-NLS-29$ //$NON-NLS-28$ //$NON-NLS-27$ //$NON-NLS-26$ //$NON-NLS-25$ //$NON-NLS-24$ //$NON-NLS-23$ //$NON-NLS-22$ //$NON-NLS-21$ //$NON-NLS-20$ //$NON-NLS-19$ //$NON-NLS-18$ //$NON-NLS-17$ //$NON-NLS-16$ //$NON-NLS-15$ //$NON-NLS-14$ //$NON-NLS-13$ //$NON-NLS-12$ //$NON-NLS-11$ //$NON-NLS-10$ //$NON-NLS-9$ //$NON-NLS-8$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
058:
059: protected IContextInformationValidator fValidator = new Validator();
060:
061: /* (non-Javadoc)
062: * Method declared on IContentAssistProcessor
063: */
064: public ICompletionProposal[] computeCompletionProposals(
065: ITextViewer viewer, int documentOffset) {
066: ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
067: for (int i = 0; i < fgProposals.length; i++) {
068: IContextInformation info = new ContextInformation(
069: fgProposals[i],
070: MessageFormat
071: .format(
072: JavaEditorMessages
073: .getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
074: result[i] = new CompletionProposal(
075: fgProposals[i],
076: documentOffset,
077: 0,
078: fgProposals[i].length(),
079: null,
080: fgProposals[i],
081: info,
082: MessageFormat
083: .format(
084: JavaEditorMessages
085: .getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
086: }
087: return result;
088: }
089:
090: /* (non-Javadoc)
091: * Method declared on IContentAssistProcessor
092: */
093: public IContextInformation[] computeContextInformation(
094: ITextViewer viewer, int documentOffset) {
095: IContextInformation[] result = new IContextInformation[5];
096: for (int i = 0; i < result.length; i++)
097: result[i] = new ContextInformation(
098: MessageFormat
099: .format(
100: JavaEditorMessages
101: .getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset) }), //$NON-NLS-1$
102: MessageFormat
103: .format(
104: JavaEditorMessages
105: .getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5) })); //$NON-NLS-1$
106: return result;
107: }
108:
109: /* (non-Javadoc)
110: * Method declared on IContentAssistProcessor
111: */
112: public char[] getCompletionProposalAutoActivationCharacters() {
113: return new char[] { '.', '(' };
114: }
115:
116: /* (non-Javadoc)
117: * Method declared on IContentAssistProcessor
118: */
119: public char[] getContextInformationAutoActivationCharacters() {
120: return new char[] { '#' };
121: }
122:
123: /* (non-Javadoc)
124: * Method declared on IContentAssistProcessor
125: */
126: public IContextInformationValidator getContextInformationValidator() {
127: return fValidator;
128: }
129:
130: /* (non-Javadoc)
131: * Method declared on IContentAssistProcessor
132: */
133: public String getErrorMessage() {
134: return null;
135: }
136: }
|