KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.codegen;
20
21 import com.sun.source.util.TreePath;
22 import java.awt.Point JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
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 /**
41  *
42  * @author Dusan Balek, Jan Lahoda
43  */

44 public class GenerateCodeAction extends BaseAction {
45
46     public static final String JavaDoc generateCode = "generate-code"; //NOI18N
47

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")); //NOI18N
59
putValue(SHORT_DESCRIPTION, NbBundle.getBundle(GenerateCodeAction.class).getString("desc-generate-code")); //NOI18N
60
putValue(POPUP_MENU_TEXT, NbBundle.getBundle(GenerateCodeAction.class).getString("popup-generate-code")); //NOI18N
61
}
62     
63     public void actionPerformed(ActionEvent JavaDoc evt, final JTextComponent JavaDoc target) {
64         try {
65             JavaSource js = JavaSource.forDocument(target.getDocument());
66             if (js != null) {
67                 final int caretOffset = target.getCaretPosition();
68                 final ArrayList JavaDoc<CodeGenerator> gens = new ArrayList JavaDoc<CodeGenerator>();
69                 js.runUserActionTask(new CancellableTask<CompilationController>() {
70                     public void cancel() {
71                     }
72                     public void run(CompilationController controller) throws Exception JavaDoc {
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 JavaDoc carretRectangle = target.modelToView(target.getCaretPosition());
83                     Point JavaDoc where = new Point JavaDoc( 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 JavaDoc ioe) {
92             Exceptions.printStackTrace(ioe);
93         } catch (BadLocationException JavaDoc ble) {
94             Exceptions.printStackTrace(ble);
95         }
96     }
97     
98     private CodeGenerator.Factory[] getCodeGeneratorFactories() {
99         return generators;
100     }
101 }
102
Popular Tags