/*
* The contents of this file are subject to the terms of the Common Development
* and Distribution License (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html
* or http://www.netbeans.org/cddl.txt.
*
* When distributing Covered Code, include this CDDL Header Notice in each file
* and include the License file at http://www.netbeans.org/cddl.txt.
* If applicable, add the following below the CDDL Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.gwt4nb;
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.JComponent;
import javax.swing.event.ChangeListener;
import org.netbeans.spi.project.ui.support.ProjectChooser;
import org.netbeans.spi.project.ui.templates.support.Templates;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;
/**
* Iterator for creating a new GWT/GAE/Maven/JDO project.
*/
public class GWTGAEProjectWizardIterator implements WizardDescriptor./*Progress*/InstantiatingIterator {
private int index;
private WizardDescriptor.Panel[] panels;
private WizardDescriptor wiz;
public static GWTGAEProjectWizardIterator createIterator() {
return new GWTGAEProjectWizardIterator();
}
private WizardDescriptor.Panel[] createPanels() {
return new WizardDescriptor.Panel[]{
new GWTGAEProjectWizardPanel(),};
}
private String[] createSteps() {
return new String[] {
NbBundle.getMessage(GWTGAEProjectWizardIterator.class,
"LBL_CreateProjectStep") // NOI18N
};
}
public Set<FileObject> instantiate(/*ProgressHandle handle*/)
throws IOException {
Set<FileObject> r = new LinkedHashSet<FileObject>();
File dirF = FileUtil.normalizeFile((File) wiz.getProperty(
"projdir")); // NOI18N
if (!dirF.mkdirs()) {
GWT4NBUtil.unexpectedException(new IOException(
"Cannnot create " + dirF)); // NOI18N
return r;
}
FileObject template = Templates.getTemplate(wiz);
FileObject dir = FileUtil.toFileObject(dirF);
unZipFile(template.getInputStream(), dir);
// Always open top dir as a project:
r.add(dir);
// Look for nested projects to open as well:
/*Enumeration<? extends FileObject> e = dir.getFolders(true);
while (e.hasMoreElements()) {
FileObject subfolder = e.nextElement();
if (ProjectManager.getDefault().isProject(subfolder)) {
resultSet.add(subfolder);
}
}*/
File parent = dirF.getParentFile();
if (parent != null && parent.exists()) {
ProjectChooser.setProjectsFolder(parent);
}
return r;
}
public void initialize(WizardDescriptor wiz) {
this.wiz = wiz;
index = 0;
panels = createPanels();
// Make sure list of steps is accurate.
String[] steps = createSteps();
for (int i = 0; i < panels.length; i++) {
Component c = panels[i].getComponent();
if (steps[i] == null) {
// Default step name to component name of panel.
// Mainly useful for getting the name of the target
// chooser to appear in the list of steps.
steps[i] = c.getName();
}
if (c instanceof JComponent) { // assume Swing components
JComponent jc = (JComponent) c;
// Step #.
// TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
jc.putClientProperty("WizardPanel_contentSelectedIndex", // NOI18N
new Integer(i));
// Step name (actually the whole list for reference).
jc.putClientProperty("WizardPanel_contentData", // NOI18N
steps);
}
}
}
public void uninitialize(WizardDescriptor wiz) {
this.wiz.putProperty("projdir", null); // NOI18N
this.wiz.putProperty("name", null); // NOI18N
this.wiz = null;
panels = null;
}
public String name() {
return MessageFormat.format(org.openide.util.NbBundle.getMessage(
GWTGAEProjectWizardIterator.class, "Of"), // NOI18N
new Object[]{new Integer(index + 1),
new Integer(panels.length)});
}
public boolean hasNext() {
return index < panels.length - 1;
}
public boolean hasPrevious() {
return index > 0;
}
public void nextPanel() {
if (!hasNext()) {
throw new NoSuchElementException();
}
index++;
}
public void previousPanel() {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
index--;
}
public WizardDescriptor.Panel current() {
return panels[index];
}
public final void addChangeListener(ChangeListener l) {
}
public final void removeChangeListener(ChangeListener l) {
}
private static void unZipFile(InputStream source,
FileObject projectRoot) throws IOException {
try {
ZipInputStream str = new ZipInputStream(source);
ZipEntry entry;
while ((entry = str.getNextEntry()) != null) {
if (entry.isDirectory()) {
FileUtil.createFolder(projectRoot, entry.getName());
} else {
FileObject fo = FileUtil.createData(projectRoot,
entry.getName());
OutputStream out = fo.getOutputStream();
try {
FileUtil.copy(str, out);
} finally {
out.close();
}
}
}
} finally {
source.close();
}
}
}