/*=========================================================================
 
  Program:   Orbit Visualization Tool
  Source:    $Source: /stor/devel/ovt2g/ovt/object/FieldlineModule.java,v $
  Date:      $Date: 2001/04/16 18:09:05 $
  Version:   $Revision: 1.19 $
 
 
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
wri{{       tten 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
 
=========================================================================*/

package ovt.object;

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


import vtk.*;

import javax.swing.*;
import java.io.File;
import java.util.*;
import java.beans.*;


/**
 * This is the fieldline tracing module for satellite, that should be added
 * to { @link ovt.datatypes.Sat }.
 * @author Mykola Khotyaintsev
 * @see ovt.datatypes.AbstractSatModule
 */
public class FieldlineModule extends AbstractVisualSatModule
implements MagPropsChangeListener {
    
    public static final int FL_2_EARTH   =  0;
    public static final int FL_2_EQUATOR  =  1;
    
    protected int fl_type = FL_2_EARTH;
    
    private MainFieldlineModule mainModule;
    
    protected vtkActor actor = null;
    
    private Hashtable actors = new Hashtable();
    private Hashtable actors_to_show = new Hashtable();
    private double actorsMjd;
    
    
    
/** Create a FieldLinesSatModule */
    
    public FieldlineModule(MainFieldlineModule mainModule, int fl_type) {
        super(mainModule.getSat(), "error name");
        setIcon(new ImageIcon(OVTCore.getImagesDir()+"fieldline.gif"));
        this.fl_type = fl_type;
        
        if (fl_type == FL_2_EARTH)   name = "toEarth";
        else if (fl_type == FL_2_EQUATOR) name = "toEquator";
        else throw new IllegalArgumentException("Wring FL type : "+fl_type);
        setName(name);
        setParent(mainModule);
        this.mainModule = mainModule;
    }
    
    
    
  /** Returns the field line type.
   * @return Field line type. {@see ovt.object.Sat}
   */
    public int getFLType() {
        return fl_type;
    }
    
    protected Fieldline getFieldline(int type, double mjd) {
        return getSat().getFieldline(type, mjd);
    }
    
    protected vtkActor getActor() {
        return getActor(getMjd());
    }
    
    private vtkActor getActor(double mjd) {
        if (!isValid()) validate();
        // look in hashtable
        vtkActor act = (vtkActor)actors.get(new Double(mjd));
        if ( act == null)  { // nothing found in hashtable
            Fieldline fl = getFieldline(getFLType(), getMjd());
            if (fl == null) {
                //System.out.println("fl = null");
                return null;
            }
            //System.out.println(" size = "+fl.size());
            act = ActorUtils.getActor(fl);
            actors.put(new Double(mjd), act);
        }
        return act;
    }
    
    public void show() {
        if (!isValid()) validate();
        if (!keep()  ||  getCS() != CoordinateSystem.GSM) actors_to_show.clear();
        actor = getActor(getMjd()); //  the line for this mjd
        if (actor != null) actors_to_show.put(new Double(getMjd()), actor);
        updateToCurrentCS();
        Enumeration e = actors_to_show.elements();
        while (e.hasMoreElements())
            getRenderer().AddActor((vtkActor)e.nextElement());
    }
    
    public void hide() {
        Enumeration e = actors_to_show.elements();
        while (e.hasMoreElements())
            getRenderer().RemoveActor((vtkActor)e.nextElement());
        
    }
    
    public void updateToCurrentCS() {
        //System.out.println("Update to current c.s.s.");
        Matrix3x3 trm = getTrans(getMjd()).gsm_trans_matrix(getCS());
        Enumeration e = actors_to_show.elements();
        while (e.hasMoreElements())
            ((vtkActor)e.nextElement()).SetUserMatrix(trm.getVTKMatrix());
    }
    
    public void validate() {
        actors.clear();
        actors_to_show.clear();
        valid = true;
    }
    
    public void timeChanged(TimeEvent evt) {
        if (evt.getPropertyName().equals("time")) {
            invalidate();
            //System.out.println("FTIME CHANGE EVENTT!!!");
        }
        
        if (isVisible()) {
            hide();
            show();
        }
    }
    
    public void setVisible(boolean visible) {
        if (isVisible() != visible) {
            if (visible) show();
            else hide();
            super.setVisible(visible);
        }
    }
    
    public void coordinateSystemChanged(CoordinateSystemEvent evt) {
        if (evt.getWindow() == Const.XYZ) {
            if (isVisible()) {
                hide();
                show();
            }
        }
    }
    
    
    public void magPropsChanged(MagPropsEvent evt) {
        invalidate();
        if (isVisible()) {
            hide();
            show();
        }
    }
    
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("keep")) {
            boolean keep = ((Boolean)evt.getNewValue()).booleanValue();
            if (!keep)
                if (isVisible()) {
                    hide();
                    show();
                }
        }
        super.propertyChange(evt);
    }
    
    private boolean keep() {
        
        return mainModule.isKeep();
    }
    
}
