KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.internal.ui.text.correction;
12
13 import org.eclipse.core.runtime.Assert;
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.CompilationUnit;
20 import org.eclipse.jdt.core.dom.IBinding;
21 import org.eclipse.jdt.core.dom.ITypeBinding;
22 import org.eclipse.jdt.core.dom.Type;
23 import org.eclipse.jdt.core.dom.TypeDeclaration;
24 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
25 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
26 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
27
28 import org.eclipse.jdt.internal.corext.dom.Bindings;
29 import org.eclipse.jdt.internal.corext.util.Messages;
30
31 import org.eclipse.jdt.internal.ui.JavaPluginImages;
32
33 /**
34  *
35  */

36 public class ImplementInterfaceProposal extends LinkedCorrectionProposal {
37
38     private IBinding fBinding;
39     private CompilationUnit fAstRoot;
40     private ITypeBinding fNewInterface;
41
42     public ImplementInterfaceProposal(ICompilationUnit targetCU, ITypeBinding binding, CompilationUnit astRoot, ITypeBinding newInterface, int relevance) {
43         super("", targetCU, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)); //$NON-NLS-1$
44

45         Assert.isTrue(binding != null && Bindings.isDeclarationBinding(binding));
46
47         fBinding= binding;
48         fAstRoot= astRoot;
49         fNewInterface= newInterface;
50
51         String JavaDoc[] args= { binding.getName(), Bindings.getRawName(newInterface) };
52         setDisplayName(Messages.format(CorrectionMessages.ImplementInterfaceProposal_name, args));
53     }
54
55     protected ASTRewrite getRewrite() throws CoreException {
56         ASTNode boundNode= fAstRoot.findDeclaringNode(fBinding);
57         ASTNode declNode= null;
58         CompilationUnit newRoot= fAstRoot;
59         if (boundNode != null) {
60             declNode= boundNode; // is same CU
61
} else {
62             newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
63             declNode= newRoot.findDeclaringNode(fBinding.getKey());
64         }
65         ImportRewrite imports= createImportRewrite(newRoot);
66         
67         if (declNode instanceof TypeDeclaration) {
68             AST ast= declNode.getAST();
69             ASTRewrite rewrite= ASTRewrite.create(ast);
70
71             Type newInterface= imports.addImport(fNewInterface, ast);
72             ListRewrite listRewrite= rewrite.getListRewrite(declNode, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
73             listRewrite.insertLast(newInterface, null);
74
75             // set up linked mode
76
final String JavaDoc KEY_TYPE= "type"; //$NON-NLS-1$
77
addLinkedPosition(rewrite.track(newInterface), true, KEY_TYPE);
78                 return rewrite;
79         }
80         return null;
81     }
82
83
84 }
85
Popular Tags