KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > entres > CallEjbPanel


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.entres;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.event.KeyAdapter JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.lang.model.element.TypeElement;
32 import javax.swing.UIManager JavaDoc;
33 import javax.swing.border.EtchedBorder JavaDoc;
34 import javax.swing.event.DocumentEvent JavaDoc;
35 import javax.swing.event.DocumentListener JavaDoc;
36 import org.netbeans.api.java.project.JavaProjectConstants;
37 import org.netbeans.api.java.source.CompilationController;
38 import org.netbeans.api.java.source.ElementHandle;
39 import org.netbeans.api.java.source.JavaSource;
40 import org.netbeans.api.project.FileOwnerQuery;
41 import org.netbeans.api.project.Project;
42 import org.netbeans.api.project.ant.AntArtifactQuery;
43 import org.netbeans.modules.j2ee.api.ejbjar.EjbReference;
44 import org.netbeans.modules.j2ee.common.source.AbstractTask;
45 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef;
46 import org.netbeans.modules.j2ee.dd.api.common.EjbRef;
47 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
48 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
49 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
50 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
51 import org.netbeans.modules.j2ee.ejbcore.Utils;
52 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
53 import org.openide.ErrorManager;
54 import org.openide.filesystems.FileObject;
55 import org.openide.loaders.DataObject;
56 import org.openide.nodes.Node;
57 import org.openide.nodes.NodeAcceptor;
58 import org.openide.util.NbBundle;
59
60
61 /**
62  *
63  * @author Chris Webster
64  * @author Martin Adamek
65  */

