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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
package com.vci.client.portal.NewNewUI.buttonmng;
 
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
 
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
 
import org.apache.commons.lang3.StringUtils;
 
import com.vci.corba.portal.*;
import com.vci.corba.portal.data.PLCommandParameter;
import com.vci.corba.portal.data.PLPageDefination;
import com.vci.corba.portal.data.PLUILayout;
import com.vci.corba.portal.data.PLTabButton;
import com.vci.client.LogonApplication;
import com.vci.client.portal.utility.UITools;
import com.vci.client.ui.process.QANProcessBar;
import com.vci.client.ui.process.QANProcessBarFrame;
import com.vci.client.ui.swing.VCISwingUtil;
import com.vci.client.ui.swing.components.VCIJButton;
import com.vci.client.ui.swing.components.VCIJOptionPane;
import com.vci.client.ui.swing.components.VCIJDialog.DialogResult;
import com.vci.common.portal.utils.PortalUtil;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
 
/**
 * 页签按钮配合窗口
 * 
 * FIXME 程序问题
 * 1、保存按钮中数据库操作尽量复用了原有的代码,参数的创建和保存处理不合理
 * 2、对于参数,没有做名称重复等校验
 * 
 * @author VCI-STGK006
 *
 */
public class ButtonSettingDialog extends JDialog {
 
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -7532763453642444087L;
    /**
     * 父窗口
     */
    private JDialog owner;
    
    /**
     * 按钮面板
     */
    private JPanel buttonPanel;
    /**
     * 系统主面板
     */
    private JSplitPane mainSplitPane;
    /**
     * 按钮树面板
     */
    private JPanel buttonTreePanel;
    /**
     * 按钮树
     */
    private JTree buttonTree;
    /**
     * 添加按钮
     */
    private JButton createButtonBtn;
    /**
     * 修改按钮信息
     */
    private JButton editButtonBtn;
    /**
     * 删除按钮
     */
    private JButton deleteButtonBtn;
    /**
     * 保存按钮
     */
    private JButton saveBtn;
    /**
     * 取消按钮
     */
    private JButton cancelBtn;
    /**
     * 加入
     */
    private JButton joinBtn;
    /**
     * 撤销
     */
    private JButton exitBtn;
    
    private boolean showCopyButton = true;
    private boolean showPasteButton = true;
    private boolean showPasteToOther = true;
    private boolean showJoinBtn = true;
    private boolean showExitBtn = true;
            
    private VCIJButton btnCopy = VCISwingUtil.createVCIJButton("copy", "复制", "复制", "copy.gif", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            copy();
        }
    });
    private VCIJButton btnPaste = VCISwingUtil.createVCIJButton("past", "粘贴", "粘贴", "paste.gif", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            paste();
        }
    });
    private VCIJButton btnCopyToOther = VCISwingUtil.createVCIJButton("copyToOther", "复制到其它组件", "复制到其它组件", "copy.gif", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            copyToOther();
        }
    });
