shichongfu
2023-02-14 aba34133ba4635b7a7c83879550e4d8d98664530
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
package com.lx.product.seal.frame;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
import com.product.common.io.FileUtils;
import com.lx.product.seal.config.Config;
import com.lx.product.seal.log.Logger;
import com.lx.product.seal.utils.CommonUtil;
 
public class MasterPanel extends JPanel {
    public static Logger log = Logger.getInstance();
    private JLabel stepl = new JLabel();
    private int step = 1;
    JComponent center = null;
    public ButtonNextLabel next = new ButtonNextLabel();
    private ButtonPreLabel pre = new ButtonPreLabel();
    private MasterFrame mainf = null;
    private ButtonOKLabel ok = new ButtonOKLabel();
    public Boolean access = true;
    public Config config=new Config();
    private ExecutorService executorService;
    public MasterPanel(MasterFrame f) {
        mainf = f;
        this.setLayout(new BorderLayout());
        updateStep();
        this.add(stepl, BorderLayout.NORTH);
        updateCenter();
        this.add(initButton(), BorderLayout.SOUTH);
    }
 
    public void updateStep() {
        ImageIcon colorIcon = new ImageIcon("./images/step" + step + ".png");
        
        colorIcon.setImage(colorIcon.getImage().getScaledInstance(690, 81, Image.SCALE_DEFAULT));//宽高根据需要设定    
        stepl.setIcon(colorIcon);
    }
 
    //-------------------------------
    public void updateCenter() {
        if (center != null) {
            this.remove(center);
        }
        if (step == 1) {
            log.writeInfo("准备产品版本", Logger.INFO_TYPE);
            center = imageCenter();
        } else if (step == 2) {
            log.writeInfo("相关路径设置", Logger.INFO_TYPE);
            center = pathCenter();
        } else if (step == 3) {
            log.writeInfo("编译Web程序", Logger.INFO_TYPE);
            JComponent o= compileCenter();
            if(o !=null) {
                center=o;//说明发生错误
            }
        } else if (step == 4) {
            log.writeInfo("复制Install代码", Logger.INFO_TYPE);
            center = copyInstallCode();
        } else if (step == 5) {
            log.writeInfo("复制Java代码", Logger.INFO_TYPE);
            center = copyJavaCode();
        } else if (step == 6) {
            log.writeInfo("复制Web代码", Logger.INFO_TYPE);
            center = copyWebCode();
        }else {
            System.out.println("结束");
            System.exit(0);
        }
        this.add(center, BorderLayout.CENTER);
        this.updateUI();
        this.repaint();
        mainf.repaint();
    }
 
    private JComponent imageCenter() {
        JLabel welcome = new JLabel();
        ImageIcon colorIcon = new ImageIcon("./images/welcome.png");
        colorIcon.setImage(colorIcon.getImage().getScaledInstance(697, 392, Image.SCALE_DEFAULT));//宽高根据需要设定    
        welcome.setIcon(colorIcon);
        return welcome;
    }
    private JComponent pathCenter() {
        PathPanel cp = new PathPanel(this);
        mainf.setPanel(cp.name, cp);
        return cp;
    }
    
