KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PMFWizard.java
21  *
22  * Created on October 10, 2003, 6:57 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.ide.sunresources.wizards;
26
27 import org.openide.WizardDescriptor;
28 import org.openide.util.NbBundle;
29 import org.openide.filesystems.FileObject;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.spi.project.ui.templates.support.Templates;
32
33 import java.util.Set JavaDoc;
34 import java.awt.Component JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
39
40 import org.netbeans.modules.j2ee.sun.sunresources.beans.Wizard;
41 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants;
42 import org.openide.ErrorManager;
43
44 /**
45  *
46  * @author nityad
47  */

48 public final class PMFWizard implements WizardDescriptor.InstantiatingIterator, ChangeListener JavaDoc, WizardConstants{
49         
50     private static Project project;
51     
52     /** An array of all wizard panels */
53     private WizardDescriptor.Panel panels[];
54     private transient WizardDescriptor.Panel[] morePanels = null; //datasource panels
55
private transient WizardDescriptor.Panel[] moreCPPanels = null;
56     private transient WizardDescriptor.Panel[] pmPanels = null;
57     private transient int index;
58     
59 // private transient WizardDescriptor wiz;
60

61     private static final String JavaDoc DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/PMFWizard.xml"; //NOI18N
62
private static final String JavaDoc DS_DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/DSWizard.xml"; //NOI18N
63
private static final String JavaDoc CP_DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/CPWizard.xml"; //NOI18N
64

65     private static final int IN_PM = 1;
66     private static final int IN_DS = 2;
67     private static final int IN_CP = 3;
68     private int stage = IN_PM;
69
70     private ResourceConfigHelper helper;
71     private ResourceConfigHelper dshelper;
72     private ResourceConfigHelper cphelper;
73     private ResourceConfigHelperHolder holder;
74   
75     private Wizard wizardInfo;
76     private Wizard dsWizardInfo;
77     private Wizard cpWizardInfo;
78   
79     private boolean addSteps = false;
80 // private boolean addStepsCP = false;
81

82     private transient String JavaDoc[] steps = null;
83     private transient String JavaDoc[] moreSteps = null;
84     private transient String JavaDoc[] moreCPSteps = null;
85     private transient String JavaDoc[] pmSteps = null;
86         
87     /** Creates a new instance of PMFWizard */
88     public static PMFWizard create () {
89         return new PMFWizard ();
90     }
91
92     private WizardDescriptor.Panel[] createPanels() {
93         WizardDescriptor.Panel[] tempPanels = null;
94         WizardDescriptor.Panel panel = new CommonAttributePanel(helper, wizardInfo, new String JavaDoc[] {"general"}); //NOI18N
95
panel.addChangeListener(this);
96         if(stage == IN_PM){
97             morePanels = null;
98             tempPanels = new WizardDescriptor.Panel[] {
99                 panel,
100                 new CommonPropertyPanel(this.helper, this.wizardInfo),
101             };
102         }else if(stage == IN_DS){
103             moreCPPanels = null;
104             CommonAttributePanel dspan = new CommonAttributePanel(this.dshelper, this.dsWizardInfo, new String JavaDoc[] {"general"}); //NOI18N
105
dspan.addChangeListener(this);
106             tempPanels = new WizardDescriptor.Panel[] {
107                 panel,
108                 new CommonPropertyPanel(this.helper, this.wizardInfo),
109                 dspan,
110                 new CommonPropertyPanel(this.dshelper, this.wizardInfo),
111             };
112         }
113         return tempPanels;
114     }
115     
116     static private String JavaDoc CONST_ADD_PROPERTY = "LBL_AddProperty"; // NOI18N
117

118     private String JavaDoc[] createSteps() {
119         String JavaDoc[] tempSteps = null;
120         if(stage == IN_PM){
121             moreSteps = null;
122             tempSteps = new String JavaDoc[] {
123                 NbBundle.getMessage(PMFWizard.class,__FirstStepChoose),
124                 NbBundle.getMessage(PMFWizard.class, "LBL_GeneralAttributes_PM"), // NOI18N
125
NbBundle.getMessage(PMFWizard.class, CONST_ADD_PROPERTY),
126             };
127         }else if(stage == IN_DS){
128             moreCPSteps = null;
129             tempSteps = new String JavaDoc[] {
130                 NbBundle.getMessage(PMFWizard.class,__FirstStepChoose),
131                 NbBundle.getMessage(PMFWizard.class, "LBL_GeneralAttributes_PM"), // NOI18N
132
NbBundle.getMessage(PMFWizard.class, CONST_ADD_PROPERTY),
133                 NbBundle.getMessage(PMFWizard.class, "LBL_GeneralAttributes_DS"), // NOI18N
134
NbBundle.getMessage(PMFWizard.class, CONST_ADD_PROPERTY),
135             };
136         }
137         return tempSteps;
138     }
139     
140     public Set JavaDoc instantiate(){
141         try{
142             if(this.holder.hasDSHelper()){
143                 FileObject fo = this.helper.getData().getTargetFileObject();
144                 String JavaDoc jdbcName = this.dshelper.getData().getString(__JndiName);
145                 this.helper.getData().setString(__JdbcResourceJndiName, jdbcName);
146                 this.dshelper.getData().setTargetFile(jdbcName);
147                 this.dshelper.getData().setTargetFileObject(fo);
148                 
149                 if(this.holder.hasCPHelper()){
150                     String JavaDoc poolName = this.cphelper.getData().getString(__Name);
151                     this.dshelper.getData().setString(__PoolName, poolName);
152                     this.cphelper.getData().setTargetFile(poolName);
153                     this.cphelper.getData().setTargetFileObject(fo);
154                     ResourceUtils.savePMFResourceDatatoXml(this.helper.getData(), this.dshelper.getData(), this.cphelper.getData());
155                 }else{
156                     //FIXME
157
//need to check to make sure that dsData has a value for pool name
158
ResourceUtils.savePMFResourceDatatoXml(this.helper.getData(), this.dshelper.getData(), null);
159                 }
160             }else{
161                 ResourceUtils.savePMFResourceDatatoXml(this.helper.getData(), null, null);
162             }
163         }catch (Exception JavaDoc ex){
164                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
165                                         ex);
166         }
167         return java.util.Collections.EMPTY_SET;
168     }
169         
170     public void initialize(WizardDescriptor wiz){
171         this.wizardInfo = getWizardInfo(DATAFILE);
172         this.holder = new ResourceConfigHelperHolder();
173         this.helper = holder.getPMFHelper();
174         
175  // this.wiz = wiz;
176
wiz.putProperty("NewFileWizard_Title", NbBundle.getMessage(PMFWizard.class, "Templates/SunResources/Persistence_Resource")); //NOI18N
177
index = 0;
178         panels = createPanels();
179         
180         // Make sure list of steps is accurate.
181
steps = createSteps();
182         
183         project = Templates.getProject(wiz);
184         try{
185             FileObject pkgLocation = project.getProjectDirectory();
186             if (pkgLocation != null) {
187                 this.helper.getData().setTargetFileObject(pkgLocation);
188             }
189         }catch (Exception JavaDoc ex){
190                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
191                                         ex);
192         }
193         this.helper.getData().setHolder(this.holder);
194         
195         for (int i = 0; i < panels.length; i++) {
196             Component JavaDoc c = panels[i].getComponent();
197             if (c instanceof JComponent JavaDoc) { // assume Swing components
198
JComponent JavaDoc jc = (JComponent JavaDoc)c;
199                 // Step #.
200
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
201
// Step name (actually the whole list for reference).
202
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
203
}
204         }
205     }
206     
207     public void uninitialize(WizardDescriptor wiz){
208  // this.wiz = null;
209
panels = null;
210     }
211     
212     public Wizard getWizardInfo(String JavaDoc filePath){
213         Wizard wizInfo = null;
214         try{
215             InputStream JavaDoc in = Wizard.class.getClassLoader().getResourceAsStream(filePath);
216             wizInfo = Wizard.createGraph(in);
217         }catch(Exception JavaDoc ex){
218                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
219                                         ex);
220         }
221         return wizInfo;
222     }
223     
224     public String JavaDoc name(){
225         return NbBundle.getMessage(PMFWizard.class, "Templates/SunResources/Persistence_Resource"); // NOI18N
226
}
227     
228     public boolean hasNext(){
229         boolean retVal = false;
230         if (stage == IN_PM && index == 0 && addSteps)
231             retVal = true;
232         else
233             retVal = index < panels.length - 1;
234         return retVal;
235     }
236     
237     public boolean hasPrevious(){
238         return index > 0;
239     }
240
241     public synchronized void nextPanel(){
242         if (index + 1 == panels.length) {
243             throw new java.util.NoSuchElementException JavaDoc();
244         }
245         if (index == 0) {
246             ((CommonPropertyPanel) panels[1]).setInitialFocus();
247         }else if(index == 1){
248             ((CommonAttributePanel) panels[2]).setInitialFocus();
249         }else if (index == 2) {
250             ((CommonPropertyPanel) panels[3]).setInitialFocus();
251         }else if (index == 3) {
252             ((CPVendorPanel) panels[4]).setInitialFocus();
253         }else if (index == 4){
254             ((CPPropertiesPanelPanel) panels[5]).refreshFields();
255         }else if (index == 5){
256             ((CommonAttributePanel) panels[6]).setPropInitialFocus();
257         }
258         
259         index ++;
260         
261         if (stage == IN_PM && index == 2) {
262             stage = IN_DS;
263         }else if(stage == IN_DS && index == 4){
264             stage = IN_CP;
265         }
266     }
267     
268     public synchronized void previousPanel(){
269         if (index == 0) {
270             throw new java.util.NoSuchElementException JavaDoc();
271         }
272         
273         index--;
274         //Required to refresh number of panels that need to be displayed
275
if( (stage == IN_DS) && (index < 2)){
276             stage = IN_PM;
277         }else{
278             if((stage == IN_CP) && (index < 4)) {
279                 stage = IN_DS;
280             }
281         }
282     }
283     
284     public WizardDescriptor.Panel current(){
285         return (WizardDescriptor.Panel)panels[index];
286     }
287     
288     public final void addChangeListener(ChangeListener JavaDoc l) {
289     }
290     public final void removeChangeListener(ChangeListener JavaDoc l) {
291     }
292     
293     public void setResourceConfigHelper(ResourceConfigHelper helper){
294         this.helper = helper;
295     }
296     
297     public ResourceConfigHelper getResourceConfigHelper(){
298         return this.helper;
299     }
300     
301     public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
302         if( (e.getSource().getClass() == CommonAttributePanel.class) || (e.getSource().getClass() == CommonAttributeVisualPanel.class) ) {
303             CommonAttributePanel commonPane = (CommonAttributePanel)this.current();
304             CommonAttributeVisualPanel visPane = (CommonAttributeVisualPanel)commonPane.getComponent();
305 // boolean oldVal = addSteps;
306
this.addSteps = visPane.isNewResourceSelected();
307             
308             WizardDescriptor.Panel[] tempPanels = null;
309             if(this.addSteps){
310                 if (stage == IN_PM){
311                     this.holder.setHasDSHelper(this.addSteps);
312                     tempPanels = morePanels;
313                 }else if(stage == IN_DS){
314                     this.holder.setHasCPHelper(this.addSteps);
315                     tempPanels = moreCPPanels;
316                 }
317             }else{
318                 if (stage == IN_PM){
319                     this.holder.setHasDSHelper(this.addSteps);
320                     tempPanels = morePanels;
321                 }else if(stage == IN_DS){
322                     this.holder.setHasCPHelper(this.addSteps);
323                     tempPanels = moreCPPanels;
324                 }
325             }
326             
327             if (addSteps && tempPanels == null) {
328                 addPanels();
329                 addSteps();
330                 for (int i = 0; i < panels.length; i++) {
331                     Component JavaDoc c = panels[i].getComponent();
332                     if (c instanceof JComponent JavaDoc) {
333                         JComponent JavaDoc jc = (JComponent JavaDoc)c;
334                         jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
335
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
336
}
337                 }
338             }else if((!addSteps) && (tempPanels != null) ){
339                 if((stage == IN_PM) && (pmPanels != null)){
340                     morePanels = null;
341                     panels = pmPanels;
342                 }else if((stage == IN_DS) && (morePanels != null)){
343                     moreCPPanels = null;
344                     panels = morePanels;
345                 }else
346                     panels = createPanels();
347                 
348                 if((stage == IN_PM) && (pmSteps != null)){
349                     moreSteps = null;
350                     steps = pmSteps;
351                 }else if((stage == IN_DS) && (moreSteps != null)){
352                     moreCPSteps = null;
353                     steps = moreSteps;
354                 }else
355                     steps = createSteps();
356                 
357                 for (int i = 0; i < panels.length; i++) {
358                     Component JavaDoc c = panels[i].getComponent();
359                     if (c instanceof JComponent JavaDoc) {
360                         JComponent JavaDoc jc = (JComponent JavaDoc)c;
361                         jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
362
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
363
}
364                 }
365                 
366                 if(stage == IN_PM){
367                     ((CommonAttributePanel)panels[0]).setInitialFocus();
368                 }else if(stage == IN_DS){
369                     ((CommonAttributePanel)panels[2]).setInitialFocus();
370                 }
371             }
372         } //Is CommonAttributePanel
373

