package nc.pub.filesystem.newui;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.KeyStroke;

import nc.bs.logging.Logger;
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.filesystem.FileManageServletClient;
import nc.vo.pub.filesystem.NCFileNode;

public class ThPasteAttachAction extends AbstractNCAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6069958008297322824L;
	private ThFileContentPanel parentpanel; 
	public ThPasteAttachAction(ThFileContentPanel para){
		parentpanel = para;
		this.putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E,KeyEvent.CTRL_DOWN_MASK ));
		this.putValue(AbstractAction.NAME,NCLangRes.getInstance().getStrByID("sftemp", "PasteAttachAction-0000")/*粘贴附件*/);
		this.putValue(Action.SHORT_DESCRIPTION, NCLangRes.getInstance().getStrByID("sftemp", "PasteAttachAction-0000")/*粘贴附件*/);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		//上传位置，树节点
		NCFileNode filenode =null ;
		if(parentpanel.getPatchTree().getSelectionPath()==null){
//			MessageDialog.showErrorDlg(parentpanel, "错误", "未选择要上传的节点！");
//			return;
			filenode =(NCFileNode)parentpanel.rootNode;
		}else{
			filenode =(NCFileNode)parentpanel.getPatchTree().getSelectionPath().getLastPathComponent();
		}
		//内存
		Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable  tb =clip.getContents(null);
		boolean isfailed=false ;
		FileInputStream finstream=null;
		try {
			
			String dsName = WorkbenchEnvironment.getInstance().getLoginBusiCenter().getDataSourceName();
			String creator = WorkbenchEnvironment.getInstance().getLoginUser().getCuserid();
			//剪切板中的 文件获取到list
			List<File>list  =(List<File>)tb.getTransferData(DataFlavor.javaFileListFlavor);
			if(list!=null){
				for(File file :list){
					//类型判断文件夹不上传
					if(file.isDirectory()){
//						//MessageDialog.showErrorDlg(parentpanel, "错误", "\""+file.getAbsolutePath()+"\"是文件夹不能上传！");						
						continue;
					}
					finstream = new FileInputStream(file);					
					//校验是否同名
					if(parentpanel.CheckTheSameFileName(filenode, file.getName())){
						continue ;
					}
					//保留使用ncv61 以前的上传方法
					NCFileNode  node =FileManageServletClient.uploadFile(dsName, filenode.getFullPath(), file.getName(), finstream, creator, file.length());
					
					//上传成功的话，node不等于null
					if(node!=null){
						//刷新界面
						parentpanel.nodeToFileVO(filenode, node);
					}
				}
			}else{
				isfailed=true;
			}
		} catch (UnsupportedFlavorException e1) {
			isfailed=true;
			Logger.error(e1);
		} catch (IOException e1) {
			isfailed=true;
			Logger.error(e1);
		} catch (Exception e1) {
			isfailed=true;
			Logger.error(e1);;
		}finally{
			try {
				if(finstream!=null){
					finstream.close();
				}
			} catch (IOException e1) {
				Logger.error(e1);
			}
		}
		//粘贴错误提示
		if(isfailed){
			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("sftemp", "PasteAttachAction-0001")/*错误*/, NCLangRes.getInstance().getStrByID("sftemp", "PasteAttachAction-0002")/*复制文件上传遇到错误！*/);
		}
	}
	
	
}
