/*=========================================================================
 
  Program:   Orbit Visualization Tool
  Source:    $Source: /stor/devel/ovt2g/ovt/object/FrameModule.java,v $
  Date:      $Date: 2001/04/03 20:13:21 $
  Version:   $Revision: 1.4 $
 
 
Copyright (c) 2000 OVT Team (Kristof Stasiewicz, Mykola Khotyaintsev,
Yuri Khotyaintsev)
All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification is permitted provided that the following conditions are met:
 
 * No part of the software can be included in any commercial package without
written consent from the OVT team.
 
 * Redistributions of the source or binary code must retain the above
copyright notice, this list of conditions and the following disclaimer.
 
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 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT OR
INDIRECT DAMAGES  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.
 
OVT Team (http://ovt.irfu.se)   K. Stasiewicz, M. Khotyaintsev, Y.
Khotyaintsev
 
=========================================================================*/

/*
 * FrameModule.java
 *
 * Created on March 22, 2000, 10:22 AM
 */

package ovt.object;

import ovt.*;
import ovt.mag.*;
import ovt.event.*;
import ovt.beans.*;
import ovt.datatype.*;
import ovt.interfaces.*;

import vtk.*;

import java.beans.*;

/**
 *
 * @author  root
 * @version
 */
public class FrameModule extends VisualObject {
    
    public static final int XOYPLANE = 1;
    public static final int XOZPLANE = 2;
    public static final int YOZPLANE = 3;
    
    protected vtkActor actor = null;
    
    private Frames parent;
    
    protected int type = XOYPLANE;
    
    /** Holds value of property position. */
    //private int position = 0;
    
  /** Creates new FrameModule */
    public FrameModule(Frames frames, int type) {
        super(frames.getCore(), getName(type), frames.getCore().getImagesDir()+"frame.gif");
        parent = frames;
        setType(type);
    }
    
    public static String getName(int type) {
        switch (type) {
            case XOYPLANE : return "XOY";
            case YOZPLANE : return "YOZ";
            case XOZPLANE : return "XOZ";
            default: return "no name";
        }
    }
    
    protected void setType(int type) {
        if (type == XOYPLANE || type == XOZPLANE || type == YOZPLANE) {
            this.type = type;
        } else throw new IllegalArgumentException("type is not XOYPLANE || XOZPLANE || YOZPLANE");
    }
    
    public int getType() {
        return type;
    }
    
    protected vtkActor createActor(int l, int dx) {
        int i;
        vtkFloatScalars xCoords = new vtkFloatScalars();
        
        for (i=0; i<l+1; i++) xCoords.InsertScalar(i,(i-l/2)*dx);
        
        vtkFloatScalars yCoords = new vtkFloatScalars();
        for (i=0; i<l+1; i++) yCoords.InsertScalar(i,(i-l/2)*dx);
        
        vtkFloatScalars zCoords = new vtkFloatScalars();
        for (i=0; i<l+1; i++) zCoords.InsertScalar(i,(i-l/2)*dx);
        
        vtkRectilinearGrid rgrid = new vtkRectilinearGrid();
        
        rgrid.SetDimensions(l+1,l+1,l+1);
        rgrid.SetXCoordinates(xCoords);
        rgrid.SetYCoordinates(yCoords);
        rgrid.SetZCoordinates(zCoords);
        
        vtkRectilinearGridGeometryFilter plane = new vtkRectilinearGridGeometryFilter();
        plane.SetInput(rgrid);
        
        switch (getType()) {
            case XOYPLANE : plane.SetExtent(0,     l,   0,   l, l/2, l/2); break;
            case YOZPLANE : plane.SetExtent(l/2, l/2,   0,   l,   0,   l); break;
            case XOZPLANE : plane.SetExtent(0,     l, l/2, l/2,   0,   l); break;
        }
        
        vtkPolyDataMapper gridMapper = new vtkPolyDataMapper();
        gridMapper.SetInput(plane.GetOutput());
        
        vtkActor actor = new vtkActor();
        actor.SetMapper(gridMapper);
        actor.GetProperty().SetRepresentationToWireframe();
        actor.GetProperty().SetColor(0, 0, 0);
        //actor.GetProperty().SetAmbientColor (0, 0, 0);
        //actor.GetProperty().SetSpecularColor(0, 0, 0);
        //actor.GetProperty().SetDiffuseColor (0, 0, 0);
        actor.SetScale((double)parent.getCellSize());
        setActorPosition(actor, getPosition() * parent.getCellSize());
        return actor;
    }
    
