package cn.net.connor.std.processedit.choosefromsql;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.Registry;

import cn.net.connor.std.aifrcp.KDialog;
import cn.net.connor.std.aifrcp.util.KUtil;
import cn.net.connor.std.processedit.util.PrefComponent;

@SuppressWarnings("serial")
public class ChooseFromSqlDialog extends KDialog {

	private ChooseFromSqlDialogController controller;
	private JButton b_ok;
	private JButton b_cel;
	private JButton b_search;
	protected String[] config;
	protected List<PrefComponent> prefComponents;
	protected Map<String, TCComponent> revChildren;
	private static final Registry REG = Registry.getRegistry(ChooseFromSqlDialog.class);

	public ChooseFromSqlDialog(PrefComponent bean, List<PrefComponent> prefComponents, Map<String, TCComponent> revChildren) {
		super();
		this.config = bean.getSqlConfig();
		this.prefComponents = prefComponents;
		this.revChildren = revChildren;
	}

	@Override
	protected void addListeners() {
		this.b_cel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
		this.b_ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					controller.doSelect();
				} catch (Exception e1) {
					e1.printStackTrace();
					MessageBox.post(ChooseFromSqlDialog.this, e1);
				}
			}
		});
		this.b_search.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					controller.doSearch();
				} catch (Exception e1) {
					e1.printStackTrace();
					MessageBox.post(ChooseFromSqlDialog.this, e1);
				}
			}
		});
	}

	@Override
	protected void initUI() {
		this.controller = (ChooseFromSqlDialogController) aifController;
		this.setTitle(REG.getString("select.TITLE"));
		this.setPreferredSize(new Dimension(900, 500));
		this.setMinimumSize(new Dimension(550, 400));
		this.setLayout(new BorderLayout());
		this.add(BorderLayout.CENTER, initTablePanel());
		this.add(BorderLayout.WEST, initSearchPanel());
	}

	private Component initBtnPanel() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
		panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
		this.b_search = new JButton(REG.getString("search"));
		this.b_ok = new JButton(REG.getString("ok"));
		this.b_cel = new JButton(REG.getString("cancel"));
		if (controller.searchCols_DB_UI.size() > 0) {
			panel.add(b_search);
		}
		panel.add(b_ok);
		panel.add(b_cel);
		return panel;
	}

	private Component initSearchPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		if (controller.searchCols_DB_UI.size() == 0) {
			this.add(BorderLayout.SOUTH, initBtnPanel());
			return panel;
		}
		JPanel searchPanel = new JPanel(new GridBagLayout());
		searchPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 10));
		GridBagConstraints s = getGridBagConstraints(0, 0, new Insets(0, 10, 10, 0));
		for (String dbColName : controller.searchCols_DB_UI.keySet()) {
			String title = controller.searchCols_DB_UI.get(dbColName);
			KUtil.newRow(searchPanel, new JLabel(title), s, 1);
			JTextField textField = new JTextField(12);
			controller.searchComps.put(dbColName, textField);
			KUtil.addCol(searchPanel, textField, s, 1, 1);
			textField.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent e) {
					if (e.getKeyChar() == KeyEvent.VK_ENTER) {
						b_search.doClick();
					}
				}
			});
		}
		s.weighty = 1;
		KUtil.newRow(searchPanel, new JLabel(""), s, 1);
		JScrollPane scroll = new JScrollPane(searchPanel);
		scroll.setBorder(null);
		panel.add(BorderLayout.CENTER, scroll);
		panel.add(BorderLayout.SOUTH, initBtnPanel());
		return panel;
	}

	private Component initTablePanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 10));
		JScrollPane scroll = new JScrollPane(controller.tableBean.getTable());
		panel.add(BorderLayout.CENTER, scroll);
		controller.tableBean.getTable().addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
					b_ok.doClick();
				}
			}
		});
		return panel;
	}

}
