// ==========================================================================
// $Id: SetTimeDialog.java,v 1.4 2002/09/09 20:12:30 hitoshi Exp $
// Set Time 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.4 $
// $Date: 2002/09/09 20:12:30 $
// $Author: hitoshi $
// ==========================================================================
package de.mpg.mpisb.timerclock;

/**
 * SetTime GUI
 * @author Yamauchi Hitoshi
 */
public class SetTimeDialog 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 SetTimeDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
	initSecond();
    }

    /** @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();
        jLabel2 = new javax.swing.JLabel();
        mStartTimePanel = new javax.swing.JPanel();
        mPeriodHHCB = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        mPeriodMMCB = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        mPeriodSSCB = new javax.swing.JComboBox();
        mSelectPresetCB = new javax.swing.JComboBox();
        
        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("timer setting"));
        jLabel2.setText("Time (hour : minute : second) ");
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        mMainPanel.add(jLabel2, gridBagConstraints1);
        
        mPeriodHHCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mPeriodHHCBActionPerformed(evt);
            }
        });
        
        mStartTimePanel.add(mPeriodHHCB);
        
        jLabel6.setText(":");
        mStartTimePanel.add(jLabel6);
        
        mPeriodMMCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mPeriodMMCBActionPerformed(evt);
            }
        });
        
        mStartTimePanel.add(mPeriodMMCB);
        
        jLabel7.setText(":");
        mStartTimePanel.add(jLabel7);
        
        mPeriodSSCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mPeriodSSCBActionPerformed(evt);
            }
        });
        
        mStartTimePanel.add(mPeriodSSCB);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        mMainPanel.add(mStartTimePanel, gridBagConstraints1);
        
        mSelectPresetCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mSelectPresetCBActionPerformed(evt);
            }
        });
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = 1;
        mMainPanel.add(mSelectPresetCB, gridBagConstraints1);
        
        getContentPane().add(mMainPanel, java.awt.BorderLayout.CENTER);
        
        pack();
    }//GEN-END:initComponents

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

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

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

    private void mSelectPresetCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mSelectPresetCBActionPerformed
      // Add your handling code here:
      this.setTime(this.mPresets[this.mSelectPresetCB.getSelectedIndex()]);
      repaint();
    }//GEN-LAST:event_mSelectPresetCBActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
      this.updateTime();
      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();
    }

  /**
   * init -- non automatic generated part
   */
  private void initSecond()
  {
    for(int i = 0; i < 60; i++)
    {
      String s = Integer.toString(i);
      if(s.length() == 1)
      {
	s = "0" + s;
      }
      this.mPeriodHHCB.addItem(s);
      this.mPeriodMMCB.addItem(s);
      this.mPeriodSSCB.addItem(s);
    }

    // preset 
    this.setPresets(PresetsDefault, PresetsStrDefault);

    this.pack();
  }

  /**
   * presets
   *   array of millisec
   */
  public void setPresets(long [] presets, String [] displayStr)
  {
    this.mPresets   = presets;
    this.mPresetStr = displayStr;
    
    for(int i = 0; i < this.mPresets.length; i++)
    {
      this.mSelectPresetCB.addItem(displayStr[i]);
    }
  }

  /**
   * update setted time 
   */
  private void updateTime()
  {
    long [] s = new long[4];
    s[3] = 0;
    
    s[0] = this.mPeriodHHCB.getSelectedIndex();
    s[1] = this.mPeriodMMCB.getSelectedIndex();
    s[2] = this.mPeriodSSCB.getSelectedIndex();
    this.mSettedTime = TimeFormatter.calcTimeToMsec(s);
  }

  /**
   * set period
   */
  public void setTime(long period)
  {
    this.mSettedTime  = period;

    long [] s = TimeFormatter.calcMSecToTime(period);
    if(s[0] >= 60)
    {
      System.out.println("Warning: too large start hours. set 59.");
      s[0] = 59;
    }
    this.mPeriodHHCB.setSelectedIndex((int)(s[0]));
    this.mPeriodMMCB.setSelectedIndex((int)(s[1]));
    this.mPeriodSSCB.setSelectedIndex((int)(s[2]));
  }

  /**
   * get setted time : the perfect tense of set is set, but ....
   */
  public long getSettedTime()
  {
    return(this.mSettedTime);
  }
  /**
   * popup 
   */
  public void popup(long time)
  {
    this.setTime(time);
    this.show();
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) 
  {
    SetTimeDialog st = new SetTimeDialog(new javax.swing.JFrame(), true);
    st.popup(1000);
    if(st.getReturnStatus() == RET_OK)
    {
      System.out.print("OK");
    }
    else
    {
      System.out.print("Cancel");
    }
    System.out.println(": period = "  + st.getSettedTime());
  }

  // 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.JLabel jLabel2;
  private javax.swing.JPanel mStartTimePanel;
  private javax.swing.JComboBox mPeriodHHCB;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JComboBox mPeriodMMCB;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JComboBox mPeriodSSCB;
  private javax.swing.JComboBox mSelectPresetCB;
  // End of variables declaration//GEN-END:variables

  private int returnStatus = RET_CANCEL;

  private long      mSettedTime = 0;
  private long   [] mPresets    = null;
  private String [] mPresetStr  = null;

  private static long [] PresetsDefault = {
    600000,
    1200000,
    1800000,
    2400000,
  };
  private static String [] PresetsStrDefault = {
    "10 min",
    "20 min",
    "30 min",
    "40 min",
  };
}
