jar
jinlin
2025-03-04 23f02e6b45dd7cf0ab2e7827144913ca59575ea4
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
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.progressTrack.model.DjJdgzNetworkLevel1;
import com.example.server.progressTrack.model.DjJdgzShip;
import com.example.server.progressTrack.service.*;
import org.apache.commons.lang3.StringUtils;
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.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
 
 
@Service
public class Level1ManageService {
    @Autowired
    private DjJdgzNetworkLevel1Service level1Service;
    @Autowired
    private DjJdgzShipService djJdgzShipService;
    @Autowired
    private Level1AddOrUpdate addOrUpdate; // 注入 AddOrupdate 实例
 
    private JTable table;
    private List<DjJdgzNetworkLevel1> list;
    private Map<String, Long> shipMap = new HashMap<>();
    private List<ColumnDto> columnDto;
 
    public JPanel createTable(Integer width, Integer height, JFrame jFrame) {
        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);
 
        List<DjJdgzShip> shipList = djJdgzShipService.getList();
        JComboBox comboBox = new JComboBox();
        JButton btnInsert = new JButton("新增");
 
        topJpanel.add(comboBox);
        topJpanel.add(btnInsert);
 
        comboBox.setPreferredSize(new Dimension(300, 28));
 
        list = level1Service.getList(shipList.get(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) / 4, "autoCreate", false, null,null));
        columnDto.add(new ColumnDto("工程", "ProjectName", (width - 10) / 4, null, false, null,null));
        columnDto.add(new ColumnDto("起始时间", "StartDate", (width - 10) / 4, "selectDate", false, null,null));
        columnDto.add(new ColumnDto("操作", "", (width - 10) / 4, "", true, buttonList,null));
 
        table = CommonTable.createCommonTable(list, columnDto);
        table.setRowHeight(25);
        table.setAutoCreateRowSorter(true);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
        final Boolean[] isFirstLoadData = {true};
        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(final ItemEvent event) {
                String content = comboBox.getSelectedItem().toString();
                Long shipId = shipMap.get(content);
                list = level1Service.getList(shipId);
                if (!isFirstLoadData[0]) {
                    CommonTable.refreshTable(list, columnDto,table);
                    table.setRowHeight(25);
                    table.setAutoCreateRowSorter(true);
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
                    tableModelListener(table,jFrame);
                }else{
                    isFirstLoadData[0] = false;
                }
            }
        });
 
        for (int i = 0; i < shipList.size(); i++) {
            shipMap.put(shipList.get(i).getShipNo(), shipList.get(i).getId());
            comboBox.addItem(shipList.get(i).getShipNo());
         }
        comboBox.setSelectedItem(shipList.get(0).getShipNo());
 
        tableModelListener(table,jFrame);
 
        btnInsert.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.setEnabled(false);
                insert(jFrame);
            }
        });
 
        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;
    }
 
    private void insert(JFrame jFrame) {
        JFrame frame1 = new JFrame("新增一级网络图");
        frame1.setSize(350, 400);
        frame1.setResizable(true);
        frame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame1.setLocationRelativeTo(null);
        frame1.setVisible(true);
        frame1.addWindowListener(new WindowAdapter() {
            //添加第二个界面的关闭事件:
            public void windowClosing(WindowEvent e) {
                //添加事件:
                jFrame.setEnabled(true);//将主界面再设置为可操作的
            }
        });
        GridBagLayout layout = new GridBagLayout();
        frame1.setLayout(layout);
        JComboBoxItem[] shipList = djJdgzShipService.getListByItem();
        JComboBoxItem[] levelList = {
                new JComboBoxItem(1, "一级"),
                new JComboBoxItem(2, "二级"),
                new JComboBoxItem(3, "三级"),
        };
 
        JLabel JLabel1 = new JLabel("玄号");
        JComboBox<JComboBoxItem> comboBox0 = new JComboBox<>(shipList);
        comboBox0.setPreferredSize(new Dimension(185, 28));
 
        JLabel JLabel2 = new JLabel("修理等级");
        JComboBox<JComboBoxItem> comboBox1 = new JComboBox<>(levelList);
        comboBox1.setPreferredSize(new Dimension(185, 28));
 
 
        JLabel JLabel3 = new JLabel("年份");
        JSpinner yearPicker = new JSpinner(new SpinnerNumberModel(2024, 1900, 2100, 1)); // 当前年份为2024,范围为1900-2100,步长为1
        yearPicker.setPreferredSize(new Dimension(185, 28));
        Font font = new Font("宋体", Font.BOLD, 23);
        yearPicker.setFont(font);
        JSpinner.NumberEditor editor1 = new JSpinner.NumberEditor(yearPicker, "#");
        JTextField textField = editor1.getTextField();
        textField.setHorizontalAlignment(JTextField.LEFT); // 文字居右
        textField.setFont(font); // 设置字体
        yearPicker.setEditor(editor1);
 
 
        JLabel JLabel4 = new JLabel("起始时间");
        JXDatePicker beginDate = new JXDatePicker();
        beginDate.setPreferredSize(new Dimension(185, 28));
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
        beginDate.setFormats(sdFormat);
        beginDate.setDate(null);
        beginDate.addActionListener(e -> {
            // 停止单元格编辑,关闭弹出窗口
            TableCellEditor editor = table.getCellEditor();
            if (editor != null) {
                editor.stopCellEditing();
            }
        });
 
        JButton btnSave = new JButton("保存");
 
        frame1.add(JLabel1, new GBC(0, 0,1,1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        frame1.add(comboBox0, new GBC(1, 0, 1, 1).setAnchor(GBC.SOUTHWEST).setInsets(5));
 
        frame1.add(JLabel2, new GBC(0, 1, 1, 1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        frame1.add(comboBox1, new GBC(1, 1, 1, 1).setAnchor(GBC.SOUTHWEST).setInsets(5));
 
        frame1.add(JLabel3, new GBC(0, 2,1,1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        frame1.add(yearPicker, new GBC(1, 2, 1, 1).setAnchor(GBC.SOUTHWEST).setInsets(5));
 
        frame1.add(JLabel4, new GBC(0, 3,1,1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        frame1.add(beginDate, new GBC(1, 3, 1, 1).setAnchor(GBC.NORTHWEST).setInsets(5));
 
        frame1.add(btnSave, new GBC(0, 4, 2, 1).setWeight(1, 0));
 
        btnSave.addActionListener((e) -> {
            JComboBoxItem ship = (JComboBoxItem) comboBox0.getSelectedItem();
            JComboBoxItem level = (JComboBoxItem) comboBox1.getSelectedItem();
            DjJdgzNetworkLevel1 data = new DjJdgzNetworkLevel1();
            data.setShipId(ship.getId());
            data.setMaintainLevel(Math.toIntExact(level.getId()));
            data.setStartDate(sdFormat.format(beginDate.getDate()));
            data.setYear((Integer) yearPicker.getValue());
            data.setAdventDay(7);
            data.setProjectName(ship.getName()+"-"+yearPicker.getValue() + "-" + level.getName());
            level1Service.insert(data);
            list = level1Service.getList(ship.getId());
            CommonTable.refreshTable(list,columnDto,table);
            table.setRowHeight(25);
            table.setAutoCreateRowSorter(true);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            tableModelListener(table,jFrame);
            frame1.dispose();
            jFrame.setEnabled(true);//将主界面再设置为可操作的
        });
    }
 
    public void tableModelListener(JTable table,JFrame jFrame){
        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")){
                    DjJdgzNetworkLevel1 data = list.get(row);
                    addOrUpdate.openDialog(data,jFrame);
                    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();
                        DjJdgzNetworkLevel1 data = list.get(row);
                        level1Service.deleteLevel1(data.getId());
                        list.remove(row);
                        model.removeRow(row);
                    }
                }
                System.out.println("单元格变化: 行=" + row + ", 列=" + column + ", 新值=" + newValue);
            }
        });
    }
 
 
}