KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > contentassist > JavaDebugContentAssistProcessor


1 /*******************************************************************************
2  * Copyright (c) 2005 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.contentassist;
12
13 import java.util.Arrays JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jdt.core.IJavaProject;
17 import org.eclipse.jdt.core.IType;
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.ui.text.java.CompletionProposalComparator;
22 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.contentassist.ICompletionProposal;
26 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
27 import org.eclipse.jface.text.contentassist.IContextInformation;
28 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
29 import org.eclipse.jface.text.templates.TemplateContextType;
30
31 /**
32  * Completion processor for the Java debugger. This completion processor
33  * operates on a client provided context.
34  *
35  * @since 3.2
36  */

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

60     public String JavaDoc getErrorMessage() {
61         if (fErrorMessage != null) {
62             return fErrorMessage;
63         }
64         if (fCollector != null) {
65             return fCollector.getErrorMessage();
66         }
67         return null;
68     }
69     
70     /**
71      * Sets the error message for why completions could not be resolved.
72      * Clients should clear this before computing completions.
73      *
74      * @param string message
75      */

76     private void setErrorMessage(String JavaDoc string) {
77         if (string != null && string.length() == 0) {
78             string = null;
79         }
80         fErrorMessage = string;
81     }
82
83     /**
84      * @see IContentAssistProcessor#getContextInformationValidator()
85      */

86     public IContextInformationValidator getContextInformationValidator() {
87         if (fValidator == null) {
88             fValidator= new JavaParameterListValidator();
89         }
90         return fValidator;
91     }
92
93     /**
94      * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
95      */

96     public char[] getContextInformationAutoActivationCharacters() {
97         return null;
98     }
99
100     /**
101      * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
102      */

103     public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
104         return null;
105     }
106     
107     /**
108      * @see IContentAssistProcessor#computeProposals(ITextViewer, int)
109      */

110     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
111         setErrorMessage(null);
112         try {
113             IType type = fContext.getType();
114             IJavaProject project = type.getJavaProject();
115                 
116             String JavaDoc[][] locals = fContext.getLocalVariables();
117             int numLocals = 0;
118             if (locals.length > 0) {
119                 numLocals = locals[0].length;
120             }
121             char[][] localVariableNames = new char[numLocals][];
122             char[][] localVariableTypeNames = new char[numLocals][];
123             for (int i = 0; i < numLocals; i++) {
124                 localVariableNames[i] = locals[0][i].toCharArray();
125                 localVariableTypeNames[i] = locals[1][i].toCharArray();
126             }
127             
128             ITextSelection selection= (ITextSelection)viewer.getSelectionProvider().getSelection();
129             configureResultCollector(project, selection);
130             
131             int[] localModifiers= new int[localVariableNames.length];
132             Arrays.fill(localModifiers, 0);
133             
134             String JavaDoc snippet = viewer.getDocument().get();
135             char[] charSnippet = fContext.getSnippet(snippet).toCharArray();
136             type.codeComplete(charSnippet, fContext.getInsertionPosition(), documentOffset,
137                  localVariableTypeNames, localVariableNames,
138                  localModifiers, fContext.isStatic(), fCollector);
139             
140             IJavaCompletionProposal[] results= fCollector.getJavaCompletionProposals();
141             
142             if (fTemplateEngine != null) {
143                 fTemplateEngine.reset();
144                 fTemplateEngine.complete(viewer, documentOffset, null);
145                 IJavaCompletionProposal[] templateResults= fTemplateEngine.getResults();
146
147                 // concatenate arrays
148
IJavaCompletionProposal[] total= new IJavaCompletionProposal[results.length + templateResults.length];
149                 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
150                 System.arraycopy(results, 0, total, templateResults.length, results.length);
151                 results= total;
152             }
153              //Order here and not in result collector to make sure that the order
154
//applies to all proposals and not just those of the compilation unit.
155
return order(results);
156         } catch (CoreException x) {
157             setErrorMessage(x.getStatus().getMessage());
158         } finally {
159             releaseCollector();
160         }
161         
162         return null;
163     }
164     
165     /**
166      * Order the given proposals.
167      */

168     private IJavaCompletionProposal[] order(IJavaCompletionProposal[] proposals) {
169         Arrays.sort(proposals, fComparator);
170         return proposals;
171     }
172     
173     /**
174      * Configures the display result collection for the current code assist session
175      */

176     private void configureResultCollector(IJavaProject project, ITextSelection selection) {
177         fCollector = new JavaDebugCompletionProposalCollector(project);
178         if (selection.getLength() != 0) {
179             fCollector.setReplacementLength(selection.getLength());
180         }
181     }
182     
183     /**
184      * Tells this processor to order the proposals alphabetically.
185      *
186      * @param order <code>true</code> if proposals should be ordered.
187      */

188     public void orderProposalsAlphabetically(boolean order) {
189         fComparator.setOrderAlphabetically(order);
190     }
191     
192     /**
193      * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
194      */

195     public char[] getCompletionProposalAutoActivationCharacters() {
196         return fProposalAutoActivationSet;
197     }
198     
199     /**
200      * Sets this processor's set of characters triggering the activation of the
201      * completion proposal computation.
202      *
203      * @param activationSet the activation set
204      */

205     public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
206         fProposalAutoActivationSet= activationSet;
207     }
208     
209     /**
210      * Clears reference to result proposal collector.
211      */

212     private void releaseCollector() {
213         if (fCollector != null && fCollector.getErrorMessage().length() > 0 && fErrorMessage != null) {
214             setErrorMessage(fCollector.getErrorMessage());
215         }
216         fCollector = null;
217     }
218     
219 }
220
Popular Tags