374     }
375     
376     protected void addPanels() {
377         if (stage == IN_PM){
378             if (panels != null && morePanels == null) {
379                 this.dshelper = this.holder.addAssociatedHelper();
380                 this.dshelper.getData().setResourceName(__JdbcResource);
381                 this.holder.setHasDSHelper(true);
382                 this.dsWizardInfo = getWizardInfo(DS_DATAFILE);
383                 this.dshelper.getData().setTargetFileObject(this.helper.getData().getTargetFileObject());
384                 this.dshelper.getData().setString(__DynamicWizPanel, "true"); //NOI18N
385

386                 CommonAttributePanel dspan = new CommonAttributePanel(this.dshelper, this.dsWizardInfo, new String JavaDoc[] {"general"}); //NOI18N
387
dspan.addChangeListener(this);
388                 morePanels = new WizardDescriptor.Panel[] {
389                     panels[0],
390                     panels[1],
391                     dspan,
392                     new CommonPropertyPanel(this.dshelper, this.wizardInfo),
393                 };
394             }
395             pmPanels = panels;
396             panels = morePanels;
397         }else if(stage == IN_DS){
398             if (panels != null && moreCPPanels == null) {
399                 this.cphelper = this.holder.addAssociatedHelper();
400                 this.cphelper.getData().setResourceName(__JdbcConnectionPool);
401                 this.holder.setHasCPHelper(true);
402                 this.cpWizardInfo = getWizardInfo(CP_DATAFILE);
403                 this.cphelper.getData().setTargetFileObject(this.helper.getData().getTargetFileObject());
404                 this.cphelper.getData().setString(__DynamicWizPanel, "true"); //NOI18N
405

406                 moreCPPanels = new WizardDescriptor.Panel[] {
407                     panels[0],
408                     panels[1],
409                     panels[2],
410                     panels[3],
411                     new CPVendorPanel(this.cphelper, this.cpWizardInfo),
412                     new CPPropertiesPanelPanel(this.cphelper, this.cpWizardInfo),
413                     new CommonAttributePanel(this.cphelper, this.cpWizardInfo, new String JavaDoc[] {"pool-setting", "pool-setting-2", "pool-setting-3"}), //NOI18N
414
};
415             }
416             //morePanels = panels; //--??
417
panels = moreCPPanels;
418         }
419     }
420     
421     protected void addSteps() {
422         if (stage == IN_PM){
423             if (steps != null && moreSteps == null) {
424                 moreSteps = new String JavaDoc[] {
425                     steps[0],
426                     steps[1],
427                     steps[2],
428                     NbBundle.getMessage(PMFWizard.class, "LBL_GeneralAttributes_DS"), // NOI18N
429
NbBundle.getMessage(PMFWizard.class, CONST_ADD_PROPERTY),
430                 };
431             }
432             pmSteps = steps;
433             steps = moreSteps;
434         }else if(stage == IN_DS){
435             if (steps != null && moreCPSteps == null) {
436                 moreCPSteps = new String JavaDoc[] {
437                     steps[0],
438                     steps[1],
439                     steps[2],
440                     steps[3],
441                     steps[4],
442                     NbBundle.getMessage(PMFWizard.class, "TITLE_ConnPoolWizardPanel_dbConn"), // NOI18N
443
NbBundle.getMessage(PMFWizard.class, "TITLE_ConnPoolWizardPanel_properties"), // NOI18N
444
NbBundle.getMessage(PMFWizard.class, "TITLE_ConnPoolWizardPanel_optionalProps") // NOI18N
445
};
446             }
447             //moreSteps = steps; //--??
448
steps = moreCPSteps;
449         }
450     }
451
452 }
453
Popular Tags