/**
 * @(#)DownloadDialog.java
 *
 * Copyright 2001 Nomura Research Institute, LTD. All rights reserved.
 *
 * RID-3 : ダウンロードダイアログ
 *
 * @author  Intelligent Technology Inc.
 * @version 1.01 2002/09/06
 */
package jp.co.nri.rid.environment;

import java.net.*;
import java.awt.*;

import java.awt.event.ActionEvent;

import javax.swing.JComponent;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import jp.co.iti.fagot.ZApp;
import jp.co.iti.fagot.IZApp;

import jp.co.iti.fagot.ui.UDialog;

import jp.co.iti.fagot.util.ZInstance;
import jp.co.iti.fagot.util.ZResource;
import jp.co.iti.fagot.util.ZDebug;

import jp.co.nri.rid.RidMIT;
import jp.co.nri.Ridual;

/**
 * ダウンロードダイアログの表示
 */
public class DownloadDialog {

	//## ダイアログ ###################################################
	/**
	 * ダウンロードダイアログの表示
	 */
    public boolean show() {
	IZApp app = ZApp.getTheApp();
	ZResource res = app.getResource();
	mDialog = new UDialog(app,"download");
	
	// ダイアログの表示
//	mDialog.setComponent( panel,false );
	mDialog.setComponent( getContentComponent(),false );
	mDialog.setControlPanel("ok cancel");

	// デフォルトボタンの設定
	mDialog.getRootPane().setDefaultButton(mDialog.getOKButton());

	boolean bRet = mDialog.doModal();

	if ( bRet ) {
		String szURL = mURL.getText();
		if ( szURL != null && szURL.length()==0 ) {
			szURL = null;
		}
		res.putString("download.url",szURL);
		
		if(mTopZoneURL.isSelected() && (null != szURL)){
			try{
				URL url = new URL(szURL);
				
				String path = url.getPath();
				
				if(!path.endsWith("/")){
					int ix = path.lastIndexOf('/');
					if(0 <= ix)	path = path.substring(0, ix);
				}
				
				Ridual.setTopZoneURL(
					new URL(url.getProtocol(),
							url.getHost(),
							url.getPort(),
							path
					).toExternalForm()
				);
				
			}catch(MalformedURLException e){ Ridual.setTopZoneURL(szURL); }
		}
		
        res.putBoolean("download.resource", mResource.isSelected());
        res.putBoolean("download.topzoneurl", mTopZoneURL.isSelected());

		String szHost = mHost.getText();
		if ( szHost != null && szHost.length()==0 ) {
			szHost = null;
		}
		res.putString("download.proxy.host",szHost);

		String szPort = mPort.getText();
		if ( szPort != null && szPort.length()==0 ) {
			szPort = null;
		}
		res.putString("download.proxy.port",szPort);

		String szUser = mUser.getText();
		if ( szUser == null || szUser.length()==0 ) {
			ZInstance.prop().remove("download.proxy.user");
		} else {
			ZInstance.prop().setProperty("download.proxy.user",szUser);
		}

		String szPass = new String(mPass.getPassword());
		if ( szPass.length()==0 ) {
			ZInstance.prop().remove("rid.perform");
		} else {
			ZInstance.prop().setProperty("rid.perform",szPass);
		}
	}
	return bRet ;
	}


