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
package dell_Fence; // °üÃû£ºÎ§À¸¹ÜÀí
import targets.Fence; // µ¼ÈëÄ¿±êÀࣺΧÀ¸
import targets.Mapdata; // µ¼ÈëÄ¿±êÀࣺµØÍ¼Êý¾Ý
import javax.swing.*; // µ¼ÈëSwing×é¼þ
import dell_map.Dell_Map; // µ¼ÈëµØÍ¼¹¤¾ßÀà
import publicsWay.XyToLatLngConverter;
 
import java.awt.*; // µ¼ÈëAWTͼÐÎ×é¼þ
import java.sql.SQLException; // µ¼ÈëSQLÒì³£Àà
import java.text.SimpleDateFormat; // µ¼ÈëÈÕÆÚ¸ñʽ»¯Àà
import java.util.Date; // µ¼ÈëÈÕÆÚÀà
import java.util.List; // µ¼ÈëÁбí½Ó¿Ú
import java.util.ResourceBundle; // µ¼Èë¹ú¼Ê»¯×ÊÔ´°ü
import java.util.Vector; // µ¼ÈëÏòÁ¿Àà
import java.awt.geom.Point2D; // µ¼Èë¶þάµãÀà
 
public class FenceDrawerFrame extends JPanel { // Î§À¸»æÖÆÖ÷½çÃæÀà
    private static final long serialVersionUID = 1L; // ÐòÁл¯°æ±¾ºÅ
 
    // UI×é¼þ
    private JComboBox<String> mapComboBox; // µØÍ¼Ñ¡ÔñÏÂÀ­¿ò
    private JComboBox<String> shapeComboBox; // ÐÎ×´Ñ¡ÔñÏÂÀ­¿ò
    private JButton startDrawButton; // ¿ªÊ¼»æÖư´Å¥
    private JButton resetDrawButton; // ÖØÖûæÖư´Å¥
    private JButton finishDrawButton; // Íê³É»æÖư´Å¥
    private JPanel mapPanel; // µØÍ¼Ãæ°å
    private JLabel coordLabel; // ×ø±ê±êÇ©
    private ResourceBundle messages; // ¹ú¼Ê»¯×ÊÔ´°ü
 
    // µØÍ¼Êý¾Ý
    private Mapdata currentMap; // µ±Ç°µØÍ¼¶ÔÏó
    private MapCanvas mapCanvas; // µØÍ¼»­²¼
 
    // »æÖÆ×´Ì¬
    protected boolean isDrawing = false; // ÊÇ·ñÕýÔÚ»æÖÆ
    protected int currentShape = 0; // µ±Ç°»æÖÆÐÎ×´
    protected Vector<Point2D.Double> drawingPoints = new Vector<>(); // »æÖƵ㼯ºÏ
 
    public FenceDrawerFrame(ResourceBundle messages) { // ¹¹Ô캯Êý
        this.messages = messages; // ³õʼ»¯×ÊÔ´°ü
        setLayout(new BorderLayout()); // ÉèÖò¼¾ÖΪ±ß½ç²¼¾Ö
        initUI(); // ³õʼ»¯½çÃæ
        loadMaps(); // ¼ÓÔØµØÍ¼Áбí
    }
 
