wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
package com.vci.client.omd.attribpool.ui;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
 
import com.vci.client.omd.attribpool.toOutside.InterAP;
import com.vci.client.omd.attribpool.toOutside.InterAPManager;
import com.vci.client.omd.attribpool.wrapper.AttribItemWrapper;
import com.vci.client.ui.table.CheckBoxEditor;
import com.vci.client.ui.table.CheckBoxRenderer;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.atm.AttribItem;
 
/**
 * 属性池界面
 * @author Administrator
 *
 */
public class AttribPoolPanel extends JPanel{
 
    /**
     * 
     */
    private static final long serialVersionUID = -9000684015694561133L;
    private static AttribPoolPanel attribPoolPanel = null;
    private JButton btnCreate, btnModify, btnDelete, btnUseTo;
    private JTextField tfFilter;
    private ApTableModel tableModel;
    private JXTable apTable;
    private JPanel northPanel, centerPanel, tablePanel, mainPanel;
    private JScrollPane scrollPane;
    private AttribItem[] abItemArray;
    private final int TABLE_HEADER_HEIGHT = 25;
    private final int ROW_HEIGHT = 30;
    //0:显示; 1: 创建; 2: 修改
    //保存当前状态
    private int flag;
    private JButton btnExp;
    private JButton btnImp;
    
    public int getFlag() {
        return flag;
    }
 
    public void setFlag(int flag) {
        this.flag = flag;
    }
 
    private AttribPoolPanel(){
        initUI();
        initTableData();
        addListener();
    }
    
    public static void cleanInstance(){
        attribPoolPanel = null;    
    }
    
