ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package com.vci.client.omd.statepool.ui;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
 
import org.dom4j.Document;
 
import com.vci.client.LogonApplication;
import com.vci.client.omd.statepool.StatePoolStart;
import com.vci.client.omd.statepool.StatePoolWrapper;
import com.vci.client.omd.statepool.itf.InterState;
import com.vci.client.omd.statepool.pubimpl.InterStateManager;
import com.vci.client.omd.ui.OmdTreeCellRenderer;
import com.vci.client.ui.image.BundleImage;
import com.vci.client.ui.swing.VCISwingUtil;
import com.vci.client.ui.util.PostorderEnumeration;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.stm.StatePool;
import com.vci.mw.InvocationUtility;
 
@SuppressWarnings("all")
public class StatePoolUI extends JPanel {
    /**
     * 
     */
 
    private ImageIcon icon = null;
 
    private String imageURL = "";
    private boolean modifyFlag = false;
    private static final long serialVersionUID = -3925091496139447035L;
    String realName = "";
    public static StatePoolUI statePoolUI = null;
    private StatePool statePool = null;
    // 定义文本框,用于用户输入数据
    private JTextField jtf1 = new JTextField(60), jtf2 = new JTextField(60), jtf3 = new JTextField(60);
    // 定义标签,显示文本框的含义
    private JLabel jl1 = new JLabel("名称"), jl2 = new JLabel("标签"), jl3 = new JLabel("图片"), jl4 = new JLabel("描述"),
            jl11 = new JLabel("名称(不能为空)"), jl22 = new JLabel("标签(对名称的解释)"), jl33 = new JLabel("图片(路径)"),
            jl44 = new JLabel("描述(可以为空)");
    // 定义文本域,用于书写描述信息
    private JTextArea jta = new JTextArea(3, 60);
    // 定义容器,用于接收需要布局的容器
    private JPanel jp;
    // test
    private ImageIcon image = new BundleImage().createImageIcon("zoom.png");
    // 定义map集合用于存放从XML文件中获取到的数据
    Map<String, String> map = new HashMap<String, String>();
    private JButton addButton, modifyButton, deleteButton, checkButton, saveButton, cancelButton,
            selectButton = new JButton(image), selectButton1;
    // 定义小容器用于界面分区的时候使用
    private JPanel northJPanel, westJPanel = new JPanel(), mainJPanel, centerJPanel, leftJPanel = new JPanel(),
            textJPanel = new JPanel();
    // 定义滚动条用于存放tree
    private JScrollPane scrollPane = new JScrollPane();
    // 定义一棵树
    private JTree tree;
    private String nodeName = null;
 
    private JButton btnExp;
 
    private JButton btnImp;
 
    private JTextField textField;
    private JButton sel;
 
    // 构造器初始化要优先执行的方法
    public StatePoolUI() {
        createPoolTree();
        initUI();
        saveButton.setEnabled(false);
        cancelButton.setEnabled(false);
        addListeneradd();
        addListenermodify();
        addListenerdelete();
        addListenersave();
        addListenercancel();
        selectListeneradd();
        addListenner();
 
        btnExp.addActionListener(new ExpActionListener());
        btnImp.addActionListener(new ImpActionListener());
    }
 
    // 新建按钮的监听器
    private void addListeneradd() {
        addButton.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
                if (mainJPanel == null) {
                    return;
                }
//                selectButton.setIcon(image);
                selectButton.setEnabled(true);
                jtf1.setEditable(true);
                jtf2.setEditable(true);
                jtf3.setEditable(false);
                jta.setEditable(true);
                modifyFlag = false;
                addButton.setEnabled(false);
                modifyButton.setEnabled(false);
                deleteButton.setEnabled(false);
                checkButton.setEnabled(false);
                tree.setEnabled(false);
                mainJPanel.removeAll();
                mainJPanel.add(addVerpanel(), BorderLayout.CENTER);
                mainJPanel.updateUI();
                saveButton.setEnabled(true);
                cancelButton.setEnabled(true);
 
            }
        });
    }
 
    // 新定义一个JFrame用于列举状态图标供用户选择
