View Javadoc
1   package edu.jiangxin.apktoolbox.file.checksum.panel;
2   
3   import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel;
4   import edu.jiangxin.apktoolbox.swing.extend.filepanel.FilePanel;
5   import edu.jiangxin.apktoolbox.utils.Constants;
6   import org.apache.commons.codec.digest.DigestUtils;
7   import org.apache.commons.lang3.Strings;
8   
9   import javax.swing.*;
10  import java.awt.*;
11  import java.io.*;
12  
13  public class CompareFilesPanel extends EasyChildTabbedPanel {
14      @Serial
15      private static final long serialVersionUID = 1L;
16  
17      private FilePanel firstFilePanel;
18      private FilePanel secondFilePanel;
19  
20      private JPanel operationPanel;
21  
22      private JTextField resultTextField;
23  
24      @Override
25      public void createUI() {
26          BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
27          setLayout(boxLayout);
28  
29          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
30          createFilePanel();
31          add(firstFilePanel);
32          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
33          add(secondFilePanel);
34  
35          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
36          createOperationPanel();
37          add(operationPanel);
38  
39          add(Box.createVerticalGlue());
40      }
41  
42      private void createFilePanel() {
43          firstFilePanel = new FilePanel("First File");
44          firstFilePanel.initialize();
45          secondFilePanel = new FilePanel("Second File");
46          secondFilePanel.initialize();
47      }
48  
49      private void createOperationPanel() {
50          operationPanel = new JPanel();
51  
52          JButton compareButton = new JButton("Compare");
53          compareButton.setFocusPainted(false);
54          compareButton.addActionListener(event -> {
55              File firstFile = firstFilePanel.getFile();
56              File secondFile = secondFilePanel.getFile();
57              if (firstFile == null || secondFile == null) {
58                  resultTextField.setText("File is not valid");
59                  resultTextField.setForeground(Color.YELLOW);
60                  return;
61              }
62  
63              boolean isChecksumSame = false;
64  
65              try (FileInputStream firstFis = new FileInputStream(firstFile);
66                   FileInputStream secondFis = new FileInputStream(secondFile)) {
67                  String firstSha512 = DigestUtils.sha512Hex(firstFis);
68                  String secondSha512 = DigestUtils.sha512Hex(secondFis);
69                  isChecksumSame = Strings.CI.equals(firstSha512, secondSha512);
70              } catch (FileNotFoundException e) {
71                  logger.error("calculate, FileNotFoundException");
72              } catch (IOException e) {
73                  logger.error("calculate, IOException");
74              }
75  
76              if (isChecksumSame) {
77                  resultTextField.setText("File is same");
78                  resultTextField.setForeground(Color.GREEN);
79                  return;
80              }
81  
82              resultTextField.setText("File is different");
83              resultTextField.setForeground(Color.RED);
84          });
85  
86          resultTextField = new JTextField("To be comparing");
87          resultTextField.setPreferredSize(new Dimension(100, 25));
88          resultTextField.setEditable(false);
89          resultTextField.setBorder(null);
90          Font font = new Font(null, Font.BOLD, 16);
91          resultTextField.setFont(font);
92          resultTextField.setForeground(Color.YELLOW);
93  
94          operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS));
95          operationPanel.add(compareButton);
96          operationPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
97          operationPanel.add(resultTextField);
98          operationPanel.add(Box.createHorizontalGlue());
99      }
100 }