66 public class CallEjbPanel extends javax.swing.JPanel JavaDoc {
67     
68     public static final String JavaDoc IS_VALID = "CallEjbPanel_isValid"; //NOI18N
69

70     private final Set JavaDoc<String JavaDoc> refNameSet;
71     private final NodeDisplayPanel nodeDisplayPanel;
72     private final ServiceLocatorStrategyPanel slPanel;
73     private final NodeAcceptor nodeAcceptor;
74     private final Project project;
75     private final String JavaDoc className;
76     private final FileObject srcFile;
77     
78     /** Creates new form CallEjbPanel */
79     public CallEjbPanel(FileObject fileObject, Node rootNode, String JavaDoc lastLocator, String JavaDoc className) throws IOException JavaDoc {
80         initComponents();
81         this.srcFile= fileObject;
82         this.project = FileOwnerQuery.getOwner(srcFile);
83         this.className = className;
84         this.nodeAcceptor = new NodeAcceptorImpl();
85         this.refNameSet = new HashSet JavaDoc<String JavaDoc>();
86         
87         // This is working only for EJB project. Will need some enhancement in EnterpriseReferenceContainer API?
88
org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(srcFile);
89         if (ejbModule != null) {
90             EjbJar ejbJar = null;
91             try {
92                 ejbJar = DDProvider.getDefault().getMergedDDRoot(ejbModule.getMetadataUnit());
93             } catch (IOException JavaDoc e) {
94                 ErrorManager.getDefault().notify(e);
95             }
96             if (ejbJar != null) {
97                 final EnterpriseBeans enterpriseBeans = ejbJar.getEnterpriseBeans();
98                 JavaSource javaSource = JavaSource.forFileObject(srcFile);
99                 if (enterpriseBeans != null && javaSource != null) {
100                     javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
101                         public void run(CompilationController controller) throws IOException JavaDoc {
102                             controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
103                             TypeElement beanClass = controller.getElements().getTypeElement(CallEjbPanel.this.className);
104                             for (Ejb ejb : enterpriseBeans.getEjbs()) {
105                                 if (beanClass.getQualifiedName().contentEquals(ejb.getEjbClass())) {
106                                     EjbRef[] ejbRefs = ejb.getEjbRef();
107                                     EjbLocalRef[] ejbLocalRefs = ejb.getEjbLocalRef();
108                                     for (int j = 0; j < ejbRefs.length; j++) {
109                                         refNameSet.add(ejbRefs[j].getEjbRefName());
110                                     }
111                                     for (int j = 0; j < ejbLocalRefs.length; j++) {
112                                         refNameSet.add(ejbLocalRefs[j].getEjbRefName());
113                                     }
114                                     return;
115                                 }
116                             }
117                         }
118                     }, true);
119                 }
120             }
121         }
122         setErrorFieldColor(true);
123         nodeDisplayPanel = new NodeDisplayPanel(rootNode);
124         nodeDisplayPanel.setBorder(new EtchedBorder JavaDoc());
125         displayPanel.add(nodeDisplayPanel);
126         nodeDisplayPanel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
127             public void propertyChange(PropertyChangeEvent JavaDoc pce) {
128                 Node[] nodes = nodeDisplayPanel.getSelectedNodes();
129                 if (nodes.length == 0) {
130                     return;
131                 }
132                 EjbReference ejbReference = (EjbReference) nodes[0].getCookie(EjbReference.class);
133                 if (ejbReference != null) {
134                     try {
135                         generateName(ejbReference, remoteRadioButton.isSelected(), nodes[0]);
136                     } catch (IOException JavaDoc ioe) {
137                         ErrorManager.getDefault().notify(ioe);
138                     }
139                 }
140                 validateReferences();
141             }
142         });
143         referenceNameTextField.addKeyListener(new KeyAdapter JavaDoc() {
144             public void keyReleased(KeyEvent JavaDoc keyEvent) {
145                 validateReferences();
146             }
147         });
148         
149         slPanel = new ServiceLocatorStrategyPanel(lastLocator);
150         slPanel.getUnreferencedServiceLocator().addItemListener(new java.awt.event.ItemListener JavaDoc() {
151             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
152                 validateReferences();
153             }
154         });
155         slPanel.getClassName().getDocument().addDocumentListener(new DocumentListener JavaDoc() {
156             public void insertUpdate(DocumentEvent JavaDoc documentEvent) {
157                 validateReferences();
158             }
159             public void removeUpdate(DocumentEvent JavaDoc documentEvent) {
160                 validateReferences();
161             }
162             public void changedUpdate(DocumentEvent JavaDoc documentEvent) {
163                 validateReferences();
164             }
165         });
166         serviceLocatorPanel.add(slPanel, BorderLayout.CENTER);
167         validateReferences();
168     }
169     
170     public void disableServiceLocator() {
171         serviceLocatorPanel.setVisible(false);
172     }
173     
174     private void setErrorFieldColor(boolean error){
175         if (error){
176             Color JavaDoc color = UIManager.getColor("nb.errorForeground"); //NOI18N
177
errorField.setForeground(color == null ? new Color JavaDoc(89, 79, 191) : color);
178         } else {
179             Color JavaDoc color = UIManager.getColor("nb.warningForeground"); //NOI18N
180
errorField.setForeground(color == null ? Color.DARK_GRAY : color);
181         }
182     }
183     
184     private void setErrorMessage(String JavaDoc message) {
185         setErrorFieldColor(true);
186         setMessage(message);
187     }
188     
189     private void setWarningMessage(String JavaDoc message){
190         setErrorFieldColor(false);
191         setMessage(message);
192     }
193     
194     private void setMessage(String JavaDoc message){
195         String JavaDoc messageCopy = (message == null ? " " : message);
196         errorField.setText(messageCopy);
197         errorField.setToolTipText(messageCopy);
198         errorField.setCaretPosition(0);
199     }
200     
201     public void validateReferences() {
202         boolean nodeAccepted = nodeAcceptor.acceptNodes(nodeDisplayPanel.getSelectedNodes());
203         if ((slPanel.getUnreferencedServiceLocator().isSelected() &&
204                 slPanel.getClassName().getText().trim().equals("")) ||
205                 !nodeAccepted) {
206             firePropertyChange(IS_VALID, true, false);
207         } else {
208             firePropertyChange(IS_VALID, false, true);
209         }
210     }
211     
212     /** This method is called from within the constructor to
213      * initialize the form.
214      * WARNING: Do NOT modify this code. The content of this method is
215      * always regenerated by the Form Editor.
216      */

