田源
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
package com.vci.client.portal.UI.v3;
 
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.swing.ButtonGroup;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
 
import com.vci.client.framework.rightConfig.modelConfig.ModuleTreeCellRenderer;
import com.vci.client.omd.btm.ui.BtmClient;
import com.vci.client.omd.linktype.LinkTypeStart;
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.VCIJLabel;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.ui.swing.components.VCIJRadioButton;
import com.vci.client.ui.swing.components.VCIJTextField;
import com.vci.client.ui.tree.VCIBaseTree;
import com.vci.client.ui.tree.VCIBaseTreeModel;
import com.vci.client.ui.tree.VCIBaseTreeNode;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.btm.BtmItem;
import com.vci.corba.omd.ltm.LinkType;
import com.vci.mw.ClientContextVariable;
 
/**
 * 业务类型&链接类型树Panel
 * 
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: VCI</p>
 * @author xiongchao
 * @time 2017-2-15
 * @version 1.0
 */
public class BtmLinkTypeTreePanel extends VCIJPanel {
 
    /**
     * 
     */
    private static final long serialVersionUID = 3357669516630770059L;
    private boolean showBtmLinkTypeRadioButton = false;
    private boolean onlyLinkType = false;
    public BtmLinkTypeTreePanel(){
        
    }
    
