KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > CodeSnippetCompletionProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 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;
12
13 import org.eclipse.jdt.core.IJavaProject;
14 import org.eclipse.jdt.core.IType;
15 import org.eclipse.jdt.core.JavaModelException;
16 import org.eclipse.jdt.internal.debug.ui.display.DisplayCompletionProcessor;
17 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateEngine;
18 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal;
19 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
20 import org.eclipse.jface.text.ITextSelection;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.contentassist.ICompletionProposal;
23
24 public class CodeSnippetCompletionProcessor extends DisplayCompletionProcessor {
25     
26     public interface ITypeProvider {
27         IType getType();
28     }
29
30     /**
31      * The dialog with which this processor is associated.
32      */

33     private ITypeProvider fTypeProvider;
34         
35     public CodeSnippetCompletionProcessor(ITypeProvider detailFormatDialog) {
36         fTypeProvider= detailFormatDialog;
37     }
38
39     /**
40      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
41      */

42     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
43         try {
44             setErrorMessage(null);
45             IType receivingType= fTypeProvider.getType();
46             if (receivingType == null) {
47                 setErrorMessage(DebugUIMessages.CodeSnippetCompletionProcessor_0); //$NON-NLS-1$
48
return new ICompletionProposal[0];
49             }
50             
51             IJavaProject project= receivingType.getJavaProject();
52             try {
53                 // Generate selections from the compilation unit
54
ITextSelection textSelection= (ITextSelection)viewer.getSelectionProvider().getSelection();
55                 configureResultCollector(project, textSelection);
56                 receivingType.codeComplete(viewer.getDocument().get().toCharArray(), -1, documentOffset,
57                      new char[0][], new char[0][],
58                      new int[0], false, getCollector());
59                      
60                 IJavaCompletionProposal[] results= getCollector().getJavaCompletionProposals();
61                 
62                 // Generate selections from the template engine
63
TemplateEngine templateEngine= getTemplateEngine();
64                 if (templateEngine != null) {
65                     templateEngine.reset();
66                     templateEngine.complete(viewer, documentOffset, null);
67                     TemplateProposal[] templateResults= templateEngine.getResults();
68     
69                     // concatenate arrays
70
IJavaCompletionProposal[] total= new IJavaCompletionProposal[results.length + templateResults.length];
71                     System.arraycopy(templateResults, 0, total, 0, templateResults.length);
72                     System.arraycopy(results, 0, total, templateResults.length, results.length);
73                     results= total;
74                 }
75                  //Order here and not in result collector to make sure that the order
76
//applies to all proposals and not just those of the compilation unit.
77
return order(results);
78             } catch (JavaModelException x) {
79                 handle(viewer, x);
80             }
81             return null;
82         } finally {
83             releaseCollector();
84         }
85     }
86 }
87
Popular Tags