/*
* 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 com.nanosn.netbeans.gwt4nb.uibinder;
import java.awt.Component;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.event.ChangeListener;
import org.netbeans.api.java.project.JavaProjectConstants;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.api.project.Sources;
import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
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.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
public final class UiBinderWizardIterator implements
WizardDescriptor.InstantiatingIterator<WizardDescriptor> {
private static final long serialVersionUID = 1;
private int index;
private WizardDescriptor wizard;
private WizardDescriptor.Panel<WizardDescriptor>[] panels;
private WizardDescriptor.Panel<WizardDescriptor> packageChooserPanel;
/**
* Initialize panels representing individual wizard's steps and sets
* various properties for them influencing wizard appearance.
*/
@SuppressWarnings("unchecked")
private WizardDescriptor.Panel<WizardDescriptor>[] getPanels() {
Project project = Templates.getProject(wizard);
Sources sources = project.getLookup().
lookup(Sources.class);
SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
packageChooserPanel = JavaTemplates.createPackageChooser(project, groups);
if (panels == null) {
panels = new WizardDescriptor.Panel[]{
packageChooserPanel,};
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;
// Sets step number of a component
// TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i)); // NOI18N
// Sets steps names for a panel
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
// Turn on subtitle creation on each step
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
// Show steps on the left side with the image on the background
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
// Turn on numbering of all steps
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
}
}
}
return panels;
}
public Set<FileObject> instantiate() throws IOException {
String className = Templates.getTargetName(wizard);
String xmlfileName = className + ".ui"; // NOI18N
FileObject pkg = Templates.getTargetFolder(wizard);
DataFolder targetFolder = DataFolder.findFolder(pkg);
final Set<FileObject> toOpen = new LinkedHashSet<FileObject>(2);
toOpen.add(processTemplate("Templates/Classes/UiBinder.java", // NOI18N
targetFolder, className));
toOpen.add(processTemplate("Templates/xml/UiBinder.ui.xml", // NOI18N
targetFolder, xmlfileName));
return toOpen;
}
private FileObject processTemplate(
final String templateName,
final DataFolder outputDir,
final String className)
throws DataObjectNotFoundException, IOException {
final DataObject template = DataObject.find(
FileUtil.getConfigFile(templateName));
return template.createFromTemplate(
outputDir,
className).getPrimaryFile();
}
public void initialize(WizardDescriptor wizard) {
this.wizard = wizard;
}
public void uninitialize(WizardDescriptor wizard) {
panels = null;
}
public WizardDescriptor.Panel<WizardDescriptor> current() {
return getPanels()[index];
}
public String name() {
return Integer.toString(index + 1) + " / " + getPanels().length; // NOI18N
}
public boolean hasNext() {
return index < getPanels().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--;
}
// If nothing unusual changes in the middle of the wizard, simply:
public void addChangeListener(ChangeListener l) {
}
public void removeChangeListener(ChangeListener l) {
}
// You could safely ignore this method. Is is here to keep steps which were
// there before this wizard was instantiated. It should be better handled
// by NetBeans Wizard API itself rather than needed to be implemented by a
// client code.
private String[] createSteps() {
String[] beforeSteps = null;
Object prop = wizard.getProperty("WizardPanel_contentData"); // NOI18N
if (prop != null && prop instanceof String[]) {
beforeSteps = (String[]) prop;
}
if (beforeSteps == null) {
beforeSteps = new String[0];
}
String[] res = new String[(beforeSteps.length - 1) + panels.length];
for (int i = 0; i < res.length; i++) {
if (i < (beforeSteps.length - 1)) {
res[i] = beforeSteps[i];
} else {
res[i] = panels[i - beforeSteps.length + 1].getComponent().getName();
}
}
return res;
}
}