001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.sun.ddloaders.multiview.ejb;
043:
044: import java.io.IOException;
045: import java.util.Map;
046: import org.netbeans.modules.j2ee.dd.api.ejb.EjbJarMetadata;
047: import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel;
048: import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelException;
049: import org.netbeans.modules.j2ee.sun.dd.api.ASDDVersion;
050: import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
051: import org.netbeans.modules.j2ee.sun.dd.api.RootInterface;
052: import org.netbeans.modules.j2ee.sun.dd.api.ejb.Ejb;
053: import org.netbeans.modules.j2ee.sun.dd.api.ejb.EnterpriseBeans;
054: import org.netbeans.modules.j2ee.sun.dd.api.ejb.SunEjbJar;
055: import org.netbeans.modules.j2ee.sun.ddloaders.multiview.common.DDBinding;
056: import org.netbeans.modules.j2ee.sun.ddloaders.multiview.common.NamedBeanGroupNode;
057: import org.netbeans.modules.xml.multiview.SectionNode;
058: import org.netbeans.modules.xml.multiview.ui.SectionNodeView;
059: import org.openide.ErrorManager;
060: import org.openide.util.NbBundle;
061:
062: /**
063: * @author Peter Williams
064: */
065: public class EjbGroupNode extends NamedBeanGroupNode {
066:
067: private SunEjbJar sunEjbJar;
068:
069: public EjbGroupNode(SectionNodeView sectionNodeView,
070: SunEjbJar sunEjbJar, ASDDVersion version) {
071: super (sectionNodeView, sunEjbJar, Ejb.EJB_NAME, Ejb.class,
072: NbBundle.getMessage(EjbGroupNode.class,
073: "LBL_EjbGroupHeader"), // NOI18N
074: ICON_EJB_GROUP_NODE, version);
075:
076: this .sunEjbJar = sunEjbJar;
077: enableAddAction(NbBundle.getMessage(EjbGroupNode.class,
078: "LBL_AddEjb")); // NOI18N
079: }
080:
081: protected SectionNode createNode(DDBinding binding) {
082: return new EjbNode(getSectionNodeView(), binding, version);
083: }
084:
085: protected CommonDDBean[] getBeansFromModel() {
086: EnterpriseBeans eb = sunEjbJar.getEnterpriseBeans();
087: if (eb != null) {
088: return eb.getEjb();
089: }
090: return null;
091: }
092:
093: protected CommonDDBean addNewBean() {
094: Ejb newEjb = (Ejb) createBean();
095: newEjb.setEjbName(getNewBeanId(PFX_EJB)); // NOI18N
096: return addBean(newEjb);
097: }
098:
099: protected CommonDDBean addBean(CommonDDBean newBean) {
100: EnterpriseBeans eb = sunEjbJar.getEnterpriseBeans();
101: if (eb == null) {
102: eb = sunEjbJar.newEnterpriseBeans();
103: sunEjbJar.setEnterpriseBeans(eb);
104: }
105: eb.addEjb((Ejb) newBean);
106: return newBean;
107: }
108:
109: protected void removeBean(CommonDDBean bean) {
110: EnterpriseBeans eb = sunEjbJar.getEnterpriseBeans();
111: if (eb != null) {
112: Ejb ejb = (Ejb) bean;
113: eb.removeEjb(ejb);
114: if (eb.isTrivial(null)) {
115: sunEjbJar.setEnterpriseBeans(null);
116: }
117: }
118: }
119:
120: /** EjbGroupNode usually gets events from <EnterpriseBeans> so we need
121: * custom event source matching.
122: */
123: @Override
124: protected boolean isEventSource(Object source) {
125: if (source != null
126: && (source == sunEjbJar.getEnterpriseBeans() || super
127: .isEventSource(source))) {
128: return true;
129: }
130: return false;
131: }
132:
133: // ------------------------------------------------------------------------
134: // DescriptorReader implementation
135: // ------------------------------------------------------------------------
136: @Override
137: public Map<String, Object> readDescriptor() {
138: Map<String, Object> resultMap = null;
139:
140: org.netbeans.modules.j2ee.dd.api.common.RootInterface stdRootDD = getStandardRootDD();
141: if (stdRootDD instanceof org.netbeans.modules.j2ee.dd.api.ejb.EjbJar) {
142: org.netbeans.modules.j2ee.dd.api.ejb.EjbJar ejbJar = (org.netbeans.modules.j2ee.dd.api.ejb.EjbJar) stdRootDD;
143: resultMap = EjbMetadataReader.readDescriptor(ejbJar);
144: }
145:
146: return resultMap;
147: }
148:
149: @Override
150: public Map<String, Object> readAnnotations() {
151: Map<String, Object> resultMap = null;
152:
153: try {
154: MetadataModel<EjbJarMetadata> ejbJarModel = getMetadataModel(EjbJarMetadata.class);
155: if (ejbJarModel != null) {
156: resultMap = ejbJarModel
157: .runReadAction(new EjbMetadataReader());
158: }
159: } catch (MetadataModelException ex) {
160: ErrorManager.getDefault().notify(
161: ErrorManager.INFORMATIONAL, ex);
162: } catch (IOException ex) {
163: ErrorManager.getDefault().notify(
164: ErrorManager.INFORMATIONAL, ex);
165: }
166:
167: return resultMap;
168: }
169:
170: // ------------------------------------------------------------------------
171: // BeanResolver interface implementation
172: // ------------------------------------------------------------------------
173: private volatile EnterpriseBeans ejbFactory = null;
174:
175: public CommonDDBean createBean() {
176: if (ejbFactory == null) {
177: ejbFactory = sunEjbJar.newEnterpriseBeans();
178: }
179: return ejbFactory.newEjb();
180: }
181:
182: public String getBeanName(CommonDDBean sunBean) {
183: return ((Ejb) sunBean).getEjbName();
184: }
185:
186: public void setBeanName(CommonDDBean sunBean, String newName) {
187: ((Ejb) sunBean).setEjbName(newName);
188: }
189:
190: public String getSunBeanNameProperty() {
191: return Ejb.EJB_NAME;
192: }
193:
194: public String getBeanName(
195: org.netbeans.modules.j2ee.dd.api.common.CommonDDBean standardBean) {
196: return ((org.netbeans.modules.j2ee.dd.api.ejb.Ejb) standardBean)
197: .getEjbName();
198: }
199:
200: public String getStandardBeanNameProperty() {
201: return STANDARD_EJB_NAME;
202: }
203: }
|