]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/java/AbstractTemplateCompletionProposalComputer.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / java / AbstractTemplateCompletionProposalComputer.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 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.ui.text.java;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.jface.text.contentassist.ICompletionProposal;
21 import org.eclipse.jface.text.contentassist.IContextInformation;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24
25 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
26 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
27 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
28 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
29
30 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateEngine;
31 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal;
32
33
34 /**
35  * An template completion proposal computer can generate template completion proposals
36  * from a given TemplateEngine.
37  *
38  * Subclasses must implement {@link #computeCompletionEngine(JavaContentAssistInvocationContext)}
39  *
40  * @since 3.4
41  */
42 public abstract class AbstractTemplateCompletionProposalComputer implements IJavaCompletionProposalComputer {
43
44         /**
45          * The engine for the current session, if any
46          */
47         private TemplateEngine fEngine;
48
49         /*
50          * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
51          */
52         public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
53                 if (!(context instanceof JavaContentAssistInvocationContext))
54                         return Collections.emptyList();
55
56                 JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
57                 ICompilationUnit unit= javaContext.getCompilationUnit();
58                 if (unit == null)
59                         return Collections.emptyList();
60
61                 fEngine= computeCompletionEngine(javaContext);
62                 if (fEngine == null)
63                         return Collections.emptyList();
64
65                 fEngine.reset();
66                 fEngine.complete(javaContext.getViewer(), javaContext.getInvocationOffset(), unit);
67
68                 TemplateProposal[] templateProposals= fEngine.getResults();
69                 List<ICompletionProposal> result= new ArrayList<ICompletionProposal>(Arrays.asList(templateProposals));
70
71                 IJavaCompletionProposal[] keyWordResults= javaContext.getKeywordProposals();
72                 if (keyWordResults.length == 0)
73                         return result;
74
75                 /* Update relevance of template proposals that match with a keyword
76                  * give those templates slightly more relevance than the keyword to
77                  * sort them first.
78                  */
79                 for (int k= 0; k < templateProposals.length; k++) {
80                         TemplateProposal curr= templateProposals[k];
81                         curr.generated_5688875060909217458(keyWordResults);
82                 }
83                 return result;
84         }
85
86         /**
87          * Compute the engine used to retrieve completion proposals in the given context
88          *
89          * @param context the context where proposals will be made
90          * @return the engine or <code>null</code> if no engine available in the context
91          */
92         protected abstract TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context);
93
94         /*
95          * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
96          */
97         public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
98                 return Collections.emptyList();
99         }
100
101         /*
102          * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
103          */
104         public String getErrorMessage() {
105                 return null;
106         }
107
108         /*
109          * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
110          */
111         public void sessionStarted() {
112         }
113
114         /* (non-Javadoc)
115          * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
116          */
117         public void sessionEnded() {
118                 if (fEngine != null) {
119                         fEngine.reset();
120                         fEngine= null;
121                 }
122         }
123
124 }