// ==========================================================================
// $Id: TCActions.java,v 1.5 2002/09/09 20:12:31 hitoshi Exp $
// Timer Clock Actions
// ==========================================================================
// Copyright (C) 2002 Yamauchi, Hitoshi
//
//   Max-Planck-Institut fuer Informatik AG4
//   Stuhlsatzenhausweg 85, 66123 Saarbruecken
//   http://www.mpi-sb.mpg.de/
// 
// This software can be redistributed only under GNU copyleft.
//
// @author Yamauchi Hitoshi »³Æâ ÀÆ
// ==========================================================================
// $Revision: 1.5 $
// $Date: 2002/09/09 20:12:31 $
// $Author: hitoshi $
// ==========================================================================
package de.mpg.mpisb.timerclock;

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

/**
 * Time Clock Actions
 * @author Yamauchi Hitoshi
 */
public class TCActions
{
  /*
   * singleton
   */
  private static TCActions MTCA = new TCActions();
  public  static TCActions getInstance()
  {
    return(MTCA);
  }

  /*
   * constructor
   */
  public TCActions() 
  {
  }

  /**
   * setTimerClockPanel
   */
  public void init(TimerClockFrame tcf, TimerClockPanel tcp, DisplayTime dt, 
		   TCOption opt)
  {
    this.mTCF = tcf;
    this.mTCP = tcp;
    this.mDispTime = dt;
    this.mOpt = opt;

    this.mTCF.addPanel(tcp);
    this.mTCF.setSize(500, 500);
    this.reset();

    // start the thread
    this.mDispTime.start();
    this.mDispTime.newResume();
    this.mTCF.show();
  }

  /**
   * peek option
   */
  public TCOption peekOpt()
  {
    return(this.mOpt);
  }

  /*============================================================
   * file 
   *===========================================================*/
  /**
   * about
   */
  public void about()
  {
    String wintitle = TCOption.ProgramName + " : "  + TCOption.VersionStr;
    String message  = wintitle + "\n" + 
      TCOption.CopyrightStr    + " (hitoshi@mpi-sb.mpg.de)\n";

    JOptionPane.showMessageDialog(this.mTCF, 
				  message,
				  wintitle,
				  JOptionPane.PLAIN_MESSAGE);
  }

  /*============================================================
   * setting
   *===========================================================*/
  /**
   * set font 
   */
  private FontSelectionDialog mFSD = null;
  public void setFont()
  {
    if(this.mFSD == null)
    {
      this.mFSD = new FontSelectionDialog(this.mTCF, true);
    }

    // the font size doesn't matter
    this.mFSD.setCurrentFont(this.mTCP.getFontName(), this.mTCP.getFontStyle(), 24);
    this.mFSD.show();
    if(this.mFSD.getReturnStatus() == FontSelectionDialog.RET_OK)
    {
      Message.verboseOut("DEBUG: set Fontname = " + this.mFSD.getSelectedFontName() +
			 ", italic = " + this.mFSD.isItalic() + 
			 ", bold = "   + this.mFSD.isBold()   + 
			 ", size = "   + this.mFSD.getSelectedFontSize() + "\n");
      this.mTCP.setFontInfo(this.mFSD.getSelectedFontName(), 
			    this.mFSD.getSelectedFontStyle());

      this.mTCP.doLayout();      
      this.mTCP.repaint();      
    }
  }

  /**
   * set default foreground color
   */
  public void setDefaultFGColor()
  {
    this.mSelectedColor = null;
    //Set up the dialog that the button brings up.
    final JColorChooser colorChooser = new JColorChooser();
    ActionListener okListener = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  // colorEditor.currentColor = colorChooser.getColor();
	  mSelectedColor = colorChooser.getColor();
	}
      };
    final JDialog dialog = 
      JColorChooser.createDialog(this.mTCF, "Pic the Foreground color",
				 true,
				 colorChooser,
				 okListener,
				 null);
    dialog.setVisible(true);
    if(this.mSelectedColor != null)
    {
      // Message.verboseOut("DEBUG: set FG color " + this.mSelectedColor + "\n");
      this.mTCP.setDefaultFGColor(this.mSelectedColor);
    }
  }
  /**
   * set default background color
   */
  public void setDefaultBGColor()
  {
    this.mSelectedColor = null;
    //Set up the dialog that the button brings up.
    final JColorChooser colorChooser = new JColorChooser();
    ActionListener okListener = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  // colorEditor.currentColor = colorChooser.getColor();
	  mSelectedColor = colorChooser.getColor();
	}
      };
    final JDialog dialog = 
      JColorChooser.createDialog(this.mTCF, "Pic the Background color",
				 true,
				 colorChooser,
				 okListener,
				 null);
    dialog.setVisible(true);
    if(this.mSelectedColor != null)
    {
      // Message.verboseOut("DEBUG: set BG color " + this.mSelectedColor + "\n");
      this.mTCP.setDefaultBGColor(this.mSelectedColor);
    }
  }
  /**
   * reset color
   */
  public void resetColor()
  {
    this.mTCP.resetColor(); 
  }

  /**
   * set timer
   */
  private SetTimeDialog mSTDialog = null;
  public void setPeriodTime()
  {
    if(this.mSTDialog == null)
    {
      this.mSTDialog = new SetTimeDialog(this.mTCF, true);
    }
    this.mSTDialog.popup(this.mTCP.getPeriodTime());
    if(this.mSTDialog.getReturnStatus() == SetTimeDialog.RET_OK)
    {
      // System.out.print("DEBUG: OK");
      this.mTCP.setPeriodTime(this.mSTDialog.getSettedTime());
      this.mTCP.reset();
    }
    else
    {
      // System.out.print("DEBUG: Cancel");
    }
  }

  /**
   * set Action
   */
  public void setAction(int idx)
  {
    if(this.mSAD[idx] == null)
    {
      this.mSAD[idx] = new SetActionDialog(this.mTCF, true);
    }
    this.mSAD[idx].popup(this.mTCP);
    if(this.mSAD[idx].getReturnStatus() == SetActionDialog.RET_OK)
    {
      Vector actionlist = new Vector();
      for(int i = 0; i < NSADialogs; i++)
      {
	if((this.mSAD[i] != null) && (this.mSAD[i].getActionable() != null))
	{
	  actionlist.add(this.mSAD[i].getActionable());
	}
      }
      synchronized(this.mDispTime)
      {
	this.mDispTime.removeAllActions();
	this.mDispTime.addAllActions(actionlist);
      }
    }
  }

  /*============================================================
   * timer control
   *===========================================================*/
  /**
   * timer start
   */
  public void start()
  {
    // System.out.println("DEBUG: START");
    this.mTCP.start();
    // It seems OK, thread is always running to update
    this.mDispTime.newResume(); 
  }
  /**
   * timer stop
   */
  public void stop()
  {
    // System.out.println("DEBUG: STOP");
    this.mTCP.stop();
    this.mDispTime.newSuspend();
  }
  /**
   * timer reset
   */
  public void reset()
  {
    this.mDispTime.resetAction();
    this.mTCP.reset();    
    this.mTCP.resetColor(); 
  }

  // private variables
  private TimerClockFrame mTCF 		 = null;
  private TimerClockPanel mTCP 		 = null;
  private DisplayTime     mDispTime 	 = null;
  private TCOption 	  mOpt		 = null;
  private Color 	  mSelectedColor = null;

  private final int NSADialogs = 5;
  private SetActionDialog [] mSAD = new SetActionDialog [NSADialogs];
}

