KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > multiview > EjbRefsTablePanel


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.ddloaders.web.multiview;
21
22 import org.netbeans.api.project.FileOwnerQuery;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean;
25 import org.netbeans.modules.j2ee.dd.api.common.EjbRef;
26 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef;
27 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
28 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject;
29 import org.netbeans.modules.xml.multiview.ui.DefaultTablePanel;
30 import org.netbeans.modules.xml.multiview.ui.EditDialog;
31 import org.netbeans.modules.xml.multiview.ui.SimpleDialogPanel;
32 import org.openide.util.NbBundle;
33  
34 /** EjbRefsTablePanel - panel containing table for EJB references
35  *
36  * @author mk115033
37  * Created on April 11, 2005
38  */

39 public class EjbRefsTablePanel extends DefaultTablePanel {
40     private EjbRefTableModel model;
41     private WebApp webApp;
42     private DDDataObject dObj;
43     /**
44      * Indicates whether we are dealing with a Java EE 5 project.
45      */

46     private boolean javaEE5;
47     
48     /** Creates new form ContextParamPanel */
49     public EjbRefsTablePanel(final DDDataObject dObj, final EjbRefTableModel model) {
50         super(model);
51         this.model=model;
52         this.dObj=dObj;
53         Project project = FileOwnerQuery.getOwner(dObj.getPrimaryFile());
54         this.javaEE5 = org.netbeans.modules.j2ee.common.Util.isJavaEE5orHigher(project);
55         
56         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
57             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
58                 dObj.modelUpdatedFromUI();
59                 dObj.setChangedFromUI(true);
60                 int row = getTable().getSelectedRow();
61                 model.removeRow(row);
62                 dObj.setChangedFromUI(false);
63             }
64         });
65         editButton.addActionListener(new TableActionListener(false));
66         addButton.addActionListener(new TableActionListener(true));
67     }
68
69     void setModel(WebApp webApp, EjbRef[] params1, EjbLocalRef[] params2) {
70         CommonDDBean[] params = new CommonDDBean[params1.length+params2.length];
71         int k=0;
72         for (int i=0;i<params1.length;i++) {
73             params[i]=params1[i];
74             k++;
75         }
76         for (int j=0;j<params2.length;j++) {
77             params[k+j]=params2[j];
78         }
79         model.setData(webApp,params);
80         this.webApp=webApp;
81     }
82     
83     private class TableActionListener implements java.awt.event.ActionListener JavaDoc {
84         private boolean add;
85         TableActionListener(boolean add) {
86             this.add=add;
87         }
88         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
89             final int row = (add?-1:getTable().getSelectedRow());
90             final EjbRefPanel dialogPanel = new EjbRefPanel();
91             if (!add) {
92                 CommonDDBean ref = model.getEjbRef(row);
93                 if (ref instanceof EjbRef) {
94                     EjbRef ejbRef = (EjbRef)ref;
95                     dialogPanel.setEjbName(ejbRef.getEjbRefName());
96                     dialogPanel.setBeanType(ejbRef.getEjbRefType());
97                     dialogPanel.setInterfaceType("Remote"); //NOI18N
98
dialogPanel.setHome(ejbRef.getHome());
99                     dialogPanel.setInterface(ejbRef.getRemote());
100                     dialogPanel.setLink(ejbRef.getEjbLink());
101                     dialogPanel.setDescription(ejbRef.getDefaultDescription());
102                 } else {
103                     EjbLocalRef ejbRef = (EjbLocalRef)ref;
104                     dialogPanel.setEjbName(ejbRef.getEjbRefName());
105                     dialogPanel.setBeanType(ejbRef.getEjbRefType());
106                     dialogPanel.setInterfaceType("Local"); //NOI18N
107
dialogPanel.setHome(ejbRef.getLocalHome());
108                     dialogPanel.setInterface(ejbRef.getLocal());
109                     dialogPanel.setLink(ejbRef.getEjbLink());
110                     dialogPanel.setDescription(ejbRef.getDefaultDescription());
111                 }
112             }
113             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(EjbRefsTablePanel.class,"TTL_EjbRef"),add) {
114                 protected String JavaDoc validate() {
115                     String JavaDoc name = dialogPanel.getEjbName().trim();
116                     String JavaDoc home = dialogPanel.getHome().trim();
117                     String JavaDoc remote = dialogPanel.getInterface().trim();
118                     if (name.length()==0) {
119                         return NbBundle.getMessage(EjbRefsTablePanel.class,"TXT_EmptyEjbRefName");
120                     } else {
121                         EjbRef[] refs = webApp.getEjbRef();
122                         boolean exists=false;
123                         int countEjbRef=refs.length;
124                         for (int i=0;i<countEjbRef;i++) {
125                             if (row!=i && name.equals(refs[i].getEjbRefName())) {
126                                 exists=true;
127                                 break;
128                             }
129                         }
130                         if (!exists) {
131                             EjbLocalRef[] localRefs = webApp.getEjbLocalRef();
132                             for (int i=0;i<localRefs.length;i++) {
133                                 if (row!=i+countEjbRef && name.equals(localRefs[i].getEjbRefName())) {
134                                     exists=true;
135                                     break;
136                                 }
137                             }
138                         }
139                         if (exists) {
140                             return NbBundle.getMessage(EjbRefsTablePanel.class,"TXT_EjbRefNameExists",name);
141                         }
142                     }
143                     if (home.length()==0 && !javaEE5) {
144                         return NbBundle.getMessage(EjbRefsTablePanel.class,"TXT_EmptyEjbHome");
145                     }
146                     if (remote.length()==0) {
147                         return NbBundle.getMessage(EjbRefsTablePanel.class,"TXT_EmptyEjbInterface");
148                     }
149                     return null;
150                 }
151             };
152             
153             if (add) dialog.setValid(false); // disable OK button
154
else dialogPanel.getInterfaceTypeCB().setEnabled(false);
155             javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
156             dialogPanel.getNameTF().getDocument().addDocumentListener(docListener);
157             dialogPanel.getHomeTF().getDocument().addDocumentListener(docListener);
158             dialogPanel.getInterfaceTF().getDocument().addDocumentListener(docListener);
159             
160             java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
161             d.setVisible(true);
162             dialogPanel.getNameTF().getDocument().removeDocumentListener(docListener);
163             dialogPanel.getHomeTF().getDocument().removeDocumentListener(docListener);
164             dialogPanel.getInterfaceTF().getDocument().removeDocumentListener(docListener);
165             
166             if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
167                 dObj.modelUpdatedFromUI();
168                 dObj.setChangedFromUI(true);
169                 String JavaDoc name = dialogPanel.getEjbName().trim();
170                 String JavaDoc beanType = dialogPanel.getBeanType();
171                 String JavaDoc interfaceType = dialogPanel.getInterfaceType();
172                 String JavaDoc home = dialogPanel.getHome().trim();
173                 String JavaDoc remote = dialogPanel.getInterface().trim();
174                 String JavaDoc link = dialogPanel.getLink().trim();
175                 String JavaDoc description = dialogPanel.getDescription();
176                 if (add) model.addRow(new String JavaDoc[]{name,beanType,interfaceType,home,remote,link,description});
177                 else model.editRow(row,new String JavaDoc[]{name,beanType,interfaceType,home,remote,link,description});
178                 dObj.setChangedFromUI(false);
179             }
180         }
181     }
182 }
183
Popular Tags