217     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
218
private void initComponents() {
219         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
220         javax.swing.JLabel JavaDoc jLabel1;
221
222         intefaceButtonGroup = new javax.swing.ButtonGroup JavaDoc();
223         serviceLocatorPanel = new javax.swing.JPanel JavaDoc();
224         convertToRuntime = new javax.swing.JCheckBox JavaDoc();
225         displayPanel = new javax.swing.JPanel JavaDoc();
226         jLabel1 = new javax.swing.JLabel JavaDoc();
227         errorField = new javax.swing.JTextField JavaDoc();
228         jLabel2 = new javax.swing.JLabel JavaDoc();
229         jLabel3 = new javax.swing.JLabel JavaDoc();
230         referenceNameTextField = new javax.swing.JTextField JavaDoc();
231         localRadioButton = new javax.swing.JRadioButton JavaDoc();
232         remoteRadioButton = new javax.swing.JRadioButton JavaDoc();
233
234         setLayout(new java.awt.GridBagLayout JavaDoc());
235
236         getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("ACS_CallEJBPanel"));
237         getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("ACS_CallEJBPanel"));
238         serviceLocatorPanel.setLayout(new java.awt.BorderLayout JavaDoc());
239
240         convertToRuntime.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_ConvertToRuntimeMneumonic").charAt(0));
241         convertToRuntime.setSelected(true);
242         convertToRuntime.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_ConvertToRuntime"));
243         serviceLocatorPanel.add(convertToRuntime, java.awt.BorderLayout.SOUTH);
244         convertToRuntime.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(CallEjbPanel.class, "ACSD_ConvertToRuntime"));
245
246         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
247         gridBagConstraints.gridx = 0;
248         gridBagConstraints.gridy = 2;
249         gridBagConstraints.gridwidth = 3;
250         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
251         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
252         gridBagConstraints.weightx = 1.0;
253         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 5, 5);
254         add(serviceLocatorPanel, gridBagConstraints);
255
256         displayPanel.setLayout(new java.awt.BorderLayout JavaDoc());
257
258         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
259         gridBagConstraints.gridx = 0;
260         gridBagConstraints.gridy = 1;
261         gridBagConstraints.gridwidth = 3;
262         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
263         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
264         gridBagConstraints.weightx = 1.0;
265         gridBagConstraints.weighty = 1.0;
266         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
267         add(displayPanel, gridBagConstraints);
268         displayPanel.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_DisplayPanel"));
269         displayPanel.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("ACSD_DisplayPanel"));
270
271         jLabel1.setLabelFor(displayPanel);
272         jLabel1.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_ModuleMustBeInSameApplication"));
273         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
274         gridBagConstraints.gridx = 0;
275         gridBagConstraints.gridy = 0;
276         gridBagConstraints.gridwidth = 3;
277         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
278         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
279         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 0, 5);
280         add(jLabel1, gridBagConstraints);
281
282         errorField.setBackground(getBackground());
283         errorField.setEditable(false);
284         errorField.setBorder(null);
285         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
286         gridBagConstraints.gridx = 0;
287         gridBagConstraints.gridy = 5;
288         gridBagConstraints.gridwidth = 3;
289         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
290         gridBagConstraints.weightx = 1.0;
291         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 5, 5);
292         add(errorField, gridBagConstraints);
293         errorField.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("ACS_ErrorField"));
294         errorField.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("ACS_ErrorField"));
295
296         jLabel2.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("MN_ReferenceName").charAt(0));
297         jLabel2.setLabelFor(referenceNameTextField);
298         jLabel2.setText(org.openide.util.NbBundle.getMessage(CallEjbPanel.class, "LBL_ReferenceName"));
299         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
300         gridBagConstraints.gridx = 0;
301         gridBagConstraints.gridy = 3;
302         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
303         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 5, 5);
304         add(jLabel2, gridBagConstraints);
305
306         jLabel3.setText(org.openide.util.NbBundle.getMessage(CallEjbPanel.class, "LBL_ReferencedInterface"));
307         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
308         gridBagConstraints.gridx = 0;
309         gridBagConstraints.gridy = 4;
310         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
311         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 5, 5);
312         add(jLabel3, gridBagConstraints);
313
314         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
315         gridBagConstraints.gridx = 1;
316         gridBagConstraints.gridy = 3;
317         gridBagConstraints.gridwidth = 2;
318         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
319         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 5, 5);
320         add(referenceNameTextField, gridBagConstraints);
321         referenceNameTextField.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_ReferenceName"));
322         referenceNameTextField.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_ReferenceName"));
323
324         intefaceButtonGroup.add(localRadioButton);
325         localRadioButton.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("MN_Local").charAt(0));
326         localRadioButton.setSelected(true);
327         localRadioButton.setText(org.openide.util.NbBundle.getMessage(CallEjbPanel.class, "LBL_Local"));
328         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
329         gridBagConstraints.gridx = 1;
330         gridBagConstraints.gridy = 4;
331         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
332         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 5, 5);
333         add(localRadioButton, gridBagConstraints);
334         localRadioButton.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_Local"));
335         localRadioButton.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_Local"));
336
337         intefaceButtonGroup.add(remoteRadioButton);
338         remoteRadioButton.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("MN_Remote").charAt(0));
339         remoteRadioButton.setText(org.openide.util.NbBundle.getMessage(CallEjbPanel.class, "LBL_Remote"));
340         remoteRadioButton.addItemListener(new java.awt.event.ItemListener JavaDoc() {
341             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
342                 remoteRadioButtonItemStateChanged(evt);
343             }
344         });
345
346         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
347         gridBagConstraints.gridx = 2;
348         gridBagConstraints.gridy = 4;
349         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
350         gridBagConstraints.weightx = 1.0;
351         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 5);
352         add(remoteRadioButton, gridBagConstraints);
353         remoteRadioButton.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_Remote"));
354         remoteRadioButton.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/Bundle").getString("LBL_Remote"));
355
356     }// </editor-fold>//GEN-END:initComponents
357

