KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > templates > SunDDWizardIterator


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 package org.netbeans.modules.j2ee.sun.share.configbean.templates;
20
21 import java.awt.Component JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29
30 import org.openide.DialogDisplayer;
31 import org.openide.ErrorManager;
32 import org.openide.NotifyDescriptor;
33 import org.openide.WizardDescriptor;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileSystem;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.Repository;
38 import org.openide.util.NbBundle;
39
40 import org.netbeans.api.project.Project;
41 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
42 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
43 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration;
44
45
46 /*
47  *
48  * @author Peter Williams
49  */

50 public final class SunDDWizardIterator implements WizardDescriptor.InstantiatingIterator {
51     
52     private int index;
53     
54     private WizardDescriptor wizard;
55     private WizardDescriptor.Panel[] panels;
56     
57     /**
58      * Initialize panels representing individual wizard's steps and sets
59      * various properties for them influencing wizard appearance.
60      */

61     private WizardDescriptor.Panel[] getPanels() {
62         if (panels == null) {
63             panels = new WizardDescriptor.Panel[] {
64                 new SunDDWizardPanel()
65             };
66             String JavaDoc[] steps = createSteps();
67             for (int i = 0; i < panels.length; i++) {
68                 Component JavaDoc c = panels[i].getComponent();
69                 if (steps[i] == null) {
70                     // Default step name to component name of panel. Mainly
71
// useful for getting the name of the target chooser to
72
// appear in the list of steps.
73
steps[i] = c.getName();
74                 }
75                 if (c instanceof JComponent JavaDoc) { // assume Swing components
76
JComponent JavaDoc jc = (JComponent JavaDoc) c;
77                     // Sets step number of a component
78
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
79
// Sets steps names for a panel
80
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
81
// Turn on subtitle creation on each step
82
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
83
// Show steps on the left side with the image on the background
84
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
85
// Turn on numbering of all steps
86
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
87
}
88             }
89         }
90         return panels;
91     }
92     
93     public Set JavaDoc instantiate() throws IOException JavaDoc {
94         Set JavaDoc result = Collections.EMPTY_SET;
95         SunDDWizardPanel wizardPanel = (SunDDWizardPanel) panels[0];
96         
97         File JavaDoc configDir = wizardPanel.getSelectedLocation();
98         if(!configDir.exists()) {
99             configDir.mkdirs();
100         }
101         FileObject configFolder = FileUtil.toFileObject(configDir);
102         Project project = wizardPanel.getProject();
103         String JavaDoc sunDDFileName = wizardPanel.getFileName();
104
105         J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
106         J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule();
107         if(configFolder != null) {
108             String JavaDoc resource = "org-netbeans-modules-j2ee-sun-ddui/" + sunDDFileName; // NOI18N
109
FileObject sunDDTemplate = Repository.getDefault().getDefaultFileSystem().findResource(resource);
110             if(sunDDTemplate != null) {
111                 FileSystem fs = configFolder.getFileSystem();
112                 XmlFileCreator creator = new XmlFileCreator(sunDDTemplate, configFolder,
113                         sunDDTemplate.getName(), sunDDTemplate.getExt());
114                 fs.runAtomicAction(creator);
115                 FileObject sunDDFO = creator.getResult();
116                 if(sunDDFO != null) {
117                     SunONEDeploymentConfiguration config =
118                             SunONEDeploymentConfiguration.getConfiguration(FileUtil.toFile(sunDDFO));
119                     if(config != null) {
120                         // Set version of target configuration file we just saved to maximum supported version.
121
config.setAppServerVersion(config.getMaxASVersion());
122                     } else {
123                         NotifyDescriptor nd = new NotifyDescriptor.Message(
124                                 NbBundle.getMessage(SunDDWizardIterator.class,"ERR_NoDeploymentConfiguration"), // NOI18N
125
NotifyDescriptor.ERROR_MESSAGE);
126                         DialogDisplayer.getDefault().notify(nd);
127                     }
128                     result = Collections.singleton(creator.getResult());
129                 } else {
130                     NotifyDescriptor nd = new NotifyDescriptor.Message(
131                             NbBundle.getMessage(SunDDWizardIterator.class,"ERR_FileCreationFailed", sunDDFileName), // NOI18N
132
NotifyDescriptor.ERROR_MESSAGE);
133                     DialogDisplayer.getDefault().notify(nd);
134                 }
135             } else {
136                 NotifyDescriptor nd = new NotifyDescriptor.Message(
137                         NbBundle.getMessage(SunDDWizardIterator.class,"ERR_TemplateNotFound", resource), // NOI18N
138
NotifyDescriptor.ERROR_MESSAGE);
139                 DialogDisplayer.getDefault().notify(nd);
140             }
141         } else {
142             NotifyDescriptor nd = new NotifyDescriptor.Message(
143                     NbBundle.getMessage(SunDDWizardIterator.class,"ERR_LocationNotFound", configDir.getAbsolutePath()), // NOI18N
144
NotifyDescriptor.ERROR_MESSAGE);
145             DialogDisplayer.getDefault().notify(nd);
146         }
147         return result;
148     }
149     
150     private static class XmlFileCreator implements FileSystem.AtomicAction {
151         
152         private final FileObject source;
153         private final FileObject destFolder;
154         private final String JavaDoc name;
155         private final String JavaDoc ext;
156         private FileObject result;
157         
158         XmlFileCreator(final FileObject source, final FileObject destFolder, final String JavaDoc name, final String JavaDoc ext) {
159             this.source = source;
160             this.destFolder = destFolder;
161             this.name = name;
162             this.ext = ext;
163             this.result = null;
164         }
165         
166         public void run() throws IOException JavaDoc {
167             result = FileUtil.copyFile(source, destFolder, name, ext);
168         }
169         
170         public FileObject getResult() {
171             return result;
172         }
173     }
174     
175     public void initialize(WizardDescriptor wizard) {
176         this.wizard = wizard;
177     }
178     
179     public void uninitialize(WizardDescriptor wizard) {
180         panels = null;
181     }
182     
183     public WizardDescriptor.Panel current() {
184         return getPanels()[index];
185     }
186     
187     public String JavaDoc name() {
188         return index + 1 + ". from " + getPanels().length;
189     }
190     
191     public boolean hasNext() {
192         return index < getPanels().length - 1;
193     }
194     
195     public boolean hasPrevious() {
196         return index > 0;
197     }
198     
199     public void nextPanel() {
200         if (!hasNext()) {
201             throw new NoSuchElementException JavaDoc();
202         }
203         index++;
204     }
205     
206     public void previousPanel() {
207         if (!hasPrevious()) {
208             throw new NoSuchElementException JavaDoc();
209         }
210         index--;
211     }
212     
213     // If nothing unusual changes in the middle of the wizard, simply:
214
public void addChangeListener(ChangeListener JavaDoc l) {}
215     public void removeChangeListener(ChangeListener JavaDoc l) {}
216     
217     // If something changes dynamically (besides moving between panels), e.g.
218
// the number of panels changes in response to user input, then uncomment
219
// the following and call when needed: fireChangeEvent();
220
/*
221     private Set<ChangeListener> listeners = new HashSet<ChangeListener>(1);
222     public final void addChangeListener(ChangeListener l) {
223         synchronized (listeners) {
224             listeners.add(l);
225         }
226     }
227     public final void removeChangeListener(ChangeListener l) {
228         synchronized (listeners) {
229             listeners.remove(l);
230         }
231     }
232     protected final void fireChangeEvent() {
233         Iterator<ChangeListener> it;
234         synchronized (listeners) {
235             it = new HashSet<ChangeListener>(listeners).iterator();
236         }
237         ChangeEvent ev = new ChangeEvent(this);
238         while (it.hasNext()) {
239             it.next().stateChanged(ev);
240         }
241     }
242      */

243     
244     // You could safely ignore this method. Is is here to keep steps which were
245
// there before this wizard was instantiated. It should be better handled
246
// by NetBeans Wizard API itself rather than needed to be implemented by a
247
// client code.
248
private String JavaDoc[] createSteps() {
249         String JavaDoc[] beforeSteps = null;
250         Object JavaDoc prop = wizard.getProperty("WizardPanel_contentData"); // NOI18N
251
if (prop != null && prop instanceof String JavaDoc[]) {
252             beforeSteps = (String JavaDoc[]) prop;
253         }
254         
255         if (beforeSteps == null) {
256             beforeSteps = new String JavaDoc[0];
257         }
258         
259         String JavaDoc[] res = new String JavaDoc[(beforeSteps.length - 1) + panels.length];
260         for (int i = 0; i < res.length; i++) {
261             if (i < (beforeSteps.length - 1)) {
262                 res[i] = beforeSteps[i];
263             } else {
264                 res[i] = panels[i - beforeSteps.length + 1].getComponent().getName();
265             }
266         }
267         return res;
268     }
269 }
270
Popular Tags