// ==========================================================================
// $Id: FGBGColorSelDialog.java,v 1.2 2002/09/09 20:12:30 hitoshi Exp $
// FG and BG Color 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  java.awt.event.*;
import  javax.swing.*;

/**
 * FG and BG Coor Select Dialog
 * @author  Yamauchi Hitoshi
 */
public class FGBGColorSelDialog 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 FGBGColorSelDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** @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();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        mSetFGColBtn = new javax.swing.JButton();
        mSetBGColBtn = new javax.swing.JButton();
        mFGColorPanel = new javax.swing.JPanel();
        mBGColorPanel = new javax.swing.JPanel();
        
        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);
        
        jPanel1.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        jPanel1.setBorder(new javax.swing.border.TitledBorder("Color Change Action Setting"));
        jLabel3.setText("Foreground Color");
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 0, 10);
        jPanel1.add(jLabel3, gridBagConstraints1);
        
        jLabel4.setText("Background Color");
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 0, 10);
        jPanel1.add(jLabel4, gridBagConstraints1);
        
        mSetFGColBtn.setText("set FG");
        mSetFGColBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mSetFGColBtnActionPerformed(evt);
            }
        });
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 0, 10);
        jPanel1.add(mSetFGColBtn, gridBagConstraints1);
        
        mSetBGColBtn.setText("set BG");
        mSetBGColBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mSetBGColBtnActionPerformed(evt);
            }
        });
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 1;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 0, 10);
        jPanel1.add(mSetBGColBtn, gridBagConstraints1);
        
        mFGColorPanel.setBackground(java.awt.Color.lightGray);
        mFGColorPanel.setMinimumSize(new java.awt.Dimension(50, 10));
        mFGColorPanel.setPreferredSize(new java.awt.Dimension(50, 10));
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        jPanel1.add(mFGColorPanel, gridBagConstraints1);
        
        mBGColorPanel.setMinimumSize(new java.awt.Dimension(50, 10));
        mBGColorPanel.setPreferredSize(new java.awt.Dimension(50, 10));
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 2;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        jPanel1.add(mBGColorPanel, gridBagConstraints1);
        
        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
        
        pack();
    }//GEN-END:initComponents

    private void mSetBGColBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mSetBGColBtnActionPerformed
      // Add your handling code here:
      this.mTmpColor = null;
      final JColorChooser colorChooser = new JColorChooser();
      ActionListener okListener = new ActionListener() {
	  public void actionPerformed(ActionEvent e) {
	    // colorEditor.currentColor = colorChooser.getColor();
	    mTmpColor = colorChooser.getColor();
	  }
	};
      final JDialog dialog = 
	JColorChooser.createDialog(this, "Pic the Background color",
				   true,
				   colorChooser,
				   okListener,
				   null);
      dialog.setVisible(true);
      if(this.mTmpColor != null)
      {
	this.mBGColor = this.mTmpColor;
	this.mBGColorPanel.setBackground(this.mBGColor);    
      }
      
    }//GEN-LAST:event_mSetBGColBtnActionPerformed

    private void mSetFGColBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mSetFGColBtnActionPerformed
        // Add your handling code here:
      this.mTmpColor = null;
      final JColorChooser colorChooser = new JColorChooser();
      ActionListener okListener = new ActionListener() {
	  public void actionPerformed(ActionEvent e) {
	    // colorEditor.currentColor = colorChooser.getColor();
	    mTmpColor = colorChooser.getColor();
	  }
	};
      final JDialog dialog = 
	JColorChooser.createDialog(this, "Pic the Foreground color",
				   true,
				   colorChooser,
				   okListener,
				   null);
      dialog.setVisible(true);
      if(this.mTmpColor != null)
      {
	this.mFGColor = this.mTmpColor;
	this.mFGColorPanel.setBackground(this.mFGColor);    
      }
    }//GEN-LAST:event_mSetFGColBtnActionPerformed

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

  /**
   * get foreground color 
   */
  public Color getFGColor() 
  {
    return(this.mFGColor);
  }
  /**
   * get background color 
   */
  public Color getBGColor() 
  {
    return(this.mBGColor);
  }

  /**
   * popup
   */
  public void popup(Color fgcol, Color bgcol) 
  {
    this.mFGColor = fgcol;
    this.mBGColor = bgcol;
    this.mFGColorPanel.setBackground(this.mFGColor);
    this.mBGColorPanel.setBackground(this.mBGColor);    
    this.pack();
    this.show();
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) 
  {
    FGBGColorSelDialog fbcsd = new FGBGColorSelDialog(new javax.swing.JFrame(), true);
    fbcsd.show();
  }

  // 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 jPanel1;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JButton mSetFGColBtn;
  private javax.swing.JButton mSetBGColBtn;
  private javax.swing.JPanel mFGColorPanel;
  private javax.swing.JPanel mBGColorPanel;
  // End of variables declaration//GEN-END:variables

  private int returnStatus = RET_CANCEL;

  private Color mFGColor  = null;  
  private Color mBGColor  = null;  
  private Color mTmpColor = null;  
}
