KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DataSourceWizard.java
21  *
22  * Created on September 30, 2003, 10:05 AM
23  */

24
25 package org.netbeans.modules.j2ee.sun.ide.sunresources.wizards;
26
27 /**
28  *
29  * @author nityad
30  */

31
32 import java.awt.Component JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37
38 import org.openide.util.NbBundle;
39 import org.openide.WizardDescriptor;
40 import org.netbeans.api.project.Project;
41 import org.openide.filesystems.FileObject;
42 import org.netbeans.spi.project.ui.templates.support.Templates;
43
44 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
45
46 import org.netbeans.modules.j2ee.sun.sunresources.beans.Wizard;
47 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants;
48 import org.openide.ErrorManager;
49
50 public final class DataSourceWizard implements WizardDescriptor.InstantiatingIterator, ChangeListener JavaDoc, WizardConstants{
51     
52     /** An array of all wizard panels */
53        
54     private static final String JavaDoc DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/DSWizard.xml"; //NOI18N
55
private static final String JavaDoc CP_DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/CPWizard.xml"; //NOI18N
56

57     private transient int index;
58     private transient WizardDescriptor.Panel[] panels;
59 // private transient WizardDescriptor wiz;
60
private transient String JavaDoc[] steps;
61     
62     private ResourceConfigHelper helper;
63     private Wizard wizardInfo;
64         
65     private boolean addSteps = false;
66 // private boolean firstTime = true;
67

68     private ResourceConfigHelper cphelper;
69     private ResourceConfigHelperHolder holder;
70     private Wizard cpWizardInfo;
71      
72     private transient WizardDescriptor.Panel[] morePanels = null;
73     private transient WizardDescriptor.Panel[] dsPanels = null;
74     
75     private transient String JavaDoc[] dsSteps = null;
76     private transient String JavaDoc[] moreSteps = null;
77     
78     /** Creates a new instance of DataSourceWizard */
79     public static DataSourceWizard create () {
80         return new DataSourceWizard ();
81     }
82     
83     private WizardDescriptor.Panel[] createPanels() {
84         morePanels = null;
85         WizardDescriptor.Panel panel = new CommonAttributePanel(helper, wizardInfo, new String JavaDoc[] {"general"}); //NOI18N
86
panel.addChangeListener(this);
87         return new WizardDescriptor.Panel[] {
88             panel,
89             new CommonPropertyPanel(this.helper, this.wizardInfo)
90         };
91     }
92     
93     private String JavaDoc[] createSteps() {
94         return new String JavaDoc[] {
95             NbBundle.getMessage(DataSourceWizard.class,__FirstStepChoose),
96             NbBundle.getMessage(DataSourceWizard.class, "LBL_GeneralAttributes_DS"), // NOI18N
97
NbBundle.getMessage(DataSourceWizard.class, "LBL_AddProperty") // NOI18N
98
};
99     }
100     
101     public Set JavaDoc instantiate(){
102         try{
103             if(this.holder.hasCPHelper()){
104                 String JavaDoc poolName = this.cphelper.getData().getString(__Name);
105                 this.helper.getData().setString(__PoolName, poolName);
106                 this.cphelper.getData().setTargetFile(poolName);
107                 this.cphelper.getData().setTargetFileObject(this.helper.getData().getTargetFileObject());
108                 ResourceUtils.saveJDBCResourceDatatoXml(this.helper.getData(), this.cphelper.getData());
109             }else{
110                 ResourceUtils.saveJDBCResourceDatatoXml(this.helper.getData(), null);
111             }
112         }catch (Exception JavaDoc ex){
113                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
114                                         ex);
115         }
116         return java.util.Collections.EMPTY_SET;
117     }
118     
119     public void initialize(WizardDescriptor wiz){
120         this.wizardInfo = getWizardInfo(DATAFILE);
121         this.holder = new ResourceConfigHelperHolder();
122         this.helper = holder.getDataSourceHelper();
123         
124         //this.wiz = wiz;
125
wiz.putProperty("NewFileWizard_Title", NbBundle.getMessage(ConnPoolWizard.class, "Templates/SunResources/JDBC_Resource")); //NOI18N
126
index = 0;
127                 
128         Project project = Templates.getProject(wiz);
129         
130         panels = createPanels();
131         // Make sure list of steps is accurate.
132
steps = createSteps();
133         
134         try{
135             FileObject pkgLocation = project.getProjectDirectory();
136             if (pkgLocation != null) {
137                 this.helper.getData().setTargetFileObject(pkgLocation);
138             }
139         }catch (Exception JavaDoc ex){
140                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
141                                         ex);
142         }
143         
144         for (int i = 0; i < panels.length; i++) {
145             Component JavaDoc c = panels[i].getComponent();
146             if (c instanceof JComponent JavaDoc) { // assume Swing components
147
JComponent JavaDoc jc = (JComponent JavaDoc)c;
148                 // Step #.
149
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
150
// Step name (actually the whole list for reference).
151
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
152
}
153         }
154         
155     }
156     
157     public void uninitialize(WizardDescriptor wiz){
158         //this.wiz = null;
159
panels = null;
160     }
161     
162     public Wizard getWizardInfo(String JavaDoc filePath){
163         try{
164             InputStream JavaDoc in = Wizard.class.getClassLoader().getResourceAsStream(filePath);
165             this.wizardInfo = Wizard.createGraph(in);
166         }catch(Exception JavaDoc ex){
167                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
168                                         ex);
169         }
170         return this.wizardInfo;
171     }
172     
173     public String JavaDoc name(){
174         return NbBundle.getMessage(DataSourceWizard.class, "Templates/SunResources/JDBC_Resource"); //NOI18N
175
}
176     
177     public boolean hasNext(){
178         return index < panels.length - 1;
179     }
180     
181     public boolean hasPrevious(){
182         return index > 0;
183     }
184     
185     public synchronized void nextPanel(){
186         if (index + 1 == panels.length) {
187             throw new java.util.NoSuchElementException JavaDoc();
188         }
189         
190         if (index == 0) {
191             ((CommonPropertyPanel) panels[1]).setInitialFocus();
192         }else if (index == 1) {
193             ((CPVendorPanel) panels[2]).setInitialFocus();
194         }else if (index == 2){
195             ((CPPropertiesPanelPanel) panels[3]).refreshFields();
196         }else if (index == 3){
197             ((CommonAttributePanel) panels[4]).setPropInitialFocus();
198         }
199         
200         index ++;
201     }
202     
203     public synchronized void previousPanel(){
204         if (index == 0) {
205             throw new java.util.NoSuchElementException JavaDoc();
206         }
207         
208         index--;
209     }
210     
211     public WizardDescriptor.Panel current(){
212         return (WizardDescriptor.Panel)panels[index];
213     }
214     
215     public final void addChangeListener(ChangeListener JavaDoc l) {
216     }
217     public final void removeChangeListener(ChangeListener JavaDoc l) {
218     }
219
220     public void setResourceConfigHelper(ResourceConfigHelper helper){
221         this.helper = helper;
222     }
223     
224     public ResourceConfigHelper getResourceConfigHelper(){
225         return this.helper;
226     }
227     
228     public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
229         if( (e.getSource().getClass() == CommonAttributePanel.class) || (e.getSource().getClass() == CommonAttributeVisualPanel.class) ) {
230             CommonAttributePanel commonPane = (CommonAttributePanel)this.current();
231             CommonAttributeVisualPanel visPane = (CommonAttributeVisualPanel)commonPane.getComponent();
232             boolean oldVal = addSteps;
233             this.addSteps = visPane.isNewResourceSelected();
234             
235             if((!oldVal && addSteps) || (oldVal && !addSteps)){
236                 this.holder.setHasCPHelper(this.addSteps);
237                 if (addSteps && morePanels == null) {
238                     addPanels();
239                     addSteps();
240                     for (int i = 0; i < panels.length; i++) {
241                         Component JavaDoc c = panels[i].getComponent();
242                         if (steps[i] == null) {
243                             steps[i] = c.getName();
244                         }
245                         if (c instanceof JComponent JavaDoc) {
246                             JComponent JavaDoc jc = (JComponent JavaDoc)c;
247                             jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
248
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
249
}
250                     }
251                 }else if((!addSteps) && (morePanels != null)){
252                     if(dsPanels != null){
253                         panels = dsPanels;
254                         morePanels = null;
255                     }else
256                         panels = createPanels();
257                     if(dsSteps != null)
258                         steps = dsSteps;
259                     else
260                         steps = createSteps();
261                     
262                     for (int i = 0; i < panels.length; i++) {
263                         Component JavaDoc c = panels[i].getComponent();
264                         if (steps[i] == null) {
265                             steps[i] = c.getName();
266                         }
267                         if (c instanceof JComponent JavaDoc) {
268                             JComponent JavaDoc jc = (JComponent JavaDoc)c;
269                             jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
270
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
271
}
272                     }
273                     ((CommonAttributePanel)panels[0]).setInitialFocus();
274                 }
275             }
276         }
277     }
278     
279     protected void addPanels() {
280         if (panels != null && morePanels == null) {
281             this.cphelper = this.holder.addAssociatedHelper();
282             this.cphelper.getData().setResourceName(__JdbcConnectionPool);
283             this.holder.setHasCPHelper(true);
284             this.cpWizardInfo = getWizardInfo(CP_DATAFILE);
285             this.cphelper.getData().setTargetFileObject(this.helper.getData().getTargetFileObject());
286             this.cphelper.getData().setString(__DynamicWizPanel, "true"); //NOI18N
287

288             morePanels = new WizardDescriptor.Panel[] {
289                 panels[0],
290                 panels[1],
291                 new CPVendorPanel(this.cphelper, this.cpWizardInfo),
292                 new CPPropertiesPanelPanel(this.cphelper, this.cpWizardInfo),
293                 new CommonAttributePanel(this.cphelper, this.cpWizardInfo, new String JavaDoc[] {"pool-setting", "pool-setting-2", "pool-setting-3"}), //NOI18N
294
};
295         }
296         dsPanels = panels;
297         panels = morePanels;
298     }
299         
300     protected void addSteps() {
301         if (steps != null && moreSteps == null) {
302             moreSteps = new String JavaDoc[] {
303                 steps[0],
304                 steps[1],
305                 steps[2],
306                 NbBundle.getMessage(DataSourceWizard.class, "TITLE_ConnPoolWizardPanel_dbConn"), // NOI18N
307
NbBundle.getMessage(DataSourceWizard.class, "TITLE_ConnPoolWizardPanel_properties"), // NOI18N
308
NbBundle.getMessage(DataSourceWizard.class, "TITLE_ConnPoolWizardPanel_optionalProps") // NOI18N
309
};
310         }
311         dsSteps = steps;
312         steps = moreSteps;
313     }
314 }
315
Popular Tags