826220679@qq.com
2025-08-07 4d6cd980c5c69e4d9d150669c89734642297e0cd
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package dell_system;
import targets.Company;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.*;
import java.awt.*;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.ResourceBundle;
public class CompanyManagementPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private JTable companyTable;
    private DefaultTableModel tableModel;
    private List<Company> allCompanies;
    private ResourceBundle messages;
    private JTextField searchField;
 
    @SuppressWarnings("serial")
    public CompanyManagementPanel(ResourceBundle messages) {
        this.messages = messages;
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                
     // ¶¥²¿Ãæ°å
        JPanel topPanel = new JPanel(new BorderLayout(5, 5));
        topPanel.setBorder(BorderFactory.createTitledBorder(getMessage("QKOP")));
 
        // ×ó²àËÑË÷ÊäÈë×é¼þ
        JPanel searchInputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        JLabel searchLabel = new JLabel(getMessage("COMPANY_NAME") + ":");
        searchField = new JTextField(20);
        JButton searchButton = new JButton(getMessage("SEARCH"));
        JButton resetButton = new JButton(getMessage("RESET"));
 
        searchInputPanel.add(searchLabel);
        searchInputPanel.add(searchField);
        searchInputPanel.add(searchButton);
        searchInputPanel.add(resetButton);
 
        // ÓÒ²à²Ù×÷°´Å¥
        JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
        JButton addButton = new JButton(getMessage("ADD"));
        JButton editButton = new JButton(getMessage("EDIT"));
        JButton deleteButton = new JButton(getMessage("DELETE"));
        JButton refreshButton = new JButton(getMessage("REFRESH"));
 
        actionPanel.add(addButton);
        actionPanel.add(editButton);
        actionPanel.add(deleteButton);
        actionPanel.add(refreshButton);
 
        // ½«×ó²àËÑË÷ºÍÓÒ²à²Ù×÷Ìí¼Óµ½ËÑË÷Ãæ°å
        topPanel.add(searchInputPanel, BorderLayout.CENTER);
        topPanel.add(actionPanel, BorderLayout.EAST);
 
        
        // ´´½¨±í¸ñÁÐÃû
        String[] columnNames = {
            getMessage("INDEX"),
            getMessage("NAME"),
            getMessage("LOGO"),
            getMessage("DATE")
        };
        
        // ´´½¨±í¸ñÄ£ÐÍ
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        // ´´½¨±í¸ñ
        companyTable = new JTable(tableModel);
        companyTable.setAutoCreateRowSorter(true);
        companyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        // ÉèÖÃÁпí
        companyTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        companyTable.getColumnModel().getColumn(1).setPreferredWidth(200);
        companyTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        companyTable.getColumnModel().getColumn(3).setPreferredWidth(200);
        
        // ÉèÖñíÍ·Ñùʽ
        JTableHeader header = companyTable.getTableHeader();
        header.setBackground(Color.GRAY);
        header.setForeground(Color.WHITE);
        header.setFont(header.getFont().deriveFont(Font.BOLD));
        
        // ´´½¨±íÍ·äÖȾÆ÷
        DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer() {
            {
                setHorizontalAlignment(SwingConstants.LEFT);
                setBackground(Color.GRAY);
                setForeground(Color.WHITE);
                setFont(getFont().deriveFont(Font.BOLD));
            }
        };
        
        // Ó¦ÓñíÍ·äÖȾÆ÷
        for (int i = 0; i < companyTable.getColumnCount(); i++) {
            companyTable.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer);
        }
        
        JScrollPane scrollPane = new JScrollPane(companyTable);
        scrollPane.setPreferredSize(new Dimension(800, 400));
        
        // Ìí¼Ó×é¼þ
        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
        
        // ¼ÓÔØÊý¾Ý
        loadCompanyData();
        
        // Ìí¼Óʼþ¼àÌýÆ÷
        addButton.addActionListener(e -> addNewCompany());
        editButton.addActionListener(e -> editSelectedCompany());
        deleteButton.addActionListener(e -> deleteSelectedCompanies());
        refreshButton.addActionListener(e -> loadCompanyData());
        searchButton.addActionListener(e -> searchCompanies());
        resetButton.addActionListener(e -> resetSearch());
    }
    
    private void loadCompanyData() {
        try {
            allCompanies = Dell_company.getAllCompanies();
            updateTable(allCompanies);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, getMessage("DATA_LOAD_ERROR") + ": " + ex.getMessage(), 
                                         getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
        }
    }
    
    // ËÑË÷¹«Ë¾
    private void searchCompanies() {
        String keyword = searchField.getText().trim();
        if (keyword.isEmpty()) {
            updateTable(allCompanies);
            return;
        }
        
        List<Company> filtered = new ArrayList<>();
        for (Company company : allCompanies) {
            if (company.getCompanyName().toLowerCase().contains(keyword.toLowerCase())) {
                filtered.add(company);
            }
        }
        
        if (filtered.isEmpty()) {
            JOptionPane.showMessageDialog(this, getMessage("SEARCH_NO_RESULTS"),
                                        getMessage("INFO"), JOptionPane.INFORMATION_MESSAGE);
        }
        
        updateTable(filtered);
    }
    
    // ÖØÖÃËÑË÷
    private void resetSearch() {
        searchField.setText("");
        updateTable(allCompanies);
    }
    
    private void updateTable(List<Company> companies) {
        tableModel.setRowCount(0);
        
        int index = 1;
        for (Company company : companies) {
            tableModel.addRow(new Object[]{
                index++,
                company.getCompanyName(),
                company.getCompanyLogoUrl() != null ? company.getCompanyLogoUrl() : "",
                company.getAddedDate()
            });
        }
    }
    
    private void addNewCompany() {
        CompanyEditDialog dialog = new CompanyEditDialog(
            (Frame) SwingUtilities.getWindowAncestor(this), 
            messages, 
            true, 
            null
        );
        dialog.setVisible(true);
        
        if (dialog.isConfirmed()) {
            Company newCompany = dialog.getCompany();
            
            // ÑéÖ¤ÊäÈë
            if (validateInput(newCompany)) {
                // ¼ì²é¹«Ë¾Ãû³ÆÊÇ·ñΨһ
                if (isCompanyNameExists(newCompany.getCompanyName())) {
                    JOptionPane.showMessageDialog(this, getMessage("COMPANY_NAME_EXISTS"),
                                                getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
                    return;
                }
                
                // ±£´æµ½Êý¾Ý¿â
                if (saveCompanyToDatabase(newCompany)) {
                    // ¸üÐÂÄÚ´æÊý¾Ý
                    allCompanies.add(newCompany);
                    updateTable(allCompanies);
                }
            }
        }
    }
    
    private void editSelectedCompany() {
        int selectedRow = companyTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, getMessage("SELECT_COMPANY_TO_EDIT"),
                                        getMessage("WARNING"), JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        // »ñȡѡÖеĹ«Ë¾
        int modelRow = companyTable.convertRowIndexToModel(selectedRow);
        String companyName = (String) tableModel.getValueAt(modelRow, 1);
        Company company = findCompanyByName(companyName);
        
        if (company == null) {
            JOptionPane.showMessageDialog(this, getMessage("COMPANY_NOT_FOUND"),
                                        getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        CompanyEditDialog dialog = new CompanyEditDialog(
            (Frame) SwingUtilities.getWindowAncestor(this), 
            messages, 
            false, 
            company
        );
        dialog.setVisible(true);
        
        if (dialog.isConfirmed()) {
            Company updatedCompany = dialog.getCompany();
            
            // ÑéÖ¤ÊäÈë
            if (validateInput(updatedCompany)) {
                // ¸üй«Ë¾ÐÅÏ¢
                company.setCompanyName(updatedCompany.getCompanyName());
                
                // ¸üÐÂÊý¾Ý¿â
                if (updateCompanyInDatabase(company)) {
                    // ¸üбí¸ñ
                    updateTable(allCompanies);
                }
            }
        }
    }
    
    private void deleteSelectedCompanies() {
        int[] selectedRows = companyTable.getSelectedRows();
        if (selectedRows.length == 0) {
            JOptionPane.showMessageDialog(this, getMessage("SELECT_COMPANY_TO_DELETE"),
                                        getMessage("WARNING"), JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        int confirm = JOptionPane.showConfirmDialog(this, 
            getMessage("CONFIRM_DELETE") + " " + selectedRows.length + " " + getMessage("COMPANIES") + "?",
            getMessage("CONFIRM"), JOptionPane.YES_NO_OPTION);
        
        if (confirm == JOptionPane.YES_OPTION) {
            Arrays.sort(selectedRows);
            for (int i = selectedRows.length - 1; i >= 0; i--) {
                int viewRow = selectedRows[i];
                int modelRow = companyTable.convertRowIndexToModel(viewRow);
                
                String companyName = (String) tableModel.getValueAt(modelRow, 1);
                Company company = findCompanyByName(companyName);
                
                if (company != null) {
                    // ´ÓÊý¾Ý¿âɾ³ý
                    if (deleteCompanyFromDatabase(company)) {
                        // ´ÓÄÚ´æÊý¾Ýɾ³ý
                        allCompanies.remove(company);
                    }
                }
            }
            
            // Ë¢Ð±í¸ñ
            updateTable(allCompanies);
        }
    }
    
    private boolean validateInput(Company company) {
        // ¹«Ë¾Ãû³Æ²»ÄÜΪ¿Õ
        if (company.getCompanyName() == null || company.getCompanyName().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, getMessage("COMPANY_NAME_REQUIRED"),
                                        getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        // ¹«Ë¾Ãû³Æ³¤¶ÈÑéÖ¤ (5-50×Ö·û)
        String name = company.getCompanyName().trim();
        if (name.length() < 5 || name.length() > 50) {
            JOptionPane.showMessageDialog(this, getMessage("COMPANY_NAME_LENGTH"),
                                        getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        return true;
    }
    
    private boolean isCompanyNameExists(String companyName) {
        for (Company company : allCompanies) {
            if (company.getCompanyName().equals(companyName)) {
                return true;
            }
        }
        return false;
    }
    
    private Company findCompanyByName(String companyName) {
        for (Company company : allCompanies) {
            if (company.getCompanyName().equals(companyName)) {
                return company;
            }
        }
        return null;
    }
    
    private boolean saveCompanyToDatabase(Company company) {
        try {
            Dell_company.insertCompany(company);
            JOptionPane.showMessageDialog(this, getMessage("SAVE_SUCCESS"),
                                        getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE);
            return true;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, getMessage("SAVE_FAILED") + ": " + ex.getMessage(),
                                        getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    
    private boolean updateCompanyInDatabase(Company company) {
        try {
            Dell_company.updateCompany(company);
            JOptionPane.showMessageDialog(this, getMessage("UPDATE_SUCCESS"),
                                        getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE);
            return true;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, getMessage("UPDATE_FAILED") + ": " + ex.getMessage(),
                                        getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    
    private boolean deleteCompanyFromDatabase(Company company) {
        try {
            Dell_company.deleteCompany(company.getId());
            JOptionPane.showMessageDialog(this, getMessage("DELETE_SUCCESS"),
                                        getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE);
            return true;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, getMessage("DELETE_FAILED") + ": " + ex.getMessage(),
                                        getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    
    private String getMessage(String key) {
        try {
            return messages.getString(key);
        } catch (Exception e) {
            return "[" + key + "]";
        }
    }
    
    // ¹«Ë¾±à¼­¶Ô»°¿ò
    class CompanyEditDialog extends JDialog {
        private static final long serialVersionUID = 1L;
        private JTextField nameField;
        private JTextField dateField;
        private boolean confirmed = false;
        
        public CompanyEditDialog(Frame parent, ResourceBundle messages, boolean isAddDialog, Company existingCompany) {
            super(parent, isAddDialog ? getMessage("ADD_COMPANY") : getMessage("EDIT_COMPANY"), true);
            setLayout(new BorderLayout());
            setSize(400, 150);
            setLocationRelativeTo(parent);
 
            JPanel formPanel = new JPanel(new GridLayout(2, 2, 5, 5));
            formPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
 
            // ¹«Ë¾Ãû³Æ
            JLabel nameLabel = new JLabel(getMessage("NAME") + ": *");
            nameField = new JTextField();
            formPanel.add(nameLabel);
            formPanel.add(nameField);
 
            // Ìí¼ÓÈÕÆÚ
            JLabel dateLabel = new JLabel(getMessage("DATE") + ":");
            dateField = new JTextField();
            dateField.setEditable(false);
            
            // ÉèÖõ±Ç°ÈÕÆÚ
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateField.setText(sdf.format(new Date()));
            
            formPanel.add(dateLabel);
            formPanel.add(dateField);
 
            // Èç¹ûÊDZ༭¶Ô»°¿ò£¬Ìî³äÏÖÓÐÊý¾Ý
            if (!isAddDialog && existingCompany != null) {
                nameField.setText(existingCompany.getCompanyName());
                dateField.setText(existingCompany.getAddedDate());
            }
 
            add(formPanel, BorderLayout.CENTER);
 
            // °´Å¥Ãæ°å
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            JButton okButton = new JButton(getMessage("OK"));
            JButton cancelButton = new JButton(getMessage("CANCEL"));
 
            okButton.addActionListener(e -> {
                confirmed = true;
                dispose();
            });
 
            cancelButton.addActionListener(e -> dispose());
 
            buttonPanel.add(okButton);
            buttonPanel.add(cancelButton);
            add(buttonPanel, BorderLayout.SOUTH);
        }
 
        public Company getCompany() {
            if (!confirmed) {
                return null;
            }
            Company company = new Company();
            company.setCompanyName(nameField.getText());
            company.setAddedDate(dateField.getText());
            return company;
        }
 
        public boolean isConfirmed() {
            return confirmed;
        }
    }
}