/*
 * @(#)AnalyzerDialog
 *
 * Copyright 2001 Nomura Research Institute, LTD. All rights reserved.
 *
 * RID-X : アナライザダイアログ
 *
 * @author  Intelligent Technology Inc.
 * @version 1.01 2001/02/04
 * 			1.10 2003/05/19 Detail Info機能追加に伴う変更
 */
package jp.co.nri.rid.environment;

import java.io.*;
import java.util.*;
import java.text.*;
import java.net.URL;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;

import jp.co.iti.fagot.ZApp;
import jp.co.iti.fagot.doc.DocDocument;
import jp.co.iti.fagot.doc.DocManager;
import jp.co.iti.fagot.util.ZDebug;
import jp.co.iti.fagot.util.ZResource;
import jp.co.iti.fagot.util.ZProperties;
import jp.co.iti.fagot.util.ZString;


/**
 * アナライザダイアログ
 */

public class AnalyzerDialog {

    /** msg.analyzer.titleプロパティ  **/
    final static public String MSG_ANALYZER_TITLE       = "msg.analyzer.title";
    /** msg.analyzer.ignoreプロパティ  **/
    final static public String MSG_ANALYZER_IGNORE      = "msg.analyzer.ignore";
    /** msg.analyzer.o3w.importプロパティ  **/
    final static public String MSG_ANALYZER_O3W_IMPORT  = "msg.analyzer.o3w.import";
    /** rid.ignore.prefixeプロパティ  **/
    final static public String IGNORE_PREFIX            = "rid.ignore.prefix";
    /** analyze.o3w.importプロパティ  **/
    final static public String ANALYZE_O3W_IMPORT       = "analyze.o3w.import";
    
// 2003/05/19
	/** detailinfoプロパティ  **/
	final static public String MSG_ANALYZER_DETAILINFO	= "msg.analyzer.detailinfo";
    final static public String ANALYZER_DETAILINFO		= "analyzer.detailinfo";

    /** パネル      **/
    protected JPanel      mPanel        = null;
    /** Ignoreキー  **/
    protected JTextField  mIgnorePrefix = null;
    /** O3W Import  **/
    protected JCheckBox   mO3WImport    = null;
    
// 2003/05/19
    /** DetailInfo  **/
    protected JCheckBox[] mDetailInfo   = null;
	
    /**
      * デフォルトコンストラクタ
      */
      public AnalyzerDialog() {  }

    /**
      * アナライザダイアログ生成
      *
      * @return boolean     ボタンの状態true(OK)/false(Cancel)
      */
    public boolean show() {

        ZResource res = ZApp.getTheApp().getResource();

        JFrame frame = (JFrame)ZApp.getTheApp().getFrame();
        EnvDialog gdlg = new EnvDialog(frame, res.getString(MSG_ANALYZER_TITLE));

        //メインコンポーネント
        mPanel = new JPanel();

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

        // Layout制約の作成
        GridBagConstraints label_gbc = createLabelConstraints();
        GridBagConstraints compo_gbc = createComponentConstraints();

        //ラベル
        label_gbc.gridy      = 0;
        label_gbc.gridx      = 0;
        JLabel label = new JLabel(res.getString(MSG_ANALYZER_IGNORE));
        mLayout.setConstraints(label, label_gbc);
        mPanel.add( label );

        //IgnorePrefix
        compo_gbc.gridy      = 0;
        compo_gbc.gridx      = 1;
        mIgnorePrefix = createIgnorePrefix();
        mLayout.setConstraints(mIgnorePrefix, compo_gbc);
        mPanel.add( mIgnorePrefix );

        //ラベル
        label_gbc.gridy      = 1;
        label_gbc.gridx      = 0;
        JLabel label2 = new JLabel(res.getString(MSG_ANALYZER_O3W_IMPORT));
        mLayout.setConstraints(label2, label_gbc);
        mPanel.add( label2 );

        //ページチェックボックス生成
        compo_gbc.gridy      = 1;
        compo_gbc.gridx      = 1;
        mO3WImport = new JCheckBox("");
        mO3WImport.setSelected(res.getBoolean(ANALYZE_O3W_IMPORT));
        mLayout.setConstraints(mO3WImport, compo_gbc);
        mPanel.add( mO3WImport );
		
// 2003/5/19 追加
		mDetailInfo = createDetailInfo();
		try{
			JPanel panel = new JPanel();
			int count = mDetailInfo.length;
			for(int i = 0; i < count; i++)	panel.add(mDetailInfo[i]);
	        
			//ラベル
	        label_gbc.gridy      = 2;
	        label_gbc.gridx      = 0;
	        JLabel label3 = new JLabel(res.getString(MSG_ANALYZER_DETAILINFO));
	        mLayout.setConstraints(label3, label_gbc);
	        mPanel.add( label3 );
			
	        //コンポーネント
	        compo_gbc.gridy      = 2;
	        compo_gbc.gridx      = 1;
	        mLayout.setConstraints(panel, compo_gbc);
	        mPanel.add( panel );
			
		} catch(NullPointerException e){}
		
        // [メインコンポーネント]と[OK][CENCEL]
        gdlg.setControlPanel(mPanel);

        //ダイアログを表示する（モーダル）
        gdlg.doModal();

        // [生成]ボタンが押された場合に変更する
        boolean ret = gdlg.isOK();
        if (ret){
            setIgnorePrefix();
            setAnalyzO3WImport();
            setDetailInfo();
        }   
        return ret;
    }