    public static AttribPoolPanel  getInstance(){
        if(attribPoolPanel == null){
            attribPoolPanel = new AttribPoolPanel();
        }
        return attribPoolPanel;
    }
 
    
    /**
     * 初始化界面
     */
    private void initUI(){
        setBackground(Color.WHITE);
        setLayout(new BorderLayout(0, 0));
        
        //northPanel
        northPanel = new JPanel();
        //FlowLayout是panel的默认布局管理器
        FlowLayout flowLayout = (FlowLayout) northPanel.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        this.add(northPanel, BorderLayout.NORTH);
        
        btnCreate = new JButton("创建");
        btnModify = new JButton("修改");
        btnDelete = new JButton("删除");
        btnUseTo = new JButton("查看应用范围");
        btnExp = new JButton("导出");
        btnImp = new JButton("导入");
        tfFilter = new JTextField(20);
        northPanel.add(btnCreate);
        northPanel.add(btnModify);
        northPanel.add(btnDelete);
        northPanel.add(btnUseTo);
        northPanel.add(btnExp);
        northPanel.add(btnImp);
        northPanel.add(new JLabel("属性名"));
        northPanel.add(tfFilter);
        
        
        //centerPanel
        centerPanel = new JPanel();
        
        this.add(centerPanel, BorderLayout.CENTER);
        centerPanel.setLayout(new GridBagLayout());
        GridBagConstraints gb = new GridBagConstraints();
        gb.gridx = 0;
        gb.gridy = 0;
        gb.weightx = 1.0;
        gb.weighty = 1.0;
        gb.fill = GridBagConstraints.BOTH;
        
        //tablePanel位于左侧,显示属性项列表
        tablePanel = new JPanel();
        //占用空间的透明边框(间距)
        tablePanel.setBorder(new EmptyBorder(5, 5, 0, 5));
        tablePanel.setAutoscrolls(true);
        
        tablePanel.setLayout(new BorderLayout(5, 5));
        
        scrollPane = new JScrollPane();
        tablePanel.add(scrollPane, BorderLayout.CENTER);
        
        tableModel = new ApTableModel();
        apTable = new JXTable(tableModel);
        TableColumn column = apTable.getColumn(0);
        column.setCellEditor(new CheckBoxEditor(new JCheckBox()));
        column.setCellRenderer(new CheckBoxRenderer());
        column.setMinWidth(30);
        column.setMaxWidth(30);
        column.setPreferredWidth(30);
        
        apTable.setRowHeight(ROW_HEIGHT);
        apTable.setHorizontalScrollEnabled(true);
        apTable.setHighlighters(HighlighterFactory.createAlternateStriping());
        //排序后,引起其他页面的数据不显示
        apTable.setSortable(false);
        //设置表头高度
        JTableHeader tableHeader = apTable.getTableHeader();
        Dimension size = tableHeader.getPreferredSize();
        size.height = TABLE_HEADER_HEIGHT;
        tableHeader.setPreferredSize(size);
        scrollPane.setViewportView(apTable);
        
        //mainPanel位于右侧,用于响应创建,修改,查看应用范围
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(0, 0));
        setMainPanel(0);
        
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tablePanel, mainPanel);
        splitPane.setDividerLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2);
        splitPane.setOneTouchExpandable(true);
        centerPanel.add(splitPane, gb);
    }
    
    /**
     * 设置apTable的数据
     */
    public void initTableData(){
        try {
            apTable.removeAll();
            String filter = tfFilter.getText();
            abItemArray = APClient.getService().getAttribItems(filter, 1, 1);
            tableModel.setRowCount(abItemArray.length);
            //重新设置table的Cell的可编辑性
            tableModel.setInitFlag(false);
            for(int i = 0; i < abItemArray.length; i++){
                AttribItem abItem = abItemArray[i];
                apTable.setValueAt(new JCheckBox(), i, 0);
                apTable.setValueAt(new AttribItemWrapper(abItem), i, 1);
                apTable.setValueAt(abItem.vtDataType, i, 2);
                apTable.setValueAt(abItem.defValue, i, 3);
                apTable.setValueAt(abItem.description, i, 4);
            }
            //重新设置table的Cell的可编辑性
            tableModel.setInitFlag(true);
            apTable.updateUI();
        } catch (VCIError e) {
            e.printStackTrace();
        }
    }
    private void addListener(){
        btnCreate.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                flag = 1;
                AttribPanel.getInstance().updateDataAndUI(null, flag);
            }
        });
        
        btnModify.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                flag = 2;
                int row = apTable.getSelectedRow();
                if(row >= 0){
                    AttribItem abItem = ((AttribItemWrapper)apTable.getValueAt(row, 1)).abItem;
                    AttribPanel.getInstance().updateDataAndUI(abItem, flag);
                }else{
                    JOptionPane.showMessageDialog(null, "请选中数据", "无数据", JOptionPane.WARNING_MESSAGE);
                }
                
            }
        });
        
        btnDelete.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteAbItems();
            }
        });
        
        //查看应用范围
        btnUseTo.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = apTable.getSelectedRow();
                AttribItem abItem;
                if(row >= 0){
                    abItem = ((AttribItemWrapper)apTable.getValueAt(row, 1)).abItem;
                    UsedToDialog usedToDialog = new UsedToDialog(abItem);
                    usedToDialog.setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(null, "请选中数据", "无数据", JOptionPane.WARNING_MESSAGE);
                    return;
                }
            }
        });
        //过滤器
        tfFilter.getDocument().addDocumentListener(new DocumentListener() {
            
            @Override
            public void removeUpdate(DocumentEvent e) {
                initTableData();
            }
            
            @Override
            public void insertUpdate(DocumentEvent e) {
        
                initTableData();
            }
            
            @Override
            public void changedUpdate(DocumentEvent e) {
                initTableData();
            }
        });
        apTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(flag == 1){
                    return;
                }
                int row = apTable.getSelectedRow();
                if(row >= 0){
                    AttribItem abItem = ((AttribItemWrapper)apTable.getValueAt(row, 1)).abItem;
                    AttribPanel.getInstance().updateDataAndUI(abItem, flag);
                }else{
                }
            }
        });
        
        btnExp.addActionListener(new ExpActionListener());
        btnImp.addActionListener(new ImpActionListener());
    }
    
    /**
     * flag 0:显示; 1: 创建; 2: 修改
     * @param flag
     */
    private void setMainPanel(int flag){
        if(mainPanel == null){
            return;
        }
        this.flag = flag;
        AttribPanel attPanel = AttribPanel.getInstance();
        attPanel.updateDataAndUI(null, this.flag);
        attPanel.addCancelActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                attribPoolPanel.flag = 0;
            }
        });
        
        mainPanel.add(attPanel, BorderLayout.CENTER);
    }
    
    /**
     * 删除checkBox选中的属性项,
     * 没有选中项时,提示
     */
    private void deleteAbItems(){
        ArrayList<AttribItem> abItemList = new ArrayList<AttribItem>();
        for(int i = 0; i < apTable.getRowCount(); i++){
            if(((JCheckBox)apTable.getValueAt(i, 0)).isSelected()){
                abItemList.add(((AttribItemWrapper)apTable.getValueAt(i, 1)).abItem);
            }
        }
        
        if(abItemList.size() == 0){
            JOptionPane.showMessageDialog(this, "请选中要删除的属性项", "未选属性", JOptionPane.WARNING_MESSAGE);
            return;
        }
        /*Object[] options = { "Yes", "No" };
        int option = JOptionPane
                .showOptionDialog(getInstance(), "确认是否删除业务类型\""
                        + abName + "\"", "业务类型删除",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, options,
                        options[1]);
//        int option = JOptionPane.showConfirmDialog(this, "是否确认删除", "删除确认", JOptionPane.YES_NO_CANCEL_OPTION);
        if(option != JOptionPane.YES_OPTION){
            return;
        }*/
        boolean usedFlag = false;
        for(int i = 0; i < abItemList.size(); i++){
            AttribItem abItem = abItemList.get(i);
            String abName = abItem.name;
            Object[] options = { "Yes", "No" };
            int option = JOptionPane
                    .showOptionDialog(getInstance(), "确认是否删除属性\""
                            + abName + "\"", "属性池属性删除",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE, null, options,
                            options[1]);
//            int option = JOptionPane.showConfirmDialog(this, "是否确认删除", "删除确认", JOptionPane.YES_NO_CANCEL_OPTION);
            if(option != JOptionPane.YES_OPTION){
                return;
            }
            if(isUsedByBtmOrLt(abName)){
                usedFlag = true;
                abItemList.remove(abItem);
            }
        }
        if(usedFlag){
            JOptionPane.showMessageDialog(this, "存在被使用的属性, 本次操作仅删除未被使用的属性", "存在被使用的属性", JOptionPane.WARNING_MESSAGE);
        }
        try {
            boolean isSuccess = APClient.getService().deleteAbItems(abItemList.toArray(new AttribItem[0]));
            if(isSuccess){
                JOptionPane.showMessageDialog(this, "删除属性项成功", "删除成功", JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this, "删除属性项失败", "删除失败", JOptionPane.WARNING_MESSAGE);
            }
            initTableData();
            AttribPanel.getInstance().updateDataAndUI(getSelectedAb(), 0);
        } catch (VCIError e) {
            JOptionPane.showMessageDialog(this, "删除属性项失败", "删除失败", JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
        }
    }
    
    /**
     * 检查属性是否被业务类型, 或链接类型使用
     * @param abName
     * @return
     */
    private boolean isUsedByBtmOrLt(String abName){
        ArrayList<InterAP> interAPList = InterAPManager.getInstance().getInterAPList();
        
        for(Iterator<InterAP> i = interAPList.iterator(); i.hasNext();){
            InterAP interAp = i.next();
            ArrayList<String> usedNameList = interAp.getUsedNameList(abName);
            if(usedNameList.size() > 0){
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取AbTable中选择的属性
     * @return
     */
    public AttribItem getSelectedAb(){
        AttribItem ab = null;
        int row = apTable.getSelectedRow();
        if(row >= 0){
            ab = ((AttribItemWrapper)apTable.getValueAt(row, 1)).abItem;
        }
        return ab;
    }
 
    /**
     * 获取AbTable中选择的属性
     * @return
     */
    public AttribItem[] getSelectedAbs(){
        List<AttribItem> abs = new ArrayList<AttribItem>();
        int[] rows = apTable.getSelectedRows();
        for(int row : rows){
            if(row >= 0){
                AttribItem ab = ((AttribItemWrapper)apTable.getValueAt(row, 1)).abItem;
                abs.add(ab);
            }
        }
        return abs.toArray(new AttribItem[0]);
    }
}