//    public JDialog listImage() {
//        List<URL> urls = null;
//        List<String> imagePaths_after = null;
//        final JDialog mainWin = new JDialog();
//        mainWin.setBounds(300, 0, 60, 300);
//        String basePath = LogonApplication.urlBasePath;
//        
//        imagePaths_after = new ArrayList<String>();
//        urls = new ArrayList<URL>();
//        
//        String[] imagePaths = StatePoolStart.getService().getImagePaths();
//        for(String imagePath:imagePaths){
//            String imagePaths_middle = basePath+imagePath;
//            imagePaths_after.add(imagePaths_middle);
//        }
//        
//        String[] friends = imagePaths_after.toArray(new String[0]);
//        
//        for(String friend:friends){
//            URL url = null;
//            try {
//                url = new URL(friend);
//                urls.add(url);
//            //    imagePaths.add(url.toString());
//            } catch (MalformedURLException e1) {
//                // TODO Auto-generated catch block
//                e1.printStackTrace();
//            }
//            if(url == null){
//                continue;
//            }
//            
//        }
//        
////        JList jList = new JList(urls.toArray(new URL[0]));
//        // 定义一个JList对象
//        final JList friendsList = new JList((URL[])urls.toArray(new URL[0]));
//
//        // 设置该JList使用ImageCellRenderer作为单元格绘制器
//        friendsList.setCellRenderer(new ImageCellRenderer());
//        friendsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置列表只能单选
//        mainWin.add(new JScrollPane(friendsList));
//        friendsList.addListSelectionListener(new ListSelectionListener() {
//            @Override
//            public void valueChanged(ListSelectionEvent e) {
//                jtf3.setText((String) friendsList.getSelectedValue());
//                mainWin.dispose();
//                selectButton.setIcon(new ImageIcon((URL) friendsList
//                        .getSelectedValue()));
//            }
//
//        });
//        mainWin.setModal(true);
//        mainWin.pack();
//        mainWin.addWindowListener(new WindowAdapter() {
//            public void windowClosing(WindowEvent e) {
//                    mainWin.dispose();
//            }
//        });
//        mainWin.setVisible(true);
//        return mainWin;
//    }
 
    // 此类只在内部使用,因此定义为内部类,实现画图功能