    /**
      * ignoreキーのJTextFieldを生成
      *
      * @return JTextField
      */
    private JTextField createIgnorePrefix() {
        ZResource res = ZApp.getTheApp().getResource();
        String  ignore_prefix = res.getString(IGNORE_PREFIX);
        JTextField text = new JTextField(ignore_prefix,30);
        return text;
    }

    /**
      * DetailInfoのJCheckBoxを生成 2003/05/19
      *
      * @return JCheckBox[]
      */
    private JCheckBox[] createDetailInfo() {
		Vector result = new Vector();
		ZResource res = ZApp.getTheApp().getResource();
		try{
			StringTokenizer st = new StringTokenizer(res.getString(ANALYZER_DETAILINFO) ," ");
			while(st.hasMoreTokens()){
				String label = st.nextToken();
				result.add( new JCheckBox(label, res.getBoolean(ANALYZER_DETAILINFO+"."+label)) );
			}
		}catch(NullPointerException e){}
		
		return (JCheckBox[])result.toArray(new JCheckBox[0]);
    }


    /**
      * ignoreキーの文字列を取得
      *
      * @return void
      */
    private String getIgnorePrefix(){
        String szTmp = mIgnorePrefix.getText();
        if ( szTmp == null || szTmp.equals("") ) {  return null;  }
        return szTmp;
    }


    /**
      * ignoreキーを[ignore.prefix]プロパティに設定
      *
      * @return void
      */
    private void setIgnorePrefix() {
        ZResource res = ZApp.getTheApp().getResource();
        /* [gnore.prefix]を設定 */
        res.putString(IGNORE_PREFIX, getIgnorePrefix() );
    }

    /**
      * O3Wのアナライズ選択状態を[analyz.o3w.import]に設定
      *
      * @return void
      */
    private void setAnalyzO3WImport() {
        ZResource res = ZApp.getTheApp().getResource();
        /* [analyz.o3w.import]を設定 */
        res.putBoolean(ANALYZE_O3W_IMPORT, mO3WImport.isSelected());
    }

    /**
      * DetailInfoのアナライズ選択状態を設定
      *
      * @return void
      */
    private void setDetailInfo() {
        ZResource res = ZApp.getTheApp().getResource();
		
		try{
			for(int i = mDetailInfo.length - 1; 0 <= i; i--){
				JCheckBox comp = mDetailInfo[i];
		        res.putBoolean(ANALYZER_DETAILINFO+"."+comp.getText() , comp.isSelected());
			}
		} catch(NullPointerException e){}
    }
    /**
      * ラベル用レイアウト制約の作成
      *
      * @return GridBagConstraints
      */
    private GridBagConstraints createLabelConstraints() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor  = GridBagConstraints.NORTHWEST;
        gbc.weightx = 0.0 ;
        gbc.insets  = new Insets(10,10,0,10) ;
        return gbc;
    }


    /**
      * コンポーネント用レイアウト制約の作成
      *
      * @return GridBagConstraints
      */
    private GridBagConstraints createComponentConstraints() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill    = GridBagConstraints.BOTH;
        gbc.weightx = 1.0 ;
        gbc.insets    = new Insets(10,10,0,10) ;
        return gbc;
    }
}
