KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > sunresources > wizards > CommonAttributePanel


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  * CommonAttributePanel.java
21  *
22  * Created on October 10, 2002
23  */

24
25 package org.netbeans.modules.j2ee.sun.ide.sunresources.wizards;
26
27 import java.awt.Component JavaDoc;
28 import javax.swing.JTextField JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import org.openide.filesystems.FileObject;
33
34 import org.openide.loaders.TemplateWizard;
35 import org.openide.WizardDescriptor;
36 import org.openide.util.HelpCtx;
37
38 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
39
40 import org.netbeans.modules.j2ee.sun.sunresources.beans.FieldGroup;
41 import org.netbeans.modules.j2ee.sun.sunresources.beans.Wizard;
42 import org.netbeans.modules.j2ee.sun.sunresources.beans.FieldGroupHelper;
43
44 /** A single panel descriptor for a wizard.
45  * You probably want to make a wizard iterator to hold it.
46  *
47  * @author shirleyc
48  */

49 public class CommonAttributePanel extends ResourceWizardPanel {
50     
51     /** The visual component that displays this panel.
52      * If you need to access the component from this class,
53      * just use getComponent().
54      */

55     private CommonAttributeVisualPanel component;
56     private ResourceConfigHelper helper;
57     private Wizard wizardInfo;
58     private String JavaDoc[] groupNames;
59     private boolean setupValid = true;
60     
61     /** Create the wizard panel descriptor. */
62     public CommonAttributePanel(ResourceConfigHelper helper, Wizard wizardInfo, String JavaDoc[] groupNames) {
63         this.helper = helper;
64         this.wizardInfo = wizardInfo;
65         this.groupNames = groupNames;
66     }
67     
68     // Get the visual component for the panel. In this template, the component
69
// is kept separate. This can be more efficient: if the wizard is created
70
// but never displayed, or not all panels are displayed, it is better to
71
// create only those which really need to be visible.
72
public Component JavaDoc getComponent() {
73         if (component == null) {
74                FieldGroup[] groups = new FieldGroup[groupNames.length];
75                 for (int i = 0; i < this.groupNames.length; i++) {
76                     groups[i] = FieldGroupHelper.getFieldGroup(wizardInfo, this.groupNames[i]); //NOI18N
77
}
78                 String JavaDoc panelType = CommonAttributeVisualPanel.TYPE_JDBC_RESOURCE;
79                 if (wizardInfo.getName().equals(__JdbcConnectionPool)) {
80                      panelType = CommonAttributeVisualPanel.TYPE_CP_POOL_SETTING;
81                 }else if (wizardInfo.getName().equals(__PersistenceManagerFactoryResource)) {
82                     panelType = CommonAttributeVisualPanel.TYPE_PERSISTENCE_MANAGER;
83                 }
84                 component = new CommonAttributeVisualPanel(this, groups, panelType, this.helper);
85         }
86         return component;
87     }
88     
89     public boolean isNewResourceSelected() {
90         if (component == null)
91             return false;
92         else
93             return component.isNewResourceSelected();
94     }
95     
96     public void setInitialFocus(){
97         if(component != null) {
98             component.setInitialFocus();
99         }
100     }
101     
102     public void setPropInitialFocus(){
103         if(component != null) {
104             component.setPropInitialFocus();
105         }
106     }
107     
108     public String JavaDoc getResourceName() {
109         return this.wizardInfo.getName();
110     }
111     
112     public HelpCtx getHelp() {
113         if (wizardInfo.getName().equals(__JdbcConnectionPool)) {
114             return new HelpCtx("AS_Wiz_ConnPool_poolSettings"); //NOI18N
115
}else if (wizardInfo.getName().equals(__JdbcResource)) {
116             return new HelpCtx("AS_Wiz_DataSource_general"); //NOI18N
117
}else if (wizardInfo.getName().equals(__PersistenceManagerFactoryResource)) {
118             return new HelpCtx("AS_Wiz_PMF_general"); //NOI18N
119
}else {
120             return HelpCtx.DEFAULT_HELP;
121         }
122     }
123     
124     public ResourceConfigHelper getHelper() {
125         return helper;
126     }
127     
128     public String JavaDoc getJndiName() {
129         if (component != null && component.jLabels != null && component.jFields != null) {
130             int i;
131             for (i=0; i < component.jLabels.length; i++) {
132                 String JavaDoc jLabel = (String JavaDoc)component.jLabels[i].getText();
133                 if (jLabel.equals(bundle.getString("LBL_" + __JndiName))) { // NO18N
134
return (String JavaDoc)((JTextField JavaDoc)component.jFields[i]).getText();
135                 }
136             }
137         }
138         return null;
139     }
140     
141     /**
142      * Checks if the JNDI Name in the wizard is duplicate name in the
143      * Unregistered resource list for JDBC Data Sources, Persistenc Managers,
144      * and Java Mail Sessions.
145      *
146      * @return boolean true if there is a duplicate name.
147      * false if not.
148      */

149       public boolean isValid () {
150           // If it is always OK to press Next or Finish, then:
151
if(! setupValid){
152               setErrorMsg (bundle.getString ("Err_InvalidSetup"));
153               return false;
154           }
155           setErrorMsg (bundle.getString ("Empty_String"));
156           if (component != null && component.jLabels != null && component.jFields != null) {
157               int i;
158               for (i=0; i < component.jLabels.length; i++) {
159                   String JavaDoc jLabel = (String JavaDoc)component.jLabels[i].getText ();
160                   if (jLabel.equals (bundle.getString ("LBL_" + __JndiName))) { // NO18N
161
String JavaDoc jndiName = (String JavaDoc)((JTextField JavaDoc)component.jFields[i]).getText ();
162                       if (jndiName == null || jndiName.length () == 0) {
163                           setErrorMsg (bundle.getString ("Err_InvalidJndiName"));
164                           return false;
165                       }else if(! ResourceUtils.isLegalResourceName (jndiName)){
166                           return false;
167                       }else{
168                           FileObject resFolder = this.helper.getData ().getTargetFileObject ();
169                           if(resFolder != null){
170                               if (wizardInfo.getName ().equals (__JdbcResource)){
171                                   if(! ResourceUtils.isUniqueFileName (jndiName, resFolder, __JDBCResource)){
172                                       setErrorMsg (bundle.getString ("Err_DuplFileJndiName"));
173                                       return false;
174                                   }
175                               }else if(wizardInfo.getName ().equals (__PersistenceManagerFactoryResource)){
176                                   if(! ResourceUtils.isUniqueFileName (jndiName, resFolder, __PersistenceResource)){
177                                       setErrorMsg (bundle.getString ("Err_DuplFileJndiName"));
178                                       return false;
179                                   }
180                               }
181                           }
182                       }
183                   }else{
184                       Set JavaDoc commonAttr = new HashSet JavaDoc (Arrays.asList (COMMON_ATTR_INTEGER));
185                       if(commonAttr.contains (jLabel.trim ())){
186                           String JavaDoc fieldValue = (String JavaDoc)((JTextField JavaDoc)component.jFields[i]).getText ();
187                           if (fieldValue == null || fieldValue.length () == 0) {
188                               setErrorMessage (bundle.getString ("Err_EmptyValue"), jLabel);
189                               return false;
190                           }
191                       }
192                   }
193               }
194               
195           }
196           if(!isNewResourceSelected ()){
197               //Need to check the poolname for jdbc
198
if((this.helper.getData ().getResourceName ()).equals (__JdbcResource)){
199                   String JavaDoc cpname = this.helper.getData ().getString (__PoolName);
200                   if(cpname == null || cpname.trim ().equals ("") ) { //NOI18N
201
setErrorMsg (bundle.getString ("Err_ChooseOrCreatePool"));
202                       return false;
203                   }
204               }
205               //Need to get jdbc data if pmf and make sure it has a poolname
206
if((this.helper.getData ().getResourceName ()).equals (__PersistenceManagerFactoryResource)){
207                   if(this.helper.getData ().getHolder ().hasDSHelper ()){
208                       String JavaDoc cpname = this.helper.getData ().getHolder ().getDSHelper ().getData ().getString (__PoolName);
209                       if(cpname == null || cpname.trim ().equals ("") ) {//NOI18N
210
setErrorMsg (bundle.getString ("Err_ChooseOrCreatePool"));
211                           return false;
212                       }
213                   }else{
214                       String JavaDoc dsname = this.helper.getData ().getString (__JdbcResourceJndiName);
215                       if(dsname == null || dsname.trim ().equals ("") ) {//NOI18N
216
setErrorMsg (bundle.getString ("Err_ChooseOrCreateDS"));
217                           return false;
218                       }
219                   }
220               }
221           }
222           return true;
223       }
224     
225     public void initData() {
226         this.component.initData();
227     }
228     
229     public void readSettings(Object JavaDoc settings) {
230         this.wizDescriptor = (WizardDescriptor)settings;
231         TemplateWizard wizard = (TemplateWizard)settings;
232         String JavaDoc targetName = wizard.getTargetName();
233         FileObject resFolder = ResourceUtils.getResourceDirectory(this.helper.getData().getTargetFileObject());
234         this.helper.getData().setTargetFileObject (resFolder);
235         if(component == null)
236             getComponent();
237         if(resFolder != null){
238             if (wizardInfo.getName ().equals (__JdbcResource)){
239                 if(this.helper.getData ().getString (__DynamicWizPanel).equals ("true")){ //NOI18N
240
targetName = null;
241                 }
242                 targetName = ResourceUtils.createUniqueFileName (targetName, resFolder, __JDBCResource);
243                 this.helper.getData ().setTargetFile (targetName);
244                 component.setHelper (this.helper);
245             }else if(wizardInfo.getName ().equals (__PersistenceManagerFactoryResource)){
246                 targetName = ResourceUtils.createUniqueFileName (targetName, resFolder, __PersistenceResource);
247                 this.helper.getData ().setTargetFile (targetName);
248                 component.setHelper (this.helper);
249             }
250         }else
251             setupValid = false;
252     }
253     
254     public boolean isFinishPanel() {
255        if(isNewResourceSelected())
256             return false;
257        else
258            return isValid();
259     }
260     
261     private boolean setupValid(){
262         return setupValid;
263     }
264 }
265
266
Popular Tags