yuxc
2025-01-15 c09f81131e8b7c83937206d7cf76f34d2020be75
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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<String, File> fileMap = null;
    
    private final VCIJButton pathBtn  = new VCIJButton("浏览");
     
    private final VCIJTextField txt = new VCIJTextField("");
    
    private String operType = "";
    
    private final String textFieldName = "";
    
    private final LinkedHashMap<String, Runnable> actionRunMap = new LinkedHashMap<String, Runnable>();
//    private Map<String, String> dataValueMap = new HashMap<String, String>();
 
    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<RMVolumeFileObject> fileListObjs = new LinkedList<RMVolumeFileObject>();
//        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<size2;i++){
//            RMVolumeFileObject doc = fileListObjs.get(i);
//            try {
//                servicePath = doc.getDesPath();//服务端文件路径(卷服务)
//                downFileName+=doc.getFileType();//用户手动输入文件名+文件类型
//                vddel.receiveFile(curDirectory, downFileName, servicePath);
//            }catch (VCIError e) {
//                e.printStackTrace();
//                return;
//            }
//                
//        }
//        VCIOptionPane.showMessageDialog(this, "下载成功!");
//    }
//
//    /**
//     * 文件类型的属性值信息格式化
//     * @param fileAttVal 文件属性值:(例如格式:C:\Users\VCI\Desktop\file\BD90DACD-918F-2F15-A232-BA6C957A2904_xx.txt)
//     * @return 文件名称:例如:xx.txt
//     */
//    public static String splitFileAttrValue(String fileAttVal){
//        if(fileAttVal.contains(fileTypeSplit)){
//            int begin = fileAttVal.lastIndexOf(fileTypeSplit);
//            fileAttVal = fileAttVal.substring(begin+1);
//        }
//        return fileAttVal;
//    }
//    
//    private void isNull(Map<String, String> dataValueMap, String attrName){
//        if(dataValueMap == null){
//            throw new IllegalArgumentException(attrName);
//        }
//    }
//
//    public Map<String, String> getDataValueMap() {
//        return dataValueMap;
//    }
//
//    public void setDataValueMap(Map<String, String> 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;
    }
}