/*****************************************************************************
 *                        Web3d.org Copyright (c) 2001-2007
 *                               Java Source
 *
 * This source is licensed under the BSD license.
 * Please read docs/BSD.txt for the text of the license.
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 ****************************************************************************/

import java.awt.*;
import java.util.HashMap;
import javax.swing.*;

import org.web3d.x3d.sai.*;

/**
 * A simple example of how to use SAI to load a scene and listen to an event,
 *
 * @author Alan Hudson
 * @version
 */
public class FieldEventDemo extends JFrame implements X3DFieldEventListener {

    /**
     * Constructor for the demo.
     */
    public FieldEventDemo() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container contentPane = getContentPane();

        // Setup browser parameters
        HashMap requestedParameters = new HashMap();

        // Create an SAI component
        X3DComponent x3dComp = BrowserFactory.createX3DComponent(requestedParameters);

        // Add the component to the UI
        JComponent x3dPanel = (JComponent)x3dComp.getImplementation();
        contentPane.add(x3dPanel, BorderLayout.CENTER);

        // Get an external browser
        ExternalBrowser x3dBrowser = x3dComp.getBrowser();

        setSize(500,500);
        setVisible(true);


        // Create an X3D scene by loading a file
        X3DScene mainScene = x3dBrowser.createX3DFromURL(new String[] { "touchy_box.x3dv" });

        // Replace the current world with the new one
        x3dBrowser.replaceWorld(mainScene);

        // Find the TouchSensor named TOUCH_SENSOR
        X3DNode touch = mainScene.getNamedNode("TOUCH_SENSOR");
        if (touch == null) {
            System.out.println("Couldn't find TouchSensor named: TOUCH_SENSOR");
            return;
        }

        SFTime ttime = (SFTime) touch.getField("touchTime");

        // Listen for changes on the touchTime field
        ttime.addX3DEventListener(this);
    }

    //----------------------------------------------------------
    // Methods defined by X3DFieldEventListener
    //----------------------------------------------------------

    public void readableFieldChanged(X3DFieldEvent evt) {
        System.out.println("Stop touching me!");
    }

    /**
     * Main method.
     *
     * @param args None handled
     */
    public static void main(String[] args) {

        FieldEventDemo demo = new FieldEventDemo();
    }
}
