KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
30 import java.util.LinkedHashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import javax.lang.model.element.Element;
34 import javax.lang.model.element.ElementKind;
35 import javax.lang.model.element.ExecutableElement;
36 import javax.lang.model.element.Modifier;
37 import javax.lang.model.element.TypeElement;
38 import javax.lang.model.element.VariableElement;
39 import javax.lang.model.util.ElementFilter;
40 import javax.lang.model.util.Elements;
41 import javax.swing.text.JTextComponent JavaDoc;
42 import org.netbeans.api.java.source.CancellableTask;
43 import org.netbeans.api.java.source.CompilationController;
44 import org.netbeans.api.java.source.ElementHandle;
45 import org.netbeans.api.java.source.JavaSource;
46 import org.netbeans.api.java.source.WorkingCopy;
47 import org.netbeans.modules.editor.java.Utilities;
48 import org.netbeans.modules.java.editor.codegen.ui.ElementNode;
49 import org.netbeans.modules.java.editor.codegen.ui.GetterSetterPanel;
50 import org.openide.DialogDescriptor;
51 import org.openide.DialogDisplayer;
52 import org.openide.util.Exceptions;
53 import org.openide.util.NbBundle;
54
55 /**
56  *
57  * @author Dusan Balek
58  */

59 public class GetterSetterGenerator implements CodeGenerator {
60     
61     public static class Factory implements CodeGenerator.Factory {
62         
63         Factory() {
64         }
65
66         public Iterable JavaDoc<? extends CodeGenerator> create(CompilationController controller, TreePath path) throws IOException JavaDoc {
67             List JavaDoc<CodeGenerator> ret = new ArrayList JavaDoc<CodeGenerator>();
68             path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path);
69             if (path == null)
70                 return ret;
71             controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
72             Elements elements = controller.getElements();
73             TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path);
74             if (!typeElement.getKind().isClass())
75                 return ret;
76             Map JavaDoc<String JavaDoc, List JavaDoc<ExecutableElement>> methods = new HashMap JavaDoc<String JavaDoc, List JavaDoc<ExecutableElement>>();
77             for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(typeElement))) {
78                 List JavaDoc<ExecutableElement> l = methods.get(method.getSimpleName().toString());
79                 if (l == null) {
80                     l = new ArrayList JavaDoc<ExecutableElement>();
81                     methods.put(method.getSimpleName().toString(), l);
82                 }
83                 l.add(method);
84             }
85             Map JavaDoc<Element, List JavaDoc<ElementNode.Description>> gDescriptions = new LinkedHashMap JavaDoc<Element, List JavaDoc<ElementNode.Description>>();
86             Map JavaDoc<Element, List JavaDoc<ElementNode.Description>> sDescriptions = new LinkedHashMap JavaDoc<Element, List JavaDoc<ElementNode.Description>>();
87             Map JavaDoc<Element, List JavaDoc<ElementNode.Description>> gsDescriptions = new LinkedHashMap JavaDoc<Element, List JavaDoc<ElementNode.Description>>();
88             for (VariableElement variableElement : ElementFilter.fieldsIn(elements.getAllMembers(typeElement))) {
89                 ElementNode.Description description = ElementNode.Description.create(variableElement, null, true, false);
90                 boolean hasGetter = GeneratorUtils.hasGetter(controller, variableElement, methods);
91                 boolean hasSetter = variableElement.getModifiers().contains(Modifier.FINAL) || GeneratorUtils.hasSetter(controller, variableElement, methods);
92                 if (!hasGetter) {
93                     List JavaDoc<ElementNode.Description> descriptions = gDescriptions.get(variableElement.getEnclosingElement());
94                     if (descriptions == null) {
95                         descriptions = new ArrayList JavaDoc<ElementNode.Description>();
96                         gDescriptions.put(variableElement.getEnclosingElement(), descriptions);
97                     }
98                     descriptions.add(description);
99                 }
100                 if (!hasSetter) {
101                     List JavaDoc<ElementNode.Description> descriptions = sDescriptions.get(variableElement.getEnclosingElement());
102                     if (descriptions == null) {
103                         descriptions = new ArrayList JavaDoc<ElementNode.Description>();
104                         sDescriptions.put(variableElement.getEnclosingElement(), descriptions);
105                     }
106                     descriptions.add(description);
107                 }
108                 if (!hasGetter && !hasSetter) {
109                     List JavaDoc<ElementNode.Description> descriptions = gsDescriptions.get(variableElement.getEnclosingElement());
110                     if (descriptions == null) {
111                         descriptions = new ArrayList JavaDoc<ElementNode.Description>();
112                         gsDescriptions.put(variableElement.getEnclosingElement(), descriptions);
113                     }
114                     descriptions.add(description);
115                 }
116             }
117             if (!gDescriptions.isEmpty()) {
118                 List JavaDoc<ElementNode.Description> descriptions = new ArrayList JavaDoc<ElementNode.Description>();
119                 for (Map.Entry JavaDoc<Element, List JavaDoc<ElementNode.Description>> entry : gDescriptions.entrySet())
120                     descriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false));
121                 Collections.reverse(descriptions);
122                 ret.add(new GetterSetterGenerator(ElementNode.Description.create(typeElement, descriptions, false, false), GeneratorUtils.GETTERS_ONLY));
123             }
124             if (!sDescriptions.isEmpty()) {
125                 List JavaDoc<ElementNode.Description> descriptions = new ArrayList JavaDoc<ElementNode.Description>();
126                 for (Map.Entry JavaDoc<Element, List JavaDoc<ElementNode.Description>> entry : sDescriptions.entrySet())
127                     descriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false));
128                 Collections.reverse(descriptions);
129                 ret.add(new GetterSetterGenerator(ElementNode.Description.create(typeElement, descriptions, false, false), GeneratorUtils.SETTERS_ONLY));
130             }
131             if (!gsDescriptions.isEmpty()) {
132                 List JavaDoc<ElementNode.Description> descriptions = new ArrayList JavaDoc<ElementNode.Description>();
133                 for (Map.Entry JavaDoc<Element, List JavaDoc<ElementNode.Description>> entry : gsDescriptions.entrySet())
134                     descriptions.add(ElementNode.Description.create(entry.getKey(), entry.getValue(), false, false));
135                 Collections.reverse(descriptions);
136                 ret.add(new GetterSetterGenerator(ElementNode.Description.create(typeElement, descriptions, false, false), 0));
137             }
138             return ret;
139         }
140     }
141
142     private ElementNode.Description description;
143     private int type;
144
145     /** Creates a new instance of GetterSetterGenerator */
146     private GetterSetterGenerator(ElementNode.Description description, int type) {
147         this.description = description;
148         this.type = type;
149     }
150
151     public String JavaDoc getDisplayName() {
152         if (type == GeneratorUtils.GETTERS_ONLY)
153             return org.openide.util.NbBundle.getMessage(GetterSetterGenerator.class, "LBL_getter"); //NOI18N
154
if (type == GeneratorUtils.SETTERS_ONLY)
155             return org.openide.util.NbBundle.getMessage(GetterSetterGenerator.class, "LBL_setter"); //NOI18N
156
return org.openide.util.NbBundle.getMessage(GetterSetterGenerator.class, "LBL_getter_and_setter"); //NOI18N
157
}
158
159     public void invoke(JTextComponent JavaDoc component) {
160         final GetterSetterPanel panel = new GetterSetterPanel(description, type);
161         String JavaDoc title;
162         if (type == GeneratorUtils.GETTERS_ONLY)
163             title = NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_getter"); //NOI18N
164
else if (type == GeneratorUtils.SETTERS_ONLY)
165             title = NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_setter"); //NOI18N
166
else
167             title = NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_getter_and_setter"); //NOI18N
168
DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, title);
169         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
170         dialog.setVisible(true);
171         if (dialogDescriptor.getValue() == DialogDescriptor.OK_OPTION) {
172             JavaSource js = JavaSource.forDocument(component.getDocument());
173             if (js != null) {
174                 try {
175                     final int caretOffset = component.getCaretPosition();
176                     js.runModificationTask(new CancellableTask<WorkingCopy>() {
177                         public void cancel() {
178                         }
179                         public void run(WorkingCopy copy) throws IOException JavaDoc {
180                             copy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
181                             TreePath path = copy.getTreeUtilities().pathFor(caretOffset);
182                             path = Utilities.getPathElementOfKind(Tree.Kind.CLASS, path);
183                             int idx = 0;
184                             SourcePositions sourcePositions = copy.getTrees().getSourcePositions();
185                             for (Tree tree : ((ClassTree)path.getLeaf()).getMembers()) {
186                                 if (sourcePositions.getStartPosition(path.getCompilationUnit(), tree) < caretOffset)
187                                     idx++;
188                                 else
189                                     break;
190                             }
191                             ArrayList JavaDoc<VariableElement> variableElements = new ArrayList JavaDoc<VariableElement>();
192                             for (ElementHandle<? extends Element> elementHandle : panel.getVariables())
193                                 variableElements.add((VariableElement)elementHandle.resolve(copy));
194                             GeneratorUtils.generateGettersAndSetters(copy, path, variableElements, type, idx);
195                         }
196                     }).commit();
197                 } catch (IOException JavaDoc ex) {
198                     Exceptions.printStackTrace(ex);
199                 }
200             }
201         }
202     }
203 }
204
Popular Tags