//**************************
// Copyright Qiuli Sun August 2001
// The University of Oklahoma
//**************************


package edu.ou.eml.fea3d;


//****Standard import *********
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.media.j3d.View;
import javax.swing.border.*;
import javax.swing.Timer;


//------------------------------------------------------------------------------------------
public class Fea3D extends JApplet implements ActionListener, Runnable
{
    JButton pStartButton;
    URL pText3DFile = null;
    URLConnection conn = null;
    Thread p3DThread;
    JFrame baseFrame = null;
    static ProgressMonitor pMonitor;
    static int pProgress;
    SceneGraph pSceneGraph;
    View pView;
    Transform3D pEyePoint;
    TransformGroup pViewPlatformTG;
    Insets pInserts;
    EtchedBorder pBorder;
    Appearance pMaterialAppear;
    IndexedGeometryArray geometry = null;
    WireframeGeometry pWireframeGeometry = null;

    public ReadGeometryData readTool;
    public Timer pTimer = null;
  
   public void init()
    {
        initializeJava3D();
        pInserts = new  Insets(0,1,0,1);
        pBorder = new EtchedBorder(EtchedBorder.LOWERED);
        pStartButton = new JButton("Render 3D");
        pStartButton.setMargin(pInserts);
        pStartButton.setFont(new Font("SanSerf",Font.PLAIN,13));
        pStartButton.addActionListener(this);
        this.getContentPane().add(pStartButton);
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e){
            System.err.println("Can't set look and feel:"+e);
        }
        pText3DFile = Fea3DFactory.getURL(Fea3D.this, "beam.txt");
        //It is extremely important to place pMonitor here
        Component parent = (Component)pStartButton;
        pMonitor = new ProgressMonitor(parent, "Loading Progress",
        "Getting Started to render...", 0, 100);
        pMonitor.setMillisToDecideToPopup(100);
        setStartProgress(0);
     }

    private void initializeJava3D()
    {
       //Initialize the basic data
        DataContainer dataContainer = new DataContainer();
        
        p3DThread = new Thread(this);
        p3DThread.setPriority(Thread.MAX_PRIORITY);
     
        //Read the group name from the HTML page
        String newChannelName = this.getParameter("NEWCHANNEL");
   
        pMaterialAppear = new Appearance();
        pMaterialAppear.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
        pMaterialAppear.setMaterial(DataContainer.material);
        
     }


    //*************** End of initializing ************

    public void run()
    {
        URL iconURL;
        Dimension myButtonSize = new Dimension(32,26);
        pStartButton.setEnabled(false);
        baseFrame = new JFrame("Collaborative 3D Finite Element Analysis");

        //Record baseFrame in the DataContainer for easy access of other classes
        DataContainer.setFrameBase(baseFrame);

        WindowListener  winListener = new FrameWindowAdapter();
        baseFrame.addWindowListener(winListener);

        Container contentPane = baseFrame.getContentPane();
        //Layout must be set, otherwise confused
        contentPane.setLayout(new BorderLayout());
        
            
        baseFrame.setSize(510,550);

        setStartProgress(20);

  
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas3D = new Canvas3D(config);
        DataContainer.canvas3D = canvas3D ;
        
        contentPane.add(canvas3D, BorderLayout.CENTER);
        CoordinateSystem coordSystem = new CoordinateSystem();
        
        //testing
        //ColoredBlock coloredBlock = new ColoredBlock(100) ;
        
        setStartProgress(70);

        // Create a simple scene and attach it to the virtual universe
        pSceneGraph = new SceneGraph(true, DataContainer.shape3D, null,
        pMaterialAppear, DataContainer.geometryTG, canvas3D);
        
        BranchGroup scene = pSceneGraph.createSceneGraph();

        SimpleUniverse u = new SimpleUniverse(canvas3D);
        setStartProgress(90);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.

        //?????????????????????????????????
        //setJ3DThreadPriority() doesn't work with windows 2000
        //while it works fine with windows NT and windows 98.
        u.setJ3DThreadPriority(Thread.MAX_PRIORITY - 1);
        //?????????????????????????????????

        u.getViewingPlatform().setNominalViewingTransform();
        pView = u.getViewer().getView();

        pView.setFrontClipDistance(DataContainer.maxXYZ*0.2f);
        pView.setBackClipDistance(DataContainer.maxXYZ*50.0f);
        pEyePoint = new Transform3D();
        pEyePoint.setTranslation(new Vector3f ( 0.0f, 0.0f, DataContainer.maxXYZ * 1.7f ) );
        pViewPlatformTG = u.getViewingPlatform().getViewPlatformTransform();
        pViewPlatformTG.setTransform(pEyePoint);
        u.addBranchGraph(scene);
        setStartProgress(100);
        pMonitor.close();
        baseFrame.setVisible(true);


       ColoredBlock coloredBlock = new ColoredBlock(20) ;
    
    } // end of run()
    //*******************************************************
    private void setStartProgress(int pProgress)
    {
        pMonitor.setProgress(pProgress);
        pMonitor.setNote("loaded "+ pProgress+"%");
    }

    //*******************************************************
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source==pStartButton && baseFrame==null)
        {
            p3DThread.start();
        }
        if(source==pStartButton && baseFrame!=null)
        {
            baseFrame.setVisible(true);
        }
    }

    //My own WindowAdapter
    private class FrameWindowAdapter extends WindowAdapter
    {
        FrameWindowAdapter()
        {
            super();
        }
        public void windowClosing(WindowEvent e){
            pStartButton.setEnabled(true);
        }
    }
}  //end of JApplet
