KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > snippeteditor > JavaSnippetCompletionProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.snippeteditor;
12
13  
14 import java.util.Arrays JavaDoc;
15
16 import org.eclipse.jdt.core.JavaModelException;
17 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
18 import org.eclipse.jdt.internal.ui.JavaPlugin;
19 import org.eclipse.jdt.internal.ui.text.java.JavaParameterListValidator;
20 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateEngine;
21 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal;
22 import org.eclipse.jdt.ui.text.java.CompletionProposalCollector;
23 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
24 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.contentassist.ICompletionProposal;
28 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
29 import org.eclipse.jface.text.contentassist.IContextInformation;
30 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
31 import org.eclipse.jface.text.templates.TemplateContextType;
32 import org.eclipse.swt.widgets.Shell;
33
34 /**
35  * Java snippet completion processor.
36  */

37 public class JavaSnippetCompletionProcessor implements IContentAssistProcessor {
38     
39     private CompletionProposalCollector fCollector;
40     private JavaSnippetEditor fEditor;
41     private IContextInformationValidator fValidator;
42     private TemplateEngine fTemplateEngine;
43     private CompletionProposalComparator fComparator;
44     private String JavaDoc fErrorMessage;
45     
46     private char[] fProposalAutoActivationSet;
47             
48     public JavaSnippetCompletionProcessor(JavaSnippetEditor editor) {
49         fEditor= editor;
50         TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType("java"); //$NON-NLS-1$
51
if (contextType != null) {
52             fTemplateEngine= new TemplateEngine(contextType);
53         }
54         
55         fComparator= new CompletionProposalComparator();
56     }
57     
58     /**
59      * @see IContentAssistProcessor#getErrorMessage()
60      */

61     public String JavaDoc getErrorMessage() {
62         return fErrorMessage;
63     }
64     
65     protected void setErrorMessage(String JavaDoc message) {
66         if (message != null && message.length() == 0) {
67             message = null;
68         }
69         fErrorMessage = message;
70     }
71
72     /**
73      * @see IContentAssistProcessor#getContextInformationValidator()
74      */

75     public IContextInformationValidator getContextInformationValidator() {
76         if (fValidator == null) {
77             fValidator= new JavaParameterListValidator();
78         }
79         return fValidator;
80     }
81
82     /**
83      * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
84      */

85     public char[] getContextInformationAutoActivationCharacters() {
86         return null;
87     }
88
89     /**
90      * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
91      */

92     public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
93         return null;
94     }
95     
96     /**
97      * @see IContentAssistProcessor#computeProposals(ITextViewer, int)
98      */

99     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int position) {
100         try {
101             setErrorMessage(null);
102             try {
103                 fCollector = new CompletionProposalCollector(fEditor.getJavaProject());
104                 fEditor.codeComplete(fCollector);
105             } catch (JavaModelException x) {
106                 Shell shell= viewer.getTextWidget().getShell();
107                 ErrorDialog.openError(shell, SnippetMessages.getString("CompletionProcessor.errorTitle"), SnippetMessages.getString("CompletionProcessor.errorMessage"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
108
JDIDebugUIPlugin.log(x);
109             }
110             
111             IJavaCompletionProposal[] results= fCollector.getJavaCompletionProposals();
112             
113             if (fTemplateEngine != null) {
114                 fTemplateEngine.reset();
115                 fTemplateEngine.complete(viewer, position, null);
116             
117                 TemplateProposal[] templateResults= fTemplateEngine.getResults();
118     
119                 // concatenate arrays
120
IJavaCompletionProposal[] total= new IJavaCompletionProposal[results.length + templateResults.length];
121                 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
122                 System.arraycopy(results, 0, total, templateResults.length, results.length);
123                 results= total;
124             }
125             return order(results);
126         } finally {
127             setErrorMessage(fCollector.getErrorMessage());
128             fCollector = null;
129         }
130     }
131     
132     /**
133      * Order the given proposals.
134      */

135     private ICompletionProposal[] order(IJavaCompletionProposal[] proposals) {
136         Arrays.sort(proposals, fComparator);
137         return proposals;
138     }
139     
140     /**
141      * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
142      */

143     public char[] getCompletionProposalAutoActivationCharacters() {
144         return fProposalAutoActivationSet;
145     }
146     
147     /**
148      * Sets this processor's set of characters triggering the activation of the
149      * completion proposal computation.
150      *
151      * @param activationSet the activation set
152      */

153     public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
154         fProposalAutoActivationSet= activationSet;
155     }
156     
157     /**
158      * Tells this processor to order the proposals alphabetically.
159      *
160      * @param order <code>true</code> if proposals should be ordered.
161      */

162     public void orderProposalsAlphabetically(boolean order) {
163         fComparator.setOrderAlphabetically(order);
164     }
165 }
166
Popular Tags