田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package com.vci.client.uif.engine.client.custom;
 
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import javax.swing.text.JTextComponent;
 
import com.vci.client.bof.ClientBusinessObject;
import com.vci.client.bof.ClientLinkObject;
import com.vci.client.common.ConfigUtils;
import com.vci.client.common.PositionMessager;
import com.vci.client.portal.custom.ICustomDefine;
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.VCIJComboBox;
import com.vci.client.ui.swing.components.VCIJDialog;
import com.vci.client.ui.swing.components.VCIJOptionPane;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.ui.swing.components.VCIJTextField;
import com.vci.client.ui.swing.components.VCIJDialog.DialogResult;
import com.vci.client.uif.actions.client.UIFUtils;
import com.vci.client.uif.engine.client.UIHelper;
import com.vci.client.uif.engine.client.UILayoutPanel;
import com.vci.client.uif.engine.client.controls.AbstractCustomControl;
import com.vci.client.uif.engine.client.objopt.ObjectAddEditPanel;
import com.vci.client.uif.engine.client.tableArea.TablePanel;
import com.vci.client.uif.engine.client.tree.TreePanel;
import com.vci.client.uif.engine.common.CBOHelper;
import com.vci.client.uif.engine.common.IDataNode;
import com.vci.corba.common.VCIError;
import com.vci.mw.ClientContextVariable;
 
/**
 * 参数值选择自定义控件
 * @Title :ReferenceValueChooseCtl.java
 * @Description :
 * @Copyright :宏博远达科技有限公司
 * @Author :平台与规划部/ZhongGY/E-mail:zhonggy@vci-tech.com
 * @Date :2015-5-26
 * @Version :1
 * @Other :产生注释:Alt+Shift+J
 */
