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.LinkedHashMap ; 30 import java.util.List ; 31 import java.util.Map ; 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 ; 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 54 public class ImplementOverrideMethodGenerator implements CodeGenerator { 55 56 public static class Factory implements CodeGenerator.Factory { 57 58 Factory() { 59 } 60 61 public Iterable <? extends CodeGenerator> create(CompilationController controller, TreePath path) throws IOException { 62 List <CodeGenerator> ret = new ArrayList <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 <Element, List <ElementNode.Description>> map = new LinkedHashMap <Element, List <ElementNode.Description>>(); 71 for (ExecutableElement method : GeneratorUtils.findUndefs(controller, typeElement)) { 72 List <ElementNode.Description> descriptions = map.get(method.getEnclosingElement()); 73 if (descriptions == null) { 74 descriptions = new ArrayList <ElementNode.Description>(); 75 map.put(method.getEnclosingElement(), descriptions); 76 } 77 descriptions.add(ElementNode.Description.create(method, null, true, false)); 78 } 79 List <ElementNode.Description> implementDescriptions = new ArrayList <ElementNode.Description>(); 80 for (Map.Entry <Element, List <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 <Element, List <ElementNode.Description>>(); 85 for (ExecutableElement method : GeneratorUtils.findOverridable(controller, typeElement)) { 86 List <ElementNode.Description> descriptions = map.get(method.getEnclosingElement()); 87 if (descriptions == null) { 88 descriptions = new ArrayList <ElementNode.Description>(); 89 map.put(method.getEnclosingElement(), descriptions); 90 } 91 descriptions.add(ElementNode.Description.create(method, null, true, false)); 92 } 93 List <ElementNode.Description> overrideDescriptions = new ArrayList <ElementNode.Description>(); 94 for (Map.Entry <Element, List <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 106 private ImplementOverrideMethodGenerator(ElementNode.Description description, boolean isImplement) { 107 this.description = description; 108 this.isImplement = isImplement; 109 } 110 111 public String getDisplayName() { 112 return org.openide.util.NbBundle.getMessage(ImplementOverrideMethodGenerator.class, isImplement ? "LBL_implement_method" : "LBL_override_method"); } 114 115 public void invoke(JTextComponent 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")); Dialog 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 { 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 <ExecutableElement> methodElements = new ArrayList <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 ex) { 150 Exceptions.printStackTrace(ex); 151 } 152 } 153 } 154 } 155 } 156 | Popular Tags |