/*
 * __NAME__.java
 *
 * Created on __DATE__
 */

package Templates.API_Support.Window_System_API;

import java.awt.Component;
import java.io.IOException;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.event.ChangeListener;

import org.openide.WizardDescriptor;
import org.openide.cookies.OpenCookie;
import org.openide.loaders.*;
import org.openide.util.NbBundle;

/** A template wizard iterator (sequence of panels).
 * Used to fill in the second and subsequent panels in the New wizard.
 * Associate this to a template inside a layer using the
 * Sequence of Panels extra property.
 * Create one or more panels from template as needed too.
 *
 * @author __USER__
 */
public class __Sample_TemplateWizard__Iterator implements TemplateWizard.Iterator {

    private static final long serialVersionUID = 1L;

    // You should define what panels you want to use here:

    protected WizardDescriptor.Panel[] createPanels() {
        return new WizardDescriptor.Panel[] {
            // Assuming you want to keep the default 2nd panel:
            wiz.targetChooser(),
            // Or you can supply your own replacement or additions.
            /* --> EDIT ME <--
            new MyPanel1 (),
            new MyPanel2 ()
             */
        };
    }

    // And the list of step names:

    protected String[] createSteps() {
        return new String[] {
            null, // for targetChooser(); take name from panel
            /* --> EDIT ME <--
            NbBundle.getMessage(__NAME__.class, "LBL_step_1"),
            NbBundle.getMessage(__NAME__.class, "LBL_step_2")
             */
        };
    }


    public Set instantiate(TemplateWizard wiz) throws IOException/*, IllegalStateException*/ {
        // Here is the default plain behavior. Simply takes the selected
        // template (you need to have included the standard second panel
        // in createPanels(), or at least set the properties targetName and
        // targetFolder correctly), instantiates it in the provided
        // position, and returns the result.
        // More advanced wizards can create multiple objects from template
        // (return them all in the result of this method), populate file
        // contents on the fly, etc.
        String name = wiz.getTargetName();
        DataFolder targetFolder = wiz.getTargetFolder();
        DataObject template = wiz.getTemplate();
        DataObject result;
        if (name == null) {
            // Default name.
            result = template.createFromTemplate(targetFolder);
        } else {
            result = template.createFromTemplate(targetFolder, name);
        }
        // Do something with the result, e.g. open it:
        OpenCookie open = (OpenCookie)result.getCookie(OpenCookie.class);
        if (open != null) {
            open.open();
        }
        // or more generically, simulate a double-click:
        /*
        Node n = result.getNodeDelegate();
        final Action action = n.getPreferredAction();
        if (action != null) {
            final ActionEvent event = new ActionEvent(n, ActionEvent.ACTION_PERFORMED, "");
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    action.actionPerformed(event);
                }
            });
        }
         */
        return Collections.singleton(result);
    }

    // --- The rest probably does not need to be touched. ---

    private transient int index;
    private transient WizardDescriptor.Panel[] panels;
    private transient TemplateWizard wiz;

    // You can keep a reference to the TemplateWizard which can
    // provide various kinds of useful information such as
    // the currently selected target name.
    // Also the panels will receive wiz as their "settings" object.
    public void initialize(TemplateWizard 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 #.
                jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i)); // NOI18N
                // Step name (actually the whole list for reference).
                jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
            }
        }
    }
    public void uninitialize(TemplateWizard wiz) {
        this.wiz = null;
        panels = null;
    }

    // --- WizardDescriptor.Iterator METHODS: ---
    // Note that this is very similar to WizardDescriptor.Iterator, but with a
    // few more options for customization. If you e.g. want to make panels appear
    // or disappear dynamically, go ahead.

    public String name() {
        return NbBundle.getMessage(__NAME__.class, "TITLE_x_of_y",
            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];
    }

    // If nothing unusual changes in the middle of the wizard, simply:
    public final void addChangeListener(ChangeListener l) {}
    public final void removeChangeListener(ChangeListener l) {}
    // If something changes dynamically (besides moving between panels),
    // e.g. the number of panels changes in response to user input, then
    // uncomment the following and call when needed:
    // fireChangeEvent();
    /*
    private transient Set<ChangeListener> listeners = new HashSet<ChangeListener>(1);
    public final void addChangeListener(ChangeListener l) {
        synchronized(listeners) {
            listeners.add(l);
        }
    }
    public final void removeChangeListener(ChangeListener l) {
        synchronized(listeners) {
            listeners.remove(l);
        }
    }
    protected final void fireChangeEvent() {
        Iterator<ChangeListener> it;
        synchronized (listeners) {
            it = new HashSet<ChangeListener>(listeners).iterator();
        }
        ChangeEvent ev = new ChangeEvent(this);
        while (it.hasNext()) {
            it.next().stateChanged(ev);
        }
    }
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        listeners = new HashSet<ChangeListener>(1);
    }
    */

}
