package nc.pub.filesystem.newui;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.funcnode.ui.action.AbstractNCAction;
import nc.ui.ml.NCLangRes;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UIRefPane;
import nc.vo.pub.filesystem.NCFileNode;

public class ThUploadRefAction extends AbstractNCAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1827737299310863063L;
	private ThFileContentPanel parentpanel ;
	public ThUploadRefAction(ThFileContentPanel para) {
		parentpanel = para ;
		this.putValue(AbstractAction.NAME, NCLangRes.getInstance().getStrByID("sftemp", "UploadRefAction-0000")/*引用文档*/);
		this.putValue(Action.SHORT_DESCRIPTION, NCLangRes.getInstance().getStrByID("sftemp", "UploadRefAction-0000")/*引用文档*/);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (parentpanel.rootNode == null) {
			return;
		}
		TreePath path = parentpanel.getPatchTree().getSelectionPath();
		if (path == null) {
			path = new TreePath(parentpanel.rootNode.getPath());
		}
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
//		if (!node.getAllowsChildren()) {
//			MessageDialog.showErrorDlg(FileManageUI.this, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000008")/*不能在文件下面新建文件*/);
//			return;
//		}
//		node = getClosedValidateNode(node);
		UIRefPane fileref = new UIRefPane(NCLangRes.getInstance().getStrByID("sftemp", "UploadRefAction-0001")/*文档*/);
		fileref.setMultiSelectedEnabled(false);
		fileref.getRefModel().setUseDataPower(false);
		
		fileref.showModel();
		String pk_doc = fileref.getRefPK();
		if(pk_doc==null)
			return;
		String pPath = "";
		if (node instanceof NCFileNode) {
			pPath = ((NCFileNode) node).getFullPath();
		}
		String creator = WorkbenchEnvironment.getInstance().getLoginUser().getCuserid();
		try{
			IFileSystemService service = NCLocator.getInstance().lookup(IFileSystemService.class);
			//文档中心上传
			NCFileNode newNode = service.createRefFileNode(pPath, creator, pk_doc);
			if (newNode != null) {
				parentpanel.nodeToFileVO((NCFileNode)node, newNode);
			}
		} catch (Exception ex) {
			Logger.error(ex.getMessage(), ex);
			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/, ex.getMessage());
		}
	}
}
