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.StringUtils; 8 9 import javax.swing.*; 10 import java.awt.*; 11 import java.io.File; 12 import java.io.FileInputStream; 13 import java.io.FileNotFoundException; 14 import java.io.IOException; 15 16 public class CompareFilesPanel extends EasyChildTabbedPanel { 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 secondFilePanel = new FilePanel("Second File"); 45 } 46 47 private void createOperationPanel() { 48 operationPanel = new JPanel(); 49 50 JButton compareButton = new JButton("Compare"); 51 compareButton.setFocusPainted(false); 52 compareButton.addActionListener(event -> { 53 File firstFile = firstFilePanel.getFile(); 54 File secondFile = secondFilePanel.getFile(); 55 if (firstFile == null || secondFile == null) { 56 resultTextField.setText("File is not valid"); 57 resultTextField.setForeground(Color.YELLOW); 58 return; 59 } 60 61 boolean isChecksumSame = false; 62 63 try (FileInputStream firstFis = new FileInputStream(firstFile); 64 FileInputStream secondFis = new FileInputStream(secondFile)) { 65 String firstSha512 = DigestUtils.sha512Hex(firstFis); 66 String secondSha512 = DigestUtils.sha512Hex(secondFis); 67 isChecksumSame = StringUtils.equalsIgnoreCase(firstSha512, secondSha512); 68 } catch (FileNotFoundException e) { 69 logger.error("calculate, FileNotFoundException"); 70 } catch (IOException e) { 71 logger.error("calculate, IOException"); 72 } 73 74 if (isChecksumSame) { 75 resultTextField.setText("File is same"); 76 resultTextField.setForeground(Color.GREEN); 77 return; 78 } 79 80 resultTextField.setText("File is different"); 81 resultTextField.setForeground(Color.RED); 82 }); 83 84 resultTextField = new JTextField("To be comparing"); 85 resultTextField.setPreferredSize(new Dimension(100, 25)); 86 resultTextField.setEditable(false); 87 resultTextField.setBorder(null); 88 Font font = new Font(null, Font.BOLD, 16); 89 resultTextField.setFont(font); 90 resultTextField.setForeground(Color.YELLOW); 91 92 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS)); 93 operationPanel.add(compareButton); 94 operationPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 95 operationPanel.add(resultTextField); 96 operationPanel.add(Box.createHorizontalGlue()); 97 } 98 }