KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > ejb > action > AddCmpFieldAction


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
20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.action;
21
22
23 import java.io.IOException JavaDoc;
24 import javax.lang.model.element.TypeElement;
25 import org.netbeans.api.java.source.ElementHandle;
26 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
27 import org.netbeans.modules.j2ee.common.method.MethodModel;
28 import org.netbeans.modules.j2ee.common.method.FieldCustomizer;
29 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EjbMethodController;
30 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
31 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EntityMethodController;
32 import org.openide.ErrorManager;
33 import org.openide.filesystems.FileObject;
34 import org.openide.nodes.Node;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.Lookup;
37 import org.openide.util.NbBundle;
38 import org.openide.util.actions.NodeAction;
39
40 /**
41  * Action that can always be invoked and work procedurally.
42  * @author Chris Webster
43  */

44 public class AddCmpFieldAction extends NodeAction {
45
46     private static final String JavaDoc NAME = NbBundle.getMessage(AddCmpFieldAction.class, "LBL_AddCmpFieldAction");
47     
48     public String JavaDoc getName() {
49         return NAME;
50     }
51
52     public HelpCtx getHelpCtx() {
53         return HelpCtx.DEFAULT_HELP;
54     }
55
56     protected boolean asynchronous() {
57         return false;
58     }
59
60     protected boolean enable(org.openide.nodes.Node[] activatedNodes) {
61         if (activatedNodes == null || activatedNodes.length < 1) {
62             return false;
63         }
64         EjbMethodController ejbMethodController;
65         ElementHandle<TypeElement> elementHandle = null;
66         try {
67              elementHandle = _RetoucheUtil.getJavaClassFromNode(activatedNodes[0]);
68             if (elementHandle == null) {
69                 return false;
70             }
71         } catch (IOException JavaDoc ioe) {
72             ErrorManager.getDefault().notify(ioe);
73             return false;
74         }
75         FileObject fileObject = activatedNodes[0].getLookup().lookup(FileObject.class);
76         if (fileObject == null) {
77             return false;
78         }
79         return activatedNodes.length == 1 &&
80                (ejbMethodController = EjbMethodController.createFromClass(fileObject, elementHandle.getQualifiedName())) != null &&
81                ejbMethodController instanceof EntityMethodController &&
82                ((EntityMethodController) ejbMethodController).isCMP();
83     }
84
85     protected void performAction(org.openide.nodes.Node[] activatedNodes) {
86         if (activatedNodes == null || activatedNodes.length < 1) {
87             return;
88         }
89         try {
90             ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(activatedNodes[0]);
91             if (elementHandle != null) {
92                 FileObject fileObject = activatedNodes[0].getLookup().lookup(FileObject.class);
93                 FileObject ddFile = EjbJar.getEjbJar(fileObject).getDeploymentDescriptor();
94                 EntityMethodController emc = (EntityMethodController) EjbMethodController.createFromClass(fileObject, elementHandle.getQualifiedName());
95                 MethodModel.Variable field = MethodModel.Variable.create("java.lang.String", "cmpField");
96                 addCmpField(emc, ddFile, field);
97             }
98         } catch (IOException JavaDoc ioe) {
99             ErrorManager.getDefault().notify(ioe);
100         }
101     }
102
103     public static boolean addCmpField(FileObject fileObject, String JavaDoc className, FileObject ddFile) {
104         MethodModel.Variable field = MethodModel.Variable.create("java.lang.String", "cmpField");
105         EntityMethodController emc = (EntityMethodController) EntityMethodController.createFromClass(fileObject, className);
106         try {
107             return addCmpField(emc, ddFile, field);
108         } catch (IOException JavaDoc ioe) {
109             ErrorManager.getDefault().notify(ioe);
110         }
111         return false;
112     }
113
114     private static boolean addCmpField(EntityMethodController emc, FileObject ddFile, MethodModel.Variable field) throws IOException JavaDoc {
115         FieldCustomizer customizer = new FieldCustomizer(emc.getModelCopy(), field, "",
116                 emc.getLocal() != null, emc.getRemote() != null, true, true, false, false);
117         if (customizer.customizeField()) {
118             MethodModel.Variable customizedField = customizer.getField();
119             emc.addField(customizedField, ddFile, customizer.isLocalGetter(), customizer.isLocalSetter(),
120                     customizer.isRemoteGetter(), customizer.isRemoteSetter(), customizer.getDescription());
121             return true;
122         }
123         return false;
124     }
125
126     public javax.swing.Action JavaDoc createContextAwareInstance(org.openide.util.Lookup actionContext) {
127         boolean enable = enable(actionContext.lookup(new Lookup.Template<Node>(Node.class)).allInstances().toArray(new Node[0]));
128         return enable ? super.createContextAwareInstance(actionContext) : null;
129     }
130
131 }
132
Popular Tags