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
package dell_Fence;
import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;
import dell_Fence.Dell_FenceType;
import dell_system.Dell_department;
import dell_suanfa.Dell_LayerManagement;
import dell_system.Dell_company;
import targets.Fence;
 
@SuppressWarnings("serial")
public class EditFenceDialog extends JDialog {
    private boolean saved = false;
    private Fence fenceData;
    private JTextField idField;
    private JTextField nameField;
    private JComboBox<String> typeComboBox;
    private JComboBox<String> deptComboBox;
    private JComboBox<String> layerComboBox;
    private JTextField startTimeField;
    private JTextField endTimeField;
    private JComboBox<String> colorComboBox;
    private JCheckBox enabledCheckBox;
    private JComboBox<String> companyComboBox;
 
    public EditFenceDialog(JFrame parent, String title, boolean modal, Fence fenceData, ResourceBundle messages) {
        super(parent, title, modal);
        this.fenceData = fenceData;
        initDialog();
    }
 
    private void initDialog() {
        setSize(600, 400);
        setLocationRelativeTo(getOwner());
        
        JPanel mainPanel = new JPanel(new GridLayout(11, 2, 5, 5));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // ÐòºÅ
        mainPanel.add(new JLabel("ÐòºÅ:"));
        idField = new JTextField(fenceData.getId());
        idField.setEditable(false);
        mainPanel.add(idField);
        
        // Î§À¸Ãû³Æ
        mainPanel.add(new JLabel("ΧÀ¸Ãû³Æ:"));
        nameField = new JTextField(fenceData.getFenceName());
        nameField.setEditable(false);
        mainPanel.add(nameField);
        
        // Î§À¸ÀàÐÍ
        mainPanel.add(new JLabel("ΧÀ¸ÀàÐÍ:"));
        typeComboBox = new JComboBox<>();
        try {
            String[] types = Dell_FenceType.getAllFenceTypeNames();
            for (String type : types) {
                typeComboBox.addItem(type);
            }
            typeComboBox.setSelectedItem(Dell_FenceType.getFenceTypeById(Integer.parseInt(fenceData.getFenceType())).getTypeName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mainPanel.add(typeComboBox);
        
        // ¹ØÁª²¿ÃÅ
        mainPanel.add(new JLabel("¹ØÁª²¿ÃÅ:"));
        deptComboBox = new JComboBox<>();
        String[] depts = Dell_department.getAllDepartmentNames();
        for (String dept : depts) {
            deptComboBox.addItem(dept);
        }
        deptComboBox.setSelectedItem(fenceData.getRelatedDepartment());
        mainPanel.add(deptComboBox);
        
        // ËùÊô²ã
        mainPanel.add(new JLabel("ËùÊô²ã:"));
        layerComboBox = new JComboBox<>();
        String[] layers = Dell_LayerManagement.getAllLayerNumbers();
        for (String layer : layers) {
            layerComboBox.addItem(layer);
        }
        layerComboBox.setSelectedItem(fenceData.getLayer());
        mainPanel.add(layerComboBox);
        
        // ÉúЧʱ¼ä
        mainPanel.add(new JLabel("ÉúЧʱ¼ä:"));
        startTimeField = new JTextField(fenceData.getEffectiveTime());
        mainPanel.add(startTimeField);
        
        // Ê§Ð§Ê±¼ä
        mainPanel.add(new JLabel("ʧЧʱ¼ä:"));
        endTimeField = new JTextField(fenceData.getExpirationTime());
        mainPanel.add(endTimeField);
        
        // ÑÕÉ«
        mainPanel.add(new JLabel("ÑÕÉ«:"));
        colorComboBox = new JComboBox<>(new String[]{"ºìÉ«", "À¶É«", "ÂÌÉ«", "×ÏÉ«", "ÇàÉ«"});
        colorComboBox.setSelectedIndex(Integer.parseInt(fenceData.getColor()));
        mainPanel.add(colorComboBox);
        
        // ÊÇ·ñÆôÓÃ
        mainPanel.add(new JLabel("ÊÇ·ñÆôÓÃ:"));
        enabledCheckBox = new JCheckBox();
        enabledCheckBox.setSelected(fenceData.getIsEnabled().equals("1"));
        mainPanel.add(enabledCheckBox);
        
        // ËùÊô¹«Ë¾
        mainPanel.add(new JLabel("ËùÊô¹«Ë¾:"));
        companyComboBox = new JComboBox<>();
        String[] companies = Dell_company.getAllCompanyNames();
        for (String company : companies) {
            companyComboBox.addItem(company);
        }
        companyComboBox.setSelectedItem(fenceData.getCompany());
        mainPanel.add(companyComboBox);
        
        // °´Å¥Ãæ°å
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton saveButton = new JButton("È·¶¨");
        saveButton.addActionListener(e -> saveFenceData());
        buttonPanel.add(saveButton);
        
        JButton cancelButton = new JButton("È¡Ïû");
        cancelButton.addActionListener(e -> dispose());
        buttonPanel.add(cancelButton);
        
        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }
 
    private void saveFenceData() {
        if (!validateFields()) {
            return;
        }        
        // ¸üÐÂΧÀ¸Êý¾Ý
        fenceData.setFenceType(getTypeCodeFromName((String) typeComboBox.getSelectedItem()));
        fenceData.setRelatedDepartment((String) deptComboBox.getSelectedItem());
        fenceData.setLayer((String) layerComboBox.getSelectedItem());
        fenceData.setEffectiveTime(startTimeField.getText());
        fenceData.setExpirationTime(endTimeField.getText());
        fenceData.setColor(String.valueOf(colorComboBox.getSelectedIndex()));
        fenceData.setIsEnabled(enabledCheckBox.isSelected() ? "1" : "0");
        fenceData.setCompany((String) companyComboBox.getSelectedItem());
        
        saved = true;
        dispose();
    }
 
    private String getTypeCodeFromName(String typeName) {
        try {
            // ÐÞ¸´: ²»Ê¹Ó÷ºÐͲÎÊý£¬Ê¹ÓÃԭʼÀàÐÍList
            List<FenceType> types = Dell_FenceType.getAllFenceTypes();
            for (Object obj : types) {
                // ÊÖ¶¯½«¶ÔÏóת»»ÎªFenceType
                FenceType type = (FenceType) obj;
                if (type.getTypeName().equals(typeName)) {
                    return type.getTypeCode();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "1"; // Ä¬ÈÏÖµ
    }
 
    private boolean validateFields() {
        // ¼òµ¥ÑéÖ¤ - Êµ¼ÊÓ¦ÓÃÖÐÐèÒª¸üÑϸñµÄÑéÖ¤
        if (startTimeField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "ÉúЧʱ¼ä²»ÄÜΪ¿Õ", "ÑéÖ¤´íÎó", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        if (endTimeField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "ʧЧʱ¼ä²»ÄÜΪ¿Õ", "ÑéÖ¤´íÎó", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        return true;
    }
 
    public boolean isSaved() {
        return saved;
    }
}