KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > UnimplementedMethodsCompletionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.jdt.internal.ui.text.correction;
13
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.jdt.core.ICompilationUnit;
17 import org.eclipse.jdt.core.dom.AST;
18 import org.eclipse.jdt.core.dom.ASTNode;
19 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
20 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
21 import org.eclipse.jdt.core.dom.CompilationUnit;
22 import org.eclipse.jdt.core.dom.IMethodBinding;
23 import org.eclipse.jdt.core.dom.ITypeBinding;
24 import org.eclipse.jdt.core.dom.MethodDeclaration;
25 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
26 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
27 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
28 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
29
30 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
31 import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
32 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2;
33 import org.eclipse.jdt.internal.corext.util.Messages;
34
35 import org.eclipse.jdt.ui.JavaElementLabels;
36
37 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 import org.eclipse.jdt.internal.ui.JavaPluginImages;
39 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
40 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
41
42 public class UnimplementedMethodsCompletionProposal extends ASTRewriteCorrectionProposal {
43
44     private ASTNode fTypeNode;
45     private IMethodBinding[] fMethodsToOverride;
46
47     public UnimplementedMethodsCompletionProposal(ICompilationUnit cu, ASTNode typeNode, int relevance) {
48         super("", cu, null, relevance, null); //$NON-NLS-1$
49
setDisplayName(CorrectionMessages.UnimplementedMethodsCompletionProposal_description);
50         setImage(JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
51
52         fTypeNode= typeNode;
53         fMethodsToOverride= null;
54     }
55
56
57     /* (non-Javadoc)
58      * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
59      */

60     protected ASTRewrite getRewrite() throws CoreException {
61         ITypeBinding binding;
62         AST ast= fTypeNode.getAST();
63
64         ASTRewrite rewrite= ASTRewrite.create(ast);
65         ListRewrite listRewrite;
66         if (fTypeNode instanceof AnonymousClassDeclaration) {
67             AnonymousClassDeclaration decl= (AnonymousClassDeclaration) fTypeNode;
68             binding= decl.resolveBinding();
69             listRewrite= rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
70         } else {
71             AbstractTypeDeclaration decl= (AbstractTypeDeclaration) fTypeNode;
72             binding= decl.resolveBinding();
73             listRewrite= rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty());
74         }
75         IMethodBinding[] methods= StubUtility2.getUnimplementedMethods(binding);
76         fMethodsToOverride= methods;
77
78         CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(getCompilationUnit().getJavaProject());
79         if (binding.isAnonymous()) {
80             settings.createComments= false;
81         }
82         ImportRewrite imports= createImportRewrite((CompilationUnit) fTypeNode.getRoot());
83         ImportRewriteContext context= new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), imports);
84         for (int i= 0; i < methods.length; i++) {
85             MethodDeclaration newMethodDecl= StubUtility2.createImplementationStub(getCompilationUnit(), rewrite, imports, ast, methods[i], binding.getName(), settings, binding.isInterface(), context);
86             listRewrite.insertLast(newMethodDecl, null);
87         }
88         return rewrite;
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#getAdditionalProposalInfo()
93      */

94     public String JavaDoc getAdditionalProposalInfo() {
95         try {
96             getChange(); // force the creation of the rewrite
97
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
98             buf.append("<b>"); //$NON-NLS-1$
99
buf.append(Messages.format(CorrectionMessages.UnimplementedMethodsCompletionProposal_info, String.valueOf(fMethodsToOverride.length)));
100             buf.append("</b><ul>"); //$NON-NLS-1$
101
for (int i= 0; i < fMethodsToOverride.length; i++) {
102                 buf.append("<li>"); //$NON-NLS-1$
103
buf.append(BindingLabelProvider.getBindingLabel(fMethodsToOverride[i], JavaElementLabels.ALL_FULLY_QUALIFIED));
104                 buf.append("</li>"); //$NON-NLS-1$
105
}
106             buf.append("</ul>"); //$NON-NLS-1$
107
return buf.toString();
108         } catch (CoreException e) {
109             JavaPlugin.log(e);
110         }
111         return null;
112     }
113 }
114
Popular Tags