KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > webproject > wizard > PHPWebProjectWizardIterator


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.webproject.wizard;
21
22 import org.netbeans.modules.scripting.php.webproject.PhpProjectProperties;
23 import java.awt.Component JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import java.util.LinkedHashSet JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33 import java.util.zip.ZipInputStream JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import org.netbeans.modules.scripting.php.webproject.PhpProject;
37 import org.netbeans.api.project.ProjectManager;
38 import org.netbeans.spi.project.ui.support.ProjectChooser;
39 import org.netbeans.spi.project.ui.templates.support.Templates;
40 import org.openide.WizardDescriptor;
41 import org.openide.filesystems.FileLock;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.util.NbBundle;
45
46 public class PHPWebProjectWizardIterator implements WizardDescriptor./*Progress*/InstantiatingIterator {
47     
48     private int index;
49     private WizardDescriptor.Panel[] panels;
50     private WizardDescriptor wiz;
51     
52     public PHPWebProjectWizardIterator() {}
53     
54     public static PHPWebProjectWizardIterator createIterator() {
55         return new PHPWebProjectWizardIterator();
56     }
57     
58     private WizardDescriptor.Panel[] createPanels() {
59         return new WizardDescriptor.Panel[] {
60             new PHPWebProjectWizardPanel(),
61         };
62     }
63     
64     private String JavaDoc[] createSteps() {
65         return new String JavaDoc[] {
66             NbBundle.getMessage(PHPWebProjectWizardIterator.class, "LBL_CreateProjectStep")
67         };
68     }
69     
70     public Set JavaDoc<FileObject> instantiate(/*ProgressHandle handle*/) throws IOException JavaDoc {
71         Set JavaDoc<FileObject> resultSet = new LinkedHashSet JavaDoc<FileObject>();
72         File JavaDoc dirF = FileUtil.normalizeFile((File JavaDoc) wiz.getProperty(PhpProjectProperties.PROJECT_DIR));
73         dirF.mkdirs();
74         
75         FileObject template = Templates.getTemplate(wiz);
76         FileObject dir = FileUtil.toFileObject(dirF);
77         unZipFile(template.getInputStream(), dir);
78         
79         // Rename the initial php file from index.php to the desired name if that's different.
80
String JavaDoc welcomePage = (String JavaDoc)wiz.getProperty(PhpProjectProperties.WELCOME_PAGE);
81         
82         if (welcomePage != null && !welcomePage.equals(PhpProjectProperties.DEFAULT_START_PAGE)) {
83             File JavaDoc welcomePageF = new File JavaDoc(dirF, PhpProjectProperties.DEFAULT_START_PAGE);
84             File JavaDoc newWelcomePageF = new File JavaDoc(dirF, welcomePage);
85             
86             welcomePageF.renameTo(newWelcomePageF);
87         }
88         
89         // Always open top dir as a project:
90
resultSet.add(dir);
91         
92         PhpProject project = (PhpProject)ProjectManager.getDefault().findProject(dir);
93         
94         project.setProperty(PhpProjectProperties.NAME,
95                 (String JavaDoc)wiz.getProperty(PhpProjectProperties.NAME));
96         project.setProperty(PhpProjectProperties.TARGET_DIR,
97                 (String JavaDoc)wiz.getProperty(PhpProjectProperties.TARGET_DIR));
98         project.setProperty(PhpProjectProperties.URL,
99                 (String JavaDoc)wiz.getProperty(PhpProjectProperties.URL));
100         project.setProperty(PhpProjectProperties.USE_SOURCE_DIR,
101                 ((Boolean JavaDoc)wiz.getProperty(PhpProjectProperties.USE_SOURCE_DIR)).toString());
102         project.setProperty(PhpProjectProperties.WELCOME_PAGE, welcomePage);
103         project.save();
104         
105         File JavaDoc parent = dirF.getParentFile();
106         if (parent != null && parent.exists()) {
107             ProjectChooser.setProjectsFolder(parent);
108         }
109         
110         return resultSet;
111     }
112     
113     public void initialize(WizardDescriptor wiz) {
114         this.wiz = wiz;
115         index = 0;
116         panels = createPanels();
117         // Make sure list of steps is accurate.
118
String JavaDoc[] steps = createSteps();
119         for (int i = 0; i < panels.length; i++) {
120             Component JavaDoc c = panels[i].getComponent();
121             if (steps[i] == null) {
122                 // Default step name to component name of panel.
123
// Mainly useful for getting the name of the target
124
// chooser to appear in the list of steps.
125
steps[i] = c.getName();
126             }
127             if (c instanceof JComponent JavaDoc) { // assume Swing components
128
JComponent JavaDoc jc = (JComponent JavaDoc) c;
129                 // Step #.
130
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i));
131                 // Step name (actually the whole list for reference).
132
jc.putClientProperty("WizardPanel_contentData", steps);
133             }
134         }
135     }
136     
137     public void uninitialize(WizardDescriptor wiz) {
138         this.wiz.putProperty(PhpProjectProperties.PROJECT_DIR, null);
139         this.wiz.putProperty(PhpProjectProperties.NAME, null);
140         this.wiz = null;
141         panels = null;
142     }
143     
144     public String JavaDoc name() {
145         return MessageFormat.format("{0} of {1}",
146                 new Object JavaDoc[] {new Integer JavaDoc(index + 1), new Integer JavaDoc(panels.length)});
147     }
148     
149     public boolean hasNext() {
150         return index < panels.length - 1;
151     }
152     
153     public boolean hasPrevious() {
154         return index > 0;
155     }
156     
157     public void nextPanel() {
158         if (!hasNext()) {
159             throw new NoSuchElementException JavaDoc();
160         }
161         index++;
162     }
163     
164     public void previousPanel() {
165         if (!hasPrevious()) {
166             throw new NoSuchElementException JavaDoc();
167         }
168         index--;
169     }
170     
171     public WizardDescriptor.Panel current() {
172         return panels[index];
173     }
174     
175     // If nothing unusual changes in the middle of the wizard, simply:
176
public final void addChangeListener(ChangeListener JavaDoc l) {}
177     public final void removeChangeListener(ChangeListener JavaDoc l) {}
178     
179     private static void unZipFile(InputStream JavaDoc source, FileObject projectRoot) throws IOException JavaDoc {
180         try {
181             ZipInputStream JavaDoc str = new ZipInputStream JavaDoc(source);
182             ZipEntry JavaDoc entry;
183             while ((entry = str.getNextEntry()) != null) {
184                 if (entry.isDirectory()) {
185                     FileUtil.createFolder(projectRoot, entry.getName());
186                 } else {
187                     FileObject fo = FileUtil.createData(projectRoot, entry.getName());
188                     FileLock lock = fo.lock();
189                     try {
190                         OutputStream JavaDoc out = fo.getOutputStream(lock);
191                         try {
192                             FileUtil.copy(str, out);
193                         } finally {
194                             out.close();
195                         }
196                     } finally {
197                         lock.releaseLock();
198                     }
199                 }
200             }
201         } finally {
202             source.close();
203         }
204     }
205     
206 }
207
Popular Tags