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
package com.vci.client.uif.engine.client.controls;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
 
import com.vci.client.bof.BOFactoryClient;
import com.vci.client.bof.ClientBusinessObject;
import com.vci.client.bof.ClientLinkObject;
import com.vci.client.common.providers.ServiceProvider;
import com.vci.client.omd.provider.ApProvider;
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.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.objopt.ObjectAddEditDialog;
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.corba.omd.atm.AttribItem;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.mw.ClientContextVariable;
import com.vci.omd.objects.OtherInfo;
 
public class RefObjChooseControl extends AbstractCustomControl {
 
    private static final long serialVersionUID = 4501645055912094463L;
 
    public static final String REF_OBJECT_CHANGED = "REF_OBJECT_CHANGED";
 
    private final Image img = VCISwingUtil.createImageIcon("search.png").getImage();
    private HashMap<String, String> refParamsMap = new HashMap<String, String>();
 
    @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);
            }
            RefObjChooseControl.this.drawRequiredFlag(g);
        }
 
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            if (!isEnabled() || !editable)
                return;
            if (isClickImage(e)) {
                showRefObjChooseDialog();
            }
        }
 
        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;
        }
    };
 
    public RefObjChooseControl(PRMItem item) {
        super(item);
        editable = item.getItemIsEditable().equals("1") ? false : true;
        init();
        setEditable(editable);
        if (editable) {
            txt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
    }
 
    private void init() {
        setLayout(new GridBagLayout());
        add(txt, getGBC(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, 0));
        
        txt.setEditable(false);
        if (editable) {
            txt.setBackground(Color.WHITE);
        }
    }
 
    private RefObjChooseDailog dialog = null;
 
    private void showRefObjChooseDialog() {
        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);
    }
 
    private UILayoutPanel uipl = null;
    private String key = "";
    private String value = "";
 
    @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);
            this.repaint();
            this.validate();
            this.update(getGraphics());
        }
    }
 
    private UILayoutPanel getCenterDataPanel() {
        //VCIJPanel pal = new VCIJPanel(new BorderLayout());
        PRMItem item = getPRMItem();
        String type = item.getItemListTable();
        String context = item.getItemStyle();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(UIHelper.TYPE, type);
        // 分析原Style参数,加入到将context参数加入到map中,其它参数存入refParamsMap中
        refParamsMap.clear();
        String[] paramItem = context.split(";");
 
        if (paramItem.length == 1 && !context.contains("=")) {// 兼容原context模式
            map.put(UIHelper.CONTEXT, paramItem[0]);
        } else {
            for (int i = 0; i < paramItem.length; i++) {
                String[] params = paramItem[i].split("=", 2);
                if (params[0].trim().toLowerCase().equalsIgnoreCase(RefAttachAttrContants.CONTANT_1)) {
                    map.put(UIHelper.CONTEXT, params[1].trim());
                } else {
                    if (params.length == 2) {
                        refParamsMap.put(params[0].toLowerCase().trim(), params[1].toLowerCase().trim());
                    }
                }
            }
        }
        map.put(UIHelper.OID, map.get(UIHelper.OID));
        // 加入来源参数
        map.put("fromType", type);
        map.put("fromContext", context);
        map.put("fromObjectId", map.get(UIHelper.OID));
        map.put("tableRowSelectType", "singleSelect");
        
        this.uipl = getUILayoutPanel(map);
        //pal.add(uipl, BorderLayout.CENTER);  //直接返回UILayoutPanel对象(之前在"uipl"之上嵌套VCIJPanel)  modify by zhonggy 2015-09-18
        uipl.repaint();
        uipl.validate();
        uipl.updateUI();
        return uipl;
    }
  
    
    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;
            }
        };
        // add by xchao 2015.08.13 begin
        // 设置参照对象选择窗口的 inputDataNode 等于 参照数据选择控件所在的Dialog的inputDataNode
        ObjectAddEditDialog oaed = getOwnedObjectAddEditDialog();
        if(oaed != null){
            //SDIsNull=true; TODO:约定根据参照属性控件设置的"SDIsNull"参数确定是否往后传递"SourceData" ,modify zhonggy 2015-09-17
            boolean SDIsNull = true;
            if(oaed.getSourceDataNode() != null 
                    && oaed.getSourceDataNode().getClientBusinessObject() != null
                    && oaed.getSourceDataNode().getClientBusinessObject().getBusinessObject() != null
                    && oaed.getSourceDataNode().getClientBusinessObject().getBusinessObject().btName != null
                    && oaed.getSourceDataNode().getClientBusinessObject().getBusinessObject().btName.equals("folder")){
                SDIsNull = false;  //临时这样处理下,统一解释"SDIsNull"参数时去掉这个临时逻辑
            }
            if (SDIsNull) {
                uipl.setSourceDataNode(oaed.getSourceDataNode());
            }
            uipl.setInputData(oaed.getInputDataNode());
        }
        // add by xchao 2015.08.13 end
        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 ObjectAddEditDialog getOwnedObjectAddEditDialog() {
        ObjectAddEditDialog res = null;
        Component compt = this.getParentComponent();
        while (compt != null) {
            if (compt instanceof ObjectAddEditDialog) {
                res = (ObjectAddEditDialog) 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
        /**
         * 增加方法,确定提取数据的区域
         * @author Zylon at 2015-7-6  下午5:29:38
         */
        {// dataregion 参数(导航区=1,控制区=2,操作区=3)
        // datafield参数 如果不设置,默认为oid
        String dataRegion = refParamsMap.get(RefAttachAttrContants.CONTANT_2);
        int reginoIndex = 1;
        // 共三个区域,为了加大兼容性只检测是否包含1、2、3
        if (dataRegion != null && !dataRegion.equals("")) {
            if (dataRegion.contains("3")) {
                reginoIndex = 3;
            }
            if (dataRegion.contains("2")) {
                reginoIndex = 2;
            }
            if (dataRegion.contains("1")) {
                reginoIndex = 1;
            }
 
            switch (reginoIndex) {
            case 1:
                compt = uipl.getNavigatorPanel();
                break;
            case 2:
                compt = uipl.getMainDataPanel();
                break;
            case 3:
                compt = uipl.getTabPanel();
                break;
            }
        } else {// 没有设置走原逻辑
            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;
        /**
         * @author Zylon at 2015-7-6  下午5:34:58
         */
        {
        // datafield 参数 如果不设置,默认为oid
        String datafield = refParamsMap.get(RefAttachAttrContants.CONTANT_3);
        if(datafield == null ||datafield.equalsIgnoreCase("") ){
            //默认值为oid
            datafield = "oid";
        }else{
            //有设置returnvalue 则取出
            datafield = datafield.toLowerCase().trim();
        }
        //保存的id
        String saveId = getNOtNullString(map.get(datafield));
        //页面回显内容       
        String showString = "";
        if(datafield.equals("t_oid") || datafield.equals("f_oid") || datafield.contains(".")){
            // datafield 等于 t_oid\f_oid\t_oid.xxx\xxx.xxx\xx.xx.xx形式时,
            // 直接从选择数据对应的map中取数据值(key 等于  item.getItemListVal()),
            showString = map.get(item.getItemListVal());
            if(showString == null){
                // 如果没有取到数据,则直接显示配置的 ‘item.getItemListVal()’ 值
                showString = item.getItemListVal(); 
            }
        } else {
            showString = getShowString(datafield,saveId,item.getItemListVal());
        }
 
        String newVal = saveId + ","+showString;
 
        setValue(newVal);
        }
        close(DialogResult.OK);
        firePropertyChange(REF_OBJECT_CHANGED, null, refObjValMap);
    }
 
    private String getShowString(String refAtrrName, String Oid, String refObjAttrName) {
            //refAtrrName-->业务类型的名称refTypeName
        try {
            AttribItem attribItem = ApProvider.getAbItemByName(refAtrrName);
            OtherInfo otherInfo = OtherInfo.getOtherInfoByText(attribItem.other);
            String refTypeName = otherInfo.getRefTypeName();
            if (refTypeName == null || refTypeName.trim().equals("")) {
                PRMItem item = getPRMItem();
                String itemField = item.getItemField();
                if (itemField.indexOf(".")>0) {
                    String[] strs = itemField.split("\\.");
                    itemField = strs[strs.length -1];
                }
                attribItem = ApProvider.getAbItemByName(itemField);
                otherInfo = OtherInfo.getOtherInfoByText(attribItem.other);
                refTypeName = otherInfo.getRefTypeName();
            }
            //根据refTypeName+Oid获取BO对象;
            BusinessObject refBO = null;
            if(refTypeName != null && !"".equals(refTypeName)){
                refBO = ServiceProvider.getBOFService().readBusinessObject(Oid, refTypeName);
                //根据BO对象+attrName获取返回值
                ClientBusinessObject helper = new ClientBusinessObject();
                helper.setBusinessObject(refBO);
                return helper.getAttributeValue(refObjAttrName);
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return "";
    }
 
 
    private String getNOtNullString(String string) {
        if(string == null){
            return "";
        }
        return string;
    }
 
    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);
    }
 
    public String getKey() {
        return key;
    }
 
    public void setKey(String key) {
        this.key = key;
    }
 
    @Override
    public String getValue() {
        return value;
    }
 
    @Override
    public void setValue(String value) {
        if (value == null || "".equals(value))
            return;
        String[] values = value.split(",");
        if (values == null || values.length < 1) {
            return;
        }
        this.value = values[0];
        if (values.length >= 2) {
            this.txt.setText(values[1]);
        }
    }
 
    private boolean editable = true;
 
    @Override
    public void setEditable(boolean editable) {
        this.editable = editable;
    }
 
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        setEditable(enabled);
        txt.setEnabled(enabled);
    }
}