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

import java.awt.BorderLayout;
import java.awt.Color;
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 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.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

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;

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

	public static final Registry REG = Registry.getRegistry(ModifyMeopNoDialog.class);
	protected JButton b_ok;
	protected DefaultTableModel tableModel;
	protected JTable table;
	private ModifyMeopNoDialogController controller;
	protected JButton b_delete;
	protected JButton b_moveup;
	protected JButton b_movedown;

	@Override
	protected void addListeners() {
		this.b_delete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					controller.delete();
				} catch (Exception e1) {
					e1.printStackTrace();
					MessageBox.post(ModifyMeopNoDialog.this, e1);
				}
			}
		});
		this.b_moveup.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					controller.moveup();
				} catch (Exception e1) {
					e1.printStackTrace();
					MessageBox.post(ModifyMeopNoDialog.this, e1);
				}
			}
		});
		this.b_movedown.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					controller.movedown();
				} catch (Exception e1) {
					e1.printStackTrace();
					MessageBox.post(ModifyMeopNoDialog.this, e1);
				}
			}
		});
		this.b_ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
	}

	@Override
	protected void initUI() {
		this.controller = (ModifyMeopNoDialogController)aifController;
		this.setTitle(REG.getString("modifymeopno.TITLE"));
		this.setPreferredSize(new Dimension(800, 600));
		this.setLayout(new BorderLayout());
		this.add(BorderLayout.CENTER, initTablePanel());
		this.add(BorderLayout.EAST, initBtnPanel());
		try {
			controller.readMeop();
		} catch (Exception e) {
			e.printStackTrace();
			MessageBox.post(this, e);
		}
		controller.checkRelease();
	}

	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_delete = new JButton(REG.getString("delete")), s, 1);
		KUtil.newRow(panel, this.b_moveup = new JButton(REG.getString("moveup")), s, 1);
		KUtil.newRow(panel, this.b_movedown = new JButton(REG.getString("movedown")), 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) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table.setRowHeight(23);
		tableModel.setDataVector(null, controller.tableHeaders);
		table.getTableHeader().setReorderingAllowed(false);
		table.setDefaultRenderer(Object.class, new ColorTableRenderer());
		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]);
		}
		colModel.getColumn(0).setMinWidth(0);
		colModel.getColumn(0).setMaxWidth(0);
		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;
	}
	
	public static class ColorTableRenderer extends DefaultTableCellRenderer {
		public static final Color DEFBLUE = new Color(51, 153, 255);
		public static final Color RED = new Color(255, 59, 48);
		public static final Color GREEN = new Color(158, 234, 106);

		@Override
		public Component getTableCellRendererComponent(JTable t, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			if (hasFocus) {
				setBackground(DEFBLUE);
			} else {
				Object obj = t.getValueAt(row, 0);
				if(obj instanceof ModifyMeopNoRow) {
					if(((ModifyMeopNoRow) obj).isReleased()) {
						setBackground(RED);
					}else {
						setBackground(GREEN);
					}
				}else {
					setBackground(Color.WHITE);
				}
			}
			return super.getTableCellRendererComponent(t, value, isSelected, hasFocus, row, column);
		}

	}

}
