jinlin
2025-03-01 86f02fee03614fef275c6e0c355d73318ca3025e
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
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.server.progressTrack.model.DjJdgzHandover;
import com.example.server.progressTrack.model.DjJdgzNetworkLevel1;
import com.example.server.progressTrack.model.DjJdgzNetworkLevel3;
import com.example.server.progressTrack.model.DjJdgzShip;
import com.example.server.progressTrack.service.DjJdgzHandoverService;
import com.example.server.progressTrack.service.DjJdgzNetworkLevel1Service;
import com.example.server.progressTrack.service.DjJdgzShipService;
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 java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
@Service
public class HandoverService {
    @Autowired
    private DjJdgzHandoverService handoverService;
    @Autowired
    private DjJdgzNetworkLevel1Service level1Service;
    @Autowired
    private HandoverAddOrUpdate addOrUpdate; // 注入 AddOrupdate 实例
 
    private List<ColumnDto> columnDto;
    private Long projectId;
 
 
    public JPanel createTable(Integer width, Integer height, JFrame jFrame) {
        JTable table;
        JPanel panel = new JPanel();
 
        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);
 
        JComboBoxItem[] projectList = level1Service.getProjectList();
        JComboBox<JComboBoxItem> comboBox = new JComboBox<>(projectList);
        JButton btnInsert = new JButton("新增");
 
        topJpanel.add(comboBox);
        topJpanel.add(btnInsert);
 
        comboBox.setPreferredSize(new Dimension(300, 28));
 
        final List<DjJdgzHandover>[] list = new List[]{handoverService.getList(projectList[0].getId())};
        projectId = projectList[0].getId();
        List<TableButton> buttonList = new ArrayList<>();
        buttonList.add(new TableButton("edit", "编辑"));
        buttonList.add(new TableButton("del", "删除"));
 
        columnDto =  new ArrayList<>();
        //columnDto.add(new ColumnDto("ID", "id", -1, null,false));
        columnDto.add(new ColumnDto("序号", "", (width - 10) / 8, "autoCreate", false, null,null));
        columnDto.add(new ColumnDto("工程", "ProjectName", (width - 10) / 8, null, false, null,null));
        columnDto.add(new ColumnDto("交方艇队", "currentTeam", (width - 10) / 8, null, false, null,null));
        columnDto.add(new ColumnDto("接方艇队", "handoverTeam", (width - 10) / 8, null, false, null,null));
        columnDto.add(new ColumnDto("交接时间", "handoverTime", (width - 10) / 8, "selectDate", false, null,null));
        columnDto.add(new ColumnDto("交接情况说明", "handoverSituation", (width - 10) / 8, null, false, null,null));
        columnDto.add(new ColumnDto("附件", "fileName", (width - 10) / 8, "", false, null,null));
        columnDto.add(new ColumnDto("操作", "", (width - 10) / 8, "", true, buttonList,null));
 
        table = CommonTable.createCommonTable(list[0], columnDto);
        table.setRowHeight(25);
        table.setAutoCreateRowSorter(true);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(final ItemEvent event) {
                JComboBoxItem selectedItemNow = (JComboBoxItem)comboBox.getSelectedItem();
                projectId = selectedItemNow.getId();
                list[0] = handoverService.getList(projectId);
                CommonTable.refreshTable(list[0], columnDto,table);
                table.setRowHeight(25);
                table.setAutoCreateRowSorter(true);
                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
                tableModelListener(table,jFrame,list[0]);
            }
        });
 
        tableModelListener(table,jFrame,list[0]);
 
        btnInsert.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DjJdgzHandover data = new DjJdgzHandover();
                addOrUpdate.openDialog(data,jFrame, columnDto,table);
                jFrame.setEnabled(false);
            }
        });
 
        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 <DjJdgzHandover> 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")){
                    DjJdgzHandover data = list.get(row);
                    addOrUpdate.openDialog(data,jFrame,columnDto,table);
                    jFrame.setEnabled(false);
                }else if(newValue.equals("del")) {
                    int n = JOptionPane.showConfirmDialog(null, "是否删除?", "提示", JOptionPane.YES_NO_OPTION);
                    if (n == 0) {
                        DefaultTableModel model = (DefaultTableModel) table.getModel();
                        DjJdgzHandover data = list.get(row);
                        handoverService.deleteLogic(data.getId());
                        list.remove(row);
                        model.removeRow(row);
                    }
                }
                System.out.println("单元格变化: 行=" + row + ", 列=" + column + ", 新值=" + newValue);
            }
        });
    }
 
 
}