KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > codegen > ImplementOverrideMethodGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.codegen;
20
21 import com.sun.source.tree.ClassTree;
22 import com.sun.source.tree.Tree;
23 import com.sun.source.util.SourcePositions;
24 import com.sun.source.util.TreePath;
25 import java.awt.Dialog JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.LinkedHashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import javax.lang.model.element.Element;
33 import javax.lang.model.element.ElementKind;
34 import javax.lang.model.element.ExecutableElement;
35 import javax.lang.model.element.TypeElement;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import org.netbeans.api.java.source.CancellableTask;
38 import org.netbeans.api.java.source.CompilationController;
39 import org.netbeans.api.java.source.ElementHandle;
40 import org.netbeans.api.java.source.JavaSource;
41 import org.netbeans.api.java.source.WorkingCopy;
42 import org.netbeans.modules.editor.java.Utilities;
43 import org.netbeans.modules.java.editor.codegen.ui.ElementNode;
44 import org.netbeans.modules.java.editor.codegen.ui.ImplementOverridePanel;
45 import org.openide.DialogDescriptor;
46 import org.openide.DialogDisplayer;
47 import org.openide.util.Exceptions;
48 import org.openide.util.NbBundle;
49
50 /**
51  *
52  * @author Dusan Balek
53  */

54 public class ImplementOverrideMethodGenerator implements CodeGenerator {
55
56     public static class Factory implements CodeGenerator.Factory {
57         
58         Factory() {
59         }
60         
61         public Iterable JavaDoc<? extends CodeGenerator> create(CompilationController controller, TreePath path) throws IOException JavaDoc {
62             List JavaDoc<CodeGenerator> ret = new ArrayList JavaDoc<CodeGenerator>();
63             path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path);
64             if (path == null)
65                 return ret;
66             controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
67             TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path);
68             if (!typeElement.getKind().isClass())
69                 return Collections.emptySet();
70             Map JavaDoc<Element, List JavaDoc<ElementNode.Description>> map = new LinkedHashMap JavaDoc<Element, List JavaDoc<ElementNode.Description>>();
71             for (ExecutableElement method : GeneratorUtils.findUndefs(controller, typeElement)) {
72                 List JavaDoc<ElementNode.Description> descriptions = map.get(method.getEnclosingElement());
73                 if (descriptions == null) {
74                     descriptions = new ArrayList JavaDoc<ElementNode.Description>();
75                     map.put(method.getEnclosingElement(), descriptions);
76                 }
77                 descriptions.add(ElementNode.Description.create(method, null, true, false));
78             }
79             List JavaDoc<ElementNode.Description> implementDescriptions = new ArrayList JavaDoc<ElementNode.Description>();
80             for (Map.Entry JavaDoc<Element, List JavaDoc<ElementNode.Description>> entry : map.entrySet())
81                 implementDescriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false));
82             if (!implementDescriptions.isEmpty())
83                 ret.add(new ImplementOverrideMethodGenerator(ElementNode.Description.create(implementDescriptions), true));
84             map = new LinkedHashMap JavaDoc<Element, List JavaDoc<ElementNode.Description>>();
85             for (ExecutableElement method : GeneratorUtils.findOverridable(controller, typeElement)) {
86                 List JavaDoc<ElementNode.Description> descriptions = map.get(method.getEnclosingElement());
87                 if (descriptions == null) {
88                     descriptions = new ArrayList JavaDoc<ElementNode.Description>();
89                     map.put(method.getEnclosingElement(), descriptions);
90                 }
91                 descriptions.add(ElementNode.Description.create(method, null, true, false));
92             }
93             List JavaDoc<ElementNode.Description> overrideDescriptions = new ArrayList JavaDoc<ElementNode.Description>();
94             for (Map.Entry JavaDoc<Element, List JavaDoc<ElementNode.Description>> entry : map.entrySet())
95                 overrideDescriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false));
96             if (!overrideDescriptions.isEmpty())
97                 ret.add(new ImplementOverrideMethodGenerator(ElementNode.Description.create(overrideDescriptions), false));
98             return ret;
99         }
100     }
101     
102     private ElementNode.Description description;
103     private boolean isImplement;
104     
105     /** Creates a new instance of OverrideMethodGenerator */
106     private ImplementOverrideMethodGenerator(ElementNode.Description description, boolean isImplement) {
107         this.description = description;
108         this.isImplement = isImplement;
109     }
110
111     public String JavaDoc getDisplayName() {
112         return org.openide.util.NbBundle.getMessage(ImplementOverrideMethodGenerator.class, isImplement ? "LBL_implement_method" : "LBL_override_method"); //NOI18N
113
}
114
115     public void invoke(JTextComponent JavaDoc component) {
116         final ImplementOverridePanel panel = new ImplementOverridePanel(description, isImplement);
117         DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, NbBundle.getMessage(ConstructorGenerator.class, isImplement ? "LBL_generate_implement" : "LBL_generate_override")); //NOI18N
118
Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
119         dialog.setVisible(true);
120         if (dialogDescriptor.getValue() == DialogDescriptor.OK_OPTION) {
121             JavaSource js = JavaSource.forDocument(component.getDocument());
122             if (js != null) {
123                 try {
124                     final int caretOffset = component.getCaretPosition();
125                     js.runModificationTask(new CancellableTask<WorkingCopy>() {
126                         public void cancel() {
127                         }
128                         public void run(WorkingCopy copy) throws IOException JavaDoc {
129                             copy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
130                             TreePath path = copy.getTreeUtilities().pathFor(caretOffset);
131                             path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path);
132                             int idx = 0;
133                             SourcePositions sourcePositions = copy.getTrees().getSourcePositions();
134                             for (Tree tree : ((ClassTree)path.getLeaf()).getMembers()) {
135                                 if (sourcePositions.getStartPosition(path.getCompilationUnit(), tree) < caretOffset)
136                                     idx++;
137                                 else
138                                     break;
139                             }
140                             ArrayList JavaDoc<ExecutableElement> methodElements = new ArrayList JavaDoc<ExecutableElement>();
141                             for (ElementHandle<? extends Element> elementHandle : panel.getSelectedMethods())
142                                 methodElements.add((ExecutableElement)elementHandle.resolve(copy));
143                             if (isImplement)
144                                 GeneratorUtils.generateAbstractMethodImplementations(copy, path, methodElements, idx);
145                             else
146                                 GeneratorUtils.generateMethodOverrides(copy, path, methodElements, idx);
147                         }
148                     }).commit();
149                 } catch (IOException JavaDoc ex) {
150                     Exceptions.printStackTrace(ex);
151                 }
152             }
153         }
154     }
155 }
156
Popular Tags