1 19 20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.action; 21 22 23 import java.io.IOException ; 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 44 public class AddCmpFieldAction extends NodeAction { 45 46 private static final String NAME = NbBundle.getMessage(AddCmpFieldAction.class, "LBL_AddCmpFieldAction"); 47 48 public String 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 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 ioe) { 99 ErrorManager.getDefault().notify(ioe); 100 } 101 } 102 103 public static boolean addCmpField(FileObject fileObject, String 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 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 { 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 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 |