1 19 package org.netbeans.modules.java.editor.codegen; 20 21 import com.sun.source.util.TreePath; 22 import java.awt.Point ; 23 import java.awt.Rectangle ; 24 import java.awt.event.ActionEvent ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import javax.swing.SwingUtilities ; 28 import javax.swing.text.BadLocationException ; 29 import javax.swing.text.JTextComponent ; 30 import org.netbeans.api.java.source.CancellableTask; 31 import org.netbeans.api.java.source.CompilationController; 32 import org.netbeans.api.java.source.JavaSource; 33 import org.netbeans.editor.BaseAction; 34 import org.netbeans.editor.ext.ExtKit; 35 import org.netbeans.modules.java.editor.codegen.ui.GenerateCodePanel; 36 import org.netbeans.modules.java.editor.overridden.PopupUtil; 37 import org.openide.util.Exceptions; 38 import org.openide.util.NbBundle; 39 40 44 public class GenerateCodeAction extends BaseAction { 45 46 public static final String generateCode = "generate-code"; 48 private CodeGenerator.Factory[] generators = new CodeGenerator.Factory[] { 49 new ConstructorGenerator.Factory(), 50 new GetterSetterGenerator.Factory(), 51 new EqualsHashCodeGenerator.Factory(), 52 new DelegateMethodGenerator.Factory(), 53 new ImplementOverrideMethodGenerator.Factory() 54 }; 55 56 public GenerateCodeAction(){ 57 super(generateCode); 58 putValue(ExtKit.TRIMMED_TEXT, NbBundle.getBundle(GenerateCodeAction.class).getString("generate-code-trimmed")); putValue(SHORT_DESCRIPTION, NbBundle.getBundle(GenerateCodeAction.class).getString("desc-generate-code")); putValue(POPUP_MENU_TEXT, NbBundle.getBundle(GenerateCodeAction.class).getString("popup-generate-code")); } 62 63 public void actionPerformed(ActionEvent evt, final JTextComponent target) { 64 try { 65 JavaSource js = JavaSource.forDocument(target.getDocument()); 66 if (js != null) { 67 final int caretOffset = target.getCaretPosition(); 68 final ArrayList <CodeGenerator> gens = new ArrayList <CodeGenerator>(); 69 js.runUserActionTask(new CancellableTask<CompilationController>() { 70 public void cancel() { 71 } 72 public void run(CompilationController controller) throws Exception { 73 controller.toPhase(JavaSource.Phase.PARSED); 74 TreePath path = controller.getTreeUtilities().pathFor(caretOffset); 75 for (CodeGenerator.Factory factory : getCodeGeneratorFactories()) { 76 for (CodeGenerator gen : factory.create(controller, path)) 77 gens.add(gen); 78 } 79 } 80 }, true); 81 if (gens.size() > 0) { 82 Rectangle carretRectangle = target.modelToView(target.getCaretPosition()); 83 Point where = new Point ( carretRectangle.x, carretRectangle.y + carretRectangle.height ); 84 SwingUtilities.convertPointToScreen( where, target); 85 GenerateCodePanel panel = new GenerateCodePanel(target, gens); 86 PopupUtil.showPopup(panel, null, where.x, where.y, true, carretRectangle.height); 87 } else { 88 target.getToolkit().beep(); 89 } 90 } 91 } catch (IOException ioe) { 92 Exceptions.printStackTrace(ioe); 93 } catch (BadLocationException ble) { 94 Exceptions.printStackTrace(ble); 95 } 96 } 97 98 private CodeGenerator.Factory[] getCodeGeneratorFactories() { 99 return generators; 100 } 101 } 102 | Popular Tags |