田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package com.vci.client.auth2.view;
 
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
 
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.JTable;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
 
import com.vci.client.common.PinyinCommon;
import com.vci.client.portal.NewNewUI.actionmng.ClientPLActionCls;
import com.vci.client.portal.utility.UITools;
import com.vci.corba.common.VCIError;
import com.vci.corba.portal.data.Constraint;
import com.vci.corba.portal.data.PLAction;
import com.vci.corba.portal.data.PLActionCls;
import com.vci.mw.ClientContextVariable;
 
public class PLActionSelectionDialog extends JDialog implements ActionListener{
 
    private static final long serialVersionUID = 1L;
    
    private static final String ADD_COMMAND = "add";
    private static final String DEL_COMMAND = "del";
    
    private MyTableModel tableModel;
    private JTable table;
    private JSplitPane contentPanel;
    private boolean isOK = false;
 
    private ArrayList<PLAction> selActions = null;
 
    private JTree clsTree;
    /**
     * 当前选择的分类节点
     * 当选择根节点时为null
     */
    private PLActionCls currentCls = null;
    
    public PLActionSelectionDialog(){
        super(ClientContextVariable.getFrame(), "action选择", true);
        this.initUI();
        setSize(900, 600);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    private void initUI(){
        this.initContentPanel();
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        this.refreshTableData();
    }
    /**
     * 设置table查询条件
     * @return
     */
    private Constraint[] getFilterConstraints(){
        Constraint[] consArray = new Constraint[1];
        if(this.currentCls == null) {
            consArray = new Constraint[0];
        }
        if(this.currentCls != null) {
            if(this.currentCls.name.equals("未分类")) {
                consArray[0] = new Constraint("plactioncls", "");
            } else {
                consArray[0] = new Constraint("plactioncls", this.currentCls.id);
            }
        }
        return consArray;
    }
    private void refreshTableData() {
        try {
            PLAction[] allPLActions = UITools.getService().getPLActionsByConsArray(getFilterConstraints());
            Arrays.sort(allPLActions, new Comparator<PLAction>(){
                @Override
                public int compare(PLAction o1, PLAction o2) {
                    String py1 = PinyinCommon.getPingYin(o1.plName);
                    String py2 = PinyinCommon.getPingYin(o2.plName);
                    return py1.compareTo(py2);
                }});
            List<PLAction> allActionList = Arrays.asList(allPLActions);
            tableModel.setData(allActionList);
        } catch (VCIError e) {
            e.printStackTrace();
        }
    }
    private void initContentPanel() {
        JPanel treePanel = this.getTreePanel();
        //刷新分类树
        refreshClsTree(clsTree.getPathForRow(0));
        JPanel rightPanel = this.getRigthPanel();
        contentPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        contentPanel.setDividerSize(2);
        //contentPanel.setDividerLocation(0.7);
        contentPanel.add(treePanel, JSplitPane.LEFT);
        contentPanel.add(rightPanel, JSplitPane.RIGHT);
    }
    private void refreshClsTree(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("", "未分类", "", "", "", System.currentTimeMillis(), (short)0);
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(
                    new ClientPLActionCls(plac));
            dtml.insertNodeInto(child, node, node.getChildCount());
        }
        dtml.reload(node);
    }
    /**
     * 树节点选择事件
     * @param e
     */
    private void clsTreeSelectionListener(TreeSelectionEvent e) {
        TreePath tp = e.getPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tp.getLastPathComponent();
        Object userObject = node.getUserObject();
        if(userObject instanceof String) {
            this.currentCls = null;
        } else {
            ClientPLActionCls pac = (ClientPLActionCls) userObject;
            this.currentCls = pac.getPac();
        }
        refreshTableData();
    }
    /**
     * 得到Action分类树面板
     * @return
     */
    private JPanel getTreePanel() {
        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);
        clsTree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                clsTreeSelectionListener(e);
            }
        });
        
        //添加树
        panel.add(new JScrollPane(clsTree), BorderLayout.CENTER);
        return panel;
    }
    private JPanel getRigthPanel() {
        JPanel btnPanel = this.getBtnPanel();
        tableModel = new MyTableModel();
        table = new JTable(tableModel);
        GridBagLayout g = new GridBagLayout();
        g.rowHeights = new int[]{0, 0};
        g.columnWidths = new int[]{0};
        g.rowWeights = new double[]{Double.MIN_VALUE, 0.0};
        g.columnWeights = new double[]{Double.MIN_VALUE};
        JPanel rightPanel = new JPanel(g);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.gridx = 0;
        gbc.gridy = 0;
        rightPanel.add(new JScrollPane(table), gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        rightPanel.add(btnPanel, gbc);
        return rightPanel;
    }
    private JPanel getBtnPanel() {
        JPanel panel = new JPanel();
        JButton addBtn = new JButton("保存");
        addBtn.setActionCommand(ADD_COMMAND);
        addBtn.addActionListener(this);
        JButton delBtn = new JButton("取消");
        delBtn.setActionCommand(DEL_COMMAND);
        delBtn.addActionListener(this);
        panel.add(addBtn);
        panel.add(delBtn);
        return panel;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if(DEL_COMMAND.equals(command)){
            selActions = null;
            this.setVisible(false);
        }else if(ADD_COMMAND.equals(command)){
            int[] selRows = table.getSelectedRows();
            selActions  = new ArrayList<PLAction>();
            for(int i : selRows){
                PLAction selAction = tableModel.getRowAt(i);
                selActions.add(selAction);
            }
            isOK = true;
            this.setVisible(false);
        }
    }
    public boolean isOK(){
        return isOK;
    }
    public List<PLAction> getResult(){
        return selActions;
    }
    
    private class MyTableModel extends AbstractTableModel{
 
        private static final long serialVersionUID = 1L;
 
        private String[] columns = new String[]{"序号", "编号", "名称", "类路径", "链接地址", "类型", "描述"};
        private List<PLAction> data;
 
        public List<PLAction> getData(){
            return data;
        }
        public MyTableModel() {
            this.data = new ArrayList<PLAction>();
        }
        public void setData(List<PLAction> data){
            this.data = data;
            fireTableDataChanged();
        }
        public void addRow(PLAction dataRule){
            this.data.add(dataRule);
            fireTableStructureChanged();
        }
        public PLAction getRowAt(int rowIndex){
            if(rowIndex >= 0 && rowIndex<data.size()){
                return data.get(rowIndex);
            }
            return null;
        }
        public void deleteRow(int[] selectedRows){
            for (int i = selectedRows.length - 1; i >= 0; --i) {
                        this.data.remove(selectedRows[i]);
            }
            fireTableDataChanged();
        }
        
        @Override
        public String getColumnName(int column) {
            return columns[column];
        }
        @Override
        public int getRowCount() {
            if(data == null){
                return 0;
            }
            return data.size();
        }
 
        @Override
        public int getColumnCount() {
            return this.columns.length;
        }
 
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if(this.data == null){
                return null;
            }
            PLAction plAction = this.data.get(rowIndex);
            if(plAction == null){
                return null;
            }
            switch(columnIndex){
                case 0: return rowIndex + 1;
                case 1: return plAction.plCode;
                case 2: return plAction.plName;
                case 3: return plAction.plCSClass;
                case 4: return plAction.plBSUrl;
                case 5: {
                    String plTypeType = plAction.plTypeType;
                    if (plTypeType.equals("business")) {
                        return "业务类型";
                    }else if (plTypeType.equals("link")) {
                        return "链接类型";
                    }
                }
                case 6: return plAction.plDesc;
            }
            return null;
        }
        
//        @Override
//        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//            PLAction plAction = this.data.get(rowIndex);
//            String value = (String)aValue;
//            switch(columnIndex){
//                case 0: break;
//                case 1: plAction.setName(value);break;
//                case 2: plAction.setDesc(value);break;
//                case 3: plAction.setDataName(value);break;
//                case 4: plAction.setDataType(value);break;
//            }
//        }
    }
}