	private JComponent getContentComponent(){
	ZResource res = ZApp.getTheApp().getResource();
	
	// パネル内レイアウトの作成
	GridBagLayout gridbag = new GridBagLayout();
	GridBagConstraints cns = new GridBagConstraints();
	cns.insets  = new Insets(10,10,0,10) ;
	JPanel panel = new JPanel(new BorderLayout(4, 4));
	panel.setBorder(new EmptyBorder(8, 8, 0, 8));
	
	JPanel pnl = new JPanel(new BorderLayout());
	
	// １行目：URL
	pnl.add(new JLabel(res.getString("msg.download.url")), BorderLayout.WEST);
	
	mURL = new JTextField(30);
	String szURL = res.getString("download.url");
	if ( szURL != null ) {
		mURL.setText( szURL );
	}
	pnl.add(mURL, BorderLayout.CENTER);
	panel.add(pnl, BorderLayout.NORTH);
	
	JTabbedPane tab = new JTabbedPane();
	panel.add(tab, BorderLayout.CENTER);
	
	pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
	tab.add("option", pnl);
	
	// ２行目：リソース
	mResource = new JCheckBox(res.getString("msg.download.resource"), res.getBoolean("download.resource"));
	pnl.add(mResource);
	
	// TopZone URL
	mTopZoneURL = new JCheckBox(res.getString("msg.download.topzoneurl"), res.getBoolean("download.topzoneurl"));
	pnl.add(mTopZoneURL);
	
	
	pnl = new JPanel(gridbag);
	tab.add("proxy", pnl);
	
	// ３行目：Proxy ホスト
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	pnl.add(new JLabel(res.getString("msg.download.proxy.host")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mHost = new JTextField(30) ;
	String szHost = res.getString("download.proxy.host");
	if ( szHost != null ) {
		mHost.setText( szHost );
	}
	pnl.add(mHost,cns);

	// ４行目：Proxy ポート
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	pnl.add(new JLabel(res.getString("msg.download.proxy.port")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mPort = new JTextField(30) ;
	String szPort = res.getString("download.proxy.port");
	if ( szPort != null ) {
		mPort.setText( szPort );
	}
	pnl.add(mPort,cns);

	// ５行目：Proxy ユーザ
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	pnl.add(new JLabel(res.getString("msg.download.proxy.user")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mUser = new JTextField(30) ;
//	String szUser = res.getString("download.proxy.user");
//	if ( szUser != null ) {
//		mUser.setText( szUser );
//	}
	pnl.add(mUser,cns);

	// ６行目：Proxy パスワード
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	pnl.add(new JLabel(res.getString("msg.download.proxy.pass")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mPass = new JPasswordField(30) ;
//	String szPass = res.getString("rid.perform");
//	szPass = RidMIT.decodeMIT(szPass);
//	if ( szPass != null ) {
//		mPass.setText( szPass );
//	}
	pnl.add(mPass,cns);
	
	return panel;
	}
	
	
/*

	private JComponent getContentComponent(){
	ZResource res = ZApp.getTheApp().getResource();
	
	// パネル内レイアウトの作成
	GridBagLayout gridbag = new GridBagLayout();
	GridBagConstraints cns = new GridBagConstraints();
	cns.insets  = new Insets(10,10,0,10) ;
	JPanel panel = new JPanel();
	panel.setLayout( gridbag );

	// １行目：URL
	cns.fill = GridBagConstraints.BOTH;
	cns.weightx = 0.0;
	panel.add(new JLabel(res.getString("msg.download.url")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mURL = new JTextField(30);
	String szURL = res.getString("download.url");
	if ( szURL != null ) {
		mURL.setText( szURL );
	}
	panel.add(mURL,cns);

	// ２行目：リソース
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	panel.add(new JLabel(res.getString("msg.download.resource")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mResource = new JCheckBox();
	boolean bResource = res.getBoolean("download.resource");
	mResource.setSelected( bResource );
	panel.add(mResource,cns);

	// ３行目：Proxy ホスト
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	panel.add(new JLabel(res.getString("msg.download.proxy.host")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mHost = new JTextField(30) ;
	String szHost = res.getString("download.proxy.host");
	if ( szHost != null ) {
		mHost.setText( szHost );
	}
	panel.add(mHost,cns);

	// ４行目：Proxy ポート
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	panel.add(new JLabel(res.getString("msg.download.proxy.port")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mPort = new JTextField(30) ;
	String szPort = res.getString("download.proxy.port");
	if ( szPort != null ) {
		mPort.setText( szPort );
	}
	panel.add(mPort,cns);

	// ５行目：Proxy ユーザ
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	panel.add(new JLabel(res.getString("msg.download.proxy.user")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mUser = new JTextField(30) ;
//	String szUser = res.getString("download.proxy.user");
//	if ( szUser != null ) {
//		mUser.setText( szUser );
//	}
	panel.add(mUser,cns);

	// ６行目：Proxy パスワード
	cns.gridwidth = GridBagConstraints.RELATIVE;
	cns.weightx = 0.0;
	panel.add(new JLabel(res.getString("msg.download.proxy.pass")),cns);

	cns.gridwidth = GridBagConstraints.REMAINDER;
	cns.weightx = 10.0;
	mPass = new JPasswordField(30) ;
//	String szPass = res.getString("rid.perform");
//	szPass = RidMIT.decodeMIT(szPass);
//	if ( szPass != null ) {
//		mPass.setText( szPass );
//	}
	panel.add(mPass,cns);
	
	return panel;
	}
*/	
	
	//## アトリビュート ###############################################
	/**
	 * ダイアログ
	 */
	protected UDialog mDialog = null;

	/**
	 * ダウンロードURL
	 */
	protected JTextField mURL = null;

	/**
	 * ダウンロードURL
	 */
	protected JCheckBox mResource = null;

	/**
	 * Proxyホスト
	 */
	protected JTextField mHost = null;

	/**
	 * Proxyポート
	 */
	protected JTextField mPort = null;

	/**
	 * Proxyユーザ
	 */
	protected JTextField mUser = null;

	/**
	 * Proxyパスワード
	 */
	protected JPasswordField mPass = null;

	/**
	 * TopZone URLを設定
	 */
	private JCheckBox mTopZoneURL = null;
}
