KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedHashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set 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.lang.model.element.VariableElement;
37 import javax.lang.model.type.DeclaredType;
38 import javax.lang.model.util.ElementFilter;
39 import javax.swing.text.JTextComponent JavaDoc;
40 import org.netbeans.api.java.source.CancellableTask;
41 import org.netbeans.api.java.source.CompilationController;
42 import org.netbeans.api.java.source.ElementHandle;
43 import org.netbeans.api.java.source.JavaSource;
44 import org.netbeans.api.java.source.WorkingCopy;
45 import org.netbeans.modules.editor.java.Utilities;
46 import org.netbeans.modules.java.editor.codegen.ui.ConstructorPanel;
47 import org.netbeans.modules.java.editor.codegen.ui.ElementNode;
48 import org.openide.DialogDescriptor;
49 import org.openide.DialogDisplayer;
50 import org.openide.util.Exceptions;
51 import org.openide.util.NbBundle;
52
53 /**
54  *
55  * @author Dusan Balek
56  */

57 public class ConstructorGenerator implements CodeGenerator {
58
59     public static class Factory implements CodeGenerator.Factory {
60         
61         Factory() {
62         }
63         
64         public Iterable JavaDoc<? extends CodeGenerator> create(CompilationController controller, TreePath path) throws IOException JavaDoc {
65             path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path);
66             if (path == null)
67                 return Collections.emptySet();
68             controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
69             TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path);
70             if (!typeElement.getKind().isClass())
71                 return Collections.emptySet();
72             final Set JavaDoc<VariableElement> initializedFields = new LinkedHashSet JavaDoc<VariableElement>();
73             final Set JavaDoc<VariableElement> uninitializedFields = new LinkedHashSet JavaDoc<VariableElement>();
74             final List JavaDoc<ExecutableElement> constructors = new ArrayList JavaDoc<ExecutableElement>();
75             final List JavaDoc<ExecutableElement> inheritedConstructors = new ArrayList JavaDoc<ExecutableElement>();
76             TypeElement superClass = (TypeElement)((DeclaredType)typeElement.getSuperclass()).asElement();
77             for (ExecutableElement executableElement : ElementFilter.constructorsIn(superClass.getEnclosedElements())) {
78                 inheritedConstructors.add(executableElement);
79             }
80             GeneratorUtils.scanForFieldsAndConstructors(controller, path, initializedFields, uninitializedFields, constructors);
81             ElementHandle<? extends Element> constructorHandle = null;
82             ElementNode.Description constructorDescription = null;
83             if (typeElement.getKind() != ElementKind.ENUM && inheritedConstructors.size() == 1) {
84                 constructorHandle = ElementHandle.create(inheritedConstructors.get(0));
85             } else if (inheritedConstructors.size() > 1) {
86                 List JavaDoc<ElementNode.Description> constructorDescriptions = new ArrayList JavaDoc<ElementNode.Description>();
87                 for (ExecutableElement constructorElement : inheritedConstructors)
88                     constructorDescriptions.add(ElementNode.Description.create(constructorElement, null, true, false));
89                 constructorDescription = ElementNode.Description.create(superClass, constructorDescriptions, false, false);
90             }
91             ElementNode.Description fieldsDescription = null;
92             if (!uninitializedFields.isEmpty()) {
93                 List JavaDoc<ElementNode.Description> fieldDescriptions = new ArrayList JavaDoc<ElementNode.Description>();
94                 for (VariableElement variableElement : uninitializedFields)
95                     fieldDescriptions.add(ElementNode.Description.create(variableElement, null, true, false));
96                 fieldsDescription = ElementNode.Description.create(typeElement, fieldDescriptions, false, false);
97             }
98             if (constructorHandle == null && constructorDescription == null && fieldsDescription == null)
99                 return Collections.emptySet();
100             return Collections.singleton(new ConstructorGenerator(constructorHandle, constructorDescription, fieldsDescription));
101         }
102     }
103
104     private ElementHandle<? extends Element> constructorHandle;
105     private ElementNode.Description constructorDescription;
106     private ElementNode.Description fieldsDescription;
107     
108     /** Creates a new instance of ConstructorGenerator */
109     private ConstructorGenerator(ElementHandle<? extends Element> constructorHandle, ElementNode.Description constructorDescription, ElementNode.Description fieldsDescription) {
110         this.constructorHandle = constructorHandle;
111         this.constructorDescription = constructorDescription;
112         this.fieldsDescription = fieldsDescription;
113     }
114
115     public String JavaDoc getDisplayName() {
116         return org.openide.util.NbBundle.getMessage(ConstructorGenerator.class, "LBL_constructor"); //NOI18N
117
}
118
119     public void invoke(JTextComponent JavaDoc component) {
120         final List JavaDoc<ElementHandle<? extends Element>> fieldHandles;
121         if (constructorDescription != null || fieldsDescription != null) {
122             ConstructorPanel panel = new ConstructorPanel(constructorDescription, fieldsDescription);
123             DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_constructor")); //NOI18N
124
Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
125             dialog.setVisible(true);
126             if (dialogDescriptor.getValue() != DialogDescriptor.OK_OPTION)
127                 return;
128             if (constructorHandle == null)
129                 constructorHandle = panel.getInheritedConstructor();
130             fieldHandles = panel.getVariablesToInitialize();
131         } else {
132             fieldHandles = null;
133         }
134         JavaSource js = JavaSource.forDocument(component.getDocument());
135         if (js != null) {
136             try {
137                 final int caretOffset = component.getCaretPosition();
138                 js.runModificationTask(new CancellableTask<WorkingCopy>() {
139                     public void cancel() {
140                     }
141                     public void run(WorkingCopy copy) throws IOException JavaDoc {
142                         copy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
143                         TreePath path = copy.getTreeUtilities().pathFor(caretOffset);
144                         path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path);
145                         int idx = 0;
146                         SourcePositions sourcePositions = copy.getTrees().getSourcePositions();
147                         for (Tree tree : ((ClassTree)path.getLeaf()).getMembers()) {
148                             if (sourcePositions.getStartPosition(path.getCompilationUnit(), tree) < caretOffset)
149                                 idx++;
150                             else
151                                 break;
152                         }
153                         ArrayList JavaDoc<VariableElement> variableElements = new ArrayList JavaDoc<VariableElement>();
154                         if (fieldHandles != null) {
155                             for (ElementHandle<? extends Element> elementHandle : fieldHandles)
156                                 variableElements.add((VariableElement)elementHandle.resolve(copy));
157                         }
158                         GeneratorUtils.generateConstructor(copy, path, variableElements, constructorHandle != null ? (ExecutableElement)constructorHandle.resolve(copy) : null, idx);
159                     }
160                 }).commit();
161             } catch (IOException JavaDoc ex) {
162                 Exceptions.printStackTrace(ex);
163             }
164         }
165     }
166 }
167
Popular Tags