358     private void remoteRadioButtonItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_remoteRadioButtonItemStateChanged
359
validateReferences();
360     }//GEN-LAST:event_remoteRadioButtonItemStateChanged
361

362     
363     // Variables declaration - do not modify//GEN-BEGIN:variables
364
private javax.swing.JCheckBox JavaDoc convertToRuntime;
365     private javax.swing.JPanel JavaDoc displayPanel;
366     private javax.swing.JTextField JavaDoc errorField;
367     private javax.swing.ButtonGroup JavaDoc intefaceButtonGroup;
368     private javax.swing.JLabel JavaDoc jLabel2;
369     private javax.swing.JLabel JavaDoc jLabel3;
370     private javax.swing.JRadioButton JavaDoc localRadioButton;
371     private javax.swing.JTextField JavaDoc referenceNameTextField;
372     private javax.swing.JRadioButton JavaDoc remoteRadioButton;
373     private javax.swing.JPanel JavaDoc serviceLocatorPanel;
374     // End of variables declaration//GEN-END:variables
375

376     public boolean convertToRuntime() {
377         return convertToRuntime.isSelected();
378     }
379     
380     public Node getEjb() {
381         Node[] selectedNodes = nodeDisplayPanel.getSelectedNodes();
382         return selectedNodes.length > 0 ? selectedNodes[0] : null;
383     }
384     
385     public String JavaDoc getServiceLocator() {
386         return slPanel.classSelected();
387     }
388     
389     public String JavaDoc getReferenceName() {
390         return referenceNameTextField.getText();
391     }
392     
393     public boolean isRemoteInterfaceSelected() {
394         return remoteRadioButton.isSelected();
395     }
396     
397     private void generateName(EjbReference ejbReference, boolean remote, Node selectedNode) throws IOException JavaDoc {
398         if (ejbReference.getClientJarTarget() == null) {
399             referenceNameTextField.setText("");
400             return;
401         }
402         String JavaDoc name = "";
403         if (remote) {
404             boolean targetIsJavaSE = Utils.isTargetJavaSE(srcFile, className);
405             if (targetIsJavaSE && Utils.isJavaEE5orHigher(project)){
406                 ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(selectedNode);
407                 name = elementHandle.getQualifiedName();
408             } else if (targetIsJavaSE){
409                 name = ejbReference.createRef().getHome();
410             } else {
411                 name = ejbReference.createRef().getEjbRefName();
412             }
413         } else {
414             name = ejbReference.createLocalRef().getEjbRefName();
415         }
416         int uniquifier = 1;
417         String JavaDoc newName = name;
418         while (refNameSet.contains(newName)) {
419             newName = name + String.valueOf(uniquifier++);
420         }
421         referenceNameTextField.setText(name);
422     }
423     
424     private class NodeAcceptorImpl implements NodeAcceptor {
425         
426         public NodeAcceptorImpl() {}
427         
428         public boolean acceptNodes(Node[] nodes) {
429             setErrorMessage(" "); //NOI18N
430

431             // no node selected
432
if (nodes.length == 0) {
433                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_SelectOneEJB")); //NOI18N
434
return false;
435             }
436             // more than one node selected
437
if (nodes.length > 1) {
438                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_MoreEJBsSelected")); //NOI18N
439
return false;
440             }
441             ElementHandle<TypeElement> elementHandle = null;
442             try {
443                 elementHandle = _RetoucheUtil.getJavaClassFromNode(nodes[0]);
444             } catch (IOException JavaDoc ioe) {
445                 ErrorManager.getDefault().notify(ioe);
446             }
447             // non-EJB node is selected
448
if (elementHandle == null) {
449                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_NodeIsNotEJB")); //NOI18N
450
return false;
451             }
452             
453             if (elementHandle.getQualifiedName().equals(className)) {
454                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_CannotCallItself", className)); //NOI18N
455
return false;
456             }
457             
458             // builded archive with beans is not available
459
if (!hasJarArtifact()) {
460                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_EJBNotInDistributionArchive")); //NOI18N
461
return false;
462             }
463             // node cannot act as EJB reference
464
if (nodes[0].getCookie(EjbReference.class) == null) {
465                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_ReferencesNotSupported")); //NOI18N
466
return false;
467             }
468             // check interfaces radiobuttons in context of selected node
469
if (!acceptInterfaces(nodes)) {
470                 return false;
471             }
472             // validate reference name
473
if (!validateRefName()) {
474                 return false;
475             }
476             
477             // if local ref is used, modules must be in same module or J2EE application
478
DataObject dataObject = (DataObject) nodes[0].getCookie(DataObject.class);
479             if (dataObject == null) {
480                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_NoSourcesForBean")); //NOI18N
481
return false;
482             }
483             Project nodeProject = FileOwnerQuery.getOwner(dataObject.getPrimaryFile());
484             if (!isRemoteInterfaceSelected() &&
485                     !nodeProject.equals(project) &&
486                     !Utils.areInSameJ2EEApp(project, nodeProject)) {
487                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_NotInSameEarOrProject")); //NOI18N
488
return false;
489             }
490             
491             //AC cannot contain references to local beans
492
if (!isRemoteInterfaceSelected() &&
493                     Utils.isAppClient(project)) {
494                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_CannotCallLocalInAC")); //NOI18N
495
return false;
496             }
497
498             //Unit tests or classes in a JSE project cannot contain references to local beans
499
try {
500                 if (!isRemoteInterfaceSelected() && Utils.isTargetJavaSE(srcFile, className)) {
501                     setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_CannotCallLocalInJSE")); //NOI18N
502
return false;
503                 }
504             } catch (IOException JavaDoc ioe) {
505                 ErrorManager.getDefault().notify(ioe);
506                 return false;
507             }
508             
509             // see #75876
510
if (!Utils.isJavaEE5orHigher(project) && Utils.isJavaEE5orHigher(nodeProject)){
511                 setWarningMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_JEESpecificationLevelsDiffer")); //NOI18N
512
}
513             
514             return true;
515         }
516         
517         private boolean acceptInterfaces(Node[] nodes) {
518             EjbReference ejbReference = (EjbReference) nodes[0].getCookie(EjbReference.class);
519             if (ejbReference == null) {
520                 return false;
521             }
522             
523             boolean shouldEnableLocal = ejbReference.supportsLocalInvocation();
524             boolean shouldEnableRemote = ejbReference.supportsRemoteInvocation();
525             localRadioButton.setEnabled(shouldEnableLocal);
526             remoteRadioButton.setEnabled(shouldEnableRemote);
527             if (!shouldEnableLocal && !shouldEnableRemote) {
528                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "LBL_ReferencesNotSupported")); //NOI18N
529
return false;
530             } else if (shouldEnableLocal && !shouldEnableRemote) {
531                 localRadioButton.setSelected(true);
532             } else if (!shouldEnableLocal && shouldEnableRemote) {
533                 remoteRadioButton.setSelected(true);
534             }
535             setErrorMessage(" "); //NOI18N
536
return true;
537         }
538         
539         private boolean hasJarArtifact() {
540             Project nodeProject = FileOwnerQuery.getOwner(srcFile);
541             if (nodeProject.equals(project)) {
542                 // we're in same project, no need for output jar
543
return true;
544             }
545             return AntArtifactQuery.findArtifactsByType(nodeProject, JavaProjectConstants.ARTIFACT_TYPE_JAR).length > 0;
546         }
547         
548         private boolean validateRefName() {
549             String JavaDoc refName = referenceNameTextField.getText();
550             if (refNameSet.contains(refName)) {
551                 setErrorMessage(NbBundle.getMessage(CallEjbPanel.class, "ERR_ReferenceNameExists", refName)); //NOI18N
552
return false;
553             }
554             return true;
555         }
556     }
557     
558 }
559
Popular Tags