/*
 * Copyright (c) 2009-2010 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.jme3.gde.terraineditor.sky;

import com.jme3.math.Vector3f;
import com.jme3.texture.Texture;
import java.awt.Component;
import javax.swing.event.ChangeListener;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;

public class SkyboxWizardPanel2 implements WizardDescriptor.Panel {

    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private Component component;

    private boolean multipleTextures;

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    public Component getComponent() {
        if (component == null) {
            component = new SkyboxVisualPanel2();
        }
        return component;
    }

    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx(SampleWizardPanel1.class);
    }

    public boolean isValid() {
        // If it is always OK to press Next or Finish, then:
        return true;
        // If it depends on some condition (form filled out...), then:
        // return someCondition();
        // and when this condition changes (last form field filled in...) then:
        // fireChangeEvent();
        // and uncomment the complicated stuff below.
    }

    public final void addChangeListener(ChangeListener l) {
    }

    public final void removeChangeListener(ChangeListener l) {
    }
    /*
    private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(1); // or can use ChangeSupport in NB 6.0
    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);
    }
    }
     */

    // You can use a settings object to keep track of state. Normally the
    // settings object will be the WizardDescriptor, so you can use
    // WizardDescriptor.getProperty & putProperty to store information entered
    // by the user.
    public void readSettings(Object settings) {
        WizardDescriptor wiz = (WizardDescriptor) settings;
        multipleTextures = (Boolean)wiz.getProperty("multipleTextures");
        SkyboxVisualPanel2 comp = (SkyboxVisualPanel2) getComponent();
        if (multipleTextures) {
            comp.getMultipleTexturePanel().setVisible(true);
            comp.getSingleTexturePanel().setVisible(false);
        } else {
            comp.getMultipleTexturePanel().setVisible(false);
            comp.getSingleTexturePanel().setVisible(true);
        }
    }

    public void storeSettings(Object settings) {
        WizardDescriptor wiz = (WizardDescriptor) settings;
        SkyboxVisualPanel2 comp = (SkyboxVisualPanel2) getComponent();
        if (multipleTextures) {
            wiz.putProperty("textureSouth", (Texture)comp.getEditorSouth().getValue());
            wiz.putProperty("textureNorth", (Texture)comp.getEditorNorth().getValue());
            wiz.putProperty("textureEast", (Texture)comp.getEditorEast().getValue());
            wiz.putProperty("textureWest", (Texture)comp.getEditorWest().getValue());
            wiz.putProperty("textureTop", (Texture)comp.getEditorTop().getValue());
            wiz.putProperty("textureBottom", (Texture)comp.getEditorBottom().getValue());
            float x = new Float(comp.getNormal1X().getText());
            float y = new Float(comp.getNormal1Y().getText());
            float z = new Float(comp.getNormal1Z().getText());
            wiz.putProperty("normalScale", new Vector3f(x,y,z) );
        } else {
            wiz.putProperty("textureSingle", (Texture)comp.getEditorSingle().getValue());
            float x = new Float(comp.getNormal2X().getText());
            float y = new Float(comp.getNormal2Y().getText());
            float z = new Float(comp.getNormal2Z().getText());
            wiz.putProperty("normalScale", new Vector3f(x,y,z) );
            wiz.putProperty("useSpheremap", comp.getSpheremapCheckBox().isSelected());
            wiz.putProperty("flipY", comp.getFlipYCheckBox().isSelected());
        }
    }
}