    private void initUI() { // ³õʼ»¯Óû§½çÃæ
        JPanel mainPanel = new JPanel(new BorderLayout()); // Ö÷Ãæ°å£¬±ß½ç²¼¾Ö
 
        // ¿ØÖÆÃæ°å
        JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10)); // ¿ØÖÆÃæ°å£¬Á÷ʽ²¼¾Ö
        controlPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // ÉèÖñ߿ò¼ä¾à
 
        // ×´Ì¬Ãæ°å
        JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); // ×´Ì¬Ãæ°å£¬ÓÒ¶ÔÆëÁ÷ʽ²¼¾Ö
        coordLabel = new JLabel("X: 0.00, Y: 0.00"); // ÏÔÊ¾×ø±ê
        statusPanel.add(coordLabel); // Ìí¼Óµ½×´Ì¬Ãæ°å
 
        // µØÍ¼Ñ¡Ôñ
        JLabel mapLabel = new JLabel(getMessage("SELECT_MAP") + ":"); // µØÍ¼Ñ¡Ôñ±êÇ©
        mapComboBox = new JComboBox<>(); // µØÍ¼ÏÂÀ­¿ò
        mapComboBox.setPreferredSize(new Dimension(200, 30)); // ÉèÖôóС
        mapComboBox.addActionListener(e -> switchMap()); // Çл»µØÍ¼Ê¼þ
 
        // ÐÎ×´Ñ¡Ôñ
        JLabel shapeLabel = new JLabel(getMessage("FENCE_SHAPE") + ":"); // ÐÎ×´Ñ¡Ôñ±êÇ©
        shapeComboBox = new JComboBox<>(new String[]{ // ÐÎ×´ÏÂÀ­¿ò
                getMessage("POLYGON"), // ¶à±ßÐÎ
                getMessage("POLYLINE"), // ÐÂÔö¶à¶ÎÏßÑ¡Ïî
                getMessage("RECTANGLE"), // ¾ØÐÎ
                getMessage("CIRCLE") // Ô²ÐÎ
        });
        shapeComboBox.setPreferredSize(new Dimension(100, 30)); // ÉèÖôóС
        shapeComboBox.addActionListener(e -> { // ÐÎ×´Çл»Ê¼þ
            currentShape = shapeComboBox.getSelectedIndex(); // ¸üе±Ç°ÐÎ×´
        });
 
        // »æÖư´Å¥
        startDrawButton = new JButton(getMessage("START_DRAW")); // ¿ªÊ¼»æÖư´Å¥
        startDrawButton.addActionListener(e -> startDrawing()); // ¿ªÊ¼»æÖÆÊ¼þ
 
        resetDrawButton = new JButton(getMessage("RESET_DRAW")); // ÖØÖûæÖư´Å¥
        resetDrawButton.addActionListener(e -> resetDrawing()); // ÖØÖûæÖÆÊ¼þ
        resetDrawButton.setEnabled(false); // ³õʼ½ûÓÃ
 
        finishDrawButton = new JButton(getMessage("FINISH_DRAW")); // Íê³É»æÖư´Å¥
        finishDrawButton.addActionListener(e -> finishDrawing()); // Íê³É»æÖÆÊ¼þ
        finishDrawButton.setEnabled(false); // ³õʼ½ûÓÃ
 
        // Ìí¼Ó×é¼þµ½¿ØÖÆÃæ°å
        controlPanel.add(mapLabel); // Ìí¼ÓµØÍ¼±êÇ©
        controlPanel.add(mapComboBox); // Ìí¼ÓµØÍ¼ÏÂÀ­¿ò
        controlPanel.add(shapeLabel); // Ìí¼ÓÐÎ×´±êÇ©
        controlPanel.add(shapeComboBox); // Ìí¼ÓÐÎ×´ÏÂÀ­¿ò
        controlPanel.add(startDrawButton); // Ìí¼Ó¿ªÊ¼°´Å¥
        controlPanel.add(resetDrawButton); // Ìí¼ÓÖØÖð´Å¥
        controlPanel.add(finishDrawButton); // Ìí¼ÓÍê³É°´Å¥
 
        // µØÍ¼Ãæ°å
        mapPanel = new JPanel(new BorderLayout()); // µØÍ¼Ãæ°å£¬±ß½ç²¼¾Ö
        mapPanel.setBackground(Color.LIGHT_GRAY); // ÉèÖñ³¾°É«
        mainPanel.add(controlPanel, BorderLayout.NORTH); // ¿ØÖÆÃæ°å·Å¶¥²¿
        mainPanel.add(mapPanel, BorderLayout.CENTER); // µØÍ¼Ãæ°å·ÅÖмä
        mainPanel.add(statusPanel, BorderLayout.SOUTH); // ×´Ì¬Ãæ°å·Åµ×²¿
 
        add(mainPanel, BorderLayout.CENTER); // Ö÷Ãæ°åÌí¼Óµ½´°¿ÚÖмä
    }
 
    private void loadMaps() { // ¼ÓÔØµØÍ¼Áбí
        List<Mapdata> maps = Dell_Map.getAllMaps(); // »ñÈ¡ËùÓеØÍ¼
        mapComboBox.removeAllItems(); // Çå¿ÕÏÂÀ­¿ò
        for (Mapdata map : maps) { // ±éÀúµØÍ¼Áбí
            mapComboBox.addItem(map.getMapName()); // Ìí¼ÓµØÍ¼Ãû³Æµ½ÏÂÀ­¿ò
        }
 
        if (mapComboBox.getItemCount() > 0) { // Èç¹ûÓеØÍ¼
            switchMap(); // Çл»µ½µÚÒ»¸öµØÍ¼
        }
    }
 
    private void switchMap() { // Çл»µØÍ¼
        String selectedMap = (String) mapComboBox.getSelectedItem(); // »ñȡѡÖеĵØÍ¼Ãû³Æ
        if (selectedMap == null) return; // Èç¹ûΪ¿ÕÔò·µ»Ø
 
        try {
            currentMap = Dell_Map.getMapByName(selectedMap); // ¸ù¾ÝÃû³Æ»ñÈ¡µØÍ¼¶ÔÏó
            if (currentMap == null) return; // Èç¹ûΪ¿ÕÔò·µ»Ø
 
            mapPanel.removeAll(); // Çå¿ÕµØÍ¼Ãæ°å
 
            mapCanvas = new MapCanvas(this, currentMap); // ´´½¨µØÍ¼»­²¼
            mapPanel.add(mapCanvas, BorderLayout.CENTER); // Ìí¼Óµ½µØÍ¼Ãæ°åÖмä
 
            mapPanel.revalidate(); // Ë¢Ð²¼¾Ö
            mapPanel.repaint(); // ÖØ»æ½çÃæ
        } catch (Exception ex) { // Òì³£´¦Àí
            JOptionPane.showMessageDialog(this, // µ¯³ö´íÎóÌáʾ
                    getMessage("LOAD_MAP_ERROR") + ex.getMessage(), // ´íÎóÐÅÏ¢
                    getMessage("ERROR"), JOptionPane.ERROR_MESSAGE); // ±êÌâºÍͼ±ê
        }
    }
 
    private void startDrawing() { // ¿ªÊ¼»æÖÆ
        isDrawing = true; // ÉèÖÃÕýÔÚ»æÖÆ×´Ì¬
        resetDrawing(); // ÖØÖûæÖƵã
        startDrawButton.setEnabled(false); // ½ûÓÿªÊ¼°´Å¥
        resetDrawButton.setEnabled(true); // ÆôÓÃÖØÖð´Å¥
        finishDrawButton.setEnabled(true); // ÆôÓÃÍê³É°´Å¥
        mapCanvas.setDrawingMode(true); // ÉèÖû­²¼Îª»æÖÆÄ£Ê½
    }
 
    private void resetDrawing() { // ÖØÖûæÖÆ
        drawingPoints.clear(); // Çå¿Õ»æÖƵã
        mapCanvas.setDrawingPoints(drawingPoints); // ¸üл­²¼µã¼¯
        mapCanvas.repaint(); // Öػ滭²¼
    }
 
    //    protected void finishDrawing() { // Íê³É»æÖÆ
    //        if ((currentShape == 0 && drawingPoints.size() < 3) || // ¶à±ßÐÎÉÙÓÚ3µã
    //            ((currentShape == 1 || currentShape == 2) && drawingPoints.size() < 2)) { // ¾ØÐλòÔ²ÐÎÉÙÓÚ2µã
    //            JOptionPane.showMessageDialog(this, // µ¯³ö¾¯¸æ
    //                currentShape == 0 ? getMessage("MIN_POINTS_WARNING") : getMessage("NEED_TWO_POINTS"), // ¸ù¾ÝÐÎ×´Ìáʾ
    //                getMessage("WARNING"), JOptionPane.WARNING_MESSAGE); // ±êÌâºÍͼ±ê
    //            return; // ·µ»Ø²»¼ÌÐø
    //        }
    //
    //        isDrawing = false; // ½áÊø»æÖÆ×´Ì¬
    //        startDrawButton.setEnabled(true); // ÆôÓÿªÊ¼°´Å¥
    //        mapCanvas.repaint(); // Öػ滭²¼
    //        showFenceInfoDialog(); // ÏÔʾΧÀ¸ÐÅÏ¢¶Ô»°¿ò
    //    }
    //    
    protected void finishDrawing() {
        // ¸ù¾ÝÐÎ×´ÀàÐͼì²é×îСµãÊýÒªÇó
        if ((currentShape == 0 && drawingPoints.size() < 3) || // ¶à±ßÐÎÖÁÉÙ3µã
                ((currentShape == 2 || currentShape == 3) && drawingPoints.size() < 2) || // ¾ØÐÎ/Ô²ÐÎÖÁÉÙ2µã
                (currentShape == 1 && drawingPoints.size() < 2)) { // ¶à¶ÎÏßÖÁÉÙ2µã
 
            String message = "";
            if (currentShape == 0) {
                message = getMessage("MIN_POINTS_WARNING"); // ¶à±ßÐÎÌáʾ
            } else if (currentShape == 1) {
                message = getMessage("MIN_POINTS_POLYLINE"); // ¶à¶ÎÏßÌáʾ
            } else {
                message = getMessage("NEED_TWO_POINTS"); // ÆäËûÐÎ×´Ìáʾ
            }
 
            JOptionPane.showMessageDialog(this,
                    message,
                    getMessage("WARNING"), JOptionPane.WARNING_MESSAGE);
            return;
        }
 
        isDrawing = false;
        startDrawButton.setEnabled(true);
        //        resetDrawButton.setEnabled(false);
        //        finishDrawButton.setEnabled(false);
        //        mapCanvas.setDrawingMode(false);
        mapCanvas.repaint();
        showFenceInfoDialog();
    }
 
    private void showFenceInfoDialog() { // ÏÔʾΧÀ¸ÐÅÏ¢¶Ô»°¿ò
        Frame parentFrame = (Frame) SwingUtilities.getWindowAncestor(this); // »ñÈ¡¸¸´°¿Ú
        FenceInfoDialog dialog = new FenceInfoDialog( // ´´½¨¶Ô»°¿ò
                parentFrame, // ¸¸´°¿Ú
                messages, // ¹ú¼Ê»¯×ÊÔ´
                currentShape, // µ±Ç°ÐÎ×´
                drawingPoints, // »æÖƵã
                this::saveFence // ±£´æ»Øµ÷
                );
        dialog.setLocationRelativeTo(this); // ¾ÓÖÐÏÔʾ
        dialog.setVisible(true); // ÏÔʾ¶Ô»°¿ò
    }
 
    private void saveFence(String name, String type, String department,
            String color, String company, String layer,
            String startTime, String endTime, String enabled) {
        Fence fence = new Fence();
        // ÉèÖûù±¾ÊôÐÔ
        fence.setFenceName(name);
        fence.setFenceType(type);
        fence.setRelatedDepartment(department);
        fence.setColor(color);
        fence.setCompany(company);
        fence.setLayer(layer);
        fence.setEffectiveTime(startTime);
        fence.setExpirationTime(endTime);
        fence.setIsEnabled(enabled);
        
        // ÉèÖÃÐÎ×´ÀàÐÍ
        String shape = "";
        switch (currentShape) {
            case 0: shape = getMessage("POLYGON"); break;
            case 1: shape = getMessage("POLYLINE"); break;
            case 2: shape = getMessage("RECTANGLE"); break;
            case 3: shape = getMessage("CIRCLE"); break;
        }
        fence.setShape(shape);
 
        // ÉèÖÃ×ø±ê·¶Î§
        StringBuilder coordinates = new StringBuilder();
        for (Point2D.Double p : drawingPoints) {
            if (coordinates.length() > 0) coordinates.append(";");
            coordinates.append(String.format("%.2f,%.2f", p.x, p.y));
        }
        fence.setValidRangeXy(coordinates.toString());
        
        // ÉèÖþ­Î³¶È×ø±ê£¨×ª»»ºóµÄ£©
        String validRangeCoordinates = convertXYToLatLng(drawingPoints, currentMap);
        fence.setValidRangeCoordinates(validRangeCoordinates != null ? validRangeCoordinates : "");
 
        // ÉèÖÃÌí¼Óʱ¼ä
        fence.setAddedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        
        // === ¹Ø¼üÐ޸ģºÎªÊý¾Ý¿â×Ö¶ÎÉèÖÃĬÈÏÖµ ===
        // ÊýÖµÐÍ×Ö¶ÎĬÈÏÖµ
        fence.setBottomHeight("-1");
        fence.setTopHeight("-1");
        fence.setGatheringRadius("-1");
        fence.setMaxPeople("-1");
        fence.setMinPeople("-1");
        fence.setMaxDuration("-1");
        fence.setMinDuration("-1");
        fence.setInspectionOrder("-1");
        fence.setInspectionCount("-1");
        fence.setSides("-1");
        
        // ×Ö·û´®×Ö¶ÎĬÈÏÖµ
        fence.setGatheringTime("-1");
        fence.setHazardousMaterial("-1");
        fence.setLatestInspectionTime("-1");
        fence.setCanEnter("-1");
        fence.setCanExit("-1");
        fence.setConstraintType("-1");
        fence.setConstraintDescription("-1");
        fence.setCameraNumber("-1");
        
        // ±£Áô×Ö¶ÎĬÈÏÖµ
        fence.setReserved1("-1");
        fence.setReserved2("-1");
        fence.setReserved3("-1");
        fence.setReserved4("-1");
        fence.setReserved5("-1");
        fence.setReserved6("-1");
        fence.setReserved7("-1");
        fence.setReserved8("-1");
        fence.setReserved9("-1");
        fence.setReserved10("-1");
        // === ½áÊø¹Ø¼üÐ޸Ġ===
 
        try {
            Dell_Fence.insertFence(fence);
            Dell_Fence.getAllFences().add(fence);
            JOptionPane.showMessageDialog(this,
                    getMessage("FENCE") + name + getMessage("SAVE_SUCCESS"),
                    getMessage("SUCCESS"), JOptionPane.INFORMATION_MESSAGE);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this,
                    getMessage("SAVE_FENCE_ERROR") + ": " + ex.getMessage(),
                    getMessage("ERROR"), JOptionPane.ERROR_MESSAGE);
        }
    }
 
    
 
    public String getMessage(String key) { // »ñÈ¡¹ú¼Ê»¯Îı¾
        try {
            return messages.getString(key); // ´Ó×ÊÔ´°ü»ñÈ¡
        } catch (Exception e) {
            return "[" + key + "]"; // Òì³£·µ»Ø¼üÃû
        }
    }
 
    public JLabel getCoordLabel() { // »ñÈ¡×ø±ê±êÇ©
        return coordLabel; // ·µ»Ø±êÇ©¶ÔÏó
    }
 
 
    @FunctionalInterface // º¯Êýʽ½Ó¿Ú
    public interface SaveFenceCallback { // ±£´æÎ§À¸»Øµ÷½Ó¿Ú
        void save(String name, String type, String department, String color,
                String company, String layer, String startTime,
                String endTime, String enabled); // ±£´æ·½·¨
    }
 
    // ÐÂÔö×ø±êת»»·½·¨
    private String convertXYToLatLng(Vector<Point2D.Double> points, Mapdata map) {
        StringBuilder result = new StringBuilder();
 
        // »ñÈ¡µØÍ¼µÄAµãºÍBµã×ø±êÐÅÏ¢
        String ajw = map.getPointALatlngXyCoordinate();
        String bjw = map.getPointBLatlngXyCoordinate();
 
        if (ajw == null || bjw == null || ajw.isEmpty() || bjw.isEmpty()) {
            return ""; // Èç¹ûAµã»òBµã×ø±ê²»´æÔÚ£¬·µ»Ø¿Õ×Ö·û´®
        }
 
        String[] A = ajw.split(";");
        String[] B = bjw.split(";");
 
        if (A.length < 4 || B.length < 4) {
            return ""; // ×ø±ê¸ñʽ²»ÕýÈ·£¬·µ»Ø¿Õ×Ö·û´®
        }
 
        try {
            double lon_a = Double.parseDouble(A[0]);
            double lat_a = Double.parseDouble(A[1]);
            double xa = Double.parseDouble(A[2]);
            double ya = Double.parseDouble(A[3]);
            double lon_b = Double.parseDouble(B[0]);
            double lat_b = Double.parseDouble(B[1]);
            double xb = Double.parseDouble(B[2]);
            double yb = Double.parseDouble(B[3]);
 
            // ¶Ôÿ¸öµã½øÐÐ×ø±êת»»
            for (Point2D.Double point : points) {
                String[] latLng = XyToLatLngConverter.convertLocalToGlobalCoordinates(
                        String.valueOf(point.x),
                        String.valueOf(point.y),
                        lat_a, lon_a,
                        lat_b, lon_b,
                        xa, ya,
                        xb, yb
                        );
 
                if (result.length() > 0) {
                    result.append(";");
                }
                // ¸ñʽ: Î³¶È,¾­¶È
                result.append(latLng[0]).append(",").append(latLng[1]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
 
        return result.toString();
    }
}