//    private VCIJButton viewI18nButton = VCISwingUtil.createVCIJButton("viewI18nButton", "查看国际化字典", "查看国际化字典", "image.png", new ActionListener() {
//        @Override
//        public void actionPerformed(ActionEvent e) {
//            i18nTable();
//        }
//    });
    
    private List<JButton> otherCustomButtons = new ArrayList<JButton>();
    
    /**
     *  按钮配置信息面板
     */
    private JPanel settingsPanel;
    /**
     * 当前选中Button
     */
    protected PLTabButton selectedTabButton;
    /**
     * 当前选中Button的兄节点
     */
    protected PLTabButton selectedBrotherTabButton;
    /**
     * 按钮配置信息
     */
    private PLPageDefination plPageDefination;
    /**
     * 上下文信息
     */
    @SuppressWarnings("unused")
    private PLUILayout plpagelayoutdefination;
    /**
     * 按钮区域
     * XXX 怎么用?
     */
    protected String buttonArea = "";
    
    
    protected OptMode flag = OptMode.View;
    private Frame ownerFrame = null;
    public ButtonSettingDialog(){}
    public ButtonSettingDialog(Frame ownerFrame, PLPageDefination plPageDefination,
             PLUILayout plpagelayoutdefination, String buttonArea) {
        super(ownerFrame, "配置按钮", true);
        this.ownerFrame = ownerFrame;
        ButtonSettingDialog_(plPageDefination, plpagelayoutdefination, buttonArea);
    }
    /**
     * 构造器
     * @param plPageDefination 不能为空
     * @param plpagelayoutdefination
     * @param buttonArea
     */
    public ButtonSettingDialog(JDialog owner, PLPageDefination plPageDefination,
             PLUILayout plpagelayoutdefination, String buttonArea) {
        super(owner, "配置按钮", true);
        this.owner = owner;
        ButtonSettingDialog_(plPageDefination, plpagelayoutdefination, buttonArea);
    }
    
    protected void ButtonSettingDialog_(PLPageDefination plPageDefination,
             PLUILayout plpagelayoutdefination, String buttonArea){
        this.plPageDefination = plPageDefination;
        this.plpagelayoutdefination = plpagelayoutdefination;
        this.buttonArea = buttonArea;
        //绘制界面
        init(null);
        //刷新树,设置树状态
        refreshTreeNode(this.buttonTree.getPathForRow(0));
        this.setLocationRelativeTo(owner);
        this.setResizable(true);
        this.setVisible(true);
    }
    
    /**
     * 绘制
     */
    protected void init(JPanel pal) {
        //add by caill
        this.setSize(900, 600);
        
        //工具条
        getButtonPanel();
        
        mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        mainSplitPane.setDividerSize(7);
        mainSplitPane.setContinuousLayout(true);
        mainSplitPane.setOneTouchExpandable(true);
        mainSplitPane.setDividerLocation(250);
        
        //Button树
        getButtonTreePanel();
        
        //Button配置详细信息
        getSettingsPanel();
        
        //添加
        mainSplitPane.add(buttonTreePanel, JSplitPane.LEFT);
        mainSplitPane.add(settingsPanel, JSplitPane.RIGHT);
        
        JPanel panel = new JPanel();
        panel.setBorder(new EmptyBorder(7, 7, 7, 7));
        panel.setLayout(new BorderLayout(0, 0));
        panel.add(buttonPanel, BorderLayout.NORTH);
        panel.add(mainSplitPane, BorderLayout.CENTER);
 
        this.getContentPane().add(panel);
        //this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
        //this.getContentPane().add(mainSplitPane, BorderLayout.CENTER);
        
        //刷新树,设置树状态
        refreshTreeNode(this.buttonTree.getPathForRow(0));
        
    }
    
    /**
     * 得到工具条
     */
    private void getButtonPanel() {
        //按钮区域
        FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
        flowLayout.setHgap(10);
        JPanel palBtns = new JPanel();
        palBtns = new JPanel(flowLayout);
        
        createButtonBtn = new JButton("添加");
        createButtonBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addButtonBtnListener();
            }
        });
        editButtonBtn = new JButton("修改");
        editButtonBtn.setEnabled(false);
        editButtonBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editButtonBtnListener();
            }
        });
        deleteButtonBtn = new JButton("删除");
        deleteButtonBtn.setEnabled(false);
        deleteButtonBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteButtonBtnListener();
            }
        });
        saveBtn = new JButton("保存");
        saveBtn.setEnabled(false);
        saveBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveBtnListener();
            }
        });
        cancelBtn = new JButton("取消");
        cancelBtn.setEnabled(false);
        cancelBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cancelBtnListener();
            }
        });
        joinBtn = new JButton("调整为下级按钮");
        joinBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                joinBtnListener();
            }
        });
        exitBtn = new JButton("调整为上级按钮");
        exitBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exitBtnListener();
            }
        });
        
        palBtns.add(createButtonBtn);
        palBtns.add(editButtonBtn);
        palBtns.add(deleteButtonBtn);
        palBtns.add(saveBtn);
        palBtns.add(cancelBtn);
        
        if(isShowJoinBtn()){
            palBtns.add(joinBtn);
        }
        if(isShowExitBtn()){
            palBtns.add(exitBtn);
        }
        if(isShowCopyButton()){
            palBtns.add(btnCopy);
        }
        if(isShowPasteButton()){
            palBtns.add(btnPaste);
        }
        if(isShowPasteToOther()){
            palBtns.add(btnCopyToOther);
        }
        //palBtns.add(viewI18nButton);
        List<JButton> othBtns = getOtherCustomButtons();
        if( othBtns != null){
            for (JButton btn : othBtns){
                palBtns.add(btn);
            }
        }
        
        buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.add(new JScrollPane(palBtns), BorderLayout.CENTER);
    }
    /**
     * 撤销
     */
    private void exitBtnListener() {
        PLTabButton plTabButton = this.selectedTabButton;
        plTabButton.plParentOid = "";
        try {
            boolean success = UITools.getService().updatePLTabButton(plTabButton);
            if(success == false) {
                JOptionPane.showMessageDialog(this, 
                        "撤销失败!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        } catch (VCIError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        TreePath selectedPath = this.buttonTree.getPathForRow(0);
        this.refreshTreeNode(selectedPath);
        buttonPanel.add(joinBtn);
        buttonPanel.add(exitBtn);
    }
    /**
     * 加入上级
     */
    private void joinBtnListener() {
        PLTabButton plTabButton = this.selectedTabButton;
        //当选择的按钮为树的第一个节点的时候,他的兄节点是他自己,导致调整为下级按钮时出错,故作此判断。
        if(this.selectedBrotherTabButton !=null && !plTabButton.plOId.equalsIgnoreCase(this.selectedBrotherTabButton.plOId) ){
            plTabButton.plParentOid = this.selectedBrotherTabButton.plOId;
            try {
                boolean success = UITools.getService().updatePLTabButton(plTabButton);
                if(success == false) {
                    JOptionPane.showMessageDialog(this, 
                            "修改失败!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
            } catch (VCIError e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            TreePath selectedPath = this.buttonTree.getSelectionPath();
            TreePath parentPath = selectedPath.getParentPath();
            this.refreshTreeNode(parentPath);
        }    
    }
 
    protected String getRootNodeShowLable(){
        return this.plPageDefination != null ? this.plPageDefination.name : "按钮定义";
    }
    /**
     * 创建Button树面板
     * @return
     */
    private void getButtonTreePanel() {
        buttonTreePanel = new JPanel(new BorderLayout());
        
        //初始化按钮树
        DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode(getRootNodeShowLable());
        DefaultTreeModel dtml = new DefaultTreeModel(dmtn);
        buttonTree = new JTree(dtml);
        buttonTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        buttonTree.setCellRenderer(new TabButtonTreeRenderer());
        buttonTree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                buttonTreeSelectionListener(e.getPath());
            }
        });
        buttonTree.addTreeExpansionListener(new TreeExpansionListener() {
            @Override
            public void treeExpanded(TreeExpansionEvent event) {
                buttonTree.setSelectionPath(event.getPath());
            }
            @Override
            public void treeCollapsed(TreeExpansionEvent event) {
                buttonTree.setSelectionPath(event.getPath());
            }
        });
        buttonTree.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if(buttonTree.isEnabled()){
                    int x = e.getX();
                    int y = e.getY();
                    TreePath pathForLocation = buttonTree.getPathForLocation(x, y);
                    if(pathForLocation == null) {
                        return;
                    }
                    buttonTree.setSelectionPath(pathForLocation);
                    if(e.getButton() == MouseEvent.BUTTON3) {
                        //TODO 树节点右键事件                        
                    }
                }else {
                //    return;
                }
                
 
            }
        });
        
        JScrollPane buttonTreeScrollPane = new JScrollPane(buttonTree);
        buttonTreeScrollPane.setBorder(new EmptyBorder(0,0, 0,0));
        buttonTreePanel.add(buttonTreeScrollPane, BorderLayout.CENTER);
    }
    /**
     * 
     * <p>国际化字典Dialog: </p>
     * @author yangyang
     * @time 2017-10-31
     */
