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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;

import com.teamcenter.rac.aif.AIFDesktop;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.classification.common.AbstractG4MApplication;
import com.teamcenter.rac.classification.common.G4MApplicationPanel;
import com.teamcenter.rac.classification.common.G4MUserAppContext;
import com.teamcenter.rac.classification.common.table.G4MTable;
import com.teamcenter.rac.classification.common.tree.G4MAutoScrollingJTree;
import com.teamcenter.rac.classification.common.tree.G4MTreeNode;
import com.teamcenter.rac.classification.icm.ICMApplication;
import com.teamcenter.rac.kernel.TCComponentICO;

import cn.net.connor.std.aifrcp.util.KUtil;

@SuppressWarnings("serial")
public class KG4MDialog extends AbstractAIFDialog {

	private JButton b_ok;
	private JButton b_cel;
	private ICMApplication app;
	private MyG4MContext m_context;
	protected TCComponentICO ico;
	private String expandClass;

	public KG4MDialog(String expandClass) {
		super(AIFDesktop.getActiveDesktop(), true);
		this.expandClass = expandClass;
		try {
			this.app = new ICMApplication();
			this.m_context = new MyG4MContext(app, "k0");
		} catch (Exception e) {
			e.printStackTrace();
		}
		initUI();
	}

	public TCComponentICO getSelectedICO() {
		showDialog();
		return ico;
	}

	private void initUI() {
		this.setTitle("");
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(800, 600));
		this.setMinimumSize(new Dimension(640, 480));
		MyG4MPanel panel = new MyG4MPanel(null);
		try {
			panel.initialize(m_context);
			this.add(BorderLayout.CENTER, panel);
			m_context.getTree().expandToClass(expandClass);
			if(!KUtil.isEmpty(expandClass)){
				G4MAutoScrollingJTree tree = m_context.getTree().getTree();
				G4MTreeNode selected = (G4MTreeNode) tree.getSelectionPath().getLastPathComponent();
				System.out.println("Set Root Node: " + selected);
				if(selected!=null) {
					m_context.getTree().setRootNode(selected, true);
					m_context.nodeSelected("", expandClass);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.add(BorderLayout.SOUTH, initBtnPanel());
		addListeners();
	}

	private void addListeners() {
		b_ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				G4MTable table = m_context.getTablePane().getG4MTable();
				List<?> list = null;
				if (table != null) {
					ISelection sel = table.getSelection();
					if (sel != null && sel instanceof StructuredSelection) {
						list = ((StructuredSelection) sel).toList();

					}
				}
				if (list == null || list.size() == 0) {
					return;
				}
				if (list.size() > 1) {
					return;
				}
				Object target = list.get(0);
				if (target != null && target instanceof TCComponentICO) {
					ico = (TCComponentICO) target;
					disposeDialog();
				} else {
					return;
				}
			}
		});
		b_cel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
	}

	private JPanel initBtnPanel() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 5));
		this.b_ok = new JButton("\u786E\u5B9A");
		this.b_cel = new JButton("\u53D6\u6D88");
		panel.add(b_ok);
		panel.add(b_cel);
		return panel;
	}

	public class MyG4MContext extends G4MUserAppContext {

		public MyG4MContext(AbstractAIFUIApplication paramAbstractAIFUIApplication, String paramString) throws Exception {
			super(paramAbstractAIFUIApplication, paramString);
		}

		@Override
		public void setBusy(String paramString) {
		}

		@Override
		public void setIdle(String paramString) {
		}

	}

	public class MyG4MPanel extends G4MApplicationPanel {

		public MyG4MPanel(AbstractG4MApplication paramAbstractG4MApplication) {
			super(paramAbstractG4MApplication);
		}

		@Override
		public void closeSignaled() {
			try {
				m_context.clear();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}