//    class ImageCellRenderer extends JPanel implements ListCellRenderer {
//        private ImageIcon icon;
//        private String name;
//        // 定义绘制单元格时的背景色
//        private Color background;
//        // 定义绘制单元格时的前景色
//        private Color foreground;
//
//        public Component getListCellRendererComponent(JList list, Object value,
//                int index, boolean isSelected, boolean cellHasFocus) {
//            icon = new ImageIcon((URL) value);
//            name = value.toString();
//            background = isSelected ? list.getSelectionBackground() : list
//                    .getBackground();
//            foreground = isSelected ? list.getSelectionForeground() : list
//                    .getForeground();
//            // 返回该JPanel对象作为单元格绘制器
//            return this;
//        }
//
//        // 重写paintComponent方法,改变JPanel的外观
//        public void paintComponent(Graphics g) {
//            int imageWidth = icon.getImage().getWidth(null);
//            int imageHeight = icon.getImage().getHeight(null);
//            g.setColor(background);
//            g.fillRect(0, 0, getWidth(), getHeight());
//            g.setColor(foreground);
//            // 绘制好友图标
//            g.drawImage(icon.getImage(), getWidth() / 2 - imageWidth / 2, 10,
//                    null);
//            g.setFont(new Font("SansSerif", Font.BOLD, 10));
//
//            // 绘制好友用户名
//            g.drawString(name, getWidth() / 2 - name.length() * 3,
//                    imageHeight + 30);
//        }
//
//        // 通过该方法来设置该ImageCellRenderer的最佳大小
//        public Dimension getPreferredSize() {
//            return new Dimension(60, 80);
//        }
//    }
 
    // 此方法是选中按钮的监听器
    private void selectListeneradd() {
        selectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (mainJPanel == null) {
                    return;
                }
//                listImage();
                ImageSelectDialog dialog = ImageSelectDialog.getInstance();
                dialog.setVisible(true);
                imageURL = dialog.getImageURL();
                if (imageURL.startsWith(".")) {
                    imageURL = dialog.getImageURLLocal();
                    // System.out.println("本地URL================"+imageURL);
                    updateImageUILocal();
                } else {
                    imageURL = dialog.getImageURL();
                    updateImageUI();
                }
            }
        });
    }
 
    public void updateImageUILocal() {
        jtf3.setText(imageURL);
//        ImageIcon icon = new ImageIcon(imageURL);
//        if(imageURL == null || imageURL == ""){
//            icon = image;
//        }
        // selectButton.setIcon(icon);
    }
 
    public void updateImageUI() {
        jtf3.setText(imageURL);
//        URL url = null;
//        try {
////            url = new URL(imageURL);
//        } catch (MalformedURLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
 
//        ImageIcon icon = new ImageIcon(url);
//        ImageIcon icon = null;
//        if(imageURL.contains("-")){
//            icon = new ImageBundle().createImageIcon(imageURL);
//        } else {
//            icon = new BundleImage().createImageIcon(imageURL);
//        }
        // selectButton.setIcon(icon);
 
    }
 
    // 新建状态的时候要显示的界面
    public JPanel addVerpanel() {
        jtf1.setText("");
        jtf1.setEnabled(true);
        jtf2.setText("");
        jtf3.setText("");
        jta.setText("");
        // selectButton.setIcon(image);
        JPanel jp = new JPanel();
        jp.setBackground(Color.WHITE);
        jp.setLayout(new BorderLayout(0, 0));
        jp.setBorder(BorderFactory.createTitledBorder("创建状态:"));
        jp.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.anchor = GridBagConstraints.WEST;
        g.insets = new Insets(10, 5, 0, 5);
        g.gridx = 0;
        g.gridy = 0;
        jp.add(jl11, g);
        GridBagConstraints g1 = new GridBagConstraints();
        g1.insets = new Insets(10, 5, 0, 5);
        g1.gridx = 1;
        g1.gridy = 0;
        jp.add(jtf1, g1);
        /*
         * jtf1.addFocusListener(new FocusListener() { String regex = "[a-z A-Z]*";
         * 
         * public void focusLost(FocusEvent e) { if (jtf1.getText().equals("") ||
         * (!jtf1.getText().matches(regex))) { JOptionPane.showMessageDialog(null,
         * jl11.getText() .substring(0, 2) + "不能为空且只能为英文"); jtf1.setText("");
         * jtf1.requestDefaultFocus(); } }
         * 
         * @Override public void focusGained(FocusEvent e) {
         * 
         * } });
         */
 
        // 限定文本框的有效输入长度为50
        jtf1.setDocument(new PlainDocument() {
            public void insertString(int offs, String str, javax.swing.text.AttributeSet a)
                    throws BadLocationException {
                String text = jtf1.getText();
                if (text.length() + str.length() > 50) {
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane.showMessageDialog(null, "名称不能超过50个字符");
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
 
        g.gridx = 0;
        g.gridy = 1;
        jp.add(jl22, g);
        g.gridx = 1;
        g.gridy = 1;
        jp.add(jtf2, g);
 
        g.gridx = 0;
        g.gridy = 2;
        jp.add(jl33, g);
        g.gridx = 1;
        g.gridy = 2;
        jp.add(jtf3, g);
        g.gridx = 2;
        g.gridy = 2;
        selectButton.setToolTipText("选择");
        selectButton.setText("选择");
        jp.add(selectButton, g);
 
        g.gridx = 0;
        g.gridy = 3;
        jp.add(jl44, g);
        jta.setLineWrap(true);
        g1.gridx = 1;
        g1.gridy = 3;
        jp.add(new JScrollPane(jta), g1);
        jta.setDocument(new PlainDocument() {
            public void insertString(int offs, String str, javax.swing.text.AttributeSet a)
                    throws BadLocationException {
                String text = jtf1.getText();
                if (text.length() + str.length() > 255) {
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane.showMessageDialog(null, "名称不能超过255个字符");
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        return jp;
    }
 
    // 主界面的可视化方法,主要是定义界面的布局,不提供具体的功能
    private void initUI() {
        setBackground(Color.WHITE);
        setLayout(new BorderLayout(0, 0));
        // northPanel
        northJPanel = new JPanel();
        FlowLayout fl = (FlowLayout) northJPanel.getLayout();
        fl.setAlignment(FlowLayout.CENTER);
        this.add(northJPanel, BorderLayout.NORTH);
 
        addButton = new JButton("创建");
        modifyButton = new JButton("修改");
        deleteButton = new JButton("删除");
        checkButton = new JButton("查看使用范围");
        saveButton = new JButton("保存");
        cancelButton = new JButton("取消");
        btnExp = new JButton("导出");
        btnImp = new JButton("导入");
 
        northJPanel.add(addButton);
        northJPanel.add(modifyButton);
        northJPanel.add(deleteButton);
        northJPanel.add(checkButton);
        northJPanel.add(saveButton);
        northJPanel.add(cancelButton);
        northJPanel.add(btnExp);
        northJPanel.add(btnImp);
 
        // leftPanel
        centerJPanel = new JPanel();
        FlowLayout fl1 = (FlowLayout) centerJPanel.getLayout();
        fl1.setAlignment(FlowLayout.LEFT);
        JScrollPane scrollPane_center = new JScrollPane();
        scrollPane_center.setViewportView(centerJPanel);
 
        scrollPane_center.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane_center.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
 
        this.add(scrollPane_center, BorderLayout.CENTER);
        centerJPanel.setLayout(new GridBagLayout());
 
        westJPanel.setBorder(new EmptyBorder(5, 5, 0, 5));
        westJPanel.setAutoscrolls(true);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
 
        // add by caill start
        centerJPanel.add(leftJPanel, gbc);
        westJPanel.setLayout(new BorderLayout(5, 5));
        // Dimension(centerJPanel.getSize().width/4,centerJPanel.getSize().height));
 
        textField = new JTextField();
        sel = new JButton("定位");
        // 搜索框的设置
        GridBagConstraints gb = new GridBagConstraints();
        gb.gridx = 0;
        gb.gridy = 0;
        gb.weightx = 0.7;
        gb.weighty = 0;
        gb.fill = GridBagConstraints.HORIZONTAL;
        textJPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        textJPanel.setLayout(new GridBagLayout());
        textJPanel.add(textField, gb);
        textJPanel.add(sel);
        // treeJPanel.add(scrollPane, BorderLayout.CENTER);
        // 将westJPanel和textJPanel容器放入到leftJPanel,并上下分隔
        leftJPanel.setLayout(new BorderLayout(5, 5));
        westJPanel.add(scrollPane, BorderLayout.CENTER);
        leftJPanel.add(textJPanel, BorderLayout.NORTH);
        leftJPanel.add(westJPanel, BorderLayout.CENTER);
        // 为定位按钮增加点击事件
        sel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // e.getSource();
                String test = textField.getText().trim();
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                model.reload();
                TreeNode root = (TreeNode) model.getRoot();
                PostorderEnumeration enumeration = new PostorderEnumeration(root);
                while (enumeration.hasMoreElements()) {
                    DefaultMutableTreeNode element = (DefaultMutableTreeNode) enumeration.nextElement();
                    if (element.getUserObject() instanceof StatePoolWrapper) {
                        StatePoolWrapper wrapper = (StatePoolWrapper) element.getUserObject();
 
                        if (wrapper != null && wrapper.getSP() != null && wrapper.getSP().name.equals(test)) {
                            TreeNode[] path = element.getPath();
                            TreePath treePath = new TreePath(path);
                            tree.setSelectionPath(treePath);
                            return;
                        }
                        ;
                    }
                }
                if (test.equals("")) {
                    JOptionPane.showMessageDialog(com.vci.client.LogonApplication.frame, "请输入状态", "提示",
                            JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(com.vci.client.LogonApplication.frame, "{ " + test + " }状态不存在", "提示",
                            JOptionPane.INFORMATION_MESSAGE);
                }
 
            }
 
        });
        // 为定位框添加事件
        textField.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e) {
 
                JTextField text = (JTextField) e.getSource();
                String test = text.getText().trim();
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                model.reload();
                TreeNode root = (TreeNode) model.getRoot();
                PostorderEnumeration enumeration = new PostorderEnumeration(root);
 
                while (enumeration.hasMoreElements()) {
                    DefaultMutableTreeNode element = (DefaultMutableTreeNode) enumeration.nextElement();
                    if (element.getUserObject() instanceof StatePoolWrapper) {
                        StatePoolWrapper wrapper = (StatePoolWrapper) element.getUserObject();
 
                        if (wrapper != null && wrapper.getSP() != null && wrapper.getSP().name.equals(test)) {
                            TreeNode[] path = element.getPath();
                            TreePath treePath = new TreePath(path);
                            tree.setSelectionPath(treePath);
                            return;
                        }
                        ;
                    }
                }
                if (test.equals("")) {
                    JOptionPane.showMessageDialog(com.vci.client.LogonApplication.frame, "请输入状态", "提示",
                            JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(com.vci.client.LogonApplication.frame, "{ " + test + " }状态不存在", "提示",
                            JOptionPane.INFORMATION_MESSAGE);
                }
 
            }
 
        });
 
        // add by caill end
 
        // mainJPanel位于右侧
        mainJPanel = new JPanel();
        // mainJPanel.setPreferredSize(new
        // Dimension(centerJPanel.getSize().width/5*4,centerJPanel.getSize().height));
        gbc.gridx = 1;
        gbc.weightx = 4.0;
        centerJPanel.add(mainJPanel, gbc);
 
        mainJPanel.setLayout(new BorderLayout(5, 5));
    }
 
    // 当树的子节点被选中的时候调用此方法显示选中节点的信息
    public StatePool poolTreeSelection() {
 
        try {
            selectButton.setEnabled(false);
            jtf1.setEditable(false);
            jtf2.setEditable(false);
            jtf3.setEditable(false);
            jta.setEditable(false);
            Document doc = null;
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (node != null && node.getUserObject() instanceof StatePoolWrapper) {
                return ((StatePoolWrapper) node.getUserObject()).getSP();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
 
    }
 
    // 新建一棵树用于显示所有的状态名称
    public void createPoolTree() {
 
        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("状态列表");
        try {
            StatePool[] statePools = StatePoolStart.getService().getStatePools();
            for (StatePool sp : statePools) {
                DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(new StatePoolWrapper(sp));
                node1.add(node2);
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
 
        tree = new JTree(node1);
        scrollPane.setViewportView(tree);
        tree.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent mouseevent) {
 
                StatePool statePool = poolTreeSelection();
                westJPanel.removeAll();
                westJPanel.add(scrollPane);
                westJPanel.updateUI();
                mainJPanel.removeAll();
                if (statePool != null) {
                    mainJPanel.add(addStatePloolPanel(statePool), BorderLayout.CENTER);
                }
 
                mainJPanel.updateUI();
            }
        });
 
        tree.setCellRenderer(new OmdTreeCellRenderer());
    }
 
    // 添加监听器这个监听器是取消按钮的,点击此按钮后界面将会被重置
    private void addListenercancel() {
        cancelButton.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showConfirmDialog(null, "您确定要执行取消操作吗?");
                buttonManager();
                buttonManager1();
                mainJPanel.removeAll();
                mainJPanel.updateUI();
            }
        });
 
    }
 
    // 保存按钮的监听器,主要任务是讲数据存入XML文件,并且重置界面
    private void addListenersave() {
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if ((!jtf1.getText().equals(""))) {
                    StatePool statePool_ = null;
                    try {
                        statePool_ = StatePoolStart.getService().getStatePool(jtf1.getText());
                    } catch (VCIError e2) {
                        e2.printStackTrace();
                    }
                    if (!statePool_.name.equals("") && !modifyFlag) {
                        JOptionPane.showMessageDialog(null, "名称重复请更换名称!");
                        return;
                    }
                    // add by caill start 2016.1.12状态池名称只能为英文字母
                    String regex = "[a-z A-Z]*";
                    if ((!jtf1.getText().matches(regex))) {
                        JOptionPane.showMessageDialog(null, "名称只能为英文!");
                        return;
                    }
                    // add by caill end
                    buttonManager();
                    try {
                        statePool = new StatePool();
                        statePool.name = jtf1.getText();
                        statePool.tag = jtf2.getText();
                        statePool.imagePath = jtf3.getText();
                        statePool.description = jta.getText();
                        String userName = InvocationUtility.getInvocation().userName;
                        statePool.modifier = userName;
                        if (!modifyFlag) {
                            statePool.creator = userName;
                            StatePoolStart.getService().addStatePool(statePool);
                        } else {
                            StatePool selectedSP = poolTreeSelection();
                            selectedSP.tag = jtf2.getText();
                            selectedSP.imagePath = jtf3.getText();
                            selectedSP.description = jta.getText();
                            selectedSP.modifier = userName;
                            nodeName = selectedSP.name;
                            StatePoolStart.getService().modifyStatePool(selectedSP);
                        }
 
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    createPoolTree();
                    westJPanel.add(scrollPane, BorderLayout.CENTER);
                    westJPanel.updateUI();
                    buttonManager1();
                    mainJPanel.removeAll();
                    mainJPanel.updateUI();
                } else {
                    if ((jtf1.getText().equals(""))) {
                        JOptionPane.showMessageDialog(null, "请检查输入的数据,将数据补充完整!");
                        return;
                    }
 
                }
            }
        });
    }
 
    // 修改按钮的监听器,主要任务是提示操作人员是否执行修改操作并且将修改后的数据存入XML文件和重置界面
    private void addListenermodify() {
        modifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                selectButton.setEnabled(true);
                jtf1.setEditable(false);
                jtf2.setEditable(true);
                jtf3.setEditable(false);
                jta.setEditable(true);
                modifyFlag = true;
                StatePool selectedSP = poolTreeSelection();
                if (selectedSP != null) {
                    JOptionPane.showConfirmDialog(null, "您确定要执行修改操作吗?");
//                    nodeName = selectedNode.toString();
                    selectButton.setToolTipText("选择");
                    selectButton.setEnabled(true);
                    jtf1.setEditable(true);
                    jtf2.setEditable(true);
                    jtf3.setEditable(false);
                    jta.setEditable(true);
                    buttonManager2();
//                    try {
//                        statePool = new StatePool();
//                        statePool.name = jtf1.getText();
//                        statePool.tag = jtf2.getText();
//                        statePool.imagePath = jtf3.getText();
//                        statePool.describe = jta.getText();
//                        statePool.id = nodeName;
//                        StatePoolStart.getService().modifyStatePool(statePool);
//                        createPoolTree();
//                        scrollPane.setViewportView(tree);
//                        westJPanel.add(scrollPane, BorderLayout.CENTER);
//                        westJPanel.updateUI();
//                        buttonManager();
//                        buttonManager1();
//                        mainJPanel.removeAll();
//                        mainJPanel.updateUI();
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
                } else {
                    buttonManager();
                    buttonManager1();
                    JOptionPane.showMessageDialog(null, "请先选中要修改的项");
                    mainJPanel.removeAll();
                    mainJPanel.updateUI();
 
                }
            }
 
        });
    }
 
    // 删除按钮的监听器,主要任务是将要删除的项在XML文件中删除并且重置界面
    private void addListenerdelete() {
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                StatePool selectedSP = poolTreeSelection();
                if (selectedSP != null) {
                    Object[] options = { "Yes", "No" };
                    int n = JOptionPane.showOptionDialog(getInstance(), "确认是否删除状态\"" + selectedSP.name + "\"",
                            "生命周期状态删除", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
                            options[1]);
                    // int n = JOptionPane.showConfirmDialog(null, "您确定要执行删除操作吗?");
                    if (n != JOptionPane.YES_OPTION) {
                        return;
                    }
                    nodeName = selectedSP.name;
 
                    try {
                        ArrayList<InterState> interVerList = InterStateManager.getInstance().getInterVerList();
                        for (Iterator i = interVerList.iterator(); i.hasNext();) {
                            InterState interState = (InterState) i.next();
                            ArrayList<String> usedNameList = interState.getUsedNameList(nodeName);
                            if (usedNameList != null && usedNameList.size() > 0) {
                                JOptionPane.showMessageDialog(null, "该状态已经被使用");
                                return;
                            }
                        }
 
                        StatePool statePool_ = StatePoolStart.getService().getStatePool(jtf1.getText());
                        StatePoolStart.getService().deleteStatePool(statePool_);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    createPoolTree();
                    scrollPane.setViewportView(tree);
                    westJPanel.add(scrollPane, BorderLayout.CENTER);
                    westJPanel.updateUI();
                    buttonManager();
                    buttonManager1();
                    mainJPanel.removeAll();
                    mainJPanel.updateUI();
                    createPoolTree();
                    westJPanel.updateUI();
                    centerJPanel.updateUI();
                } else {
                    JOptionPane.showMessageDialog(null, "请先选中要删除的项");
                }
            }
 
        });
    }
 
    public static void cleanInstance() {
        statePoolUI = null;
    }
 
    // 此方法是一个静态方法,初始化的时候判断主容器是否存在,存在则继续执行,不存在则新建容器
    public static StatePoolUI getInstance() {
        if (statePoolUI == null) {
            statePoolUI = new StatePoolUI();
        }
        return statePoolUI;
    }
 
    // 此方法是点击树节点的时候要显示的界面与新建界面略微有所不同因此不能用同一个界面
    public JPanel addStatePloolPanel(StatePool statePool) {
        StatePool selectedSP = poolTreeSelection();
        if (selectedSP != null) {
            nodeName = selectedSP.name;
        }
        jp = new JPanel();
        jp.setBackground(Color.WHITE);
        jp.setLayout(new BorderLayout(0, 0));
        jp.setBorder(BorderFactory.createTitledBorder(nodeName));
 
        setBackground(Color.WHITE);
        setLayout(new BorderLayout(0, 0));
        jp.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.anchor = GridBagConstraints.WEST;
        g.insets = new Insets(10, 5, 0, 5);
        g.gridx = 0;
        g.gridy = 0;
        jp.add(jl1, g);
        jtf1.setText(statePool.name);
        GridBagConstraints g1 = new GridBagConstraints();
        g1.anchor = GridBagConstraints.WEST;
        g1.insets = new Insets(10, 5, 0, 5);
        g1.gridx = 1;
        g1.gridy = 0;
        jtf1.setEnabled(false);
        jtf1.updateUI();
        jp.add(jtf1, g1);
 
        g.gridx = 0;
        g.gridy = 1;
        jp.add(jl2, g);
        g.gridx = 1;
        g.gridy = 1;
        jtf2.setText(statePool.tag);
        jp.add(jtf2, g);
 
        g.gridx = 0;
        g.gridy = 2;
        jp.add(jl3, g);
        g.gridx = 1;
        g.gridy = 2;
        jtf3.setText(statePool.imagePath);
        jtf3.setEnabled(false);
        jtf3.updateUI();
        jp.add(jtf3, g);
        g.gridx = 2;
        g.gridy = 2;
 
//        if(!statePool.imagePath.startsWith("http://")){
////             icon = new ImageIcon(statePool.imagePath);
//            String imagePath = statePool.imagePath;
//            if(imagePath.contains("-")){
//                icon = new ImageBundle().createImageIcon(imagePath);
//            } else {
//                icon = new BundleImage().createImageIcon(imagePath);
//            }
//             if(statePool.imagePath == "" || statePool.imagePath == null){
//                    icon = image;
//                }
//        }else {
//            URL url = null;
//            try {
//                url = new URL(statePool.imagePath);
//                icon = new ImageIcon(url);
//            } catch (MalformedURLException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
        // selectButton.setIcon(icon);
        jp.add(selectButton, g);
 
        g.gridx = 0;
        g.gridy = 3;
        jp.add(jl4, g);
        jta.setText(statePool.description);
        jta.setLineWrap(true);
        g1.gridx = 1;
        g1.gridy = 3;
        jp.add(new JScrollPane(jta), g1);
        return jp;
    }
 
    // 按钮管理,由于点击不同的按钮,按钮区各个按钮的显示状态不同,因此要对按钮状态进行设置
    public void buttonManager() {
        addButton.setEnabled(true);
        modifyButton.setEnabled(true);
        deleteButton.setEnabled(true);
        checkButton.setEnabled(true);
        tree.setEnabled(true);
    }
 
    // 按钮管理,由于点击不同的按钮,按钮区各个按钮的显示状态不同,因此要对按钮状态进行设置
    public void buttonManager1() {
        saveButton.setEnabled(false);
        cancelButton.setEnabled(false);
    }
 
    // 按钮管理,由于点击不同的按钮,按钮区各个按钮的显示状态不同,因此要对按钮状态进行设置
    public void buttonManager2() {
        addButton.setEnabled(false);
        modifyButton.setEnabled(false);
        deleteButton.setEnabled(false);
        checkButton.setEnabled(false);
        tree.setEnabled(false);
        saveButton.setEnabled(true);
        cancelButton.setEnabled(true);
    }
 
    private void addListenner() {
        /**
         * 查看使用范围
         */
        checkButton.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e) {
                StatePool selectedSP = poolTreeSelection();
                if (selectedSP == null) {
                    JOptionPane.showMessageDialog(null, "请选中要查看的状态", "无状态", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                String name = selectedSP.name;
                if (name == null || name.equals("") || name.equals("状态池")) {
                    JOptionPane.showMessageDialog(null, "请选中要查看的状态", "无状态", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                UsedToDialog usedToDialog = new UsedToDialog(name);
                usedToDialog.setVisible(true);
            }
        });
    }
 
    public List<StatePool> getSelectedSPs() {
        TreePath[] selectionPaths = tree.getSelectionPaths();
        // 未选中, 返回
        if (selectionPaths == null || selectionPaths.length == 0) {
            return null;
        }
        // 选中根节点, 返回
        if (selectionPaths.length == 1 && !(((DefaultMutableTreeNode) selectionPaths[0].getLastPathComponent())
                .getUserObject() instanceof StatePoolWrapper)) {
            return null;
        }
        List<StatePool> list = new ArrayList<StatePool>();
        for (TreePath path : selectionPaths) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            Object obj = node.getUserObject();
            if (obj instanceof StatePoolWrapper) {
                list.add(((StatePoolWrapper) obj).getSP());
            }
        }
        return list;
    }
}