//    private void i18nTable(){
//        VciI18nTableDialog vi18n = new VciI18nTableDialog();
//        vi18n.setModal(true);
//        vi18n.init();
//        vi18n.setSize(900,600);
//        vi18n.setLocationRelativeTo(ClientContextVariable.getFrame());
//        vi18n.setVisible(true);
//    }
//    
    /**
     * 刷新按钮树节点
     * @param selectedPath 待刷新节点的树路径
     */
    protected void refreshTreeNode(TreePath selectedPath) {
        if(selectedPath == null) {
            return;
        }
        //加载页签区域按钮配置
        PLTabButton[] buttons = this.downloadTabButton();
        if(buttons == null) {
            return;
        }
        
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)selectedPath.getLastPathComponent();
        DefaultTreeModel dtml = (DefaultTreeModel) this.buttonTree.getModel();
        this.refreshTreeNode(node, dtml, buttons);
    }
    
    /**
     * 刷新树节点,创建节点对象并加载
     * @param parent 父节点
     * @param dtml 树模型
     * @param buttons PLTabButton数组
     */
    private void refreshTreeNode(DefaultMutableTreeNode parent,
            DefaultTreeModel dtml, PLTabButton[] buttons) {
        parent.removeAllChildren();
        String poid = "";
        
        Object userObject = parent.getUserObject();
        if(userObject instanceof ClientPLTabButton) {
            ClientPLTabButton cptb = (ClientPLTabButton) userObject;
            poid = cptb.getPtb().plOId;
        }
        
        for(PLTabButton ptb : buttons) {
            if(ptb.plParentOid.equals(poid)) {
                ClientPLTabButton cptb = new ClientPLTabButton(ptb);
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(cptb);
                dtml.insertNodeInto(child, parent, parent.getChildCount());
                refreshTreeNode(child, dtml, buttons);
            }
        }
        
        dtml.reload(parent);
        this.buttonTree.updateUI();
        
    }
    
    /**
     * 加载页签区域按钮配置信息
     * @return
     */
    protected PLTabButton[] downloadTabButton() {
        PLTabButton[] buttons;
        try {
//            buttons = Tool.getService().getPLTabButtonsByTableOId(
//                    plPageDefination.plOId);
            buttons = UITools.getService().getPLTabButtonsByTableOId(plPageDefination.plOId);
            
            return buttons;
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, 
                    "加载页签区域按钮配置信息异常:" + e.getMessage(),
                    "系统提示", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }
    
    /**
     * 树节点点击事件
     */
    protected void buttonTreeSelectionListener(TreePath selectedPath) {
        if(selectedPath == null) {
            this.selectedTabButton = null;
        } else {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedPath.getLastPathComponent();
            DefaultMutableTreeNode nodeBrother =  node.getPreviousSibling();
            Object userObject = node.getUserObject();
            if(nodeBrother != null){
                Object userObjectBrother = nodeBrother.getUserObject();
                this.selectedBrotherTabButton = ((ClientPLTabButton) userObjectBrother).getPtb();
            }
            if(userObject instanceof String) {
                this.selectedTabButton = null;
            } else {
                this.selectedTabButton = ((ClientPLTabButton) userObject).getPtb();
            }
        }
        if(this.flag == OptMode.View) {
            if(this.selectedTabButton == null) {
                this.createButtonBtn.setEnabled(true);
                this.editButtonBtn.setEnabled(false);
                this.deleteButtonBtn.setEnabled(false);
            } else {
                this.createButtonBtn.setEnabled(true);
                this.editButtonBtn.setEnabled(true);
                this.deleteButtonBtn.setEnabled(true);
            }
            refreshSettingsPanel();
        } else if(this.flag == OptMode.Add) {
            this.addButtonBtnListener();
        } else if(this.flag == OptMode.Update) {
            if(this.selectedTabButton == null) {
                JOptionPane.showMessageDialog(this, 
                        "不能修改根节点信息", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                this.cancelBtnListener();
            } else {
                this.editButtonBtnListener();
            }
        }
    }
    
    protected ButtonInfoPanel btnInfoPanel = null;
    public ButtonInfoPanel getBtnInfoPanel() {
        return btnInfoPanel;
    }
    /**
     * 得到TabButton属性信息面板
     */
    private void getSettingsPanel() {
        btnInfoPanel = new ButtonInfoPanel(this, null);
        btnInfoPanel.buildPanel();
        this.settingsPanel = btnInfoPanel;
    }
    
//    /**
//     * 初始化按钮参数面板
//     * @param params
//     * @return
//     */
//    private void getTabButtonParamterPanel(PLCommandParameter[] params) {
//        if(params == null || params.length < 1) {
//            tabButtonParamsPanel.removeAll();
//            tabButtonParamsPanel.updateUI();
//            tabButtonParamsPanel.setPreferredSize(new Dimension(0, 0));
//            tabButtonParamsPanel.setBounds(new Rectangle(3, 285,
//                    tabButtonParamsPanel.getPreferredSize().width,
//                    tabButtonParamsPanel.getPreferredSize().height));
//            settingsPanel.setPreferredSize(new Dimension(
//                    460, 285 + tabButtonParamsPanel.getPreferredSize().height));
//            return;
//        }
//        Color backgroudColor = Color.WHITE;
//        tabButtonParamsPanel.removeAll();
//        tabButtonParamsPanel.updateUI();
//        tabButtonParamsPanel.setLayout(null);
//        tabButtonParamsPanel.setBackground(backgroudColor);
//        //tabButtonParamsPanel.setBorder(BorderFactory.createTitledBorder("按钮参数"));
//        
//        //面板宽度、高度
//        int width = 460, height = 250;
//        //自定义控件ButtonParamTableCompt接受参数类型为PLActionParam,把params转一下使用
//        PLActionParam[] params2 = new PLActionParam[params.length];
//        for(int i = 0; i < params.length; i++){
//            params2[i] = new PLActionParam();
//            params2[i].name = params[i].plKey;
//            params2[i].defaultValue = params[i].plValue;
//        }
//        ArrayList<PLActionParam> datas = new ArrayList<PLActionParam>(Arrays.asList(params2));
//        ButtonParamTableCompt tableCompt = new ButtonParamTableCompt(datas);
//        tabButtonParamsPanel.add(tableCompt.getTablePanel());
//        
//        tabButtonParamsPanel.setPreferredSize(new Dimension(width, height));
//        tabButtonParamsPanel.setBounds(new Rectangle(3, 285,
//                tabButtonParamsPanel.getPreferredSize().width,
//                tabButtonParamsPanel.getPreferredSize().height));
//        settingsPanel.setPreferredSize(new Dimension(
//                460, 285 + tabButtonParamsPanel.getPreferredSize().height));
//    }
    
    /**
     * 刷新按钮设置面板
     */
    //String show2="0";
    private void refreshSettingsPanel() {
        btnInfoPanel.setTabButton(selectedTabButton);
        btnInfoPanel.setControlEnable(flag != OptMode.View);
        
        int location = mainSplitPane.getDividerLocation();
        mainSplitPane.setDividerLocation(location);    
    }
    
    
    /**
     * 添加按钮事件
     */
    protected void addButtonBtnListener() {
        btnInfoPanel.setOptMode(OptMode.Add);
        this.flag = OptMode.Add;
 
        this.refreshSettingsPanel();
        this.setAddEditButtonEnable();
        this.btnInfoPanel.getParamTablePanel().setEditable(true);
        this.btnInfoPanel.getParamTablePanel().setShowButton(true);
    }
    
    /**
     * 修改按钮事件
     */
    protected void editButtonBtnListener() {
        btnInfoPanel.setOptMode(OptMode.Update);
        this.flag = OptMode.Update;
 
        this.refreshSettingsPanel();
        this.setAddEditButtonEnable();
        this.btnInfoPanel.getParamTablePanel().setEditable(true);
        this.btnInfoPanel.getParamTablePanel().setShowButton(true);
    }
    
    private void setAddEditButtonEnable(){
        //重置按钮的状态
        this.createButtonBtn.setEnabled(false);
        this.editButtonBtn.setEnabled(false);
        this.deleteButtonBtn.setEnabled(false);
        this.saveBtn.setEnabled(true);
        this.cancelBtn.setEnabled(true);
        this.buttonTree.setEnabled(false);
    }
    
    /**
     * 按钮删除事件
     */
    protected void deleteButtonBtnListener() {
        try {
            
            //得到当前选择节点父节点,用于刷新使用
            TreePath selectedPath = this.buttonTree.getSelectionPath();
            TreePath parentPath = null;
            if(selectedPath != null) {
                parentPath = selectedPath.getParentPath();
            }
            
            if(this.selectedTabButton == null) {
                JOptionPane.showMessageDialog(this, 
                        "根节点不能删除!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }            
            /*int confirm = JOptionPane.showConfirmDialog(this, 
                    "是否删除当前按钮?", "系统提示", JOptionPane.OK_CANCEL_OPTION);*/
            Object[] options = { "Yes", "No" };
            int option = JOptionPane
                    .showOptionDialog(this, "确认是否删除按钮\""
                            + this.selectedTabButton.plLabel + "\"", "按钮定义删除",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE, null, options,
                            options[1]);
            if(option != JOptionPane.YES_OPTION){
                return;
            }
            if(option == JOptionPane.OK_OPTION) {
                boolean success = UITools.getService().deletePLTabButton(this.selectedTabButton);
                if(success == false){
                    JOptionPane.showMessageDialog(this, 
                            "该有子级按钮,不能删除!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
 
                JOptionPane.showMessageDialog(this, 
                        "删除成功!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                //刷新树
                if(parentPath != null) {
                    this.refreshTreeNode(parentPath);
                    this.buttonTree.setSelectionPath(parentPath);
                }
            }
        } catch (VCIError e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, 
                    "执行删除操作时发生异常:" + e.getMessage(), "系统提示", JOptionPane.ERROR_MESSAGE);            
        }
    }
    
    /**
     * 保存事件
     */
    
    public void saveBtnListener() {
        String btnParamValidate = getBtnInfoPanel().getParamTablePanel().geCheckRes();
        if(btnParamValidate != null && btnParamValidate.trim().length() != 0){
            JOptionPane.showMessageDialog(this, btnParamValidate , "系统提示", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        //创建PLTabButton对象
        PLTabButton plTabButton = null;
        if(this.flag == OptMode.Add) {
            //如果是增加操作,直接创建PLTabButton对象
            plTabButton = new PLTabButton();
            plTabButton.plOId = ObjectUtility.getNewObjectID36();
        } else {
            //修改操作
            plTabButton = this.selectedTabButton;
            plTabButton.plModifyUser = LogonApplication.getUserEntityObject().getUserName();
        }
        plTabButton = getBtnInfoPanel().getFillVaueButton(plTabButton);
        if(plPageDefination != null){
            plTabButton.plTableOId = plPageDefination.plOId;
        }
        
        if (plTabButton.plSeq < 1 || plTabButton.plSeq > 63) {
            JOptionPane.showMessageDialog(this, "按序号超出范围,请修改,按钮【编号】只能在【1-63】范围内。", "系统提示", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        plTabButton.plCreateUser = LogonApplication.getUserEntityObject().getUserName();
        plTabButton.plModifyUser = LogonApplication.getUserEntityObject().getUserName();
        plTabButton.plAreaType = buttonArea;
        try {
            if(flag == OptMode.Add){
                boolean success = UITools.getService().savePLTabButton(plTabButton);
                if(success == false) {
                    JOptionPane.showMessageDialog(this, 
                            "编号重复,编号已经在当前页签下存在!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
            } else if(flag == OptMode.Update){
                UITools.getService().updatePLTabButton(plTabButton);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "保存按钮信息时发生异常:" 
                    + e.getMessage(), "系统提示", JOptionPane.ERROR_MESSAGE);
            return;
        }
        //复用以前的代码,对于参数一条一条删除,一条一条创建
        //数据量及并发较少,暂时这么处理没有什么问题
        if(this.flag == OptMode.Update) {
            try {
                UITools.getService().deletePLCommandParameterByTabButtonId(plTabButton.plOId);
            } catch (VCIError e) {
                e.printStackTrace();
            }
        }
        
        LinkedHashMap<String, String> buttonParams = getBtnInfoPanel().getParamTablePanel().getButtonParams();
        if(!buttonParams.isEmpty()) {
            Iterator<Entry<String, String>> iterator = buttonParams.entrySet().iterator();
            while(iterator.hasNext()){
                Entry<String, String> next = iterator.next();
                if(StringUtils.isEmpty(next.getKey()) || StringUtils.isEmpty(next.getValue())){
                    iterator.remove();
                }
            }
            if(!buttonParams.isEmpty()){
                Iterator<Entry<String, String>> kvItor = buttonParams.entrySet().iterator();
                while(kvItor.hasNext()){
                    Entry<String, String> next = kvItor.next();
                    PLCommandParameter plCommandParameter = new PLCommandParameter();
                    plCommandParameter.plOId = ObjectUtility.getNewObjectID36();
                    plCommandParameter.plCommandOId = plTabButton.plOId;
                    plCommandParameter.plKey = next.getKey();
                    plCommandParameter.plValue = next.getValue();
                    plCommandParameter.plCreateUser = LogonApplication.getUserEntityObject().getUserName();
                    plCommandParameter.plModifyUser = LogonApplication.getUserEntityObject().getUserName();
                    try {
                        UITools.getService().savePLCommandParameter(plCommandParameter);
                    } catch (VCIError e) {
                        e.printStackTrace();
                        JOptionPane.showMessageDialog(this, "保存按钮信息时发生异常:" 
                                + e.getMessage(), "系统提示", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }
            }
        }
    
        if(this.flag == OptMode.Add) {
            //添加的时候刷新树节点
            TreePath selectedPath = this.buttonTree.getSelectionPath();
            this.refreshTreeNode(selectedPath);
            //遍历子节点,设置新增节点为选中状态
            selectChildPath(selectedPath, plTabButton.plOId);
        }else{
            //修改的时候刷新树节点
            TreePath selectedPath = this.buttonTree.getSelectionPath();
            //add by caill  加非空判断为了避免空指针异常
            if(selectedPath != null){
                TreePath parentPath = selectedPath.getParentPath();
                this.refreshTreeNode(parentPath);
            }
        }
        //保存完成后重置面板
        cancelBtnListener();
        buttonTree.setEnabled(true);
    }
    
    /**
     * 取消按钮事件
     */
    protected void cancelBtnListener() {
        this.flag = OptMode.View;
        btnInfoPanel.setOptMode(this.flag);
 
//        this.selectedTabButton = null;
        
        this.refreshSettingsPanel();
        
        //重置按钮状态
        this.createButtonBtn.setEnabled(true);
        if(this.selectedTabButton != null) {
            this.editButtonBtn.setEnabled(true);
            this.deleteButtonBtn.setEnabled(true);
        }
        this.saveBtn.setEnabled(false);
        this.cancelBtn.setEnabled(false);
        
        //重置参数
        this.flag = OptMode.View;
        buttonTree.setEnabled(true);
        
        this.btnInfoPanel.getParamTablePanel().setEditable(false);
        this.btnInfoPanel.getParamTablePanel().setShowButton(false);
    }
    
    /**
     * 遍历子节点,设置ploid节点为选中状态
     * @param parentPath 遍历节点路径
     * @param ploid 子节点ploid
     */
    protected void selectChildPath(TreePath parentPath, String ploid) {
        if(parentPath == null) {
            return;
        }
        //设置当前节点展开
        this.buttonTree.expandPath(parentPath);
        //遍历子节点
        DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) parentPath.getLastPathComponent();
        int childCount = parentNode.getChildCount();
        for(int i = 0; i < childCount; i++) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) parentNode.getChildAt(i);
            ClientPLTabButton cptb = (ClientPLTabButton) childNode.getUserObject();
            PLTabButton ptb = cptb.getPtb();
            if(ploid.equals(ptb.plOId)) {
                int row = this.buttonTree.getRowForPath(parentPath);
                this.buttonTree.setSelectionRow(row + i + 1);
                break;
            }
        }
    }
    
    
    /**
     * 去除""字符串
     * @param str 
     * @return
     */
    public String getNotNullString(String str) {
        if(str == null) {
            return "";
        }
        return str.trim();
    }
    
    
    private boolean checkSelectedIsButtonNode(boolean allowRoot){
        TreePath selectedPath = this.buttonTree.getSelectionPath();
        if(selectedPath == null){
            return false;
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedPath.getLastPathComponent();
        if(!allowRoot && node.isRoot()){
            return false;
        }
        return true;
    }
    private boolean isCanContinue(boolean allowRoot){
        if(!checkSelectedIsButtonNode(allowRoot)){
            VCIJOptionPane.showMessage(this, "请先选择有效的按钮!");
            return false;
        }
        return true;
    }
    private void copy(){
        if(!isCanContinue(false)) return;
 
        TreePath selectedPath = this.buttonTree.getSelectionPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedPath.getLastPathComponent();
        ClientPLTabButton btn = (ClientPLTabButton)node.getUserObject();
        String tabBtnOid = btn.getPtb().plOId;
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
                new StringSelection(tabBtnOid), null);
    }
    
    private void paste(){
        if(!isCanContinue(true)) return;
        try{
            Transferable trans = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
            if(Toolkit.getDefaultToolkit().getSystemClipboard().isDataFlavorAvailable(DataFlavor.stringFlavor)){
                Object obj = trans.getTransferData(DataFlavor.stringFlavor);
                if(obj instanceof String){
                    String tabBtnOid = (String)obj;
                    pasteTabBtnToNewCompt(tabBtnOid);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            VCIJOptionPane.showError(this, "从剪切模板中提取数据时发生错误!\n" + e.getMessage());
        }
    }
    
    private void pasteTabBtnToNewCompt(String tabBtnOid){
        try {
            PLTabButton btn = UITools.getService().getPLTabButtonById(tabBtnOid);
            if("".equals(btn.plOId)){
                VCIJOptionPane.showMessage(this, "按钮无效,无法执行粘贴!");
                return;
            }
            addButtonToTree(btn);
        } catch (VCIError e) {
            e.printStackTrace();
            VCIJOptionPane.showError(this, "根据oid查询按钮时发生错误!\n" + e.getMessage());
        }
    }
    
    private void addButtonToTree(PLTabButton btnSrc){
        String parentOid = "";
        TreePath selectedPath = this.buttonTree.getSelectionPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedPath.getLastPathComponent();
        if(node.getUserObject() instanceof String){
            parentOid = "";
        } else if(node.getUserObject() instanceof ClientPLTabButton){
            ClientPLTabButton btnSelected = (ClientPLTabButton)node.getUserObject();
            parentOid = btnSelected.getPtb().plOId;
        }
        
        doPasteDet(btnSrc, parentOid,
                new String[]{this.plPageDefination.plOId}, true);
        
        refreshTreeNode(this.buttonTree.getPathForRow(0));
    }
    
    private void doPasteDet(PLTabButton btnSrc, 
            String btnParentOid, 
            String[] targetComptOids,
            boolean showSuccessMessage){
 
        PLCommandParameter[] cmdParSrcs = new PLCommandParameter[0];
        try{
            cmdParSrcs = UITools.getService().getPLCommandParametersByCommandOId(btnSrc.plOId);
        } catch (VCIError e) {
            e.printStackTrace();
            VCIJOptionPane.showError(this, "查询按钮参数时发生错误!\n" + e.getMessage());
            return;
        }
        
        List<PLTabButton> newBtns = new LinkedList<PLTabButton>();
        List<PLCommandParameter> newBtnParams = new LinkedList<PLCommandParameter>();
        for(String comptOid : targetComptOids){
            doPastDetToCompt(btnSrc, cmdParSrcs, btnParentOid, comptOid, newBtns, newBtnParams);
        }
        
        doSaveNewPLTabButtonAndParams(newBtns, newBtnParams, this, showSuccessMessage);
    }
    
    private void doSaveNewPLTabButtonAndParams(
            final List<PLTabButton> newBtns,
            final List<PLCommandParameter> newBtnParams,
            final Component messageOwnerCompt,
            final boolean showSuccessMessage){
 
        final QANProcessBarFrame frame = new QANProcessBarFrame();
        final QANProcessBar bar = new QANProcessBar(new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    frame.setContent("正在保存按钮,请稍候...");
                    UITools.getService().savePLTabButtonAndParams(
                            newBtns.toArray(new PLTabButton[]{}), 
                            newBtnParams.toArray(new PLCommandParameter[]{}));
                    frame.setProcessBarCancel(true);
                    if(showSuccessMessage){
                        VCIJOptionPane.showMessage(messageOwnerCompt, "保存按钮成功!");
                    }
                }catch (VCIError e) {
                    e.printStackTrace();
                    frame.setProcessBarCancel(true);
                    VCIJOptionPane.showError(messageOwnerCompt, "保存按钮(包含按钮参数)时发生错误!\n" + e.getMessage());
                }finally{
                    frame.setProcessBarCancel(true);
                }
            }
        }), (JDialog)this, frame, false);
        bar.setTitle("保存按钮");
        frame.setTitle(bar.getTitle());
        bar.setVisible(true);
    }
    
    private void doPastDetToCompt(PLTabButton btnSrc, PLCommandParameter[] cmdParamsSrc,
            String btnParentOid, String targetComptOid,
            List<PLTabButton> newBtns,
            List<PLCommandParameter> newBtnParams){
        
        PLTabButton btn = PortalUtil.getInstance().clonePLTabButton(btnSrc);
        newBtns.add(btn);
        
        for (int i = 0; i < cmdParamsSrc.length; i++) {
            PLCommandParameter newParam = PortalUtil.getInstance().clonePLCommandParameter(cmdParamsSrc[i]);
            newParam.plCommandOId = btn.plOId;
            newBtnParams.add(newParam);
        }
        btn.plParentOid = btnParentOid;
        btn.plTableOId = targetComptOid;
    }
    
    private void copyToOther(){
        if(!isCanContinue(false)){
            return ;
        }
        
        ButtonCopyToOtherComptDialog dlg = new ButtonCopyToOtherComptDialog(this);
        dlg.buildDialog();
        dlg.setVisible(true);
        DialogResult dlgRes = dlg.getDialogResult();
        if(dlgRes == DialogResult.OK){
            List<String> targetComptOids = dlg.getTargetComptOidList();
            PLTabButton btnSrc = getSelectedPLTabButton();
            doPasteDet(btnSrc, "", targetComptOids.toArray(new String[]{}), true);
        }
    }
    
    private PLTabButton getSelectedPLTabButton(){
        TreePath selectedPath = this.buttonTree.getSelectionPath();
        if(selectedPath == null){
            return null;
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectedPath.getLastPathComponent();
        if(node.getUserObject() instanceof ClientPLTabButton){
            ClientPLTabButton btnSelected = (ClientPLTabButton)node.getUserObject();
            return btnSelected.getPtb();
        }
        return null;
    }
    public boolean isShowCopyButton() {
        return showCopyButton;
    }
    public void setShowCopyButton(boolean showCopyButton) {
        this.showCopyButton = showCopyButton;
    }
    public boolean isShowPasteButton() {
        return showPasteButton;
    }
    public void setShowPasteButton(boolean showPasteButton) {
        this.showPasteButton = showPasteButton;
    }
    public boolean isShowPasteToOther() {
        return showPasteToOther;
    }
    public void setShowPasteToOther(boolean showPasteToOther) {
        this.showPasteToOther = showPasteToOther;
    }
    public boolean isShowJoinBtn() {
        return showJoinBtn;
    }
    public void setShowJoinBtn(boolean showJoinBtn) {
        this.showJoinBtn = showJoinBtn;
    }
    public boolean isShowExitBtn() {
        return showExitBtn;
    }
    public void setShowExitBtn(boolean showExitBtn) {
        this.showExitBtn = showExitBtn;
    }
    public List<JButton> getOtherCustomButtons() {
        return otherCustomButtons;
    }
    public void setOtherCustomButtons(List<JButton> otherCustomButtons) {
        this.otherCustomButtons = otherCustomButtons;
    }
    public JTree getButtonTree() {
        return buttonTree;
    }
}