/*
 * @(#)EnvColorEditor.java
 *
 * Copyright 2001 Nomura Research Institute, LTD. All rights reserved.
 *
 * RID-X : カラーコード入力エリア
 *
 * @author  Intelligent Technology Inc.
 * @version 1.01 2002/11/07
 */
package jp.co.nri.rid.environment;

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

import javax.swing.JComponent;
import javax.swing.JColorChooser;
import jp.co.iti.fagot.util.ZGraphicUtil;

/**
  *カラーコード入力エリア
  **/
public class EnvColorEditor extends JPanel {
    /** パネル **/
    protected JPanel        mPanel    = null;
    /** 入力エリア **/
    protected JTextField    mText     = null;
    /** ボタン **/
    protected JToggleButton mTButton  = null;

    /**
      *  入力TEXT領域の作成
      */
    public EnvColorEditor(String value) {

        // パネルの生成
        mPanel = new JPanel();

        // Layoutを「GridBagLayout」にする
        GridBagLayout mLayout = new GridBagLayout();
        mPanel.setLayout(mLayout);

        // Layout制約の作成
        GridBagConstraints gbc = new GridBagConstraints();
        //gbc.insets   = new Insets(0,10,0,10) ;

        // TextFieldの生成
        mText = new JTextField(value);
        setTextColor();
        mText.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) { setTextColor(); }
            }
        );
        mPanel.add(mText);

        //gbc.fill    = GridBagConstraints.HORIZONTAL; 
        gbc.anchor  = GridBagConstraints.NORTHWEST;
        gbc.fill    = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.gridx   = 0;
        mLayout.setConstraints(mText, gbc);


        mTButton = new JToggleButton("Edit");
        mTButton.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) { colorChooser(); }
            }
        );
        gbc.fill    = GridBagConstraints.NONE;
        gbc.weightx = 0.0;
        gbc.gridx   = 1;
        mLayout.setConstraints(mTButton, gbc);
        mPanel.add(mTButton);
    }


    /**
      * JPanelの取得
      */
    public JPanel getPanel(){
        return mPanel;
    }

    /**
      * JTextFieldの取得
      */
    public JTextField getTextField() {
        return mText;
    }


    /**
      * JTextField値の設定
      * @param szValue 設定文字列
      */
    public void setString( String szValue ){
        mText.setText( szValue );
    }

    /**
      * JTextField値の取得
      */
    public String getString(){
        String szTmp = mText.getText();
        if ( szTmp == null || szTmp.equals("") ) {  return null;  }
        return szTmp;
    }

    /**
      * JToggleButtonの取得
      */
    public JToggleButton getTButton() {
        return mTButton;
    }


    /**
      *　ボタンが押されたときの処理
      */
    public void colorChooser() {

        Color  current = ZGraphicUtil.stringToColor(getString());
        Color  c = JColorChooser.showDialog(mPanel,"Choose a color",current);
        //Color  c = JColorChooser.showDialog(this,"Choose a color",current);
        //JDialog dlg = JColorChooser.createDialog(c ,"Choose a color" ,true ,this ,this ,this);
        if ( c != null && c != current ) {
            setString( ZGraphicUtil.colorToString(c) );
            setTextColor();
        }
        mTButton.setSelected(false);

      }


    /**
      * テキストのバックカラーを変更
      */
    public void setTextColor() {
        Color col = ZGraphicUtil.stringToColor(getString());
        if ( col != null ) {
            mText.setForeground(ZGraphicUtil.inverseColor(col));
            mText.setBackground(col);
        } else {
            mText.setForeground(Color.black);
            mText.setBackground(Color.white);
        }
    }
}