    protected vtkActor getActor() {
        if (actor == null) actor = createActor(parent.getCellsNumber(), 1);
        return actor;
    }
    
    protected void show() {
        getRenderer().AddActor(getActor());
    }
    
    protected void hide() {
        getRenderer().RemoveActor(getActor());
    }
    
    public void setVisible(boolean visible) {
        if (visible != isVisible()) {
            if (visible) show();
            else hide();
            super.setVisible(visible);
        }
    }
    
    private void setActorPosition(vtkActor actor, double pos) {
        switch(getType()) {
            case XOYPLANE: actor.SetPosition(0, 0, pos); break;
            case XOZPLANE: actor.SetPosition(0, pos, 0); break;
            case YOZPLANE: actor.SetPosition(pos, 0, 0); break;
        }
    }
    
    public void propertyChange(PropertyChangeEvent evt) {
        if (!isVisible()) {
            actor = null;
            return;
        }
        
        String prop = evt.getPropertyName();
        //System.out.println("FrameModule: Property changed: " + prop);
        if (prop == "cellsNumber") {
            //System.out.println("recreating actor...");
            int oldCellsNumber = ((Integer)evt.getOldValue()).intValue();
            int newCellsNumber = ((Integer)evt.getNewValue()).intValue();
            if (newCellsNumber != oldCellsNumber) {
                //if (Math.abs(getPosition()) > Math.abs(newCellsNumber)/2) {
                //    parent.setYOZPosition(newCellsNumber / 2 * (getPosition() > 0 ? 1 : -1));
                //}
                hide();
                actor = createActor(newCellsNumber, 1);
                show();
                Render();
            }
        }
        if (prop == "cellSize") {
            if (actor != null) {
                actor.SetScale((double)parent.getCellSize());
                setActorPosition(actor, getPosition() * parent.getCellSize());
                Render();
            }
        }
        
        if (prop == "YOZPosition") {
            if (type == YOZPLANE && actor != null) {
                int oldPos = ((Integer)evt.getOldValue()).intValue();
                int newPos = ((Integer)evt.getNewValue()).intValue();
                if (newPos != oldPos) {
                    setActorPosition(actor, newPos * parent.getCellSize());
                    Render();
                }
            }
        }
    }
    
    /** Getter for property position.
     * @return Value of property position.
     */
    public int getPosition() {
        return (type == YOZPLANE) ? parent.getYOZPosition() : 0;
    }
    
    /** Setter for property position. Metod called only by parent
     * @param position New value of property position.  
     */
    /*public void setPosition(int position) {
        int oldPosition = this.position;
        this.position = position;
        propertyChangeSupport.firePropertyChange ("position", new Integer (oldPosition), new Integer (position));
    }*/
    
    /*
    public Descriptors getDescriptors() {
        if (descriptors == null) {
            descriptors = super.getDescriptors();
            
            try {
                BasicPropertyDescriptor pd = new BasicPropertyDescriptor("position", this);
                pd.setDisplayName(getName() + " frame position");
                SliderPropertyEditor sliderEditor = new SliderPropertyEditor(pd);
                sliderEditor.makeLinear(-5, 5, 1, 1);
                addPropertyChangeListener("position", sliderEditor);
                //sliderEditor.setFrameOwner(getCore().getXYZWin());    // for better initial position
                pd.setPropertyEditor(new WindowedPropertyEditor(sliderEditor));
                descriptors.put(pd);

            } catch (IntrospectionException e2) {
                System.out.println(getClass().getName() + " -> " + e2.toString());
                System.exit(0);
            }
        }
        return descriptors;
    }
    */
}