1 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 ; 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.LinkedHashMap ; 31 import java.util.List ; 32 import java.util.Map ; 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 ; 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 59 public class GetterSetterGenerator implements CodeGenerator { 60 61 public static class Factory implements CodeGenerator.Factory { 62 63 Factory() { 64 } 65 66 public Iterable <? extends CodeGenerator> create(CompilationController controller, TreePath path) throws IOException { 67 List <CodeGenerator> ret = new ArrayList <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 <String , List <ExecutableElement>> methods = new HashMap <String , List <ExecutableElement>>(); 77 for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(typeElement))) { 78 List <ExecutableElement> l = methods.get(method.getSimpleName().toString()); 79 if (l == null) { 80 l = new ArrayList <ExecutableElement>(); 81 methods.put(method.getSimpleName().toString(), l); 82 } 83 l.add(method); 84 } 85 Map <Element, List <ElementNode.Description>> gDescriptions = new LinkedHashMap <Element, List <ElementNode.Description>>(); 86 Map <Element, List <ElementNode.Description>> sDescriptions = new LinkedHashMap <Element, List <ElementNode.Description>>(); 87 Map <Element, List <ElementNode.Description>> gsDescriptions = new LinkedHashMap <Element, List <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 <ElementNode.Description> descriptions = gDescriptions.get(variableElement.getEnclosingElement()); 94 if (descriptions == null) { 95 descriptions = new ArrayList <ElementNode.Description>(); 96 gDescriptions.put(variableElement.getEnclosingElement(), descriptions); 97 } 98 descriptions.add(description); 99 } 100 if (!hasSetter) { 101 List <ElementNode.Description> descriptions = sDescriptions.get(variableElement.getEnclosingElement()); 102 if (descriptions == null) { 103 descriptions = new ArrayList <ElementNode.Description>(); 104 sDescriptions.put(variableElement.getEnclosingElement(), descriptions); 105 } 106 descriptions.add(description); 107 } 108 if (!hasGetter && !hasSetter) { 109 List <ElementNode.Description> descriptions = gsDescriptions.get(variableElement.getEnclosingElement()); 110 if (descriptions == null) { 111 descriptions = new ArrayList <ElementNode.Description>(); 112 gsDescriptions.put(variableElement.getEnclosingElement(), descriptions); 113 } 114 descriptions.add(description); 115 } 116 } 117 if (!gDescriptions.isEmpty()) { 118 List <ElementNode.Description> descriptions = new ArrayList <ElementNode.Description>(); 119 for (Map.Entry <Element, List <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 <ElementNode.Description> descriptions = new ArrayList <ElementNode.Description>(); 126 for (Map.Entry <Element, List <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 <ElementNode.Description> descriptions = new ArrayList <ElementNode.Description>(); 133 for (Map.Entry <Element, List <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 146 private GetterSetterGenerator(ElementNode.Description description, int type) { 147 this.description = description; 148 this.type = type; 149 } 150 151 public String getDisplayName() { 152 if (type == GeneratorUtils.GETTERS_ONLY) 153 return org.openide.util.NbBundle.getMessage(GetterSetterGenerator.class, "LBL_getter"); if (type == GeneratorUtils.SETTERS_ONLY) 155 return org.openide.util.NbBundle.getMessage(GetterSetterGenerator.class, "LBL_setter"); return org.openide.util.NbBundle.getMessage(GetterSetterGenerator.class, "LBL_getter_and_setter"); } 158 159 public void invoke(JTextComponent component) { 160 final GetterSetterPanel panel = new GetterSetterPanel(description, type); 161 String title; 162 if (type == GeneratorUtils.GETTERS_ONLY) 163 title = NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_getter"); else if (type == GeneratorUtils.SETTERS_ONLY) 165 title = NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_setter"); else 167 title = NbBundle.getMessage(ConstructorGenerator.class, "LBL_generate_getter_and_setter"); DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, title); 169 Dialog 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 { 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 <VariableElement> variableElements = new ArrayList <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 ex) { 198 Exceptions.printStackTrace(ex); 199 } 200 } 201 } 202 } 203 } 204 | Popular Tags |