    public void buildUI(){
        init();
    }
    private void init(){
        initUI();
    }
    private void initUI(){
        setLayout(new BorderLayout());
        add(getNorthPanel(), BorderLayout.NORTH);
        add(new JScrollPane(getCenterTreePanel()), BorderLayout.CENTER);
    }
    private VCIJLabel lblSearch = new VCIJLabel("查找:");
    private VCIJTextField txtSearch = new VCIJTextField("", false);
    private VCIJButton btnSearch = VCISwingUtil.createVCIJButton("search", "", "", "search.gif", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            search_exec();
        }
    });
    private VCIJButton btnRefresh = VCISwingUtil.createVCIJButton("refresh", "", "", "pagination_refresh.png", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            refresh_exec();
        }
    });
    private VCIJRadioButton rbtnBtmType = new VCIJRadioButton("业务类型", true);
    private VCIJRadioButton rbtnLinkType = new VCIJRadioButton("链接类型", false);
    private VCIJPanel getNorthPanel(){
        VCIJPanel pal = new VCIJPanel();
        pal.setLayout(new GridBagLayout());
        pal.add(lblSearch, getGBC(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 1));
        pal.add(txtSearch, getGBC(1, 0, 2, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, 1));
        pal.add(btnSearch, getGBC(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 1));
        pal.add(btnRefresh, getGBC(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 1));
        rbtnBtmType.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rbtn_change(rbtnBtmType.getText());
            }
        });
        rbtnLinkType.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rbtn_change(rbtnLinkType.getText());                
            }
        });
        if(isShowBtmLinkTypeRadioButton() && !isOnlyLinkType()){
            ButtonGroup rbtnGroup = new ButtonGroup();
            rbtnGroup.add(rbtnBtmType);
            rbtnGroup.add(rbtnLinkType);
            pal.add(rbtnBtmType, getGBC(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 1));
            pal.add(rbtnLinkType, getGBC(2, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, 1));
        }
        return pal;
    }
    
    private VCIBaseTreeNode rootNode = null;
    private VCIBaseTreeModel treeModel = null;
    private VCIBaseTree tree = null;
    private VCIBaseTree getCenterTreePanel(){
        VCIBaseTree res = null;
        rootNode = new VCIBaseTreeNode("", "root");
        treeModel = new VCIBaseTreeModel(rootNode);
        updateRootNodeInfo();
        reloadTreeData();
        tree = new VCIBaseTree(treeModel, new ModuleTreeCellRenderer(){
            private static final long serialVersionUID = 6253740682089786968L;
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus){
                Component compt = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                VCIBaseTreeNode node = null;
                if(value instanceof VCIBaseTreeNode){
                    node = (VCIBaseTreeNode)value;
                }
                if(node != null){
                    if(node.isRoot()){
                        setIcon(VCISwingUtil.createImageIcon("house.png"));
                    }
                    if(node.getObj() instanceof BtmItem){
                        setIcon(VCISwingUtil.createImageIcon("folder.png"));
                    } else if(node.getObj() instanceof LinkType){
                        setIcon(VCISwingUtil.createImageIcon("folder_link.png"));
                    }
                }
                return compt;
            }
        });
        res = tree;
        return res; 
    }
    private String getRootNodeShowLabel(){
        String res = rbtnBtmType.getText();
        if(isShowBtmLinkTypeRadioButton()){
            if(rbtnLinkType.isSelected()){
                res = rbtnLinkType.getText();
            }
        }
        if(isOnlyLinkType()){
            res = rbtnLinkType.getText();
            rbtnLinkType.setSelected(isOnlyLinkType());
            rbtnBtmType.setSelected(!isOnlyLinkType());
        }
        return res;
    }
    private void search_exec(){
        searchedTreeNodeList.clear();
        String text = txtSearch.getText().trim();
        if("".equals(text)) return;
        searchInTree(rootNode, searchedTreeNodeList, text);
        
        if(searchedTreeNodeList.size() < 0){
            return;
        }
        if(searchedTreeNodeList.size() == 1){
            VCIBaseTreeNode treeNode = searchedTreeNodeList.get(0);
            selectedTreeNodeToTree(treeNode);
            return;
        }
        showSearchResultDialg(searchedTreeNodeList);
    }
    
    private void selectedTreeNodeToTree(VCIBaseTreeNode treeNode){
        tree.clearSelection();
        TreePath navToTreePath = new TreePath(treeNode.getPath()); 
        tree.setSelectionPath(navToTreePath);
        tree.scrollPathToVisible(navToTreePath);
        tree.updateUI();
        tree.validate();
    }
    
    private int index = -1;
    private List<VCIBaseTreeNode> searchedTreeNodeList = new ArrayList<VCIBaseTreeNode>();
    private VCIJDialog dialog = new VCIJDialog(ClientContextVariable.getFrame(), true);
    private VCIJButton btnPrev = VCISwingUtil.createVCIJButton("prev", "上一个", "上一个", "pagination_previous.png", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            index = index - 1;
            prevOrNext(index);
        }
    });
    private VCIJButton btnNext = VCISwingUtil.createVCIJButton("next", "下一个", "下一个", "pagination_next.png", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            index = index + 1;
            prevOrNext(index);
        }
    });
    private VCIJButton btnClose = VCISwingUtil.createVCIJButton("close", "关闭", "关闭", "close.png", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    private VCIJLabel lblSearchInfo = new VCIJLabel("", null, VCIJLabel.LEADING);
    private void showSearchResultDialg(List<VCIBaseTreeNode> list){
        index = -1;
        btnPrev.setEnabled(false);
        btnNext.setEnabled(true);
        
        VCIJPanel pal = new VCIJPanel(new FlowLayout(FlowLayout.LEADING));
        lblSearchInfo.setText("" +
                "<html>" +
                "<p>共查找到 " + String.valueOf(searchedTreeNodeList.size()) + " 个结果</p>" +
                "<p>&nbsp;&nbsp;</p>" +
                "<p>&nbsp;&nbsp;</p>" +
                "</html>" +
                "");
        pal.add(lblSearchInfo);
        
        dialog.setTitle("查找结果");
        dialog.setSize(300, 125);
        dialog.setLocationRelativeTo(ClientContextVariable.getFrame());
        VCIJPanel palBtn = new VCIJPanel(new FlowLayout());
        
        palBtn.add(btnPrev);
        palBtn.add(btnNext);
        palBtn.add(btnClose);
        VCIJPanel palContent = new VCIJPanel(new BorderLayout());
        palContent.add(pal, BorderLayout.CENTER);
        palContent.add(palBtn, BorderLayout.SOUTH);
        dialog.setContentPane(palContent);
        dialog.setVisible(true);
    }
    
    private void prevOrNext(int index){
        VCIBaseTreeNode treeNode = searchedTreeNodeList.get(index);
        selectedTreeNodeToTree(treeNode);
        
        btnPrev.setEnabled(index > 0);
        btnNext.setEnabled(index < searchedTreeNodeList.size() - 1);
 
        lblSearchInfo.setText("" +
                "<html>" +
                "<p>共查找到 " + String.valueOf(searchedTreeNodeList.size()) + " 个结果,当前显示</p>" +
                "<p>第 " + String.valueOf(index + 1) + " 个 : " + treeNode.getUserObject() + "</p>" +
                "<p>&nbsp;&nbsp;</p>" +
                "</html>" +
                "");
        lblSearchInfo.updateUI();
        updateUI();
    }
    
    
    private void searchInTree(VCIBaseTreeNode parentTreeNode, List<VCIBaseTreeNode> list, String text){
        int childCount = parentTreeNode.getChildCount();
        for (int i = 0; i < childCount; i++) {
            VCIBaseTreeNode treeNode = (VCIBaseTreeNode) parentTreeNode.getChildAt(i);
            Object obj = treeNode.getObj();
            if(isOnlyLinkType() || 
                    (isShowBtmLinkTypeRadioButton() && rbtnLinkType.isSelected())){
                if(obj instanceof LinkType){
                    LinkType lt = (LinkType)obj;
                    if(lt.name.contains(text) || lt.tag.contains(text)){
                        list.add(treeNode);
                    }
                }
            } else if(rbtnBtmType.isSelected()){
                if(obj instanceof BtmItem){
                    BtmItem bi = (BtmItem)obj;
                    if(bi.name.contains(text) || bi.label.contains(text)){
                        list.add(treeNode);
                    }
                }
            }
            searchInTree(treeNode, list, text);
        }
    }
    
    private void refresh_exec(){
        btmItemMap.clear();
        linkTypeMap.clear();
        reloadTreeData();
    }
    private String lastRbtnText = rbtnBtmType.getText();
    private void rbtn_change(String text){
        if(text.equals(lastRbtnText)){
            return;
        }
        lastRbtnText = text;
        reloadTreeData();
        updateRootNodeInfo();
        updateUI();
    }
    private void updateRootNodeInfo(){
        rootNode.setUserObject(getRootNodeShowLabel());
    }
    private void reloadTreeData(){
        loadTreeData(rootNode, treeModel);
    }
    private Map<String, BtmItem[]> btmItemMap = new HashMap<String, BtmItem[]>();
    private Map<String, LinkType[]> linkTypeMap = new HashMap<String, LinkType[]>();
    private void loadTreeData(VCIBaseTreeNode parentTreeNode, VCIBaseTreeModel treeModel) {
        try{
            parentTreeNode.removeAllChildren();
            if(rbtnBtmType.isSelected()){
                BtmItem[] bis = new BtmItem[0];
                String name = "";
                if(!btmItemMap.containsKey(name)){
                    bis = BtmClient.getService().getAllBtmItem(name);
                    btmItemMap.put(name, bis);
                } else {
                    bis = btmItemMap.get(name);
                }
                addBtmItemToTreeNode(bis, parentTreeNode);
            } else {
                LinkType[] lts = new LinkType[0];
                String name = "all";
                if(!linkTypeMap.containsKey("")){
                    lts = LinkTypeStart.getService().getLinkTypes();
                    linkTypeMap.put(name, lts);
                }else {
                    lts = linkTypeMap.put(name, lts);
                }
                addLinkTypeToTreeNode(lts, parentTreeNode);
            }
            treeModel.reload();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private void addBtmItemToTreeNode(BtmItem[] btmItems, VCIBaseTreeNode parentTreeNode) throws VCIError{
        Arrays.sort(btmItems, btmItemComparator);
        for (BtmItem btmItem : btmItems) {
            String nodeName = btmItem.name + " " + btmItem.label;
            VCIBaseTreeNode treeNode = new VCIBaseTreeNode(nodeName, btmItem);
            treeNode.setExpand(true);
            treeNode.setLeaf(true);
            treeModel.insertNodeInto(treeNode, parentTreeNode, treeModel.getChildCount(parentTreeNode));
            BtmItem[] childBtmItems = new BtmItem[0];
            String name = btmItem.name;
            if(!btmItemMap.containsKey(name)){
                childBtmItems = BtmClient.getService().getChildrenBtms(name);
                btmItemMap.put(name, childBtmItems);
            } else {
                childBtmItems = btmItemMap.get(name);
            }
            addBtmItemToTreeNode(childBtmItems, treeNode);
        }
    }
    private void addLinkTypeToTreeNode(LinkType[] linkTypes, VCIBaseTreeNode parentTreeNode){
        Arrays.sort(linkTypes, linkTypeComparator);
        for (LinkType linkType : linkTypes) {
            String nodeName = linkType.name + " " + linkType.tag;
            VCIBaseTreeNode treeNode = new VCIBaseTreeNode(nodeName, linkType);
            treeNode.setLeaf(true);
            treeNode.setExpand(true);
            treeModel.insertNodeInto(treeNode, parentTreeNode, treeModel.getChildCount(parentTreeNode));
        }
    }
    private Comparator<BtmItem> btmItemComparator = new Comparator<BtmItem>(){
        @Override
        public int compare(BtmItem o1, BtmItem o2) {
            return o1.name.compareTo(o2.name);
        }
    };
    private Comparator<LinkType> linkTypeComparator = new Comparator<LinkType>(){
        @Override
        public int compare(LinkType o1, LinkType o2) {
            return o1.name.compareTo(o2.name);
        }
    };
    private GridBagConstraints getGBC(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, int padxy) {
        return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(padxy, padxy, padxy, padxy), padxy, padxy);
    }
 
    public boolean isShowBtmLinkTypeRadioButton() {
        return showBtmLinkTypeRadioButton;
    }
 
    public boolean isOnlyLinkType() {
        return onlyLinkType;
    }
    /**
     * 设置是否显示 业务类型 链接类型 单选按钮
     * <p>显示后 可以切换显示 业务类型 或 链接类型</p>
     * <p>默认值为false,此时按只显示 业务类型进行处理 </p>
     * @param showBtmLinkTypeRadioButton
     */
    public void setShowBtmLinkTypeRadioButton(boolean showBtmLinkTypeRadioButton) {
        this.showBtmLinkTypeRadioButton = showBtmLinkTypeRadioButton;
    }
    /**
     * 设置是否只显示链接类型(只显示链接类型时,没有 业务类型 链接类型 切换的单选按钮)
     * @param onlyLinkType
     */
    public void setOnlyLinkType(boolean onlyLinkType) {
        this.onlyLinkType = onlyLinkType;
    }
    /**
     * 添加树节点选择后的事件处理
     * @param tsl 树节点选择后的事件处理程序  TreeSelectionListener
     */
    public void addTreeSelectionListener(TreeSelectionListener tsl) {
        tree.addTreeSelectionListener(tsl);
    }
    /**
     * 返回当前选择的业务类型列表
     * @return 当前选择的业务类型列表 List<BtmItem>
     */
    public List<BtmItem> getSelectedBtmItems(){
        List<BtmItem> list = new ArrayList<BtmItem>();
        if(isShowBtmLinkTypeRadioButton() && rbtnBtmType.isSelected()){
            TreePath[] treePaths = tree.getSelectionPaths();
            for (TreePath treePath : treePaths) {
                Object pathObj = treePath.getLastPathComponent();
                if(pathObj != null && pathObj instanceof VCIBaseTreeNode){
                    VCIBaseTreeNode treeNode = (VCIBaseTreeNode)pathObj;
                    Object obj = treeNode.getObj();
                    if(obj instanceof BtmItem){
                        list.add(((BtmItem)obj));
                    }
                }
            }
        }
        return list;
    }
    /**
     * 返回当前选择的链接类型列表
     * @return 当前选择的链接类型列表  List<LinkType>
     */
    public List<LinkType> getSelectedLinkTypes(){
        List<LinkType> list = new ArrayList<LinkType>();
        if(!isOnlyLinkType() && rbtnLinkType.isSelected()){
            TreePath[] treePaths = tree.getSelectionPaths();
            for (TreePath treePath : treePaths) {
                Object pathObj = treePath.getLastPathComponent();
                if(pathObj != null && pathObj instanceof VCIBaseTreeNode){
                    VCIBaseTreeNode treeNode = (VCIBaseTreeNode)pathObj;
                    Object obj = treeNode.getObj();
                    if(obj instanceof LinkType){
                        list.add(((LinkType)obj));
                    }
                }
            }
        }
        return list;
    }
}