wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
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
package com.vci.client.portal.NewNewUI.actionmng;
 
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
 
import com.vci.client.portal.utility.UITools;
import com.vci.corba.common.VCIError;
import com.vci.corba.portal.data.PLActionCls;
import com.vci.mw.ClientContextVariable;
 
public class PLActionClsSelectDialog extends JDialog {
 
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -5667907302621398073L;
    /**
     * 分类树
     */
    private JTree clsTree;
    /**
     * 选择的分类
     */
    private PLActionCls currentCls;
    /**
     * 确定还是取消
     */
    private boolean ok = false;
    
    /**
     * 构造器
     * @param dialog
     */
    public PLActionClsSelectDialog(JDialog dialog) {
        super(dialog, "选择分类", true);
        init();
        this.setLocationRelativeTo(dialog);
        this.setVisible(true);
    }
    
    /**
     * 绘制
     */
    private void init() {
        JPanel panel = new JPanel(new BorderLayout());
        //初始化树
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Action分类");
        DefaultTreeModel dtml = new DefaultTreeModel(root);
        clsTree = new JTree(dtml);
        clsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        refreshClsTree(clsTree, clsTree.getPathForRow(0));
        //添加树
        JScrollPane scrollPane = new JScrollPane(clsTree);
        panel.add(scrollPane, BorderLayout.CENTER);
        
        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        //创建分类按钮
        JButton createBtn = new JButton("确定");
        createBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                okBtnAction();
            }
        });
        //修改分类按钮
        JButton editBtn = new JButton("取消");
        editBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cancelBtnAction();
            }
        });
        btnPanel.add(createBtn);
        btnPanel.add(editBtn);
        
        this.setLayout(new BorderLayout());
        this.add(panel, BorderLayout.CENTER);
        this.add(btnPanel, BorderLayout.SOUTH);
        this.setSize(600, 400);
    }
    
    /**
     * 确定事件
     */
    private void okBtnAction() {
        TreePath tp = clsTree.getSelectionPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tp.getLastPathComponent();
        Object userObject = node.getUserObject();
        if(userObject instanceof String) {
            JOptionPane.showMessageDialog(
                    this, "不能选择根节点!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        } else {
            ClientPLActionCls pac = (ClientPLActionCls) userObject;
            this.currentCls = pac.getPac();
        }
        this.ok = true;
        this.dispose();
    }
    
    /**
     * 取消
     */
    private void cancelBtnAction() {
        this.currentCls = null;
        this.ok = false;
        this.dispose();
    }
    
    
    private void refreshClsTree(JTree clsTree, TreePath tp) {
        try {
            //分类较少,直接查询所有分类
            PLActionCls[] clses = UITools.getService().getPLActionClsArray();
            
            DefaultTreeModel dtml = (DefaultTreeModel) clsTree.getModel();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) 
                    tp.getLastPathComponent();
            addClsTreeNode(dtml, node, clses);
        } catch (VCIError e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(ClientContextVariable.getFrame(), 
                    "刷新分类树异常:" + e1.getMessage(), "系统异常", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * 添加分类树节点
     * @param dtml 树模型
     * @param node 节点
     * @param clses 分类 
     */
    private void addClsTreeNode(DefaultTreeModel dtml, 
            DefaultMutableTreeNode node, PLActionCls[] clses) {
        node.removeAllChildren();
        String pid = "";
        Object userObject = node.getUserObject();
        if(!(userObject instanceof String)) {
            PLActionCls pac = ((ClientPLActionCls) userObject).getPac();
            pid = pac.id;
        }
        //添加子节点
        for(PLActionCls plac : clses) {
            if(plac.pid.equals(pid)) {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(
                        new ClientPLActionCls(plac));
                dtml.insertNodeInto(child, node, node.getChildCount());
                addClsTreeNode(dtml, child, clses);
            }
        }
        if(pid.equals("")) {
            PLActionCls plac = new PLActionCls("", "未分类", "", "", "", 0, (short)0);
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(
                    new ClientPLActionCls(plac));
            dtml.insertNodeInto(child, node, node.getChildCount());
        }
        dtml.reload(node);
    }
 
    public PLActionCls getCurrentCls() {
        return currentCls;
    }
 
    public boolean isOk() {
        return ok;
    }
}