// ==========================================================================
// $Id: SetActionDialog.java,v 1.2 2002/09/09 20:12:30 hitoshi Exp $
// Action Setting Dialog
// ==========================================================================
// 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.2 $
// $Date: 2002/09/09 20:12:30 $
// $Author: hitoshi $
// ==========================================================================
package de.mpg.mpisb.timerclock;

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

/**
 * SetActionDialog
 * @author  Yamauchi Hitoshi
 */
public class SetActionDialog extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;

    /** Creates new form OkCancelDialog */
    public SetActionDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
	initAfterInit();
    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        mMainPanel = new javax.swing.JPanel();
        mActionSettingBtn = new javax.swing.JButton();
        mActionTypeCB = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        mSetTimeBtn = new javax.swing.JButton();
        mTimeText = new javax.swing.JTextField();
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        
        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
        
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        
        buttonPanel.add(okButton);
        
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        
        buttonPanel.add(cancelButton);
        
        getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);
        
        mMainPanel.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        mMainPanel.setBorder(new javax.swing.border.TitledBorder("Action Setting"));
        mActionSettingBtn.setText("Setting");
        mActionSettingBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mActionSettingBtnActionPerformed(evt);
            }
        });
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 2;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
        mMainPanel.add(mActionSettingBtn, gridBagConstraints1);
        
        mActionTypeCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mActionTypeCBActionPerformed(evt);
            }
        });
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 0, 10);
        mMainPanel.add(mActionTypeCB, gridBagConstraints1);
        
        jLabel2.setText("Action Type : ");
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        mMainPanel.add(jLabel2, gridBagConstraints1);
        
        jLabel1.setText("Elapsed Time :  ");
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 0;
        mMainPanel.add(jLabel1, gridBagConstraints1);
        
        mSetTimeBtn.setText("Set Time");
        mSetTimeBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mSetTimeBtnActionPerformed(evt);
            }
        });
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 2;
        gridBagConstraints1.gridy = 0;
        mMainPanel.add(mSetTimeBtn, gridBagConstraints1);
        
        mTimeText.setEditable(false);
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 0, 10);
        mMainPanel.add(mTimeText, gridBagConstraints1);
        
        getContentPane().add(mMainPanel, java.awt.BorderLayout.CENTER);
        
        pack();
    }//GEN-END:initComponents

    private void mActionTypeCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mActionTypeCBActionPerformed
      // Add your handling code here:
      
    }//GEN-LAST:event_mActionTypeCBActionPerformed

    private void mActionSettingBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mActionSettingBtnActionPerformed
      // Add your handling code here:
      String itemStr = 
	(String)(this.mActionTypeCB.getItemAt(this.mActionTypeCB.getSelectedIndex()));
      if("(No Action)".equals(itemStr))
      {
	JOptionPane.showMessageDialog(SetActionDialog.this, 
				      "This is No Action... No Setting.",
				      "warning",
				      JOptionPane.PLAIN_MESSAGE);

      }
      else if("Beep".equals(itemStr))
      {
	JOptionPane.showMessageDialog(SetActionDialog.this, 
				      "No setting for Beep",
				      "warning",
				      JOptionPane.PLAIN_MESSAGE);
      }
      else if("Color Change".equals(itemStr))
      {
	if(this.mFBCSDialog == null)
	{
	  this.mFBCSDialog = new FGBGColorSelDialog(null, true);
	}
	this.mFBCSDialog.popup(this.mColorChangeFGColor, this.mColorChangeBGColor);
	if(this.mFBCSDialog.getReturnStatus() == FGBGColorSelDialog.RET_OK)
	{
	  this.mColorChangeFGColor = this.mFBCSDialog.getFGColor();
	  this.mColorChangeBGColor = this.mFBCSDialog.getBGColor();
	}
      }
      else
      {
	JOptionPane.showMessageDialog(SetActionDialog.this, 
				      "Internal Error! : no such Action",
				      "Error",
				      JOptionPane.PLAIN_MESSAGE);
	
      }
    }//GEN-LAST:event_mActionSettingBtnActionPerformed

    private void mSetTimeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mSetTimeBtnActionPerformed
      // Add your handling code here:
      if(this.mSTDialog == null)
      {
	this.mSTDialog = new SetTimeDialog(null, true);
      }
      this.mSTDialog.popup(this.mSettedTime);
      if(this.mSTDialog.getReturnStatus() == SetTimeDialog.RET_OK)
      {
	// System.out.print("DEBUG: OK");
	this.mSettedTime = this.mSTDialog.getSettedTime();
	this.updateTimeTextField();
      }
    }//GEN-LAST:event_mSetTimeBtnActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
      String itemStr = 
	(String)(this.mActionTypeCB.getItemAt(this.mActionTypeCB.getSelectedIndex()));
      if("(No Action)".equals(itemStr))
      {
	this.mKeepAction   = null;
	this.mReturnAction = null;
      }
      else if("Beep".equals(itemStr))
      {
	this.mKeepAction   = new AktionBeep(this.mSettedTime);
	this.mReturnAction = new AktionBeep(this.mSettedTime);
      }
      else if("Color Change".equals(itemStr))
      {
	this.mKeepAction = 
	  new AktionColorChange(this.mSettedTime, 
				this.mColorChangeFGColor, this.mColorChangeBGColor, 
				this.mTCP);
	this.mReturnAction = 
	  new AktionColorChange(this.mSettedTime, 
				this.mColorChangeFGColor, this.mColorChangeBGColor, 
				this.mTCP);
      }
      else
      {
	throw new RuntimeException("Internal Error. Can not new Actionable.");
      }
      doClose(RET_OK);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }


  /**
   * update time text field
   */
  private void updateTimeTextField()
  {
    long [] s = TimeFormatter.calcMSecToTime(this.mSettedTime);
    if(s[0] >= 60)
    {
      System.out.println("Warning: too large start hours. set 59.");
      s[0] = 59;
    }
    String timeStr = TimeFormattable.zeroP(s[0], 2) + ":" +
      TimeFormattable.zeroP(s[1], 2) + ":" + 
      TimeFormattable.zeroP(s[2], 2);
    this.mTimeText.setText(timeStr);
    this.pack();
  }

  /**
   * init after initComponents (editing initComponents() is prohibited
   * by the forte (GUI builder).)
   */
  public void initAfterInit()
  {
    this.mActionTypeCB.addItem("(No Action)");
    this.mActionTypeCB.addItem("Beep");
    this.mActionTypeCB.addItem("Color Change");

    this.updateTimeTextField();
    this.pack();
  }

  /**
   * get Actionable
   */
  public Actionable getActionable()
  {
    return(this.mReturnAction);
  }
  
  /**
   * update GUI
   */
  private void updateGUI()
  {
    if(this.mKeepAction == null)
    {
      this.mActionTypeCB.setSelectedIndex(0); // (No Action)
    }
    else if(this.mKeepAction instanceof AktionBeep)
    {
      this.mActionTypeCB.setSelectedIndex(1); // Beep
      this.updateTimeTextField();
    }
    else if(this.mKeepAction instanceof AktionColorChange)
    {
      this.mActionTypeCB.setSelectedIndex(2); // Color Change
      this.updateTimeTextField();
    }
    else
    {
      throw new RuntimeException("Internal Error!");
    }
  }

  /**
   * popup 
   */
  public void popup(TimerClockPanel tcpRef)
  {
    this.mTCP = tcpRef;
    this.updateGUI();
    this.show();
  }
  
  /**
   * main for test
   * @param args the command line arguments
   */
  public static void main(String args[]) 
  {
    SetActionDialog sad = new SetActionDialog(new javax.swing.JFrame(), true);
    sad.popup(null);
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel buttonPanel;
  private javax.swing.JButton okButton;
  private javax.swing.JButton cancelButton;
  private javax.swing.JPanel mMainPanel;
  private javax.swing.JButton mActionSettingBtn;
  private javax.swing.JComboBox mActionTypeCB;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JButton mSetTimeBtn;
  private javax.swing.JTextField mTimeText;
  // End of variables declaration//GEN-END:variables

  private int returnStatus = RET_CANCEL;
  private SetTimeDialog mSTDialog   = null;
  private long          mSettedTime = 0;  
  private FGBGColorSelDialog mFBCSDialog = null;
  private Color mColorChangeFGColor = Color.black;
  private Color mColorChangeBGColor = Color.white;
  private Actionable mKeepAction    = null;
  private Actionable mReturnAction  = null; // copy of Keep Action
  private TimerClockPanel mTCP      = null;
}
