org.praxislive.ide.project.templates.TemplateProjectWizardIterator Maven / Gradle / Ivy
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2021 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details.
*
* You should have received a copy of the GNU General Public License version 3
* along with this work; if not, see http://www.gnu.org/licenses/
*
*
* Please visit https://www.praxislive.org if you need additional information or
* have any questions.
*/
package org.praxislive.ide.project.templates;
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Set;
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;
public class TemplateProjectWizardIterator implements WizardDescriptor./*Progress*/InstantiatingIterator {
private int index;
private WizardDescriptor.Panel[] panels;
private WizardDescriptor wiz;
public TemplateProjectWizardIterator() {
}
public static TemplateProjectWizardIterator createIterator() {
return new TemplateProjectWizardIterator();
}
private WizardDescriptor.Panel[] createPanels() {
return new WizardDescriptor.Panel[]{
new TemplateProjectWizardPanel(),};
}
private String[] createSteps() {
return new String[]{
NbBundle.getMessage(TemplateProjectWizardIterator.class, "LBL_CreateProjectStep")
};
}
@Override
public Set/**/ instantiate(/*ProgressHandle handle*/) throws IOException {
Set resultSet = new LinkedHashSet<>();
File dirF = FileUtil.normalizeFile((File) wiz.getProperty("projdir"));
dirF.mkdirs();
FileObject template = Templates.getTemplate(wiz);
String examplePath = template.getAttribute(TemplateUtils.ZIP_PATH_ATTRIBUTE).toString();
FileObject dir = FileUtil.toFileObject(dirF);
try {
unZipExample(dir, examplePath);
} catch (IOException ex) {
throw ex;
} catch (Exception ex) {
throw new IOException(ex);
}
// Always open top dir as a project:
resultSet.add(dir);
File parent = dirF.getParentFile();
if (parent != null && parent.exists()) {
ProjectChooser.setProjectsFolder(parent);
}
return resultSet;
}
@Override
public void initialize(WizardDescriptor wiz) {
this.wiz = wiz;
wiz.putProperty("name", Templates.getTemplate(wiz).getName());
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", i);
// Step name (actually the whole list for reference).
jc.putClientProperty("WizardPanel_contentData", steps);
}
}
}
@Override
public void uninitialize(WizardDescriptor wiz) {
this.wiz.putProperty("projdir", null);
this.wiz.putProperty("name", null);
this.wiz = null;
panels = null;
}
@Override
public String name() {
return MessageFormat.format("{0} of {1}",
new Object[]{index + 1, panels.length});
}
@Override
public boolean hasNext() {
return index < panels.length - 1;
}
@Override
public boolean hasPrevious() {
return index > 0;
}
@Override
public void nextPanel() {
if (!hasNext()) {
throw new NoSuchElementException();
}
index++;
}
@Override
public void previousPanel() {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
index--;
}
@Override
public WizardDescriptor.Panel current() {
return panels[index];
}
@Override
public final void addChangeListener(ChangeListener l) {
}
@Override
public final void removeChangeListener(ChangeListener l) {
}
private static void unZipExample(FileObject projectDir, String examplePath) throws Exception {
FileObject exampleZip = FileUtil.getConfigFile(TemplateUtils.CONFIG_ZIP_PATH);
FileObject zipRoot = FileUtil.getArchiveRoot(exampleZip);
FileObject exampleProject = zipRoot.getFileObject(examplePath);
for (FileObject fo : exampleProject.getChildren()) {
fo.copy(projectDir, fo.getName(), fo.getExt());
}
}
}