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

import java.util.Arrays;

import javax.swing.ListSelectionModel;

import com.teamcenter.rac.aif.AIFDesktop;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.classification.common.G4MInClassDialog;
import com.teamcenter.rac.cme.bvr.connect.assign.AssignResourceCommand;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.Utilities;

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

public class KAssignResourceCommand extends AssignResourceCommand {

	private AIFComponentContext[] paramArrayOfAIFComponentContext;
	private G4MInClassDialog paramG4MInClassDialog;

	@SuppressWarnings("serial")
	public KAssignResourceCommand(String expandClass, TCComponent typeIndicator) {
		super(AIFDesktop.getActiveDesktop().getFrame(), AIFUtility.getCurrentApplication(), new AIFComponentContext[] { new AIFComponentContext(null, typeIndicator, "") });
		G4MInClassDialog m_dialog = new G4MInClassDialog(AIFUtility.getCurrentApplication(), ListSelectionModel.SINGLE_SELECTION, true, true, false, expandClass, null) {
			@Override
			public void setOccurrenceTypeSelectionVisible(boolean paramBoolean) {
			}
		};
		m_dialog.setModal(true);
		m_dialog.addG4MInClassDialogListener(this);
		m_dialog.getContext().addTableSelectionListener(this);
		m_dialog.getContext().addG4MInstanceListener(this);
		setRunnable(m_dialog);
		Utilities.setPrivateField(this, "m_dialog", m_dialog);
	}
	
	@Override
	public void executeCommand() throws TCException {
		super.executeCommand();
	}

	@Override
	public void inClassDialogInstanceSelected(AIFComponentContext[] paramArrayOfAIFComponentContext, G4MInClassDialog paramG4MInClassDialog) {
		System.out.println("Select: " + Arrays.toString(paramArrayOfAIFComponentContext) + " -> " + paramG4MInClassDialog.getOccurrenceType());
		this.paramArrayOfAIFComponentContext = paramArrayOfAIFComponentContext;
		this.paramG4MInClassDialog = paramG4MInClassDialog;
	}

	public TCComponent getSelected() {
		if (KUtil.getLen(paramArrayOfAIFComponentContext) == 0) {
			return null;
		}
		return (TCComponent) paramArrayOfAIFComponentContext[0].getComponent();
	}

	public void doAssign(TCComponentBOMLine pLine) {
		AIFComponentContext[] targets = new AIFComponentContext[] { new AIFComponentContext(null, pLine, "") };
		setPasteTarget(targets);
		super.inClassDialogInstanceSelected(paramArrayOfAIFComponentContext, paramG4MInClassDialog);
	}

}
