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

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import com.teamcenter.rac.aif.AbstractAIFDialog;
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.KDialogController;
import cn.net.connor.std.aifrcp.util.KUtil;

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

	private JButton b_insert;
	private JButton b_ok;
	protected DefaultTableModel tableModel;
	protected JTable table;
	private MeopNoDialogController controller;
	private boolean ok = false;
	private static final Registry REG = Registry.getRegistry(MeopNoDialog.class);

	public MeopNoDialog(AbstractAIFDialog parent, boolean model, KDialogController controller) {
		super(parent, model, controller);
		this.controller = (MeopNoDialogController) controller;
	}

	@Override
	protected void addListeners() {
		this.b_insert.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					controller.insert();
				} catch (Exception e1) {
					e1.printStackTrace();
					MessageBox.post(MeopNoDialog.this, e1);
				}
			}
		});
		this.b_ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ok = true;
				disposeDialog();
			}
		});
	}

	public Map<String, String> getMeopInfo() throws Exception {
		if (!initDialog()) {
			return null;
		}
		controller.readMeop();
		showDialog();
		if (!ok) {
			return null;
		}
		return controller.getInsertedData();
	}

	@Override
	protected void initUI() {
		this.setTitle(REG.getString("applymeopno.TITLE"));
		this.setPreferredSize(new Dimension(800, 600));
		this.setLayout(new BorderLayout());
		this.add(BorderLayout.CENTER, initTablePanel());
		this.add(BorderLayout.EAST, initBtnPanel());
	}

	private Component initBtnPanel() {
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setBorder(BorderFactory.createEmptyBorder(16, 0, 7, 7));
		GridBagConstraints s = getGridBagConstraints(0, 0, new Insets(0, 0, 12, 0));
		KUtil.newRow(panel, this.b_insert = new JButton(REG.getString("insert")), s, 1);
		KUtil.newRow(panel, this.b_ok = new JButton(REG.getString("ok")), s, 1);
		s.weighty = 1;
		KUtil.newRow(panel, new JLabel(), s, 1);
		return panel;
	}

	private Component initTablePanel() {
		this.tableModel = new DefaultTableModel();
		this.table = new JTable(tableModel);
		table.setRowHeight(23);
		tableModel.setDataVector(null, controller.tableHeaders);
		table.getTableHeader().setReorderingAllowed(false);
		int colCnt = KUtil.getLen(controller.tableHeaders);
		TableColumnModel colModel = this.table.getColumnModel();
		for (int i = 0; i < colCnt; i++) {
			colModel.getColumn(i).setPreferredWidth(controller.tableHeaderWidth[i]);
		}
		if (colCnt > 5) {
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		}
		JScrollPane scroll = new JScrollPane(table);
		scroll.setBorder(null);
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(BorderLayout.CENTER, scroll);
		panel.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
		return panel;
	}

}
