826220679@qq.com
2025-10-31 9aca70f16836952e2e3462ecc69dabe679811eb7
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
package PublicPannel;
import javax.swing.*;
import home.ButtonUtils;
import home.MainFrame;
import java.awt.*;
import java.awt.event.ActionListener;
 
public class UpgradePanel extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private MainFrame mainFrame;
    private JButton upgradeBtn;
    private JTextField filePathField;
    private JButton selectBinBtn;
    private JProgressBar progressBar;
    private JLabel selectFileLabel; // ÐÂÔö£ºÓÃÓÚÎļþÑ¡Ôñ±êÇ©
    private JLabel progressLabel;   // ÐÂÔö£ºÓÃÓÚ½ø¶È±êÇ©
    
    public UpgradePanel(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        initializeUI();
    }
    
    private void initializeUI() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        
        // ÄÚÈÝÃæ°å - Ê¹Óô¹Ö±²¼¾Ö£¬ÓëBaseParameterPanelÒ»ÖÂ
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        
        // ÎļþÑ¡ÔñÃæ°å
        JPanel filePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
        
        filePathField = new JTextField();
        filePathField.setEditable(false);
        filePathField.setPreferredSize(new Dimension(200, 25)); // ÉèÖÃÎļþ·¾¶¿í¶È
        
        selectBinBtn = ButtonUtils.createBlueButton(mainFrame.getString("select.bin.file"));
        selectBinBtn.addActionListener(e -> selectBinFile());
        
        // Ê¹ÓñäÁ¿ÒýÓñêÇ©£¬ÒÔ±ãºóÐø¸üÐÂ
        selectFileLabel = new JLabel(mainFrame.getString("select.bin.file"));
        
        filePanel.add(selectFileLabel);
        filePanel.add(filePathField);
        filePanel.add(selectBinBtn);
        
        // ½ø¶ÈÌõºÍÉý¼¶°´Å¥Ãæ°å - ·ÅÔÚͬһÐÐ
        JPanel progressButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
        
        // ½ø¶ÈÌõ
        progressBar = new JProgressBar(0, 100);
        progressBar.setStringPainted(true);
        progressBar.setVisible(false);
        progressBar.setPreferredSize(new Dimension(200, 25)); // ÉèÖýø¶ÈÌõ¿í¶È
        
        // Éý¼¶°´Å¥
        upgradeBtn = ButtonUtils.createBlueButton(mainFrame.getString("start.upgrade"));
        upgradeBtn.addActionListener(e -> startUpgrade());
        
        // Ê¹ÓñäÁ¿ÒýÓýø¶È±êÇ©£¬ÒÔ±ãºóÐø¸üÐÂ
        progressLabel = new JLabel(mainFrame.getString("upgrade.progress")); // ÐèÒªÔÚÓïÑÔ×ÊÔ´ÎļþÖÐÌí¼Ó"upgrade.progress"¼ü
        
        progressButtonPanel.add(progressLabel);
        progressButtonPanel.add(progressBar);
        progressButtonPanel.add(upgradeBtn);
        
        // ÉèÖù̶¨¸ß¶È
        filePanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
        progressButtonPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
        
        // ½«ËùÓÐ×é¼þÌí¼Óµ½ÄÚÈÝÃæ°å
        contentPanel.add(Box.createVerticalStrut(10)); // ¶¥²¿10ÏñËØ¼ä¾à
        contentPanel.add(filePanel);
        contentPanel.add(progressButtonPanel);
        
        // Ìí¼ÓÄÚÈÝÃæ°å£¬²»Ìí¼Ó¹ö¶¯Ãæ°å
        add(contentPanel);
    }
    
    private void selectBinFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setDialogTitle(mainFrame.getString("select.bin.file"));
        fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("BIN files (*.bin)", "bin"));
        
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            // ½«Ñ¡ÔñµÄÎļþ·¾¶ÏÔʾÔÚÎļþ·¾¶¿òÖÐ
            String selectedFile = fileChooser.getSelectedFile().getAbsolutePath();
            filePathField.setText(selectedFile);
            
            JOptionPane.showMessageDialog(this, 
                mainFrame.getString("selected.file") + ": " + selectedFile,
                mainFrame.getString("file.selected"),
                JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    private void startUpgrade() {
        // ¼ì²éÉý¼¶Ìõ¼þ
        if (filePathField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this,
                mainFrame.getString("please.select.file.first"),
                mainFrame.getString("warning"),
                JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        progressBar.setVisible(true);
        upgradeBtn.setEnabled(false);
        
        // Ä£ÄâÉý¼¶½ø¶È
        Timer timer = new Timer(100, null);
        ActionListener actionListener = new ActionListener() {
            int progress = 0;
            
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                progress += 5;
                progressBar.setValue(progress);
                
                if (progress >= 100) {
                    ((Timer) e.getSource()).stop();
                    upgradeBtn.setEnabled(true);
                    JOptionPane.showMessageDialog(UpgradePanel.this,
                        mainFrame.getString("upgrade.completed"),
                        mainFrame.getString("success"),
                        JOptionPane.INFORMATION_MESSAGE);
                }
            }
        };
        
        timer.addActionListener(actionListener);
        timer.start();
    }
    
    public void updateLanguage() {
        // ¸üа´Å¥Îı¾
        selectBinBtn.setText(mainFrame.getString("select.bin.file"));
        upgradeBtn.setText(mainFrame.getString("start.upgrade"));
        
        // ¸üбêÇ©Îı¾
        selectFileLabel.setText(mainFrame.getString("select.bin.file"));
        progressLabel.setText(mainFrame.getString("upgrade.progress")); // ÐèÒªÔÚÓïÑÔ×ÊÔ´ÎļþÖÐÌí¼Ó"upgrade.progress"¼ü
        
        // ¸üÐÂÎļþÑ¡ÔñÆ÷¶Ô»°¿ò±êÌâ
        // ×¢Ò⣺ÕâÀïÎÞ·¨Ö±½Ó¸üÐÂÒÑ´´½¨µÄJFileChooser£¬µ«Ï´δò¿ªÊ±»áʹÓÃеÄÎı¾
        
        revalidate();
        repaint();
    }
}