public class ReferenceValueChooseCtl extends AbstractCustomControl implements
        ICustomDefine {
 
    private static final long serialVersionUID = 1L;
    //private PRMItem item;
    private String value = "";
    private VCIJTextField text = new VCIJTextField("");
    private final Image img = VCISwingUtil.createImageIcon("search.png").getImage();
    private VCIJButton button=VCISwingUtil.createVCIJButton("","", "选择参照对象", "search.png");//search.gif
    /*@SuppressWarnings("unused")
    private VCIJButton button = new VCIJButton("参照对象");*/
    /*@SuppressWarnings("serial")
    private final VCIJTextField txt = new VCIJTextField("") {
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            if (img != null) {
                g2.drawImage(img, getWidth() - img.getWidth(null), getHeight()
                        - img.getHeight(null), img.getWidth(null),
                        img.getHeight(null), null);
            }
            ReferenceValueChooseCtl.this.drawRequiredFlag(g);
        }
 
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            if (!isEnabled() || !editable)
                return;
            if (isClickImage(e)) {
                try {
                    showRefObjChooseDialog();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
 
        private boolean isClickImage(MouseEvent e) {
            boolean res = false;
            int imgWidth = img.getWidth(null);
            int imgHeight = img.getHeight(null);
            int width = getWidth();
            int height = getHeight();
            int pointX = e.getX();
            int pointY = e.getY();
            if (width - imgWidth < pointX && pointX < width
                    && height - imgHeight < pointY && pointY < height) {
                res = true;
            }
            // ignore cals
            res = true;
            return res;
        }
    };*/
    //private ICustomAttributeInteceptor inteceptor = null;
    //private VCIJFileChooser vciJFileChooser = null;
    //private String filePath = "";
    // private String localfilePath = "";
    //private boolean fileChanged = false;
 
    public ReferenceValueChooseCtl() {
    }
    
    //private boolean editable = false;
    public ReferenceValueChooseCtl(PRMItem item) {
        super(item);
        /*init();
        editable = item.getItemIsEditable().equals("1") ? false : true;
        setEditable(editable);
        if (editable) {
            txt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }*/
        editable = item.getItemIsEditable().equals("1") ? false : true;
        init();
        setEditable(editable);
        if (editable) {
            //text.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            button.setEnabled(true);
        }else {
            text.setEditable(false);
            button.setEnabled(false);
        }
    }
    
/*    private void init() {
        setLayout(new GridBagLayout());
        add(txt,
                getGBC(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST,
                        GridBagConstraints.BOTH, 2));
        txt.setEditable(false);
    }*/
    protected void init() {
        setLayout(new BorderLayout());
        add(this.text, "Center");
        add(button, "East");
        if (editable) {
            button.addMouseListener(new MouseListener() {
                @Override
                public void mouseReleased(MouseEvent paramMouseEvent) {
                }
                @Override
                public void mousePressed(MouseEvent paramMouseEvent) {
                }
                @Override
                public void mouseExited(MouseEvent paramMouseEvent) {
                }
                @Override
                public void mouseEntered(MouseEvent paramMouseEvent) {
                }
                @Override
                public void mouseClicked(MouseEvent paramMouseEvent) {
                    try {
                        showRefObjChooseDialog();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }
 
    @Override
    public String getValue() {
        return this.value;
    }
 
    @Override
    public void setValue(String value) {
        if (value == null || "".equals(value)){
            this.text.setText("");
        }else {
            this.text.setText(value);
        }
        this.value = this.text.getText();
    }
 
 
    protected ICustomAttributeInteceptor getCustomInteceptorInstance() {
        ICustomAttributeInteceptor res = null;
        String className = getPRMItem().getItemValue();
        try {
            if (className == null || "".equals(className))
                return res;
            Object obj = Class.forName(className).getConstructor()
                    .newInstance();
            if (obj == null)
                return res;
            if (obj instanceof ICustomAttributeInteceptor) {
                res = (ICustomAttributeInteceptor) obj;
                res.setCustomControl(this);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }
 
    @Override
    public void getCustomValue(String columnName,
            Map<String, PRMItem> customItemMap, List<Map<String, String>> datas)
            throws VCIError {
    }
 
    private RefObjChooseDailog dialog = null;
    
    private Map<String, String> customExpDefMap = new HashMap<String, String>();
    private String validDefEstimateContent = null; // 如果为空表示定义有效,否则提示无效定义提示
    
    public void showRefObjChooseDialog() throws Exception {
        if (validDefEstimateContent != null && !validDefEstimateContent.equals("")) {
            VCIJOptionPane.showError(dialog, validDefEstimateContent);
            return;
        }
        if (validDefEstimateContent == null) {
            validDefEstimateContent = "";
            //先判断当前自定义控件扩展定义的正确性,否则提示错误
            PRMItem item = getPRMItem();
            // 参照值自定义控件的"扩展定义"格式:type=业务类型名,context=UI上下文名称,fieldName0=refOBJAttr1,fieldName1=refOBJAttr1,fieldName2=refOBJAttr2,fieldName3=refOBJAttr3,...
            String customExpDef = item.getItemStyle();                //"附加属性"定义
            if (customExpDef == null || customExpDef.equals("")) {
                validDefEstimateContent = "请在当前表单字段定义的\"附加属性\"中进行该自定义控件扩展定义;\r\n扩展定义格式:\"type=参照的业务类型名;context=UI上下文名称;当前对象字段0=参照对象属性0;当前对象字段1=参照对象属性1;...\"";
            }else {
                //customExpDef = customExpDef.trim().toLowerCase();
                if(customExpDef.indexOf("type")<0 || customExpDef.indexOf("context")<0 || customExpDef.indexOf("=")<0 || customExpDef.indexOf(";")<0){
                    validDefEstimateContent = "当前表单字段在\"附加属性\"中进行该自定义控件的扩展定义有误;\r\n扩展定义格式:\"type=参照的业务类型名;context=UI上下文名称;当前对象字段0=参照对象属性0;当前对象字段1=参照对象属性1;...\"";
                }else {
                    String[] strings = customExpDef.split(";");
                    for (int i = 0; i < strings.length; i++) {
                        String[] defItem = strings[i].trim().split("=");
                        if (defItem.length == 2) {
                            if (!customExpDefMap.containsKey(defItem[0].trim())) {
                                customExpDefMap.put(defItem[0].trim(), defItem[1].trim());
                            }
                        }
                    }
                    if (!customExpDefMap.containsKey("type") || !customExpDefMap.containsKey("context") || customExpDefMap.size() < 3) {
                        validDefEstimateContent = "当前表单字段在\"附加属性\"中进行该自定义控件的扩展定义有误;\r\n扩展定义格式:\"type=参照的业务类型名;context=UI上下文名称;当前对象字段0=参照对象属性0;当前对象字段1=参照对象属性1;...\"";
                    }
                }
            }
            if (!validDefEstimateContent.equals("")) {
                VCIJOptionPane.showError(dialog, validDefEstimateContent);
                return;
            }
        }
        VCISwingUtil.invokeLater(new Runnable() {
            @Override
            public void run() {
                dialog = new RefObjChooseDailog();
                dialog.init();
                dialog.setSize(UIHelper.DIALOG_DEFAULT_WIDTH,
                        UIHelper.DIALOG_DEFAULT_HEIGHT);
                dialog.setLocationRelativeTo(ClientContextVariable.getFrame());
                dialog.setVisible(true);
            }
        }, 0);
    }
 
    public VCIJTextField getText() {
        return text;
    }
 
    /**
     * 在窗口右下角显示提示信息 *内部类,不可修改*
     * @param message
     */
    @SuppressWarnings("unused")
    private static void showMessage(String message) {
        // 获得弹出窗口显示时间
        int displayTime = 3000;
        String displayTimeStr = ConfigUtils
                .getConfigValue("PromptMessage.DisplayTime");
        if (displayTimeStr == null || displayTimeStr.trim().equals("")) {
            displayTimeStr = "3000";
            try {
                displayTime = Integer.parseInt(displayTimeStr);
            } catch (Exception e) {
            }
        }
        PositionMessager msger = new PositionMessager();
        // 消息显示停留时间,单位为毫秒
        msger.setShowTimes(displayTime);
        msger.show(message);
    }
    
    @SuppressWarnings("serial")
    private class RefObjChooseDailog extends VCIJDialog {
        public RefObjChooseDailog() {
            super(ClientContextVariable.getFrame(), true);
        }
        public void init() {
            setTitle("参照对象选择");
            setIconImage(img);
            setLayout(new BorderLayout());
            add(getCenterDataPanel(), BorderLayout.CENTER);
            add(getSouthButtonPanel(), BorderLayout.SOUTH);
        }
    }
    
    private UILayoutPanel uipl = null;
    
    private VCIJPanel getCenterDataPanel() {
        VCIJPanel pal = new VCIJPanel(new BorderLayout());
        /*PRMItem item = getPRMItem();*/
        /*String type = item.getItemListTable();            //一级参照定义
        String context = item.getItemStyle();                //"附加属性"定义
        String refValueDef = item.getItemListVal();        //"参照值"定义
        //统一修改为"附加属性"定义,格式:
        type = "part";
        context = "selectProducts";*/
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(UIHelper.TYPE, customExpDefMap.get("type"));
        map.put(UIHelper.CONTEXT, customExpDefMap.get("context"));
        map.put(UIHelper.OID, map.get(UIHelper.OID));
        // 加入来源参数
        map.put("fromType", customExpDefMap.get("type"));
        map.put("fromContext", customExpDefMap.get("context"));
        map.put("fromObjectId", map.get(UIHelper.OID));
        this.uipl = getUILayoutPanel(map);
        pal.add(uipl, BorderLayout.CENTER);
        return pal;
    }
    
    private UILayoutPanel getUILayoutPanel(HashMap<String, String> map) {
        UILayoutPanel uipl = new UILayoutPanel(map) {
            private static final long serialVersionUID = 7167045011920499526L;
 
            @Override
            public String getValue(String key) {
                String value = super.getValue(key);
                ObjectAddEditPanel compt = getOwnedObjectAddEditPanel();
                if (compt != null && compt.getRegionPanel() != null
                        && compt.getRegionPanel().getBaseLayoutPanel() != null) {
                    value = compt.getRegionPanel().getBaseLayoutPanel()
                            .getValue(key);
                }
                return value;
            }
        };
        uipl.setDataValueMap(map);
        uipl.prep();
        uipl.initMainPanel();
        return uipl;
    }
    
    private ObjectAddEditPanel getOwnedObjectAddEditPanel() {
        ObjectAddEditPanel res = null;
        Component compt = this.getParentComponent();
        while (compt != null) {
            if (compt instanceof ObjectAddEditPanel) {
                res = (ObjectAddEditPanel) compt;
                break;
            }
            compt = compt.getParent();
        }
        return res;
    }
    
    private VCIJPanel getSouthButtonPanel() {
        VCIJPanel pal = new VCIJPanel();
        VCIJButton btnOk = VCISwingUtil.createVCIJButton("ok", "确定", "确定",
                "accept.png");
        VCIJButton btnCancel = VCISwingUtil.createVCIJButton("cancel", "取消",
                "取消", "cancel.png");
        btnOk.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ok();
            }
        });
        btnCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cancel();
            }
        });
        pal.add(btnOk);
        pal.add(btnCancel);
        return pal;
    }
    @SuppressWarnings("deprecation")
    private void ok() {
        Component compt = uipl.getMainDataPanel(); // 在定义参照上下文时三个UI区域原则上都可以定义,且获取优先级为“右上-->右下-->导航”
                                                    // //modify by ZhongGY
                                                    // 2015-05-26
        if (compt == null) {
            compt = uipl.getTabPanel();
            if (compt == null) {
                compt = uipl.getNavigatorPanel();
            }
        }
        Object[] objs = null;
        if (compt instanceof TablePanel) {
            TablePanel tablePanel = (TablePanel) compt;
            objs = tablePanel.getTablePanelModel().getSelectObjects();
        } else if (compt instanceof TreePanel) {
            TreePanel treePanel = (TreePanel) compt;
            objs = treePanel.getDataModel().getSelectObjects();
            // 判断选择的是否为根节点,如果跟为String类型,表示选择的是分类的根
            // 因为下面做了非空的校验
            if (objs != null && objs.length > 0) {
                if (objs[0] instanceof IDataNode) {
                    if (((IDataNode) objs[0]).getMaterObject() instanceof String) {
                        UIFUtils.showMessage(ClientContextVariable.getFrame(),
                                "不能选择根节点");
                        return;
                    }
                }
            }
        }
        Map<String, String> valueMap = new HashMap<String, String>();
        if (objs == null || objs.length <= 0) {
            VCIJOptionPane.showError(dialog, "请先选择数据!");
            return;
        }
        if (objs.length > 1) {
            VCIJOptionPane.showError(dialog, "一次只能选择一条数据!");
            return;
        }
        if (objs[0] instanceof IDataNode) {
            IDataNode dataNode = (IDataNode) objs[0];
            valueMap = dataNode.getValueMap();
            if (valueMap == null || valueMap.size() == 0) {
                Object obj = dataNode.getMaterObject();
                if (obj instanceof ClientLinkObject) {
                    valueMap = CBOHelper.getLOValueMap(((ClientLinkObject) obj)
                            .getLinkObject());
                } else if (obj instanceof ClientBusinessObject) {
                    valueMap = CBOHelper
                            .getBOValueMap(((ClientBusinessObject) obj)
                                    .getBusinessObject());
                }
            }
        }
        setChooseRecObjToControl(valueMap);
    }
 
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void setChooseRecObjToControl(Map refObjValMap) {
        //String ownOBJOID = "";
        ObjectAddEditPanel compt = getOwnedObjectAddEditPanel();
        /*if (compt != null && compt.getRegionPanel() != null
                && compt.getRegionPanel().getBaseLayoutPanel() != null) {
            Object[] ownSelectObjects = compt.getRegionPanel().getDataModel()
                    .getSelectObjects();
            if (ownSelectObjects != null) {
                Object ownSelectObject = ownSelectObjects[0];
                if (ownSelectObject instanceof DefaultTableNode) {
                    ownOBJOID = ((DefaultTableNode) ownSelectObject)
                            .getClientBusinessObject().getAttributeValue("oid");
                }
            }
        }*/
        /*if (refObjValMap.get("oid") != null && !ownOBJOID.equals("")
                && ownOBJOID.equals(refObjValMap.get("oid"))) {
            // 不能参照自身
            VCIJOptionPane.showError(dialog, "不能参照自身!"); // modify by ZhongGY
                                                            // 2015-05-26
            return;
        }*/
        PRMItem item = getPRMItem();
        Map<String, String> map = refObjValMap;
        //customExpDefMap
        String curfieldValue = item.getItemField().toLowerCase();
        if (customExpDefMap.containsKey(curfieldValue)) {
            String newVal = map.get(customExpDefMap.get(curfieldValue));
            setValue(newVal);
        }
        HashMap<String, Component> ctrlComptMap= compt.getCtrlComptMap();
        Iterator<String> it = ctrlComptMap.keySet().iterator();
        while(it.hasNext()){
            //如果设置原色在界面上显示并且设置为必填项
            String itemFieldName = it.next();//comp.getPRMItem().getItemField();
            Object obj = ctrlComptMap.get(itemFieldName);
            if (obj != null  && customExpDefMap.containsKey(itemFieldName.toLowerCase())) {
                String attrName = customExpDefMap.get(itemFieldName.toLowerCase());
                String newVal = "";
                if (attrName != null && attrName.indexOf("@") > 0) {
                    //对参照属性特殊处理
                    String[] strs = attrName.split("@");
                    if(map.containsKey(strs[0]) && map.containsKey(strs[1])){
                        newVal = map.get(strs[0]) +"," + map.get(strs[1]);
                    }
                }else {
                    newVal = map.get(attrName);
                }
                if (obj instanceof AbstractCustomControl) {
                    AbstractCustomControl comp = (AbstractCustomControl) obj;
                    comp.setValue(newVal);
                }else if(obj instanceof JTextComponent){
                    ((JTextComponent)obj).setText(newVal);
                }else if(obj instanceof VCIJComboBox){
                    //((VCIJComboBox)obj).set (newVal);
                }else {
                    //...
                }
                //comp.setValue(newVal);
            }
            /*if(obj != null && obj instanceof AbstractCustomControl){    
                AbstractCustomControl comp = (AbstractCustomControl) obj;
                String itemFieldName = comp.getPRMItem().getItemField();
                if (!curfieldValue.equals(itemFieldName.toLowerCase()) && customExpDefMap.containsKey(itemFieldName.toLowerCase())) {
                    comp.setValue(newVal);
                }
            }*/
        }
        close(DialogResult.OK);
        //firePropertyChange(REF_OBJECT_CHANGED, null, refObjValMap);
    }
 
    public void firePropertyChangeManual(String propertyName, String oldValue,
            Map<String, String> newValue) {
        super.firePropertyChange(propertyName, oldValue, newValue);
    }
 
    private void cancel() {
        close(DialogResult.CANCEL);
    }
 
    private void close(DialogResult dialogRes) {
        dialog.setDialogResult(dialogRes);
        dialog.setVisible(false);
    }
    
    private boolean editable = true;
 
    @Override
    public void setEditable(boolean editable) {
        this.editable = editable;
    }
 
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        setEditable(enabled);
        text.setEnabled(enabled);
    }
}