/*=========================================================================

  Program:   Orbit Visualization Tool
  Source:    $Source: /stor/devel/ovt2g/ovt/object/editor/CurrentMjdVCRComponents.java,v $
  Date:      $Date: 2001/03/24 16:55:49 $
  Version:   $Revision: 1.8 $


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

=========================================================================*/

/*
 * CurrentMjdVCRComponents.java
 *
 * Created on March 9, 2000, 8:22 PM
 */
 
package ovt.object.editor;

import ovt.*;
import ovt.beans.*;
import ovt.datatype.*;

import java.beans.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** 
 *
 * @author  root
 * @version 
 */
public class CurrentMjdVCRComponents implements PropertyChangeListener, ActionListener {

  private CurrentMjdEditor editor;
  private JButton rewind, back, play, forward, fastForward, stop;
  private JComboBox timeBox;
  
  /** Creates new CurrentMjdVCRComponents */
  public CurrentMjdVCRComponents(CurrentMjdEditor ed) {
    editor = ed;
    String imgDir = OVTCore.getImagesDir();
    rewind = new JButton(new ImageIcon(imgDir+"VCRRewind.gif"));
    rewind.setName("rewind");
    rewind.addActionListener(this);
    rewind.setAlignmentY(0.5f);
    rewind.setToolTipText("Go to start");
        
    back = new JButton(new ImageIcon(imgDir+"VCRBack.gif"));
    back.setName("back");
    back.addActionListener(this);
    back.setAlignmentY(0.5f);
    back.setToolTipText("Step back");
        
    timeBox = new JComboBox();
    timeBox.setToolTipText("Time");
    
    forward = new JButton(new ImageIcon(imgDir+"VCRForward.gif"));
    forward.setName("forward");
    forward.addActionListener(this);
    forward.setAlignmentY(0.5f);
    forward.setToolTipText("Step forward");
        
    fastForward = new JButton(new ImageIcon(imgDir+"VCRFastForward.gif"));
    fastForward.setName("fastForward");
    fastForward.addActionListener(this);
    fastForward.setAlignmentY(0.5f);
    fastForward.setToolTipText("Go to finish");
        
    stop = new JButton(new ImageIcon(imgDir+"VCRStop.gif"));
    stop.setName("stop");
    stop.addActionListener(this);
    stop.setAlignmentY(0.5f);
    
    
    updateComboBox();
    refresh();
  }
  
  public Component[] getComponents() {
    return new Component[]{rewind, back, timeBox, forward, fastForward};
  }
  
  public void propertyChange(PropertyChangeEvent evt) {
    String property = evt.getPropertyName();
    //System.out.println("CurrentMJDVCRComponents recieved change of  '" + property +"'");
    if (property.equals("time")) {
      updateComboBox();
      refresh();
    }
    
    if (property.equals("currentMjd")) {
      refresh();
    }
  }
  public void actionPerformed(ActionEvent evt) {
    
    boolean nothingHeppend = false;
    
    if (evt.getSource() == timeBox) {
      // It's better to use getSelectedIndex here, but we use this for testing
      String time = (String)timeBox.getSelectedItem();
      try {
        System.out.println("Trying to set '" + time +"'");
        editor.setAsText(time);
      } catch (PropertyVetoException e2) {System.out.println("" + e2.getMessage());}
    
    } else if (evt.getSource() == rewind) {
      
      try {
        editor.setValue(new Double(editor.getTimeSet().getStartMjd()));
      } catch (PropertyVetoException e2) {System.out.println("" + e2.getMessage());}
    
    } else if (evt.getSource() == fastForward) {
      
      try {
        Object[] values = editor.getValues(); 
        editor.setValue(values[values.length -1]);
      } catch (PropertyVetoException e2) {System.out.println("" + e2.getMessage());}
    
    } else if (evt.getSource() == back) {
      TimeSet ts = editor.getTimeSet();
      try {
        editor.setValue(new Double(ts.get(ts.getCurrentMjdIndex() - 1)));
      } catch (PropertyVetoException e2) {System.out.println("" + e2.getMessage());}
    
    } else if (evt.getSource() == forward) {
      TimeSet ts = editor.getTimeSet();
      try {
        editor.setValue(new Double(ts.get(ts.getCurrentMjdIndex() + 1)));
      } catch (PropertyVetoException e2) {System.out.println("" + e2.getMessage());}
    
    } else nothingHeppend = true; 
    
    if (!nothingHeppend) fireEditingFinished();
    
  }
  
  public void refresh() {
    // adjust timeBox
    timeBox.removeActionListener(this);
    //System.out.println("adjust textBox by '" + editor.getAsText() +"'");
    //timeBox.setSelectedItem(editor.getAsText());
    timeBox.setSelectedIndex(editor.getTimeSet().getCurrentMjdIndex());
    timeBox.addActionListener(this);
    
    // adjust buttons
    if (editor.isStart()) {
      //System.out.println("Start!!!!!!!!");
      rewind.setEnabled(false);
      back.setEnabled(false);
      forward.setEnabled(true);
      fastForward.setEnabled(true);
    } else if (editor.isStop()) {
      //System.out.println("Stop!!!!!!!!");
      rewind.setEnabled(true);
      back.setEnabled(true);
      forward.setEnabled(false);
      fastForward.setEnabled(false);
    } else {
      //System.out.println("aaa!!!!!!!!");
      rewind.setEnabled(true);
      back.setEnabled(true);
      forward.setEnabled(true);
      fastForward.setEnabled(true);
    }
    
  }
  
  public void updateComboBox() {
    if (OVTCore.DEBUG > 3) System.out.print("CurrentMjdVCRComponents.updateCombobox ");
    timeBox.removeActionListener(this);
    timeBox.removeAllItems();
    String[] tags = editor.getTags();
    for (int i=0; i<tags.length; i++) {
      timeBox.addItem(tags[i]);
      if (OVTCore.DEBUG > 3) System.out.print(" "+i);
    }
    timeBox.setMaximumSize(timeBox.getPreferredSize());
    timeBox.addActionListener(this);
    if (OVTCore.DEBUG > 3) System.out.println("done.");
  }
  
  public void fireEditingFinished() {
    editor.fireEditingFinished();
  }
}