jinlin
2025-03-21 77d58298d00c11ade8862ca8acb0fdef5a45322e
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
package com.example.client.service;
 
import com.example.client.dto.ColumnDto;
import com.example.client.dto.JComboBoxItem;
import com.example.client.model.TableButton;
import com.example.client.utils.CommonTable;
import com.example.client.utils.GBC;
import com.example.server.boatFleet.service.BoatFleetService;
import com.example.server.progressTrack.model.DjJdgzHandover;
import com.example.server.progressTrack.model.DjJdgzNetworkLevel1;
import com.example.server.progressTrack.model.DjJdgzShip;
import com.example.server.progressTrack.service.DjJdgzNetworkLevel1Service;
import com.example.server.progressTrack.service.DjJdgzShipService;
import com.example.server.teamGroup.service.SysTeamGroupClassService;
import com.example.server.user.model.SysUser;
import com.example.server.user.service.UserService;
import org.jdesktop.swingx.JXDatePicker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
@Service
public class UserManageService {
    @Autowired
    private UserService userService;
    @Autowired
    private SysTeamGroupClassService sysTeamGroupClassService;
    @Autowired
    private BoatFleetService boatFleetService;
    @Autowired
    private UserAddOrUpdate addOrUpdate;
 
    private JTable table;
    private List<ColumnDto> columnDto;
 
    public JPanel createTable(Integer width, Integer height, JFrame jFrame) {
        JPanel panel = new JPanel();
        List<SysUser> list;
        JPanel topJpanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topJpanel.setPreferredSize(new Dimension(width, 37));
        topJpanel.setBackground(Color.WHITE);
        JPanel centerJpanel = new JPanel();
        centerJpanel.setPreferredSize(new Dimension(width - 20, height - 100));
        centerJpanel.setBackground(Color.WHITE);
        panel.add(topJpanel, BorderLayout.NORTH);
        panel.add(centerJpanel, BorderLayout.CENTER);
 
        JButton btnInsert = new JButton("新增");
 
        topJpanel.add(btnInsert);
 
 
        List<TableButton> buttonList = new ArrayList<>();
        buttonList.add(new TableButton("edit", "编辑"));
        buttonList.add(new TableButton("del", "删除"));
        list = userService.getList();
 
        JComboBoxItem[] teamList = sysTeamGroupClassService.getTeamList(null);
 
        JComboBoxItem[] deptList = sysTeamGroupClassService.getDeptList();
 
 
        columnDto = new ArrayList<>();
        //columnDto.add(new ColumnDto("ID", "id", -1, null,false));
        columnDto.add(new ColumnDto("序号", "", (width - 10) / 7, "autoCreate", false, null,null));
        columnDto.add(new ColumnDto("用户名", "username", (width - 10) / 7, null, false, null,null));
        columnDto.add(new ColumnDto("昵称", "nickName", (width - 10) / 7, null, false, null,null));
        columnDto.add(new ColumnDto("密码", "password", (width - 10) / 7, null, false, null,null));
        //columnDto.add(new ColumnDto("所属T队", "boatFleetId", (width - 10) / 8, "dict", true, null,boatList));
        columnDto.add(new ColumnDto("所属部门", "deptId", (width - 10) / 7, "dict", true, null,deptList));
        columnDto.add(new ColumnDto("所属专业", "teamgroupId", (width - 10) / 7, "dict", true, null,teamList));
        columnDto.add(new ColumnDto("操作", "", (width - 10) / 7, "", true, buttonList,null));
 
        table = CommonTable.createCommonTable(list, columnDto);
        table.setRowHeight(25);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
        tableModelListener(table,jFrame,list);
 
        btnInsert.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.setEnabled(false);
                SysUser data = new SysUser();
                addOrUpdate.openDialog(data,jFrame, columnDto,table);
            }
        });
 
        JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setViewportView(table);
        scrollPane.getViewport().setBackground(Color.WHITE);
        scrollPane.setPreferredSize(new Dimension(width - 20, height - 100));
        centerJpanel.add(scrollPane);
 
        return panel;
    }
 
    public void tableModelListener(JTable table,JFrame jFrame,List<SysUser> list){
        table.getModel().addTableModelListener(e -> {
            // 检查事件类型
            if (e.getType() == TableModelEvent.UPDATE) {
                // 获取变化的行和列
                int row = e.getFirstRow();
                int column = e.getColumn();
 
                // 获取新的值
                Object newValue = table.getModel().getValueAt(row, column);
                // 输出变化信息
                if (newValue.equals("edit")){
                    jFrame.setEnabled(false);
                    SysUser data = list.get(row);
                    addOrUpdate.openDialog(data,jFrame, columnDto,table);
                }else if(newValue.equals("del")) {
                    int n = JOptionPane.showConfirmDialog(null, "是否删除?", "提示", JOptionPane.YES_NO_OPTION);
                    if (n == 0) {
                        DefaultTableModel model = (DefaultTableModel) table.getModel();
                        SysUser data = list.get(row);
                        userService.deleteLogic(data.getId());
                        list.remove(row);
                        model.removeRow(row);
                    }
                }
                System.out.println("单元格变化: 行=" + row + ", 列=" + column + ", 新值=" + newValue);
            }
        });
    }
 
 
}