    /**
     * 编译Web
     * @return
     */
    private JComponent compileCenter() {
        JLabel web=new JLabel("  正在编译Web程序,请勿关闭窗口");
        next.setEnabled(false);
        executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                String path=((PathPanel)mainf.getPanel("path_panel")).product_web_root.getText()+File.separator+"packed.bat";
                String dist=((PathPanel)mainf.getPanel("path_panel")).product_web_root.getText()+File.separator+"dist";
                File distd=new File(dist);
                if(distd.exists()) {
                    FileUtils.delFile(dist);
                }
                try {
                    System.out.println(path);
                    CommonUtil.run("start "+path,web);
                    while(!distd.exists()) {
                        Thread.sleep(1000);
                        web.setText(web.getText()+"..");
                    }
                    
                    web.setText("  编译Web程序完成。");
                    next.setEnabled(true);
                }catch(Exception e) {
                    e.printStackTrace();
                    web.setText("  编译Web程序发生错误:"+e.getMessage());
                }
//                mainf.mpanel.add(center, BorderLayout.CENTER);
//                mainf.mpanel.updateUI();
//                mainf.mpanel.repaint();
//                mainf.repaint();
                executorService.shutdown();
            }
        });
        return web;
    }
    private JComponent copyInstallCode() {
        JLabel welcome = new JLabel("  开始复制安装程序........");
        next.setEnabled(false);
        executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                String install_path=((PathPanel)mainf.getPanel("path_panel")).install_project_root.getText()+File.separator+"bin";
                String version_path=((PathPanel)mainf.getPanel("path_panel")).product_version_root.getText()+File.separator+((PathPanel)mainf.getPanel("path_panel")).product_version.getText();
                File vf=new File(version_path);
                if(vf.exists()) {
                    welcome.setText("【"+version_path+"】目录已存在,请返回上一步重新指定新版本号或手动删除该目录。");
                    //不显示下一步按钮,只能点上一步
                    
                }else {
                    vf.mkdirs();
                    String install=version_path+File.separator+"install";
                    File inf=new File(install);
                    if(inf.exists()) {
                        FileUtils.deleteDirectory(install);
                    }
                    inf.mkdirs();
                    File src=new File(install_path);
                    try {
                        for(int i=0;i<src.listFiles().length;i++) {
                            welcome.setText("正在复制文件:"+src.listFiles()[i].getAbsolutePath());
                            System.out.println("正在复制文件:"+src.listFiles()[i].getAbsolutePath());
                            if(src.listFiles()[i].isDirectory()) {
                                FileUtils.copyDirectory(src.listFiles()[i], new File(install+File.separator+src.listFiles()[i].getName()));
                            }else {
                                FileUtils.copyFile(src.listFiles()[i], new File(install+File.separator+src.listFiles()[i].getName()));
                            }
                        }
                        welcome.setText("  复制安装程序完成,共复制"+src.listFiles().length+"个文件!");
                        next.setEnabled(true);
                    }catch(Exception e) {
                        e.printStackTrace();
                        welcome.setText("  复制安装文件错误:"+e.getMessage()+",请处理后返回上一步重新操作。");
                        //不显示下一步按钮,只能点上一步
                        
                    }
                }
                executorService.shutdown();
            }
        });
        return welcome;
    }
    private JComponent copyWebCode() {
        JLabel welcome = new JLabel("  正在复制Web程序........");
        next.setEnabled(false);
        executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                String web_path=((PathPanel)mainf.getPanel("path_panel")).product_web_root.getText()+File.separator+"dist";
                File src=new File(web_path);
                if(!src.exists()) {
                    welcome.setText("  前端代码没有编译:"+web_path);
                    return ;
                }
                String version_path=((PathPanel)mainf.getPanel("path_panel")).product_version_root.getText()+File.separator+((PathPanel)mainf.getPanel("path_panel")).product_version.getText();
                String web=version_path+File.separator+"install"+File.separator+"product-server"+File.separator+"web";
                File w=new File(web);
                if(w.exists()) {
                    FileUtils.deleteDirectory(web);
                }
                w.mkdirs();
                try {
                    for(int i=0;i<src.listFiles().length;i++) {
                        welcome.setText("正在复制文件:"+src.listFiles()[i].getAbsolutePath());
                        System.out.println("正在复制文件:"+src.listFiles()[i].getAbsolutePath());
                        if(src.listFiles()[i].isDirectory()) {
                            FileUtils.copyDirectory(src.listFiles()[i], new File(web+File.separator+src.listFiles()[i].getName()));
                        }else {
                            FileUtils.copyFile(src.listFiles()[i], new File(web+File.separator+src.listFiles()[i].getName()));
                        }
                        
                    }
                    welcome.setText("   复制Web程序完成,共复制"+src.listFiles().length+"个文件!");
                
                    next.setEnabled(true);
                }catch(Exception e) {
                    welcome.setText("复制Web文件错误:"+e.getMessage()+",请处理后返回上一步重新操作。");
                    //不显示下一步按钮,只能点上一步
                }
                executorService.shutdown();
            }
        });
        return welcome;
    }
    private JComponent copyJavaCode() {
        JLabel welcome = new JLabel("  正在复制Java程序........");
        next.setEnabled(false);
        executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                String lib_path=((PathPanel)mainf.getPanel("path_panel")).product_java_root.getText()+File.separator+"target"+File.separator+"lib";
                String resources_path=((PathPanel)mainf.getPanel("path_panel")).product_java_root.getText()+File.separator+"target"+File.separator+"resources";
                String version_path=((PathPanel)mainf.getPanel("path_panel")).product_version_root.getText()+File.separator+((PathPanel)mainf.getPanel("path_panel")).product_version.getText();
                String att_path=((PathPanel)mainf.getPanel("path_panel")).product_java_root.getText()+File.separator+"attachment";
                String resources=version_path+File.separator+"install"+File.separator+"product-server"+File.separator+"resources";
                String lib=version_path+File.separator+"install"+File.separator+"product-server"+File.separator+"lib";
                String att=version_path+File.separator+"install"+File.separator+"product-server"+File.separator+"attachment";
                
                
                File resourcesf=new File(resources);
                if(resourcesf.exists()) {
                    FileUtils.deleteDirectory(resources);
                }
                resourcesf.mkdirs();
                File libf=new File(lib);
                if(libf.exists()) {
                    FileUtils.deleteDirectory(lib);
                }
                libf.mkdirs();
                try {
                    //resources
                    File src=new File(resources_path);
                    for(int i=0;i<src.listFiles().length;i++) {
                        welcome.setText("正在复制文件:"+src.listFiles()[i].getAbsolutePath());
                        System.out.println("正在复制文件:"+src.listFiles()[i].getAbsolutePath());
                        if(src.listFiles()[i].isDirectory()) {
                            FileUtils.copyDirectory(src.listFiles()[i], new File(resources+File.separator+src.listFiles()[i].getName()));
                        }else {
                            FileUtils.copyFile(src.listFiles()[i], new File(resources+File.separator+src.listFiles()[i].getName()));
                        }
                        
                    }
                    //lib/*.jar
                    int fs=src.listFiles().length;
                    src=new File(lib_path);
                    for(int i=0;i<src.listFiles().length;i++) {
                        if(src.listFiles()[i].getName().indexOf("spring-server-master-1.2.6.0")>=0
                                ||src.listFiles()[i].getName().indexOf("product-server-core-1.0.0-SNAPSHOT")>=0) {
                            continue;
                        }
                        if(src.listFiles()[i].isDirectory()) {
                            FileUtils.copyDirectory(src.listFiles()[i], new File(lib+File.separator+src.listFiles()[i].getName()));
                        }else {
                            FileUtils.copyFile(src.listFiles()[i], new File(lib+File.separator+src.listFiles()[i].getName()));
                        }
                        welcome.setText("正在复制文件:"+src.listFiles()[i].getAbsolutePath());
                    }
                    String src1=((PathPanel)mainf.getPanel("path_panel")).product_java_root.getText()+File.separator+"target"+File.separator+"product-server-web-1.0.0-SNAPSHOT.jar";
                    String aims7=version_path+File.separator+"install"+File.separator+"product-server"+File.separator+"product-server-web-1.0.0-SNAPSHOT.jar";
                    if(new File(aims7).exists()) {
                        FileUtils.deleteFile(aims7);
                    }
                    //web.jar
                    FileUtils.copyFile(src1, aims7);
                    //复制附件
                    FileUtils.copyDir(att_path, att);
                    fs+=1;
                    welcome.setText("  复制Java程序完成,共复制"+(src.listFiles().length+fs)+"个文件!");
                    next.setEnabled(true);
                }catch(Exception e) {
                    e.printStackTrace();
                    welcome.setText("  复制Java文件错误:"+e.getMessage()+",请处理后返回上一步重新操作。");
                    //不显示下一步按钮,只能点上一步
                }
                executorService.shutdown();
            }
        });
        return welcome;
    }
    //-------------------------
    public JComponent initButton() {
        JPanel b = new JPanel();
        b.setPreferredSize(new Dimension(690, 48));
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (access) {
                    step += 1;
                    updateStep();
                    updateSouth();
                    updateCenter();
                }
            }
        });
        pre.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                step -= 1;
                updateStep();
                updateSouth();
                updateCenter();
                
            }
        });
        b.add(pre);
        b.add(next);
        b.add(ok);
        updateSouth();
        return b;
    }
 
    public void updateSouth() {
        ok.setEnabled(false);
        if (step == 1) {
            firstButton();
        }else if (step == 6) {
            preButton();
        }else {
            nextButton();
        } 
    }
 
    /**
     * 进入安装系统页面初始化按钮为空
     */
    public void startInstallStepButton() {
        pre.setEnabled(false);
        next.setOperate(false);
        ok.setEnabled(false);
    }
 
    /**
     * 安装系统完成后显示按钮 下一步
     */
    public void finalInstallStepButton() {
        pre.setEnabled(false);
        next.setOperate(true);
        ok.setEnabled(false);
    }
 
 
    private void firstButton() {
        pre.setEnabled(false);
        next.setEnabled(true);
        next.setOperate(true);
        access = true;
 
    }
    private void nextButton() {
        pre.setEnabled(true);
        next.setEnabled(true);
        next.setOperate(true);
        access = true;
 
    }
 
    private void preButton() {
        pre.setEnabled(true);
        next.setEnabled(false);
        access = true;
    }
 
}