package com.vci.client.uif.engine.client.controls; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.util.LinkedHashMap; import com.vci.client.bof.ClientBusinessObject; import com.vci.client.fm.ClientFileObjectOperation; import com.vci.client.fm.FileObject; import com.vci.client.portal.utility.PRMItem; import com.vci.client.ui.swing.VCISwingUtil; import com.vci.client.ui.swing.components.VCIJButton; import com.vci.client.ui.swing.components.VCIJTextField; import com.vci.client.uif.actions.client.FileOperation; import com.vci.corba.common.VCIError; public class FileChooseControlPanel extends AbstractCustomControl { private static final long serialVersionUID = 1L; private final static String fileTypeSplit = "_"; private final int UPLOAD_FILE_MAX_SIZE = 30;//getFileMaxSize("fileUpload.fileSize"); private final LinkedHashMap fileMap = null; private final VCIJButton pathBtn = new VCIJButton("浏览"); private final VCIJTextField txt = new VCIJTextField(""); private String operType = ""; private final String textFieldName = ""; private final LinkedHashMap actionRunMap = new LinkedHashMap(); // private Map dataValueMap = new HashMap(); private boolean fileChanged = false; /** * 控件的FileObject对象 */ private FileObject fo = null; /** * 本地文件路径 */ private String localfilePath = ""; /** * 判断是否进行过上传操作 */ private boolean isUpload = false; public FileChooseControlPanel(PRMItem item){ super(item); init(); } private void setButtonInfo(String operType, String text, String icon){ this.operType = operType; pathBtn.setText(""); pathBtn.setToolTipText(text); pathBtn.setIcon(VCISwingUtil.createImageIcon(icon)); } private void init(){ boolean isUpload = true; if(isUpload){ setButtonInfo("upLoad", "上传", "add.png"); } else { setButtonInfo("downnload", "下载", "download.png"); } initActionRunMap(); initFilePanle(); } private void initActionRunMap(){ actionRunMap.put("upLoad", new Runnable() {@Override public void run() { uploadAction(); } }); // actionRunMap.put("downLoad", new Runnable() {public void run() { // downLoadAction(); // } // }); } private void initFilePanle(){ buttonActionListener(); initCompanel(); } private void initCompanel(){ setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1; c.weightx = 1; c.weighty = 0; c.anchor = GridBagConstraints.NORTHWEST; c.fill = GridBagConstraints.BOTH; txt.setPreferredSize(new Dimension(100, 25)); txt.setEditable(false); add(txt, c); c.gridx = 1; c.gridheight = 1; c.gridwidth = 1; c.weightx = 0; c.weighty = 0; c.anchor = GridBagConstraints.EAST; c.fill = GridBagConstraints.BOTH; Dimension dim = new Dimension(27, 23); pathBtn.setMinimumSize(dim); pathBtn.setMaximumSize(dim); pathBtn.setPreferredSize(dim); add(pathBtn, c); } private void buttonActionListener(){ pathBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { actionRunMap.get(operType).run(); } }); } /** * 上传事件 */ private void uploadAction(){ String selectedFile = VCISwingUtil.getFileURL(this.getParentComponent(), false, null, ""); if(selectedFile == null) return; try { if(selectedFile.endsWith(".")){ selectedFile = selectedFile.substring(0, selectedFile.lastIndexOf(".")); } File newFile = new File(selectedFile); this.fo = new FileOperation().createFileObject(newFile, 9, ""); if(fo != null && fo.getBusinessObject() != null){ this.value = fo.getBusinessObject().oid; } this.localfilePath = selectedFile; this.txt.setText(selectedFile); setFileChanged(true); isUpload = false; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 执行上传 * @param cbo 关联的CBO对象 * @return 返回上传后的 FileObject oid */ public String executeUpload(ClientBusinessObject cbo){ String res = ""; try { if(isFileChanged() && !isUpload){ //通过文件控件上传的文件在附件列表中时不存在的 //if(cbo != null && cbo.getBusinessObject() != null // && cbo.getBusinessObject().oid != null){ // fo.setAttributeValue("documentoid", cbo.getBusinessObject().oid); //} new ClientFileObjectOperation().createNewFile(localfilePath, fo); this.value = fo.getBusinessObject().oid; res = this.value; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return res; } // /** // * 下载事件 // */ // static RMVolumeFileClientDelegate rmVolumeFileDel = new RMVolumeFileClientDelegate(LogonApplication.getUserEntityObject()); // // static PvolumeObject pvobj = new PvolumeObject(); // // static PvolumeClientDelegate pcdel = new PvolumeClientDelegate(LogonApplication.getUserEntityObject()); // // // private void downLoadAction(){ // LinkedList fileListObjs = new LinkedList(); // isNull(getDataValueMap(), "所操作的数据对象为空,无法执行下载操作!"); // // TODO // String[] datas = null;//rmDataObject.getDatas(); // String textFileName = getTextFieldName(); // if(!(textFileName.equals(""))){ // for(String data : datas){ // if(textFileName.equals(data)){ // RMVolumeFileObject fileObj = null; // try { // fileObj = rmVolumeFileDel.getVolumeFileByFiledesPath(data); // if(!fileObj.getId().equals("")){ // fileListObjs.add(fileObj); // break; // } // } catch (VCIException e) { // e.printStackTrace(); // } // } // } // } // int size2 = fileListObjs.size(); // if(size2 == 0){ // VCIJOptionPane.showMessage(this, "该条数据没有文件!"); // return; // } // String file = VCISwingUtil.getFileURL(this, true, null, ""); // if(file == null||file.trim().equals("")){ // return; // } // // File downFile = new File(file); // downFile.getPath(); // downFile.getName(); // // String servicePath = ""; // String downFileName = downFile.getName();//客户端文件名称 // String curDirectory = downFile.getParent();//客户端文件路径 // VolumeDelegate vddel = new VolumeDelegate(); // for(int i = 0;i dataValueMap, String attrName){ // if(dataValueMap == null){ // throw new IllegalArgumentException(attrName); // } // } // // public Map getDataValueMap() { // return dataValueMap; // } // // public void setDataValueMap(Map dataValueMap) { // this.dataValueMap = dataValueMap; // } private String value = ""; @Override public String getValue() { return value; } @Override public void setValue(String value) { if(value == null || "".equals(value)) return; String[] values = value.split(":"); this.value = values[0]; this.txt.setText(getFileNameByFOId(this.value)); } public static String getFileNameByFOId(String fileObjectOid){ if(fileObjectOid == null || "".equals(fileObjectOid)) return ""; String res = fileObjectOid; try { FileObject fileObject = new ClientFileObjectOperation().getFileObject(fileObjectOid); res = fileObject.getBusinessObject().name; } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } return res; } @Override public void setEditable(boolean editable) { pathBtn.setEnabled(editable); txt.setEnabled(editable); } @Override public void setEnabled(boolean enabled) { setEditable(enabled); } public boolean isFileChanged() { return fileChanged; } public void setFileChanged(boolean fileChanged) { this.fileChanged = fileChanged; } }